MdeModulePkg/FileExplorer: Enable functionality of creating new file/folder

https://bugzilla.tianocore.org/show_bug.cgi?id=324

Enhance the FileExplorerlib so that user can create a new file/folder
through the UI page.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
Reviewed-by: Eric Dong <eric.dong@intel.com>
This commit is contained in:
Dandan Bi
2017-01-12 20:25:40 +08:00
committed by Hao Wu
parent 9cf1aa29cf
commit a409875592
5 changed files with 267 additions and 6 deletions

View File

@@ -73,6 +73,24 @@ VOID *mLibEndOpCodeHandle = NULL;
EFI_IFR_GUID_LABEL *mLibStartLabel = NULL;
EFI_IFR_GUID_LABEL *mLibEndLabel = NULL;
UINT16 mQuestionIdUpdate;
CHAR16 mNewFileName[MAX_FILE_NAME_LEN];
CHAR16 mNewFolderName[MAX_FOLDER_NAME_LEN];
UINTN mNewFileQuestionId = NEW_FILE_QUESTION_ID_BASE;
UINTN mNewFolderQuestionId = NEW_FOLDER_QUESTION_ID_BASE;
/**
Create a new file or folder in current directory.
@param FileName Point to the fileNmae or folder.
@param CreateFile CreateFile== TRUE means create a new file.
CreateFile== FALSE means create a new Folder.
**/
EFI_STATUS
LibCreateNewFile (
IN CHAR16 *FileName,
IN BOOLEAN CreateFile
);
/**
This function allows a caller to extract the current configuration for one
@@ -175,9 +193,13 @@ LibCallback (
{
EFI_STATUS Status;
BOOLEAN NeedExit;
CHAR16 *NewFileName;
CHAR16 *NewFolderName;
NeedExit = TRUE;
NewFileName = NULL;
NewFolderName = NULL;
if (Action != EFI_BROWSER_ACTION_CHANGING && Action != EFI_BROWSER_ACTION_CHANGED) {
//
// Do nothing for other UEFI Action. Only do call back when data is changed.
@@ -189,7 +211,55 @@ LibCallback (
if ((Value == NULL) || (ActionRequest == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (QuestionId == KEY_VALUE_CREATE_FILE_AND_EXIT) {
*ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
if (!IsZeroBuffer (mNewFileName, sizeof (mNewFileName))) {
Status = LibCreateNewFile (mNewFileName,TRUE);
ZeroMem (mNewFileName,sizeof (mNewFileName));
}
}
if (QuestionId == KEY_VALUE_NO_CREATE_FILE_AND_EXIT) {
ZeroMem (mNewFileName,sizeof (mNewFileName));
*ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
}
if (QuestionId == KEY_VALUE_CREATE_FOLDER_AND_EXIT) {
*ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
if (!IsZeroBuffer (mNewFolderName, sizeof (mNewFolderName))) {
Status = LibCreateNewFile (mNewFolderName, FALSE);
ZeroMem (mNewFolderName,sizeof (mNewFolderName));
}
}
if (QuestionId == KEY_VALUE_NO_CREATE_FOLDER_AND_EXIT) {
ZeroMem (mNewFolderName,sizeof (mNewFolderName));
*ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
}
if (QuestionId == NEW_FILE_NAME_ID) {
NewFileName = HiiGetString (gFileExplorerPrivate.FeHiiHandle, Value->string, NULL);
if (NewFileName != NULL) {
StrCpyS (mNewFileName, MAX_FILE_NAME_LEN, NewFileName);
FreePool (NewFileName);
NewFileName = NULL;
} else {
return EFI_INVALID_PARAMETER;
}
}
if (QuestionId == NEW_FOLDER_NAME_ID) {
NewFolderName = HiiGetString (gFileExplorerPrivate.FeHiiHandle, Value->string, NULL);
if (NewFolderName != NULL) {
StrCpyS (mNewFolderName, MAX_FOLDER_NAME_LEN, NewFolderName);
FreePool (NewFolderName);
NewFolderName = NULL;
} else {
return EFI_INVALID_PARAMETER;
}
}
if (QuestionId >= FILE_OPTION_OFFSET) {
LibGetDevicePath(QuestionId);
@@ -208,8 +278,8 @@ LibCallback (
if (Value == NULL) {
return EFI_INVALID_PARAMETER;
}
if (QuestionId >= FILE_OPTION_OFFSET) {
LibGetDevicePath(QuestionId);
Status = LibUpdateFileExplorer (QuestionId);
if (EFI_ERROR (Status)) {
return Status;
@@ -985,6 +1055,72 @@ Done:
return Status;
}
/**
Create a new file or folder in current directory.
@param FileName Point to the fileNmae or folder name.
@param CreateFile CreateFile== TRUE means create a new file.
CreateFile== FALSE means create a new Folder.
**/
EFI_STATUS
LibCreateNewFile (
IN CHAR16 *FileName,
IN BOOLEAN CreateFile
)
{
EFI_FILE_HANDLE FileHandle;
EFI_FILE_HANDLE NewHandle;
EFI_HANDLE DeviceHandle;
EFI_STATUS Status;
CHAR16 *ParentName;
CHAR16 *FullFileName;
NewHandle = NULL;
FullFileName = NULL;
LibGetFileHandleFromDevicePath(gFileExplorerPrivate.RetDevicePath, &FileHandle, &ParentName, &DeviceHandle);
FullFileName = LibAppendFileName (ParentName, FileName);
if (FullFileName == NULL) {
return EFI_OUT_OF_RESOURCES;
}
if (CreateFile) {
Status = FileHandle->Open(
FileHandle,
&NewHandle,
FullFileName,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE| EFI_FILE_MODE_CREATE,
0
);
if (EFI_ERROR (Status)) {
FileHandle->Close (FileHandle);
return Status;
}
} else {
Status = FileHandle->Open(
FileHandle,
&NewHandle,
FullFileName,
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE| EFI_FILE_MODE_CREATE,
EFI_FILE_DIRECTORY
);
if (EFI_ERROR (Status)) {
FileHandle->Close (FileHandle);
return Status;
}
}
FileHandle->Close (FileHandle);
//
// Return the DevicePath of the new created file or folder.
//
gFileExplorerPrivate.RetDevicePath = FileDevicePath (DeviceHandle, FullFileName);
return EFI_SUCCESS;
}
/**
Find files under current directory.
@@ -1177,9 +1313,11 @@ LibUpdateFileExplorePage (
MENU_ENTRY *NewMenuEntry;
FILE_CONTEXT *NewFileContext;
MENU_OPTION *MenuOption;
BOOLEAN CreateNewFile;
NewMenuEntry = NULL;
NewFileContext = NULL;
CreateNewFile = FALSE;
LibRefreshUpdateData ();
MenuOption = gFileExplorerPrivate.FsOptionMenu;
@@ -1190,6 +1328,32 @@ LibUpdateFileExplorePage (
NewMenuEntry = LibGetMenuEntry (MenuOption, Index);
NewFileContext = (FILE_CONTEXT *) NewMenuEntry->VariableContext;
if (!NewFileContext->IsRoot && !CreateNewFile) {
HiiCreateGotoOpCode (
mLibStartOpCodeHandle,
FORM_ADD_NEW_FILE_ID,
STRING_TOKEN (STR_NEW_FILE),
STRING_TOKEN (STR_NEW_FILE_HELP),
EFI_IFR_FLAG_CALLBACK,
(UINT16) (mNewFileQuestionId++)
);
HiiCreateGotoOpCode (
mLibStartOpCodeHandle,
FORM_ADD_NEW_FOLDER_ID,
STRING_TOKEN (STR_NEW_FOLDER),
STRING_TOKEN (STR_NEW_FOLDER_HELP),
EFI_IFR_FLAG_CALLBACK,
(UINT16) (mNewFolderQuestionId++)
);
HiiCreateTextOpCode(
mLibStartOpCodeHandle,
STRING_TOKEN (STR_NULL_STRING),
STRING_TOKEN (STR_NULL_STRING),
0
);
CreateNewFile = TRUE;
}
if (!NewFileContext->IsDir) {
//
// Create Text opcode for directory, also create Text opcode for file in FileExplorerStateBootFromFile.