Permalink
Browse files

Merge pull request #248 from adobe/move-to-trash2

Move to trash
  • Loading branch information...
2 parents 9f10945 + d91c582 commit 8f31d9263aa4c5631b804f0d847861647d079976 @redmunds redmunds committed May 15, 2013
@@ -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
@@ -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
@@ -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++) {
@@ -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();
@@ -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()
{

0 comments on commit 8f31d92

Please sign in to comment.