Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add fs.makedir() and fs.rename() APIs #120

Merged
merged 6 commits into from

3 participants

@gruehle
Owner

Add two new functions to brackets.fs:

makedir()

/**
 * 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.
 **/
function makedir(path, mode, callback);

NOTE
The mode parameter of makedir() is ignored for now. The directory is created with the full
permissions available to the current user.

rename()

/**
 * 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.
 **/
function rename(oldPath, newPath, callback);

BONUS FEATURES
This pull request also includes two small bonus features on the mac:

  • The main window size and position is now remembered when quitting and relaunching Brackets. This change will also fix the issues on the Mac where some of the UI is not shown if the initial window size is too small (adobe/brackets#1689)
  • The file dialogs are now modal sheets instead of modal dialogs.
@gruehle gruehle referenced this pull request in adobe/brackets
Merged

Add "New Folder" and "Rename" features #1719

@redmunds redmunds was assigned
@redmunds
Collaborator

I am getting a build error on Mac OS X v10.6 due to the NSFileWriteFileExistsError flag is only available on OS X v10.7 and later.

@gruehle
Owner

Thanks for reviewing!

Are you using XCode 4.x? XCode 3.2.6 is no longer supported.

I don't have a 10.6 system with me for testing, but this should be fine when running on a 10.6 system. If you try to rename an item to a name of an existing item, you will get a less friendly error message, but everything should work fine.

@jasonsanjose
Owner

I'll take on the mac side of the review.

@jasonsanjose jasonsanjose was assigned
@redmunds
Collaborator

Windows looks good. Re-assigning to @jasonsanjose to take a look at Mac (since I need to upgrade to Xcode 4).

appshell/appshell_extensions_mac.mm
@@ -337,6 +339,28 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& 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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
appshell/appshell_extensions_win.cpp
@@ -554,6 +554,22 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
return NO_ERROR;
}
+int32 MakeDir(ExtensionString path, int32 mode)
+{
+ if (!CreateDirectory(path.c_str(), NULL))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jasonsanjose
Owner

Looks good. Merging

@jasonsanjose jasonsanjose merged commit 5f4cf3c into master
@DennisKehrig DennisKehrig referenced this pull request from a commit in DennisKehrig/brackets-shell
@DennisKehrig DennisKehrig Modified fix for adobe/brackets#1147 based on Glenn's work in #120
Makes sure that the default size definitely fits the available space. It will only be used on the first launch of Brackets.
Subsequent launches use stored window size and position which is adjusted automatically by the OS.
bc3cc66
@DennisKehrig DennisKehrig referenced this pull request
Merged

Substitute for #124 #125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
36 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
View
35 appshell/appshell_extensions.js
@@ -95,6 +95,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.
*
@@ -144,7 +149,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.
*
View
27 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;
}
@@ -337,6 +339,28 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
return ConvertNSErrorCode(error, true);
}
+int32 MakeDir(ExtensionString path, int32 mode)
+{
+ NSError* error = nil;
+ NSString* pathStr = [NSString stringWithUTF8String:path.c_str()];
+
+ // TODO (issue #1759): 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()];
@@ -493,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
View
5 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;
@@ -67,6 +68,10 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,
int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& 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);
View
19 appshell/appshell_extensions_win.cpp
@@ -554,6 +554,23 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
return NO_ERROR;
}
+int32 MakeDir(ExtensionString path, int32 mode)
+{
+ // TODO (issue #1759): honor 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 +757,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;
}
View
42 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
Something went wrong with that request. Please try again.