Commit c2d98cb9 authored by Eugene Popov's avatar Eugene Popov 🇺🇦 Committed by Christoph Cullmann
Browse files

Better code to open folder

When opening a folder, if the Projects plugin is not enabled, then enable it at the user's request.
parent f55d14e9
Pipeline #249014 passed with stage
in 8 minutes and 1 second
......@@ -299,7 +299,7 @@ void KateMainWindow::setupActions()
m_fileOpenRecent = KStandardAction::openRecent(
m_viewManager,
[this](const QUrl &url) {
Utils::openUrlOrProject(viewManager(), url);
viewManager()->openUrlOrProject(url);
},
this);
m_fileOpenRecent->setMaxItems(KateConfigDialog::recentFilesMaxCount());
......
......@@ -13,6 +13,7 @@
#include "katemainwindow.h"
#include "kateupdatedisabler.h"
#include "kateviewspace.h"
#include <kwidgetsaddons_version.h>
#include "welcomeview/welcomeview.h"
#include <KTextEditor/Attribute>
......@@ -20,6 +21,7 @@
#include <KTextEditor/View>
#include <KActionCollection>
#include <KAboutData>
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
......@@ -461,6 +463,80 @@ void KateViewManager::openUrl(const QUrl &url)
openUrl(url, QString());
}
void KateViewManager::openUrlOrProject(const QUrl &url)
{
if (!url.isLocalFile()) {
openUrl(url);
return;
}
const QDir dir(url.toLocalFile());
if (!dir.exists()) {
openUrl(url);
return;
}
QString text;
if (KateApp::isKWrite()) {
text = i18n("%1 cannot open folders", KAboutData::applicationData().displayName());
KMessageBox::error(mainWindow(), text);
return;
}
// try to open the folder
static const QString projectPluginId = QStringLiteral("kateprojectplugin");
QObject *projectPluginView = mainWindow()->pluginView(projectPluginId);
if (!projectPluginView) {
// try to find and enable the Projects plugin
KatePluginList &pluginList = KateApp::self()->pluginManager()->pluginList();
KatePluginList::iterator i = std::find_if(pluginList.begin(),
pluginList.end(),
[](const KatePluginInfo &pluginInfo) {
return pluginInfo.metaData.pluginId() == projectPluginId;
});
QString text;
if (i == pluginList.end()) {
text = i18n("The plugin required to open folders was not found");
KMessageBox::error(mainWindow(), text);
return;
}
KatePluginInfo &projectPluginInfo = *i;
text = i18n("In order to open folders, the <b>%1</b> plugin must be enabled. Enable it?",
projectPluginInfo.metaData.name());
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
if (KMessageBox::questionTwoActions(
#else
if (KMessageBox::questionYesNo(
#endif
mainWindow(),
text,
i18nc("@title:window", "Open Folder"),
KGuiItem(i18nc("@action:button", "Enable"), QStringLiteral("dialog-ok")),
KStandardGuiItem::cancel())
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
== KMessageBox::SecondaryAction) {
#else
== KMessageBox::No) {
#endif
return;
}
if (!KateApp::self()->pluginManager()->loadPlugin(&projectPluginInfo)) {
text = i18n("Failed to enable <b>%1</b> plugin", projectPluginInfo.metaData.name());
KMessageBox::error(mainWindow(), text);
return;
}
KateApp::self()->pluginManager()->enablePluginGUI(&projectPluginInfo);
projectPluginView = mainWindow()->pluginView(projectPluginId);
}
Q_ASSERT(projectPluginView);
QMetaObject::invokeMethod(projectPluginView, "openDirectoryOrProject", Q_ARG(const QDir &, dir));
}
KTextEditor::View *KateViewManager::openViewForDoc(KTextEditor::Document *doc)
{
// forward to currently active view space
......
......@@ -80,6 +80,7 @@ public:
public Q_SLOTS:
void openUrl(const QUrl &url);
void openUrlOrProject(const QUrl &url);
void addPositionToHistory(const QUrl &url, KTextEditor::Cursor pos);
public:
......
......@@ -6,9 +6,7 @@
#include "ktexteditor_utils.h"
#include "katemainwindow.h"
#include "kateviewmanager.h"
#include <QDir>
#include <QFontDatabase>
#include <QIcon>
#include <QMimeDatabase>
......@@ -18,7 +16,6 @@
#include <KActionCollection>
#include <KLocalizedString>
#include <KMessageBox>
#include <KTextEditor/Application>
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
......@@ -193,24 +190,4 @@ QVariantMap projectMapForDocument(KTextEditor::Document *doc)
}
return projectMap;
}
void openUrlOrProject(KateViewManager *viewManager, const QUrl &url)
{
if (!url.isLocalFile()) {
viewManager->openUrl(url);
return;
}
QDir dir = {url.toLocalFile()};
if (!dir.exists()) {
viewManager->openUrl(url);
return;
}
if (QObject *pview = viewManager->mainWindow()->pluginView(QStringLiteral("kateprojectplugin"))) {
QMetaObject::invokeMethod(pview, "openDirectoryOrProject", Q_ARG(const QDir &, dir));
} else {
KMessageBox::error(viewManager->mainWindow(), i18n("Please enable the project plugin to load directories"));
}
}
}
......@@ -14,7 +14,6 @@ class QFont;
class QIcon;
class QVariant;
class QWidget;
class QUrl;
typedef QMap<QString, QVariant> QVariantMap;
QT_END_NAMESPACE
......@@ -25,7 +24,6 @@ class Document;
class MainWindow;
}
struct DiffParams;
class KateViewManager;
namespace Utils
{
......@@ -90,11 +88,4 @@ KATE_PRIVATE_EXPORT QString projectBaseDirForDocument(KTextEditor::Document *doc
* Returns project map for provided document
*/
KATE_PRIVATE_EXPORT QVariantMap projectMapForDocument(KTextEditor::Document *doc);
/**
* Opens a directory or project for the provided url
*
* Returns whether the url is a directory
*/
KATE_PRIVATE_EXPORT void openUrlOrProject(KateViewManager *viewManager, const QUrl &url);
}
......@@ -82,7 +82,7 @@ WelcomeView::WelcomeView(KateViewManager *viewManager, QWidget *parent)
if (index.isValid()) {
const QUrl url = m_recentItemsModel->url(index);
Q_ASSERT(url.isValid());
Utils::openUrlOrProject(m_viewManager, url);
m_viewManager->openUrlOrProject(url);
}
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment