void FolderMenu::createCreateNewMenu() { QMenu* createMenu = new QMenu(this); createNewMenu_ = createMenu; QAction* action = new QAction(tr("Folder"), this); connect(action, SIGNAL(triggered(bool)), SLOT(onCreateNewFolder())); createMenu->addAction(action); action = new QAction(tr("Blank File"), this); connect(action, SIGNAL(triggered(bool)), SLOT(onCreateNewFile())); createMenu->addAction(action); // add more items to "Create New" menu from templates GList* templates = fm_template_list_all(fm_config->only_user_templates); if(templates) { createMenu->addSeparator(); for(GList* l = templates; l; l = l->next) { FmTemplate* templ = (FmTemplate*)l->data; /* we support directories differently */ if(fm_template_is_directory(templ)) continue; FmMimeType* mime_type = fm_template_get_mime_type(templ); const char* label = fm_template_get_label(templ); QString text = QString("%1 (%2)").arg(QString::fromUtf8(label)).arg(QString::fromUtf8(fm_mime_type_get_desc(mime_type))); FmIcon* icon = fm_template_get_icon(templ); if(!icon) icon = fm_mime_type_get_icon(mime_type); QAction* action = createMenu->addAction(IconTheme::icon(icon), text); action->setObjectName(QString::fromUtf8(fm_template_get_name(templ, NULL))); connect(action, SIGNAL(triggered(bool)), SLOT(onCreateNew())); } } }
CreateNewMenu::CreateNewMenu(QWidget* dialogParent, FmPath* dirPath, QWidget* parent): QMenu(parent), dialogParent_(dialogParent), dirPath_(dirPath) { QAction* action = new QAction(QIcon::fromTheme("folder-new"), tr("Folder"), this); connect(action, &QAction::triggered, this, &CreateNewMenu::onCreateNewFolder); addAction(action); action = new QAction(QIcon::fromTheme("document-new"), tr("Blank File"), this); connect(action, &QAction::triggered, this, &CreateNewMenu::onCreateNewFile); addAction(action); // add more items to "Create New" menu from templates GList* templates = fm_template_list_all(fm_config->only_user_templates); if(templates) { addSeparator(); for(GList* l = templates; l; l = l->next) { FmTemplate* templ = (FmTemplate*)l->data; /* we support directories differently */ if(fm_template_is_directory(templ)) continue; FmMimeType* mime_type = fm_template_get_mime_type(templ); const char* label = fm_template_get_label(templ); QString text = QString("%1 (%2)").arg(QString::fromUtf8(label)).arg(QString::fromUtf8(fm_mime_type_get_desc(mime_type))); FmIcon* icon = fm_template_get_icon(templ); if(!icon) icon = fm_mime_type_get_icon(mime_type); QAction* action = addAction(IconTheme::icon(icon), text); action->setObjectName(QString::fromUtf8(fm_template_get_name(templ, NULL))); connect(action, &QAction::triggered, this, &CreateNewMenu::onCreateNew); } } }
// templateFile is a file path used as a template of the new file. void createFile(CreateFileType type, FmPath* parentDir, FmTemplate* templ, QWidget* parent) { QString defaultNewName; QString prompt; switch(type) { case CreateNewTextFile: prompt = QObject::tr("Please enter a new file name:"); defaultNewName = QObject::tr("New text file"); break; case CreateNewFolder: prompt = QObject::tr("Please enter a new folder name:"); defaultNewName = QObject::tr("New folder"); break; case CreateWithTemplate: { FmMimeType* mime = fm_template_get_mime_type(templ); prompt = QObject::tr("Enter a name for the new %1:").arg(QString::fromUtf8(fm_mime_type_get_desc(mime))); defaultNewName = QString::fromUtf8(fm_template_get_name(templ, NULL)); } break; } _retry: // ask the user to input a file name bool ok; QString new_name = QInputDialog::getText(parent, QObject::tr("Create File"), prompt, QLineEdit::Normal, defaultNewName, &ok); if(!ok) return; GFile* parent_gf = fm_path_to_gfile(parentDir); GFile* dest_gf = g_file_get_child(G_FILE(parent_gf), new_name.toLocal8Bit().data()); g_object_unref(parent_gf); GError* err = NULL; switch(type) { case CreateNewTextFile: { GFileOutputStream* f = g_file_create(dest_gf, G_FILE_CREATE_NONE, NULL, &err); if(f) { g_output_stream_close(G_OUTPUT_STREAM(f), NULL, NULL); g_object_unref(f); } break; } case CreateNewFolder: g_file_make_directory(dest_gf, NULL, &err); break; case CreateWithTemplate: fm_template_create_file(templ, dest_gf, &err, false); break; } g_object_unref(dest_gf); if(err) { if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_EXISTS) { g_error_free(err); err = NULL; goto _retry; } QMessageBox::critical(parent, QObject::tr("Error"), err->message); g_error_free(err); } }