Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds .webloc support #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 61 additions & 24 deletions Stapler/StaplerApp.swift
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase this against current source. Thanks!

Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class StaplerViewModel: ObservableObject {
return false
}

func handleError(_ error: Error) {
func handleError(_ error: Error) {
DispatchQueue.main.async {
self.errorMessage = error.localizedDescription
}
Expand Down Expand Up @@ -355,29 +355,66 @@ struct ContentView: View {
}
.frame(minWidth: 300, minHeight: 200)
.focused($isViewFocused)
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
let wasEmpty = viewModel.document.aliases.isEmpty
for provider in providers {
_ = provider.loadObject(ofClass: URL.self) { url, error in
if let error = error {
viewModel.handleError(error)
} else if let url = url {
DispatchQueue.main.async {
do {
let newAlias = try AliasItem(url: url)
viewModel.addAlias(newAlias)
if wasEmpty {
updateDocument()
} else {
document.aliases = viewModel.document.aliases
}
} catch {
viewModel.handleError(error)
}
}
}
}
}
.onDrop(of: [.url, .urlBookmarkData], isTargeted: nil) { providers in
let wasEmpty = viewModel.document.aliases.isEmpty
for provider in providers {
_ = provider.loadObject(ofClass: URL.self) { url, error in
if let error = error {
viewModel.handleError(error)
} else if let url = url {
DispatchQueue.main.async {
do {
let newAlias = try AliasItem(url: url)
viewModel.addAlias(newAlias)
if wasEmpty {
updateDocument()
} else {
document.aliases = viewModel.document.aliases
}
} catch {
viewModel.handleError(error)
}
}
}
}
}
// If there are no items conforming to the specified types, check for file promise receivers in the pasteboard
let pasteboard = NSPasteboard(name: .drag)
guard let filePromises = pasteboard.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil),
let receiver = filePromises.first as? NSFilePromiseReceiver else {
return false
}

// Process the first file promise receiver
let queue = OperationQueue()
receiver.receivePromisedFiles(atDestination: FileManager.default.temporaryDirectory, //URL.temporaryDirectory,
operationQueue: queue) { (url, error) in
if let error = error {
print("Error loading dropped item from pasteboard: \(error.localizedDescription)")
} else {
DispatchQueue.main.async {
// url is a .webloc
let data = try? Data.init(contentsOf: url) as Data
// turn webloc contents (xml) into plist dict
let dict = try! PropertyListSerialization.propertyList(from:data ?? Data(), options: [], format: nil) as! [String:Any]
let urlString = dict["URL"] as! String
let newUrl = URL(string: urlString)
do {
let newAlias = try AliasItem(url: url)
viewModel.addAlias(newAlias)
if wasEmpty {
updateDocument()
} else {
document.aliases = viewModel.document.aliases
}
} catch {
viewModel.handleError(error)
}

}
}
}

return true
}
.alert(isPresented: $showingErrorAlert) {
Expand Down