kateprojecttreeviewcontextmenu.cpp 8.53 KB
Newer Older
1
2
/*  This file is part of the Kate project.
 *
3
 *  SPDX-FileCopyrightText: 2013 Dominik Haumann <dhaumann.org>
4
 *
5
 *  SPDX-License-Identifier: LGPL-2.0-or-later
6
7
8
 */

#include "kateprojecttreeviewcontextmenu.h"
9
#include "git/gitutils.h"
10
#include "kateproject.h"
11
#include "kateprojectinfoviewterminal.h"
Waqar Ahmed's avatar
Waqar Ahmed committed
12
#include "kateprojectviewtree.h"
13

Christoph Cullmann's avatar
Christoph Cullmann committed
14
#include <KApplicationTrader>
Ahmad Samir's avatar
Ahmad Samir committed
15
16
#include <KIO/ApplicationLauncherJob>
#include <KIO/JobUiDelegate>
17
#include <KIO/OpenFileManagerWindowJob>
Christoph Cullmann's avatar
Christoph Cullmann committed
18
#include <KLocalizedString>
19
20
#include <KNS3/KMoreTools>
#include <KNS3/KMoreToolsMenuFactory>
21
#include <KPropertiesDialog>
Christoph Cullmann's avatar
Christoph Cullmann committed
22

23
#include <QAction>
24
25
#include <QApplication>
#include <QClipboard>
26
#include <QDir>
27
28
#include <QFileInfo>
#include <QIcon>
29
#include <QInputDialog>
30
#include <QMenu>
31
#include <QMessageBox>
Dominik Haumann's avatar
Dominik Haumann committed
32
#include <QMimeDatabase>
33
34
#include <QMimeType>
#include <QStandardPaths>
35

Krzysztof Stokop's avatar
Krzysztof Stokop committed
36
#include <KToolInvocation>
37
38
39
40
#include <ktexteditor/editor.h>
#include <ktexteditor/application.h>


41

42
static QString getName()
43
{
44
    QInputDialog dlg;
Waqar Ahmed's avatar
Waqar Ahmed committed
45
    dlg.setLabelText(i18n("Enter name:"));
46
47
48
49
50
51
52
    dlg.setOkButtonText(i18n("Add"));
    dlg.setInputMode(QInputDialog::TextInput);

    int res = dlg.exec();
    bool suc = res == QDialog::Accepted;
    if (!suc || dlg.textValue().isEmpty()) {
        return {};
53
    }
54
55
    return dlg.textValue();
}
56

Waqar Ahmed's avatar
Waqar Ahmed committed
57
void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelIndex &index, const QPoint &pos, KateProjectViewTree *parent)
58
{
59
    /**
60
     * Create context menu
61
62
63
     */
    QMenu menu;

64
65
66
67
68
    /**
     * Copy Path, always available, put that to the top
     */
    QAction *copyAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy File Path"));

69
70
71
    QAction *addFile = nullptr;
    QAction *addFolder = nullptr;
    if (index.data(KateProjectItem::TypeRole).toInt() == KateProjectItem::Directory) {
Waqar Ahmed's avatar
Waqar Ahmed committed
72
73
        addFile = menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("Add File"));
        addFolder = menu.addAction(QIcon::fromTheme(QStringLiteral("folder-new")), i18n("Add Folder"));
74
75
    }

76
77
78
79
80
81
82
83
    // we can ATM only handle file renames
    QAction *rename = nullptr;
    QAction *fileDelete = nullptr;
    if (index.data(KateProjectItem::TypeRole).toInt() == KateProjectItem::File) {
        rename = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("&Rename"));
        fileDelete = menu.addAction(QIcon::fromTheme(QStringLiteral("delete")), i18n("Delete"));
    }

84
    /**
85
     * File Properties Dialog
86
     */
87
88
    auto filePropertiesAction = menu.addAction(QIcon::fromTheme(QStringLiteral("dialog-object-properties")), i18n("Properties"));

89
    /**
90
     * Handle "open with",
91
92
     * find correct mimetype to query for possible applications
     */
93
    menu.addSeparator();
94
95
    QMenu *openWithMenu = menu.addMenu(i18n("Open With"));
    QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filename);
Nicolas Fella's avatar
Nicolas Fella committed
96
    const KService::List offers = KApplicationTrader::queryByMimeType(mimeType.name());
97
    // For each one, insert a menu item...
98
    for (const auto &service : offers) {
99
        if (service->name() == QLatin1String("Kate")) {
100
            continue; // omit Kate
101
102
103
        }
        QAction *action = openWithMenu->addAction(QIcon::fromTheme(service->icon()), service->name());
        action->setData(service->entryPath());
104
    }
105
106
    // Perhaps disable menu, if no entries
    openWithMenu->setEnabled(!openWithMenu->isEmpty());
107

108
    /**
109
     * Open external terminal here
110
     */
111
112
113
114
115
116
117
118
119
120
121
    if (KateProjectInfoViewTerminal::isLoadable()) {
        menu.addAction(QIcon::fromTheme(QStringLiteral("terminal")), i18n("Open Internal Terminal Here"), [parent, &filename]() {
            QFileInfo checkFile(filename);
            if (checkFile.isFile()) {
                parent->openTerminal(checkFile.absolutePath());
            } else {
                parent->openTerminal(filename);
            }
        });
    }
    QAction *terminal = menu.addAction(QIcon::fromTheme(QStringLiteral("utilities-terminal")), i18n("Open External Terminal Here"));
