Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Move to trash #248

Merged
merged 2 commits into from

3 participants

@gruehle
Owner

The is a rebased/squashed version of #245

Adds a new appshell.fs.moveToTrash() function, which, as it's name implies, moves the specified file or folder to the Trash (on the mac) or Recycle Bin (on Windows).

leonuh and others added some commits
@leonuh leonuh Directory and file delete to trash 640c98e
@gruehle gruehle Update Windows implementation
Use NSWorkspace recycleURLs to move items to the trash
d91c582
@gruehle gruehle referenced this pull request
Closed

Move to trash #245

@redmunds
Collaborator

Looks good. Merging.

@redmunds redmunds merged commit 8f31d92 into master
@redmunds redmunds deleted the move-to-trash2 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 15, 2013
  1. @leonuh @gruehle

    Directory and file delete to trash

    leonuh authored gruehle committed
  2. @gruehle

    Update Windows implementation

    gruehle authored
    Use NSWorkspace recycleURLs to move items to the trash
This page is out of date. Refresh to see the latest.
View
18 appshell/appshell_extensions.cpp
@@ -302,6 +302,24 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
// No additional response args for this function
}
+ } else if (message_name == "MoveFileOrDirectoryToTrash") {
+ // Parameters:
+ // 0: int32 - callback id
+ // 1: string - path
+ if (argList->GetSize() != 2 ||
+ argList->GetType(1) != VTYPE_STRING) {
+ error = ERR_INVALID_PARAMS;
+ }
+
+ if (error == NO_ERROR) {
+ ExtensionString path = argList->GetString(1);
+
+ MoveFileOrDirectoryToTrash(path, browser, response);
+
+ // Skip standard callback handling. MoveFileOrDirectoryToTrash fires the
+ // callback asynchronously.
+ return true;
+ }
} else if (message_name == "ShowDeveloperTools") {
// Parameters - none
View
25 appshell/appshell_extensions.js
@@ -19,7 +19,7 @@
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
- */
+ */
// This is the JavaScript code for bridging to native functionality
// See appshell_extentions_[platform] for implementation of native methods.
@@ -40,7 +40,7 @@ if (!appshell.fs) {
if (!appshell.app) {
appshell.app = {};
}
-(function () {
+(function () {
// Error values. These MUST be in sync with the error values
// at the top of appshell_extensions_platform.h.
@@ -352,7 +352,7 @@ if (!appshell.app) {
};
/**
- * Delete a file.
+ * Delete a file permanently.
*
* @param {string} path The path of the file to delete
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
@@ -369,6 +369,25 @@ if (!appshell.app) {
appshell.fs.unlink = function (path, callback) {
DeleteFileOrDirectory(callback, path);
};
+
+ /**
+ * Delete a file non-permanently (to trash).
+ *
+ * @param {string} path The path of the file or directory to delete
+ * @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
+ * Possible error values:
+ * NO_ERROR
+ * ERR_UNKNOWN
+ * ERR_INVALID_PARAMS
+ * ERR_NOT_FOUND
+ * ERR_NOT_FILE
+ *
+ * @return None. This is an asynchronous call that sends all return information to the callback.
+ */
+ native function MoveFileOrDirectoryToTrash();
+ appshell.fs.moveToTrash = function (path, callback) {
+ MoveFileOrDirectoryToTrash(callback, path);
+ };
/**
* Return the number of milliseconds that have elapsed since the application
View
44 appshell/appshell_extensions_mac.mm
@@ -491,21 +491,51 @@ int32 DeleteFileOrDirectory(ExtensionString filename)
NSString* path = [NSString stringWithUTF8String:filename.c_str()];
BOOL isDirectory;
- // Contrary to the name of this function, we don't actually delete directories
- if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
+ // Contrary to the name of this function, we don't actually delete directories
+ if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
if (isDirectory) {
- return ERR_NOT_FILE;
- }
+ return ERR_NOT_FILE;
+ }
} else {
return ERR_NOT_FOUND;
- }
-
+ }
+
if ([[NSFileManager defaultManager] removeItemAtPath:path error:&error])
- return NO_ERROR;
+ return NO_ERROR;
return ConvertNSErrorCode(error, false);
}
+void MoveFileOrDirectoryToTrash(ExtensionString filename, CefRefPtr<CefBrowser> browser, CefRefPtr<CefProcessMessage> response)
+{
+ NSString* pathStr = [NSString stringWithUTF8String:filename.c_str()];
+ NSURL* fileUrl = [NSURL fileURLWithPath: pathStr];
+
+ static CefRefPtr<CefProcessMessage> s_response;
+ static CefRefPtr<CefBrowser> s_browser;
+
+ if (s_response) {
+ // Already a pending request. This will only happen if MoveFileOrDirectoryToTrash is called
+ // before the previous call has completed, which is not very likely.
+ response->GetArgumentList()->SetInt(1, ERR_UNKNOWN);
+ browser->SendProcessMessage(PID_RENDERER, response);
+ return;
+ }
+
+ s_browser = browser;
+ s_response = response;
+
+ [[NSWorkspace sharedWorkspace] recycleURLs:[NSArray arrayWithObject:fileUrl] completionHandler:^(NSDictionary *newURLs, NSError *error) {
+ // Invoke callback
+ s_response->GetArgumentList()->SetInt(1, ConvertNSErrorCode(error, false));
+ s_browser->SendProcessMessage(PID_RENDERER, s_response);
+
+ s_response = nil;
+ s_browser = nil;
+ }];
+}
+
+
void NSArrayToCefList(NSArray* array, CefRefPtr<CefListValue>& list)
{
for (NSUInteger i = 0; i < [array count]; i++) {
View
2  appshell/appshell_extensions_platform.h
@@ -92,6 +92,8 @@ int32 SetPosixPermissions(ExtensionString filename, int32 mode);
int32 DeleteFileOrDirectory(ExtensionString filename);
+void MoveFileOrDirectoryToTrash(ExtensionString filename, CefRefPtr<CefBrowser> browser, CefRefPtr<CefProcessMessage> response);
+
int32 GetNodeState(int32& state);
void OnBeforeShutdown();
View
24 appshell/appshell_extensions_win.cpp
@@ -716,6 +716,30 @@ int32 DeleteFileOrDirectory(ExtensionString filename)
return NO_ERROR;
}
+void MoveFileOrDirectoryToTrash(ExtensionString filename, CefRefPtr<CefBrowser> browser, CefRefPtr<CefProcessMessage> response)
+{
+ DWORD dwAttr = GetFileAttributes(filename.c_str());
+ int32 error = NO_ERROR;
+
+ if (dwAttr == INVALID_FILE_ATTRIBUTES)
+ error = ERR_NOT_FOUND;
+
+ if (error == NO_ERROR) {
+ WCHAR filepath[MAX_PATH+1] = {0};
+ wcscpy(filepath, filename.c_str());
+ SHFILEOPSTRUCT operation = {0};
+ operation.wFunc = FO_DELETE;
+ operation.pFrom = filepath;
+ operation.fFlags = FOF_ALLOWUNDO | FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
+
+ if (SHFileOperation(&operation)) {
+ error = ERR_UNKNOWN;
+ }
+ }
+
+ response->GetArgumentList()->SetInt(1, error);
+ browser->SendProcessMessage(PID_RENDERER, response);
+}
void OnBeforeShutdown()
{
Something went wrong with that request. Please try again.