From 7bd36d031a1c1533784f4f85a8bd9024139f74d4 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Mon, 27 Aug 2012 08:47:36 -0700 Subject: [PATCH 1/6] Add brackets.fs.makedir() and brackets.fs.rename() methods. Implement on mac. --- appshell/appshell_extensions.cpp | 36 +++++++++++++++++++++++++ appshell/appshell_extensions.js | 30 ++++++++++++++++++++- appshell/appshell_extensions_mac.mm | 22 +++++++++++++++ appshell/appshell_extensions_platform.h | 4 +++ 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/appshell/appshell_extensions.cpp b/appshell/appshell_extensions.cpp index ccacbd5bd..ad3f21385 100644 --- a/appshell/appshell_extensions.cpp +++ b/appshell/appshell_extensions.cpp @@ -150,6 +150,42 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate { // Set response args for this function responseArgs->SetList(2, directoryContents); + } else if (message_name == "MakeDir") { + // Parameters: + // 0: int32 - callback id + // 1: string - directory path + // 2: number - mode + if (argList->GetSize() != 3 || + argList->GetType(1) != VTYPE_STRING || + argList->GetType(2) != VTYPE_INT) { + error = ERR_INVALID_PARAMS; + } + + if (error == NO_ERROR) { + ExtensionString pathname = argList->GetString(1); + int32 mode = argList->GetInt(2); + + error = MakeDir(pathname, mode); + } + // No additional response args for this function + } else if (message_name == "Rename") { + // Parameters: + // 0: int32 - callback id + // 1: string - old path + // 2: string - new path + if (argList->GetSize() != 3 || + argList->GetType(1) != VTYPE_STRING || + argList->GetType(2) != VTYPE_STRING) { + error = ERR_INVALID_PARAMS; + } + + if (error == NO_ERROR) { + ExtensionString oldName = argList->GetString(1); + ExtensionString newName = argList->GetString(2); + + error = Rename(oldName, newName); + } + // No additional response args for this function } else if (message_name == "GetFileModificationTime") { // Parameters: // 0: int32 - callback id diff --git a/appshell/appshell_extensions.js b/appshell/appshell_extensions.js index 34e849953..1eb3cae65 100644 --- a/appshell/appshell_extensions.js +++ b/appshell/appshell_extensions.js @@ -144,7 +144,35 @@ if (!appshell.app) { appshell.fs.readdir = function (path, callback) { var resultString = ReadDir(callback, path); }; - + + /** + * Create a new directory. + * + * @param {string} path The path of the directory to create. + * @param {number} mode The permissions for the directory, in numeric format (ie 0777) + * @param {function(err)} callback Asynchronous callback function. The callback gets one argument. + * + * @return None. This is an asynchronous call that sends all return information to the callback. + **/ + native function MakeDir(); + appshell.fs.makedir = function (path, mode, callback) { + MakeDir(callback, path, mode); + }; + + /** + * Rename a file or directory. + * + * @param {string} oldPath The old name of the file or directory. + * @param {string} newPath The new name of the file or directory. + * @param {function(err)} callback Asynchronous callback function. The callback gets one argument. + * + * @return None. This is an asynchronous call that sends all return information to the callback. + **/ + native function Rename(); + appshell.fs.rename = function(oldPath, newPath, callback) { + Rename(callback, oldPath, newPath); + }; + /** * Get information for the selected file or directory. * diff --git a/appshell/appshell_extensions_mac.mm b/appshell/appshell_extensions_mac.mm index 2ac96281d..258fd80ff 100644 --- a/appshell/appshell_extensions_mac.mm +++ b/appshell/appshell_extensions_mac.mm @@ -337,6 +337,28 @@ int32 ReadDir(ExtensionString path, CefRefPtr& directoryContents) return ConvertNSErrorCode(error, true); } +int32 MakeDir(ExtensionString path, int32 mode) +{ + NSError* error = nil; + NSString* pathStr = [NSString stringWithUTF8String:path.c_str()]; + + // TODO: honor mode + [[NSFileManager defaultManager] createDirectoryAtPath:pathStr withIntermediateDirectories:FALSE attributes:nil error:&error]; + + return ConvertNSErrorCode(error, false); +} + +int32 Rename(ExtensionString oldName, ExtensionString newName) +{ + NSError* error = nil; + NSString* oldPathStr = [NSString stringWithUTF8String:oldName.c_str()]; + NSString* newPathStr = [NSString stringWithUTF8String:newName.c_str()]; + + [[NSFileManager defaultManager] moveItemAtPath:oldPathStr toPath:newPathStr error:&error]; + + return ConvertNSErrorCode(error, false); +} + int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir) { NSString* path = [NSString stringWithUTF8String:filename.c_str()]; diff --git a/appshell/appshell_extensions_platform.h b/appshell/appshell_extensions_platform.h index 22131451c..adc200a73 100644 --- a/appshell/appshell_extensions_platform.h +++ b/appshell/appshell_extensions_platform.h @@ -67,6 +67,10 @@ int32 ShowOpenDialog(bool allowMulitpleSelection, int32 ReadDir(ExtensionString path, CefRefPtr& directoryContents); +int32 MakeDir(ExtensionString path, int32 mode); + +int32 Rename(ExtensionString oldName, ExtensionString newName); + int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir); int32 ReadFile(ExtensionString filename, ExtensionString encoding, std::string& contents); From 7c992f8b19d51e266e826ec59d439b423361e619 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Wed, 29 Aug 2012 17:45:38 -0700 Subject: [PATCH 2/6] Use sheets for file dialogs --- appshell/appshell_extensions_mac.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/appshell/appshell_extensions_mac.mm b/appshell/appshell_extensions_mac.mm index 258fd80ff..25e9851a5 100644 --- a/appshell/appshell_extensions_mac.mm +++ b/appshell/appshell_extensions_mac.mm @@ -312,11 +312,13 @@ int32 ShowOpenDialog(bool allowMulitpleSelection, [openPanel setAllowedFileTypes:allowedFileTypes]; + [openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil]; if ([openPanel runModal] == NSOKButton) { NSArray* files = [openPanel filenames]; NSArrayToCefList(files, selectedFiles); } + [NSApp endSheet:openPanel]; return NO_ERROR; } From c9f03c9938eab66854988fcc6d2c524aca85d76a Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Mon, 10 Sep 2012 09:11:29 -0700 Subject: [PATCH 3/6] Add ERR_FILE_EXISTS --- appshell/appshell_extensions.js | 5 +++++ appshell/appshell_extensions_mac.mm | 3 +++ appshell/appshell_extensions_platform.h | 1 + 3 files changed, 9 insertions(+) diff --git a/appshell/appshell_extensions.js b/appshell/appshell_extensions.js index 1eb3cae65..0fe88c60f 100644 --- a/appshell/appshell_extensions.js +++ b/appshell/appshell_extensions.js @@ -94,6 +94,11 @@ if (!appshell.app) { */ appshell.fs.ERR_NOT_DIRECTORY = 9; + /** + * @constant Specified file already exists. + */ + appshell.fs.ERR_FILE_EXISTS = 10; + /** * Display the OS File Open dialog, allowing the user to select * files or directories. diff --git a/appshell/appshell_extensions_mac.mm b/appshell/appshell_extensions_mac.mm index 25e9851a5..f86d91aa0 100644 --- a/appshell/appshell_extensions_mac.mm +++ b/appshell/appshell_extensions_mac.mm @@ -517,6 +517,9 @@ int32 ConvertNSErrorCode(NSError* error, bool isReading) case NSFileWriteOutOfSpaceError: return ERR_OUT_OF_SPACE; break; + case NSFileWriteFileExistsError: + return ERR_FILE_EXISTS; + break; } // Unknown error diff --git a/appshell/appshell_extensions_platform.h b/appshell/appshell_extensions_platform.h index adc200a73..71fc7e369 100644 --- a/appshell/appshell_extensions_platform.h +++ b/appshell/appshell_extensions_platform.h @@ -42,6 +42,7 @@ static const int ERR_CANT_WRITE = 6; static const int ERR_OUT_OF_SPACE = 7; static const int ERR_NOT_FILE = 8; static const int ERR_NOT_DIRECTORY = 9; +static const int ERR_FILE_EXISTS = 10; #if defined(OS_WIN) typedef std::wstring ExtensionString; From 22882109415d9b3e733b0a4b2d88e72e0a931768 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Thu, 13 Sep 2012 09:38:15 -0700 Subject: [PATCH 4/6] Remember window size and location on the mac. --- appshell/cefclient_mac.mm | 42 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/appshell/cefclient_mac.mm b/appshell/cefclient_mac.mm index 09559853e..349e0f753 100644 --- a/appshell/cefclient_mac.mm +++ b/appshell/cefclient_mac.mm @@ -353,23 +353,41 @@ - (void)createApp:(id)object { settings.web_security_disabled = true; - window_info.SetAsChild(contentView, 0, 0, kWindowWidth, kWindowHeight); + [[mainWnd windowController] setShouldCascadeWindows: NO]; + + // Set the initial default size of the window. + NSRect defSize = [mainWnd contentRectForFrameRect:[mainWnd frame]]; + defSize.size.width = kWindowWidth; + defSize.size.height = kWindowHeight +#ifdef SHOW_TOOLBAR_UI + + URLBAR_HEIGHT +#endif + ; + + [mainWnd setFrame:[mainWnd frameRectForContentRect:defSize] display:NO]; + + // Set the "autosave" name for the window. If there is a previously stored + // size for the window, it will be loaded here. + [mainWnd setFrameAutosaveName:APP_NAME @"MainWindow"]; + + // Get the actual content size of the window since setFrameAutosaveName could + // result in the window size changing. + NSRect r = [mainWnd contentRectForFrameRect:[mainWnd frame]]; + + window_info.SetAsChild(contentView, 0, 0, + r.size.width, + r.size.height +#ifdef SHOW_TOOLBAR_UI + + URLBAR_HEIGHT +#endif + ); + CefBrowserHost::CreateBrowser(window_info, g_handler.get(), [[startupUrl absoluteString] UTF8String], settings); // Show the window. + [mainWnd display]; [mainWnd makeKeyAndOrderFront: nil]; - - // Size the window. - NSRect r = [mainWnd contentRectForFrameRect:[mainWnd frame]]; - r.size.width = kWindowWidth; - r.size.height = kWindowHeight -#ifdef SHOW_TOOLBAR_UI - + URLBAR_HEIGHT -#endif - ; - - [mainWnd setFrame:[mainWnd frameRectForContentRect:r] display:YES]; } // Sent by the default notification center immediately before the application From a119b6bcd34814c54a84ab495ee8d075ffd34567 Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Tue, 25 Sep 2012 18:24:04 -0700 Subject: [PATCH 5/6] Windows implementation of MakeDir and Rename. --- appshell/appshell_extensions_win.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/appshell/appshell_extensions_win.cpp b/appshell/appshell_extensions_win.cpp index e198cd62a..185c3d5b7 100644 --- a/appshell/appshell_extensions_win.cpp +++ b/appshell/appshell_extensions_win.cpp @@ -554,6 +554,22 @@ int32 ReadDir(ExtensionString path, CefRefPtr& directoryContents) return NO_ERROR; } +int32 MakeDir(ExtensionString path, int32 mode) +{ + if (!CreateDirectory(path.c_str(), NULL)) + return ConvertWinErrorCode(GetLastError(), false); + + return NO_ERROR; +} + +int32 Rename(ExtensionString oldName, ExtensionString newName) +{ + if (!MoveFile(oldName.c_str(), newName.c_str())) + return ConvertWinErrorCode(GetLastError()); + + return NO_ERROR; +} + int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir) { DWORD dwAttr = GetFileAttributes(filename.c_str()); @@ -740,6 +756,8 @@ int ConvertWinErrorCode(int errorCode, bool isReading) return ERR_CANT_WRITE; case ERROR_HANDLE_DISK_FULL: return ERR_OUT_OF_SPACE; + case ERROR_ALREADY_EXISTS: + return ERR_FILE_EXISTS; default: return ERR_UNKNOWN; } From 3973bf78937c72f205953a462251a4837906f048 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Wed, 3 Oct 2012 11:12:49 -0700 Subject: [PATCH 6/6] add TODO for issue #1759 --- appshell/appshell_extensions_mac.mm | 2 +- appshell/appshell_extensions_win.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/appshell/appshell_extensions_mac.mm b/appshell/appshell_extensions_mac.mm index f86d91aa0..a7da50c0b 100644 --- a/appshell/appshell_extensions_mac.mm +++ b/appshell/appshell_extensions_mac.mm @@ -344,7 +344,7 @@ int32 MakeDir(ExtensionString path, int32 mode) NSError* error = nil; NSString* pathStr = [NSString stringWithUTF8String:path.c_str()]; - // TODO: honor mode + // TODO (issue #1759): honor mode [[NSFileManager defaultManager] createDirectoryAtPath:pathStr withIntermediateDirectories:FALSE attributes:nil error:&error]; return ConvertNSErrorCode(error, false); diff --git a/appshell/appshell_extensions_win.cpp b/appshell/appshell_extensions_win.cpp index 185c3d5b7..142456b85 100644 --- a/appshell/appshell_extensions_win.cpp +++ b/appshell/appshell_extensions_win.cpp @@ -556,6 +556,7 @@ int32 ReadDir(ExtensionString path, CefRefPtr& directoryContents) int32 MakeDir(ExtensionString path, int32 mode) { + // TODO (issue #1759): honor mode if (!CreateDirectory(path.c_str(), NULL)) return ConvertWinErrorCode(GetLastError(), false);