122

123
    /**
124
     * Open Containing folder
125
     */
126
    auto openContaingFolderAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("&Open Containing Folder"));
127

128
129
130
    /**
     * Git menu
     */
131
    QAction *fileHistory = nullptr;
Laurent Montel's avatar
Laurent Montel committed
132
    KMoreToolsMenuFactory menuFactory(QStringLiteral("kate/addons/project/git-tools"));
Gregor Mi's avatar
Gregor Mi committed
133
    QMenu gitMenu; // must live as long as the maybe filled menu items should live
134
    if (GitUtils::isGitRepo(QFileInfo(filename).absolutePath())) {
135
136
        menu.addSeparator();
        fileHistory = menu.addAction(i18n("Show Git History"));
137
        menuFactory.fillMenuFromGroupingNames(&gitMenu, {QLatin1String("git-clients-and-actions")}, QUrl::fromLocalFile(filename));
138
139
        const auto gitActions = gitMenu.actions();
        for (auto action : gitActions) {
140
            menu.addAction(action);
141
        }
142
    }
143

Ahmad Samir's avatar
Ahmad Samir committed
144
145
146
147
148
149
150
151
    auto handleOpenWith = [parent](QAction *action, const QString &filename) {
        KService::Ptr app = KService::serviceByDesktopPath(action->data().toString());
        // If app is null, ApplicationLauncherJob will invoke the open-with dialog
        auto *job = new KIO::ApplicationLauncherJob(app);
        job->setUrls({QUrl::fromLocalFile(filename)});
        job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, parent));
        job->start();
    };
Krzysztof Stokop's avatar
Krzysztof Stokop committed
152
    
153
    auto handleDeleteFile = [parent, index](const QString &path)
154
155
    {
        //message box
156
157
158
        const QString title = i18n("Delete File");
        const QString text = i18n("Do you want to delete the file '%1'?", path);
        if (QMessageBox::Yes == QMessageBox::question(parent, title, text, QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes)) {
Krzysztof Stokop's avatar
Krzysztof Stokop committed
159
            const QList< KTextEditor::Document* > openDocuments = KTextEditor::Editor::instance()->application()->documents();
160
161
162
              
            //if is open, close
            for(auto doc : openDocuments)
163
            {
164
                if(doc->url().adjusted(QUrl::RemoveScheme) == QUrl(path).adjusted(QUrl::RemoveScheme))
165
                {
166
167
                    KTextEditor::Editor::instance()->application()->closeDocument(doc);
                    break;
168
169
                }
            }
170
            parent->removeFile(index, path);
171
172
        }
    };
Ahmad Samir's avatar
Ahmad Samir committed
173

174
175
176
    /**
     * run menu and handle the triggered action
     */
177
    if (QAction *const action = menu.exec(pos)) {
178
        if (action == copyAction) {
179
            QApplication::clipboard()->setText(filename);
Krzysztof Stokop's avatar
Krzysztof Stokop committed
180
        } else if (action == terminal) {
181
            // handle "open terminal here"
Krzysztof Stokop's avatar
Krzysztof Stokop committed
182
            QFileInfo checkFile(filename);
183
184
185
            if (checkFile.isFile()) {
                KToolInvocation::invokeTerminal(QString(), {}, checkFile.absolutePath());
            } else {
Krzysztof Stokop's avatar
Krzysztof Stokop committed
186
187
                KToolInvocation::invokeTerminal(QString(), {}, filename);
            }
188
        } else if (action->parentWidget() == openWithMenu) {
189
            // handle "open with"
Ahmad Samir's avatar
Ahmad Samir committed
190
            handleOpenWith(action, filename);
191
192
        } else if (action == openContaingFolderAction) {
            KIO::highlightInFileManager({QUrl::fromLocalFile(filename)});
193
        } else if (fileDelete && action == fileDelete) {
194
            handleDeleteFile(filename);
195
        } else if (action == filePropertiesAction) {
196
197
            // code copied and adapted from frameworks/kio/src/filewidgets/knewfilemenu.cpp
            KFileItem fileItem(QUrl::fromLocalFile(filename));
198
            QDialog *dlg = new KPropertiesDialog(fileItem);
199
200
            dlg->setAttribute(Qt::WA_DeleteOnClose);
            dlg->show();
201
        } else if (rename && action == rename) {
Waqar Ahmed's avatar
Waqar Ahmed committed
202
203
204
205
206
207
208
209
210
211
212
213
            /**
             * hack:
             * We store a reference to project in the item so that
             * after rename we can update file2Item map properly.
             */
            KateProjectItem *item = parent->project()->itemForFile(index.data(Qt::UserRole).toString());
            if (!item) {
                return;
            }
            item->setData(QVariant::fromValue(parent->project()), KateProjectItem::ProjectRole);

            /** start the edit */
Waqar Ahmed's avatar
Waqar Ahmed committed
214
            parent->edit(index);
215
        } else if (action == fileHistory) {
216
            showFileHistory(index.data(Qt::UserRole).toString());
217
218
219
220
        } else if (addFile && action == addFile) {
            QString name = getName();
            if (!name.isEmpty()) {
                parent->addFile(index, name);
221
            }
222
223
224
225
        } else if (addFolder && action == addFolder) {
            QString name = getName();
            if (!name.isEmpty()) {
                parent->addDirectory(index, name);
226
            }
227
        } else {
228
229
            // One of the git actions was triggered
        }
230
231
    }
}