-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfileitemactioninsyncplugin.cpp
More file actions
129 lines (105 loc) · 5.09 KB
/
fileitemactioninsyncplugin.cpp
File metadata and controls
129 lines (105 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*****************************************************************************
* Copyright (C) 2021-2026 by Kurt Ko <kurt@insynchq.com> *
* Copyright (C) 2014 by Luis Manuel R. Pugoy <lpugoy@insynchq.com> *
* Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
* Copyright (C) 2012 by Sergei Stolyarov <sergei@regolit.com> *
* Copyright (C) 2010 by Thomas Richard <thomas.richard@proan.be> *
* Copyright (C) 2009-2010 by Peter Penz <peter.penz19@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
*****************************************************************************/
#include "fileitemactioninsyncplugin.h"
#include "insyncdolphinpluginhelper.h"
#include <KFileItem>
#include <KFileItemListProperties>
#include <KActionMenu>
#include <KPluginFactory>
#include <QPointer>
#include <QLocalSocket>
#include <QStringBuilder>
#include <QJsonObject>
K_PLUGIN_CLASS_WITH_JSON(FileItemActionInsyncPlugin, "fileitemactioninsyncplugin.json")
FileItemActionInsyncPlugin::FileItemActionInsyncPlugin(QObject *parent, const QList<QVariant> &args) : KAbstractFileItemActionPlugin(parent)
{
Q_UNUSED(args);
controlSocket = new QLocalSocket(parent);
helper->connectWithInsync(controlSocket);
}
FileItemActionInsyncPlugin::~FileItemActionInsyncPlugin()
{
delete controlSocket;
}
QList<QAction *> FileItemActionInsyncPlugin::actions(const KFileItemListProperties &fileItemInfos,
QWidget *parentWidget)
{
Q_UNUSED(parentWidget);
// The FileItemActionInsyncPlugin::contextMenuActions implemented by Luis only works when
// you right click a single file/directory. The snippet below is a part of the code
// to handle multiple files/directories selected when opening the context menu
// for (const KFileItem& item : fileItemInfos.items()) {
// d->contextFilePaths << QDir(item.localPath()).canonicalPath();
// }
// For simplicity, let's just handle a single file for now
if (fileItemInfos.items().size() > 1 || fileItemInfos.items().size() == 0)
{
return QList<QAction *>();
}
KFileItem item = fileItemInfos.items().at(0);
return getContextMenuActions(item.url().path());
}
void FileItemActionInsyncPlugin::handleContextAction(const QJsonObject &action)
{
helper->sendCommand(action, controlSocket);
}
QList<QAction *> FileItemActionInsyncPlugin::getContextMenuActions(const QString &url)
{
QJsonObject command = QJsonObject();
command.insert(QStringLiteral("command"), QStringLiteral("CONTEXT-MENU-ITEMS"));
command.insert(QStringLiteral("full_path"), url);
const QVariant reply = helper->sendCommand(command, controlSocket, InsyncDolphinPluginHelper::WaitForReply);
if (reply.isNull())
return QList<QAction *>();
QList<QVariant> menuinfo = reply.toList();
QString title = menuinfo.at(0).toString();
QList<QVariant> menuitems = menuinfo.at(1).toList();
QPointer<KActionMenu> topContextMenu = new KActionMenu(
QIcon::fromTheme(QStringLiteral("insync")),
title,
this);
for (int i = 0; i < menuitems.size(); i++)
{
QList<QVariant> commandinfo = menuitems.at(i).toList();
QString text = commandinfo.at(0).toString();
QString method = commandinfo.at(1).toString();
if (text == QStringLiteral("separator"))
{
topContextMenu->addSeparator();
}
else
{
QAction *actionItem = new QAction(text, this);
QJsonObject actionJson = QJsonObject();
actionJson.insert(QStringLiteral("method"), method);
actionJson.insert(QStringLiteral("full_path"), url);
connect(actionItem, &QAction::triggered, [=] {
handleContextAction(actionJson);
});
topContextMenu->addAction(actionItem);
}
}
return QList<QAction *>{topContextMenu};
}
#include "fileitemactioninsyncplugin.moc"