diff --git a/krusader/Panel/listpanel.h b/krusader/Panel/listpanel.h --- a/krusader/Panel/listpanel.h +++ b/krusader/Panel/listpanel.h @@ -113,7 +113,7 @@ return _actions; } QString lastLocalPath() const; - QString getCurrentName(); + QString getCurrentName() const; QStringList getSelectedNames(); void setButtons(); void setJumpBack(QUrl url); diff --git a/krusader/Panel/listpanel.cpp b/krusader/Panel/listpanel.cpp --- a/krusader/Panel/listpanel.cpp +++ b/krusader/Panel/listpanel.cpp @@ -879,13 +879,10 @@ PanelContextMenu::run(loc, this); } -QString ListPanel::getCurrentName() +QString ListPanel::getCurrentName() const { - QString name = view->getCurrentItem(); - if (name != "..") - return name; - else - return QString(); + const QString name = view->getCurrentItem(); + return name == ".." ? QString() : name; } QStringList ListPanel::getSelectedNames() diff --git a/krusader/Panel/listpanelactions.h b/krusader/Panel/listpanelactions.h --- a/krusader/Panel/listpanelactions.h +++ b/krusader/Panel/listpanelactions.h @@ -54,8 +54,8 @@ public: QAction *actRenameF2, *actViewFileF3, *actEditFileF4, *actCopyF5, *actMoveF6, *actNewFolderF7, *actDeleteF8, *actTerminalF9; - QAction *actCopyDelayedF5, *actMoveDelayedShiftF6; - QAction *actProperties, *actPack, *actUnpack, *actTest, *actCompDirs, *actCalculate, *actSync; + QAction *actNewFileShiftF4, *actCopyDelayedF5, *actMoveDelayedShiftF6; + QAction *actProperties, *actPack, *actUnpack, *actTest, *actCompDirs, *actCalculate, *actSync; QAction *actFTPConnect, *actFTPNewConnect, *actFTPDisconnect; QAction *actLocationBar, *actJumpBack, *actSetJumpBack; QAction *actCreateChecksum, *actMatchChecksum; diff --git a/krusader/Panel/listpanelactions.cpp b/krusader/Panel/listpanelactions.cpp --- a/krusader/Panel/listpanelactions.cpp +++ b/krusader/Panel/listpanelactions.cpp @@ -80,15 +80,15 @@ // Fn keys actRenameF2 = action(i18n("Rename"), "edit-rename", Qt::Key_F2, _func, SLOT(rename()) , "F2_Rename"); actViewFileF3 = action(i18n("View File"), nullptr, Qt::Key_F3, _func, SLOT(view()), "F3_View"); - actEditFileF4 = action(i18n("Edit File"), nullptr, Qt::Key_F4, _func, SLOT(edit()) , "F4_Edit"); + actEditFileF4 = action(i18n("Edit File"), nullptr, Qt::Key_F4, _func, SLOT(editFile()), "F4_Edit"); + actNewFileShiftF4 = action(i18n("&New Text File..."), "document-new", Qt::SHIFT + Qt::Key_F4, _func, SLOT(editNewFile()), "edit_new_file"); actCopyF5 = action(i18n("Copy to other panel"), nullptr, Qt::Key_F5, _func, SLOT(copyFiles()) , "F5_Copy"); actMoveF6 = action(i18n("Move to other panel"), nullptr, Qt::Key_F6, _func, SLOT(moveFiles()) , "F6_Move"); actCopyDelayedF5 = action(i18n("Copy delayed..."), nullptr, Qt::SHIFT + Qt::Key_F5, _func, SLOT(copyFilesDelayed()) , "F5_Copy_Queue"); actMoveDelayedShiftF6 = action(i18n("Move delayed..."), nullptr, Qt::SHIFT + Qt::Key_F6, _func, SLOT(moveFilesDelayed()) , "F6_Move_Queue"); actNewFolderF7 = action(i18n("New Folder..."), "folder-new", Qt::Key_F7, _func, SLOT(mkdir()) , "F7_Mkdir"); actDeleteF8 = action(i18n("Delete"), "edit-delete", Qt::Key_F8, _func, SLOT(defaultDeleteFiles()) , "F8_Delete"); actTerminalF9 = action(i18n("Start Terminal Here"), "utilities-terminal", Qt::Key_F9, _func, SLOT(terminal()) , "F9_Terminal"); - action(i18n("&New Text File..."), "document-new", Qt::SHIFT + Qt::Key_F4, _func, SLOT(editNew()), "edit_new_file"); action(i18n("F3 View Dialog"), nullptr, Qt::SHIFT + Qt::Key_F3, _func, SLOT(viewDlg()), "F3_ViewDlg"); // file operations diff --git a/krusader/Panel/panelcontextmenu.h b/krusader/Panel/panelcontextmenu.h --- a/krusader/Panel/panelcontextmenu.h +++ b/krusader/Panel/panelcontextmenu.h @@ -73,8 +73,6 @@ SYNC_SELECTED_ID, SEND_BY_EMAIL_ID, EJECT_ID, - MKDIR_ID, - NEW_TEXT_FILE_ID, SERVICE_LIST_ID // ALWAYS KEEP THIS ONE LAST!!! }; diff --git a/krusader/Panel/panelcontextmenu.cpp b/krusader/Panel/panelcontextmenu.cpp --- a/krusader/Panel/panelcontextmenu.cpp +++ b/krusader/Panel/panelcontextmenu.cpp @@ -313,11 +313,8 @@ void PanelContextMenu::addCreateNewMenu() { auto *createNewMenu = new QMenu(this); - - createNewMenu->addAction(Icon("folder"), - i18n("Folder..."))->setData(QVariant(MKDIR_ID)); - createNewMenu->addAction(Icon("text-plain"), - i18n("Text File..."))->setData(QVariant(NEW_TEXT_FILE_ID)); + createNewMenu->addAction(panel->gui->actions()->actNewFolderF7); + createNewMenu->addAction(panel->gui->actions()->actNewFileShiftF4); QAction *newMenuAction = addMenu(createNewMenu); newMenuAction->setText(i18n("Create New")); @@ -394,12 +391,6 @@ SLOTS->sendFileByEmail(_items.urlList()); break; } - case MKDIR_ID : - panel->func->mkdir(); - break; - case NEW_TEXT_FILE_ID: - panel->func->editNew(); - break; #ifdef SYNCHRONIZER_ENABLED case SYNC_SELECTED_ID : { QStringList selectedNames; diff --git a/krusader/Panel/panelfunc.h b/krusader/Panel/panelfunc.h --- a/krusader/Panel/panelfunc.h +++ b/krusader/Panel/panelfunc.h @@ -65,8 +65,9 @@ void terminal(); void view(); void viewDlg(); - void edit(); - void editNew(); // create a new textfile and edit it + void editFile(const QUrl &fileToCreate = QUrl()); + /** Create a new textfile and edit it. */ + void editNewFile(); void moveFilesDelayed() { moveFiles(true); } void copyFilesDelayed() { copyFiles(true); } void moveFiles(bool enqueue = false) { copyFiles(enqueue, true); } @@ -130,7 +131,7 @@ protected slots: // Load the current url from history and refresh filesystem and panel to it void doRefresh(); - void slotFileCreated(KJob *job); // a file has been created by editNewFile() + void slotFileCreated(KJob *job, const QUrl filePath); // a file has been created by editNewFile() void historyGotoPos(int pos); void clipboardChanged(QClipboard::Mode mode); // Update the directory size in view @@ -151,7 +152,6 @@ FileSystem* fileSystemP; // pointer to fileSystem. QTimer delayTimer; QUrl syncURL; - QUrl fileToCreate; // file that's to be created by editNewFile() bool urlManuallyEntered; static QPointer copyToClipboardOrigin; diff --git a/krusader/Panel/panelfunc.cpp b/krusader/Panel/panelfunc.cpp --- a/krusader/Panel/panelfunc.cpp +++ b/krusader/Panel/panelfunc.cpp @@ -82,6 +82,7 @@ #include "../KViewer/krviewer.h" #include "../MountMan/kmountman.h" + QPointer ListPanelFunc::copyToClipboardOrigin; ListPanelFunc::ListPanelFunc(ListPanel *parent) : QObject(parent), @@ -470,77 +471,84 @@ SLOTS->runTerminal(panel->lastLocalPath()); } -void ListPanelFunc::edit() +void ListPanelFunc::editFile(const QUrl &newFilePath) { panel->searchBar->hideBarIfSearching(); - KFileItem tmp; - if (fileToCreate.isEmpty()) { - QString name = panel->getCurrentName(); + QUrl editPath; + if (!newFilePath.isEmpty()) { + editPath = newFilePath; + } else { + const QString name = panel->getCurrentName(); if (name.isNull()) return; - fileToCreate = files()->getUrl(name); + editPath = files()->getUrl(name); } - tmp = KFileItem(fileToCreate); + const KFileItem fileToEdit = KFileItem(newFilePath); - if (tmp.isDir()) { + if (fileToEdit.isDir()) { KMessageBox::sorry(krMainWindow, i18n("You cannot edit a folder")); - fileToCreate = QUrl(); return; } - if (!tmp.isReadable()) { + if (!fileToEdit.isReadable()) { KMessageBox::sorry(nullptr, i18n("No permissions to edit this file.")); - fileToCreate = QUrl(); return; } - KrViewer::edit(fileToCreate); - fileToCreate = QUrl(); + KrViewer::edit(editPath); } -void ListPanelFunc::editNew() +void ListPanelFunc::editNewFile() { - if(!fileToCreate.isEmpty()) - return; - // ask the user for the filename to edit - fileToCreate = KChooseDir::getFile(i18n("Enter the filename to edit:"), QUrl(panel->getCurrentName()), panel->virtualPath()); - if(fileToCreate.isEmpty()) + const QUrl filePath = KChooseDir::getFile(i18n("Enter the filename to edit:"), + QUrl(panel->getCurrentName()), panel->virtualPath()); + if(filePath.isEmpty()) return ; // the user canceled - // if the file exists, edit it instead of creating a new one - QFile f(fileToCreate.toLocalFile()); - - if(f.exists()) { - edit(); - } else { - auto *tempFile = new QTemporaryFile; - tempFile->open(); - - KIO::CopyJob *job = KIO::copy(QUrl::fromLocalFile(tempFile->fileName()), fileToCreate); - job->setUiDelegate(nullptr); - job->setDefaultPermissions(true); - connect(job, &KIO::CopyJob::result, this, &ListPanelFunc::slotFileCreated); - connect(job, &KIO::CopyJob::result, tempFile, &QTemporaryFile::deleteLater); + if (filePath.isLocalFile()) { + // if the file exists, edit it instead of creating a new one + QFile file(filePath.toLocalFile()); + if (file.exists()) { + editFile(); + return; + } else { + // simply create a local file + // also because KIO::CopyJob::setDefaultPermissions does not work + file.open(QIODevice::NewOnly); + file.close(); + slotFileCreated(nullptr, filePath); + return; + } } + + auto *tempFile = new QTemporaryFile; + tempFile->setAutoRemove(false); // done below + tempFile->open(); // create file + + KIO::CopyJob *job = KIO::copy(QUrl::fromLocalFile(tempFile->fileName()), filePath); + job->setUiDelegate(nullptr); + job->setDefaultPermissions(true); + connect(job, &KIO::CopyJob::result, this, [=](KJob *job) { slotFileCreated(job, filePath); }); + connect(job, &KIO::CopyJob::result, tempFile, &QTemporaryFile::deleteLater); } -void ListPanelFunc::slotFileCreated(KJob *job) +void ListPanelFunc::slotFileCreated(KJob *job, const QUrl filePath) { - if(!job->error() || job->error() == KIO::ERR_FILE_ALREADY_EXIST) { - KrViewer::edit(fileToCreate); + if (!job || (!job->error() || job->error() == KIO::ERR_FILE_ALREADY_EXIST)) { + KrViewer::edit(filePath); - if(KIO::upUrl(fileToCreate).matches(panel->virtualPath(), QUrl::StripTrailingSlash)) + if (KIO::upUrl(filePath).matches(panel->virtualPath(), QUrl::StripTrailingSlash)) { refresh(); - else if(KIO::upUrl(fileToCreate).matches(panel->otherPanel()->virtualPath(), QUrl::StripTrailingSlash)) + } + if (KIO::upUrl(filePath).matches(panel->otherPanel()->virtualPath(), QUrl::StripTrailingSlash)) { otherFunc()->refresh(); - } - else + } + } else { KMessageBox::sorry(krMainWindow, job->errorString()); - - fileToCreate = QUrl(); + } } void ListPanelFunc::copyFiles(bool enqueue, bool move)