// Read directory contents bool FileSpecifier::ReadDirectory(vector<dir_entry> &vec) { vec.clear(); #if defined(__MVCPP__) WIN32_FIND_DATA findData; // We need to add a wildcard to the search name string search_name; search_name = name; search_name += "\\*.*"; HANDLE hFind = ::FindFirstFile(search_name.c_str(), &findData); if (hFind == INVALID_HANDLE_VALUE) { err = ::GetLastError(); return false; } do { // Exclude current and parent directories if (findData.cFileName[0] != '.' || (findData.cFileName[1] && findData.cFileName[1] != '.')) { // Return found files to dir_entry int32 fileSize = (findData.nFileSizeHigh * MAXDWORD) + findData.nFileSizeLow; vec.push_back(dir_entry(findData.cFileName, fileSize, (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0, false)); } } while(::FindNextFile(hFind, &findData)); if (!::FindClose(hFind)) err = ::GetLastError(); // not sure if we should return this or not else err = 0; return true; #else DIR *d = opendir(GetPath()); if (d == NULL) { err = errno; return false; } struct dirent *de = readdir(d); while (de) { FileSpecifier full_path = name; full_path += de->d_name; struct stat st; if (stat(full_path.GetPath(), &st) == 0) { // Ignore files starting with '.' and the directories '.' and '..' if (de->d_name[0] != '.' || (S_ISDIR(st.st_mode) && !(de->d_name[1] == '\0' || de->d_name[1] == '.'))) vec.push_back(dir_entry(de->d_name, st.st_size, S_ISDIR(st.st_mode), false, st.st_mtime)); } de = readdir(d); } closedir(d); err = 0; return true; #endif }
//------------------------------------------------------------------------- // // Show - Display the file dialog // //------------------------------------------------------------------------- NS_IMETHODIMP nsFilePicker::Show(PRInt16 *retval) { PRBool result = PR_TRUE; nsFilePanelBeOS *ppanel; file_panel_mode panel_mode; bool allow_multiple_selection = false; uint32 node_flavors; if (mMode == modeGetFolder) { node_flavors = B_DIRECTORY_NODE; panel_mode = B_OPEN_PANEL; } else if (mMode == modeOpen) { node_flavors = B_FILE_NODE; panel_mode = B_OPEN_PANEL; } else if (mMode == modeOpenMultiple) { node_flavors = B_FILE_NODE; panel_mode = B_OPEN_PANEL; allow_multiple_selection = true; } else if (mMode == modeSave) { node_flavors = B_FILE_NODE; panel_mode = B_SAVE_PANEL; } else { printf("nsFilePicker::Show() wrong mode"); return PR_FALSE; } ppanel = new nsFilePanelBeOS( panel_mode, //file_panel_mode mode node_flavors, //uint32 node_flavors allow_multiple_selection, //bool allow_multiple_selection false, //bool modal true //bool hide_when_done ); if (!ppanel) return PR_FALSE; // set title if (!mTitle.IsEmpty()) { char *title_utf8 = ToNewUTF8String(mTitle); ppanel->Window()->SetTitle(title_utf8); Recycle(title_utf8); } // set default text if (!mDefault.IsEmpty()) { char *defaultText = ToNewUTF8String(mDefault); ppanel->SetSaveText(defaultText); Recycle(defaultText); } // set initial directory nsCAutoString initialDir; if (mDisplayDirectory) mDisplayDirectory->GetNativePath(initialDir); if(initialDir.IsEmpty()) { #ifdef FILEPICKER_SAVE_LAST_DIR if (strlen(mLastUsedDirectory) < 2) initialDir.Assign("/boot/home"); else initialDir.Assign(mLastUsedDirectory); #else ppanel->SetPanelDirectory(initialDir.get()); #endif } #ifdef FILEPICKER_SAVE_LAST_DIR ppanel->SetPanelDirectory(initialDir.get()); #endif // set modal feel if (ppanel->LockLooper()) { ppanel->Window()->SetFeel(B_MODAL_APP_WINDOW_FEEL); ppanel->UnlockLooper(); } // Show File Panel ppanel->Show(); ppanel->WaitForSelection(); if (ppanel->IsCancelSelected()) { result = PR_FALSE; } if ((mMode == modeOpen || mMode == modeOpenMultiple || mMode == modeGetFolder) && ppanel->IsOpenSelected()) { BList *list = ppanel->OpenRefs(); uint32 numfiles = list->CountItems(); if ((list) && numfiles >= 1) { nsresult rv = NS_NewISupportsArray(getter_AddRefs(mFiles)); for (uint32 i = 0; i< numfiles; i++) { BPath *path = (BPath *)list->ItemAt(i); if (path->InitCheck() == B_OK) { mFile.Truncate(); // Single and Multiple are exclusive now, though, maybe there is sense // to assign also first list element to mFile even in openMultiple case ? if (mMode == modeOpenMultiple) { nsCOMPtr<nsILocalFile> file = do_CreateInstance("@mozilla.org/file/local;1", &rv); NS_ENSURE_SUCCESS(rv,rv); rv = file->InitWithNativePath(nsDependentCString(path->Path())); NS_ENSURE_SUCCESS(rv,rv); rv = mFiles->AppendElement(file); NS_ENSURE_SUCCESS(rv,rv); } else { if (i == 0) mFile.Assign(path->Path()); } } else { printf("path.init failed \n"); } } } else { printf("list not init \n"); } } else if (mMode == modeSave && ppanel->IsSaveSelected()) { BString savefilename = ppanel->SaveFileName(); entry_ref ref = ppanel->SaveDirRef(); BPath path(&ref); if (path.InitCheck() == B_OK) { path.Append(savefilename.String(), true); mFile.Assign(path.Path()); } } else { result = PR_FALSE; } // set current directory to mDisplayDirectory entry_ref dir_ref; ppanel->GetPanelDirectory(&dir_ref); BEntry dir_entry(&dir_ref); BPath dir_path; dir_entry.GetPath(&dir_path); if (!mDisplayDirectory) mDisplayDirectory = do_CreateInstance("@mozilla.org/file/local;1"); if (mDisplayDirectory) mDisplayDirectory->InitWithNativePath(nsDependentCString(dir_path.Path())); if (ppanel->Lock()) { ppanel->Quit(); } if (result) { PRInt16 returnOKorReplace = returnOK; #ifdef FILEPICKER_SAVE_LAST_DIR strncpy(mLastUsedDirectory, dir_path.Path(), B_PATH_NAME_LENGTH+1); if (mDisplayDirectory) mDisplayDirectory->InitWithNativePath( nsDependentCString(mLastUsedDirectory) ); #endif if (mMode == modeSave) { // we must check if file already exists PRBool exists = PR_FALSE; nsCOMPtr<nsILocalFile> file(do_CreateInstance("@mozilla.org/file/local;1")); NS_ENSURE_TRUE(file, NS_ERROR_FAILURE); file->InitWithNativePath(mFile); file->Exists(&exists); if (exists) returnOKorReplace = returnReplace; } *retval = returnOKorReplace; } else { *retval = returnCancel; } return NS_OK; // TODO: implement filters }