Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #248 from adobe/move-to-trash2
Browse files Browse the repository at this point in the history
Move to trash
  • Loading branch information
redmunds committed May 15, 2013
2 parents 95d7395 + a224e95 commit 4d4b0b3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 10 deletions.
18 changes: 18 additions & 0 deletions appshell/appshell_extensions.cpp
Expand Up @@ -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

Expand Down
25 changes: 22 additions & 3 deletions appshell/appshell_extensions.js
Expand Up @@ -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.
Expand All @@ -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.

Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand Down
44 changes: 37 additions & 7 deletions appshell/appshell_extensions_mac.mm
Expand Up @@ -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++) {
Expand Down
2 changes: 2 additions & 0 deletions appshell/appshell_extensions_platform.h
Expand Up @@ -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();
Expand Down
24 changes: 24 additions & 0 deletions appshell/appshell_extensions_win.cpp
Expand Up @@ -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()
{
Expand Down

0 comments on commit 4d4b0b3

Please sign in to comment.