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 #120 from adobe/glenn/new-features
Browse files Browse the repository at this point in the history
Add fs.makedir() and fs.rename() APIs
  • Loading branch information
jasonsanjose committed Oct 3, 2012
2 parents a7e3eea + 3973bf7 commit 5f4cf3c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 13 deletions.
36 changes: 36 additions & 0 deletions appshell/appshell_extensions.cpp
Expand Up @@ -150,6 +150,42 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {


// Set response args for this function // Set response args for this function
responseArgs->SetList(2, directoryContents); 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") { } else if (message_name == "GetFileModificationTime") {
// Parameters: // Parameters:
// 0: int32 - callback id // 0: int32 - callback id
Expand Down
35 changes: 34 additions & 1 deletion appshell/appshell_extensions.js
Expand Up @@ -94,6 +94,11 @@ if (!appshell.app) {
*/ */
appshell.fs.ERR_NOT_DIRECTORY = 9; 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 * Display the OS File Open dialog, allowing the user to select
* files or directories. * files or directories.
Expand Down Expand Up @@ -144,7 +149,35 @@ if (!appshell.app) {
appshell.fs.readdir = function (path, callback) { appshell.fs.readdir = function (path, callback) {
var resultString = ReadDir(callback, path); 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. * Get information for the selected file or directory.
* *
Expand Down
27 changes: 27 additions & 0 deletions appshell/appshell_extensions_mac.mm
Expand Up @@ -312,11 +312,13 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,


[openPanel setAllowedFileTypes:allowedFileTypes]; [openPanel setAllowedFileTypes:allowedFileTypes];


[openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];
if ([openPanel runModal] == NSOKButton) if ([openPanel runModal] == NSOKButton)
{ {
NSArray* files = [openPanel filenames]; NSArray* files = [openPanel filenames];
NSArrayToCefList(files, selectedFiles); NSArrayToCefList(files, selectedFiles);
} }
[NSApp endSheet:openPanel];


return NO_ERROR; return NO_ERROR;
} }
Expand All @@ -337,6 +339,28 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
return ConvertNSErrorCode(error, true); 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) int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir)
{ {
NSString* path = [NSString stringWithUTF8String:filename.c_str()]; NSString* path = [NSString stringWithUTF8String:filename.c_str()];
Expand Down Expand Up @@ -493,6 +517,9 @@ int32 ConvertNSErrorCode(NSError* error, bool isReading)
case NSFileWriteOutOfSpaceError: case NSFileWriteOutOfSpaceError:
return ERR_OUT_OF_SPACE; return ERR_OUT_OF_SPACE;
break; break;
case NSFileWriteFileExistsError:
return ERR_FILE_EXISTS;
break;
} }


// Unknown error // Unknown error
Expand Down
5 changes: 5 additions & 0 deletions appshell/appshell_extensions_platform.h
Expand Up @@ -42,6 +42,7 @@ static const int ERR_CANT_WRITE = 6;
static const int ERR_OUT_OF_SPACE = 7; static const int ERR_OUT_OF_SPACE = 7;
static const int ERR_NOT_FILE = 8; static const int ERR_NOT_FILE = 8;
static const int ERR_NOT_DIRECTORY = 9; static const int ERR_NOT_DIRECTORY = 9;
static const int ERR_FILE_EXISTS = 10;


#if defined(OS_WIN) #if defined(OS_WIN)
typedef std::wstring ExtensionString; typedef std::wstring ExtensionString;
Expand All @@ -67,6 +68,10 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,


int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents); 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 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir);


int32 ReadFile(ExtensionString filename, ExtensionString encoding, std::string& contents); int32 ReadFile(ExtensionString filename, ExtensionString encoding, std::string& contents);
Expand Down
19 changes: 19 additions & 0 deletions appshell/appshell_extensions_win.cpp
Expand Up @@ -554,6 +554,23 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
return NO_ERROR; 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) int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir)
{ {
DWORD dwAttr = GetFileAttributes(filename.c_str()); DWORD dwAttr = GetFileAttributes(filename.c_str());
Expand Down Expand Up @@ -740,6 +757,8 @@ int ConvertWinErrorCode(int errorCode, bool isReading)
return ERR_CANT_WRITE; return ERR_CANT_WRITE;
case ERROR_HANDLE_DISK_FULL: case ERROR_HANDLE_DISK_FULL:
return ERR_OUT_OF_SPACE; return ERR_OUT_OF_SPACE;
case ERROR_ALREADY_EXISTS:
return ERR_FILE_EXISTS;
default: default:
return ERR_UNKNOWN; return ERR_UNKNOWN;
} }
Expand Down
42 changes: 30 additions & 12 deletions appshell/cefclient_mac.mm
Expand Up @@ -353,23 +353,41 @@ - (void)createApp:(id)object {


settings.web_security_disabled = true; 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(), CefBrowserHost::CreateBrowser(window_info, g_handler.get(),
[[startupUrl absoluteString] UTF8String], settings); [[startupUrl absoluteString] UTF8String], settings);


// Show the window. // Show the window.
[mainWnd display];
[mainWnd makeKeyAndOrderFront: nil]; [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 // Sent by the default notification center immediately before the application
Expand Down

0 comments on commit 5f4cf3c

Please sign in to comment.