-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotedevplugin.cpp
500 lines (408 loc) · 18.4 KB
/
remotedevplugin.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "remotedevplugin.h"
#include "remotedevconstants.h"
#include <QAction>
#include <QMenu>
#include <QTime>
#include <QStandardItemModel>
#include <coreplugin/icore.h>
#include <coreplugin/icontext.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/documentmanager.h>
#include <coreplugin/idocument.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <projectexplorer/devicesupport/devicemanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
//#include <projectexplorer/propertiespanel.h>
#include <projectexplorer/projectpanelfactory.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectnodes.h>
#include "connectionhelper.h"
#include "connectionmanager.h"
#include "optionspage.h"
#include "projectsettingswidget.h"
#include "devicemanager.h"
#include "mappingsmanager.h"
#include <QDebug>
using namespace RemoteDev::Internal;
RemoteDevPlugin::RemoteDevPlugin() :
m_uploadFile(nullptr),
m_uploadDirectory(nullptr),
m_connManager(new ConnectionManager),
m_mapManager(new MappingsManager),
m_devManager(new DeviceManager),
m_downloadActionGroup(new QActionGroup(this))
{
m_downloadActionGroup->setExclusive(true);
}
RemoteDevPlugin::~RemoteDevPlugin()
{
// Unregister objects from the plugin manager's object pool
// Delete members
delete m_connManager;
delete m_mapManager;
delete m_devManager;
}
bool RemoteDevPlugin::initialize(const QStringList &arguments, QString *errorString)
{
// Register objects in the plugin manager's object pool
// Load settings
// Add actions to menus
// Connect to other plugins' signals
// In the initialize function, a plugin can be sure that the plugins it
// depends on have initialized their members.
Q_UNUSED(arguments)
Q_UNUSED(errorString)
QAction *action = new QAction(tr("RemoteDev action"), this);
Core::Command *cmd = Core::ActionManager::registerAction(
action, Constants::ACTION_ID,
//Core::Context(Core::Constants::C_NAVIGATION_PANE)
Core::Context(Core::Constants::C_GLOBAL)
);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Meta+A")));
connect(action, SIGNAL(triggered()), this, SLOT(triggerAction()));
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::MENU_ID);
menu->menu()->setTitle(tr("RemoteDev"));
menu->addAction(cmd);
Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
//auto *editorManager = Core::EditorManager::instance();
// NOTE: currentEditorChanged is also triggered upon editorOpened
// connect(editorManager, &Core::EditorManager::editorOpened,
// this, &RemoteDevPlugin::uploadCurrentDocument);
// connect(editorManager, &Core::EditorManager::currentEditorChanged,
// this, &RemoteDevPlugin::uploadCurrentDocument);
QAction *saveAction = Core::ActionManager::command(Core::Constants::SAVE)->action();
connect(saveAction, &QAction::triggered,
this, &RemoteDevPlugin::uploadCurrentDocument);
ConnectionManager *connectionManager = ConnectionManager::instance();
connect(connectionManager, &ConnectionManager::connectionError,
this, &RemoteDevPlugin::onConnectionError);
createOptionsPage();
createProjectSettingsPage();
createFileMenus();
return true;
}
void RemoteDevPlugin::extensionsInitialized()
{
// Retrieve objects from the plugin manager's object pool
// In the extensionsInitialized function, a plugin can be sure that all
// plugins that depend on it are completely initialized.
}
bool RemoteDevPlugin::delayedInitialize()
{
// Perforn non-trivial startup sequence after application startup
// Return true, if implemented
m_devManager->startDeviceSync();
return true;
}
ExtensionSystem::IPlugin::ShutdownFlag RemoteDevPlugin::aboutToShutdown()
{
// Save settings
// Disconnect from signals that are not needed during shutdown
// Hide UI (if you add UI that is not in the main window directly)
// If required to wait, until external process finishes
// return AsynchronousShutdown;
// emit asynchronousShutdownFinished();
// TODO: disconnect all existing connections?
// Should it be done asynchronously?
disconnect(this, SLOT(onConnectionError(Connection::Ptr)));
disconnect(this, SLOT(uploadCurrentDocument()));
disconnect(this, SLOT(uploadCurrentNode()));
disconnect(m_uploadFile, SIGNAL(triggered(bool)));
disconnect(m_uploadDirectory, SIGNAL(triggered(bool)));
return SynchronousShutdown;
}
void RemoteDevPlugin::uploadCurrentDocument()
{
auto document = Core::EditorManager::currentDocument();
if (document) {
auto *project = ProjectExplorer::SessionManager::projectForFile(document->filePath());
if (project) {
upload(document->filePath(), *project, &Connection::uploadFile);
} else {
qDebug() << "No project for file: " << document->filePath();
}
}
}
void RemoteDevPlugin::uploadCurrentNode()
{
auto *node = ProjectExplorer::ProjectTree::currentNode();
if (!node) return;
auto *project = ProjectExplorer::ProjectTree::projectForNode(node);
if (!project) return;
switch (node->nodeType()) {
case ProjectExplorer::FileNodeType:
upload(node->filePath(), *project, &Connection::uploadFile);
break;
case ProjectExplorer::FolderNodeType:
upload(node->filePath(), *project, &Connection::uploadDirectory);
break;
case ProjectExplorer::ProjectNodeType:
upload(project->projectDirectory(), *project, &Connection::uploadDirectory);
break;
case ProjectExplorer::VirtualFolderNodeType:
case ProjectExplorer::SessionNodeType:
default:
showDebug(
QStringLiteral("FIXME: unsupported node type ProjectExplorer::%1").arg(node->nodeType()));
}
}
void RemoteDevPlugin::downloadCurrentNodeUsingActiveMapping()
{
// find active (checked) mapping
// NOTE: this relies that menu items cortespond 1:1 to existing mappings
auto actions = m_downloadActionGroup->actions();
auto checked = std::find_if(actions.begin(), actions.end(), [](const QAction *a) {
return a->isChecked();
});
if (checked == actions.end()) return;
auto *project = ProjectExplorer::ProjectTree::currentProject();
if (!project) return;
auto index = static_cast<std::size_t>(checked - actions.begin());
const Mapping &mapping = m_mapManager->mappingsForProject(*project)[index];
downloadCurrentNode(mapping);
}
void RemoteDevPlugin::downloadCurrentNode(const Mapping &mapping)
{
auto node = ProjectExplorer::ProjectTree::currentNode();
if (!node) return;
auto *project = ProjectExplorer::ProjectTree::projectForNode(node);
if (!project) return;
switch (node->nodeType()) {
case ProjectExplorer::FileNodeType:
download(node->filePath(), *project, &Connection::downloadFile, mapping);
break;
case ProjectExplorer::FolderNodeType:
case ProjectExplorer::ProjectNodeType:
case ProjectExplorer::VirtualFolderNodeType:
case ProjectExplorer::SessionNodeType:
default:
showDebug(
QStringLiteral("FIXME: unsupported node type ProjectExplorer::%1").arg(node->nodeType()));
}
}
void RemoteDevPlugin::triggerAction()
{
this->showDebug(tr("Action triggered"));
}
void RemoteDevPlugin::onConnectionError(Connection::Ptr connection)
{
if (connection) {
this->showDebug(tr("Remote connection error")
+ QString::fromLatin1(": ")
+ connection->errorString());
} else {
this->showDebug(tr("Remote connection error"));
}
}
void RemoteDevPlugin::onDownloadMenuAboutToShow()
{
// populate menu with download sources for this project
for (auto *action : m_downloadActionGroup->actions()) {
delete action;
}
auto *project = ProjectExplorer::ProjectTree::currentProject();
if (!project) return;
const auto &mappings = m_mapManager->mappingsForProject(*project);
for (const Mapping &mapping : mappings) {
auto *action = new QAction(mapping.name(), this);
action->setCheckable(true);
action->setActionGroup(m_downloadActionGroup);
connect(action, &QAction::triggered, [&mapping, this]() {
this->downloadCurrentNode(mapping);
});
// TODO: add flag to mapping (if it was selected as default for download)
m_downloadMenu->menu()->addAction(action);
}
auto actions = m_downloadActionGroup->actions();
if (!actions.empty())
actions.front()->setChecked(true);
}
void RemoteDevPlugin::upload(const Utils::FileName &file,
ProjectExplorer::Project &project,
RemoteDevPlugin::UploadMethod uploadMethod)
{
// FIXME:
// CMocka-1: Upload "src/cmocka.c": "/home/elvenfighter/Projects/cmocka" -> "/tmp/cmocka-1"
// CMocka-2: Upload "src/cmocka.c": "/home/elvenfighter/Projects/cmocka" -> "/var/tmp/cmocka-2"
// CMockaTest-2: failure [25 ms]: /home/elvenfighter/Projects/cmocka-test: No such file
// CMockaTest-1: failure [41 ms]: /home/elvenfighter/Projects/cmocka-test: No such file
const auto local = project.projectDirectory();
const auto relpath = file.relativeChildPath(local);
qDebug() << "Upload file for project" << project.displayName() << ":"
<< relpath.toString();
auto deviceMgr = ProjectExplorer::DeviceManager::instance();
for (const Mapping& mapping : m_mapManager->mappingsForProject(project)) {
if (!mapping.isEnabled()) continue;
const auto mappingName = mapping.name();
auto device = deviceMgr->find(mapping.deviceId());
if (device.isNull()) {
qDebug() << "No device can be found for mapping" << mappingName;
continue;
}
auto connection = ConnectionManager::connectionForDevice(*device);
if (connection.isNull()) {
qDebug() << "No connection for mapping" << mappingName
<< "and device" << device->displayName();
continue;
}
const auto remote = mapping.remotePath();
showDebug(QStringLiteral("%1: Upload \"%2\": \"%3\" -> \"%4\"").arg(
mappingName, relpath.toString(), local.toString(), remote.toString()
));
auto &helper = getConnectionHelper(*connection, mappingName, local.toString());
helper.startJob([&]() {
auto job = (connection.data()->*uploadMethod)(local, remote, relpath, OverwriteExisting);
qDebug() << "Started job" << mappingName << "->" << job;
return job;
});
}
}
void RemoteDevPlugin::download(const Utils::FileName &file,
ProjectExplorer::Project &project,
RemoteDevPlugin::DownloadMethod downloadMethod,
const Mapping &mapping)
{
const auto localDir = project.projectDirectory();
const auto relpath = file.relativeChildPath(localDir);
qDebug() << "Download file for project " << project.displayName() << ":"
<< relpath.toString() << "and mapping" << mapping.name();
const auto mappingName = mapping.name();
auto deviceMgr = ProjectExplorer::DeviceManager::instance();
auto device = deviceMgr->find(mapping.deviceId());
if (device.isNull()) {
qDebug() << "No device can be found for mapping" << mappingName;
return;
}
auto connection = ConnectionManager::connectionForDevice(*device);
if (connection.isNull())
return;
const auto remote = mapping.remotePath();
showDebug(QStringLiteral("%1: Download \"%2\": \"%3\" -> \"%4\"").arg(
mappingName, relpath.toString(), localDir.toString(), remote.toString()
));
auto &helper = getConnectionHelper(*connection, mappingName, localDir.toString());
helper.startJob([&]() {
auto job = (connection.data()->*downloadMethod)(localDir,
remote,
relpath,
OverwriteExisting);
qDebug() << "Started job" << mappingName << "->" << job;
return job;
});
}
ConnectionHelper &RemoteDevPlugin::getConnectionHelper(RemoteDev::Connection &connection,
const QString &mapping,
const QString &target) const
{
auto helper = connection.findChild<ConnectionHelper *>();
if (!helper) {
helper = new ConnectionHelper(&connection);
// FIXME: transform to methods
connect(&connection, &Connection::uploadFinished,
[this, helper, target, mapping] (RemoteJobId job) {
auto elapsed = helper->endJob(job);
this->showDebug(QString::fromLatin1("%1: %2 [%3 ms]: %4")
.arg(mapping, tr("success"),
QString::number(elapsed), target));
}
);
connect(&connection, &Connection::uploadError,
[this, helper, target, mapping] (RemoteJobId job, const QString &reason) {
auto elapsed = helper->endJob(job);
this->showDebug(QString::fromLatin1("%1: %2 [%3 ms]: %4: %5")
.arg(mapping, tr("failure"),
QString::number(elapsed), target, reason));
}
);
}
return *helper;
}
void RemoteDevPlugin::createOptionsPage()
{
m_optionsPage = new OptionsPage(this);
// TODO: load Core::ICore::settings();
addAutoReleasedObject(m_optionsPage);
}
void RemoteDevPlugin::createProjectSettingsPage()
{
auto panelFactory = new ProjectExplorer::ProjectPanelFactory();
panelFactory->setPriority(100); // this sets panel order in tabs
panelFactory->setDisplayName(tr("RemoteDev"));
// QIcon icon /* = QIcon(QLatin1String(":/projectexplorer/images/EditorSettings.png")) */;
// panelFactory->setSimpleCreateWidgetFunction<ProjectSettingsWidget>(icon);
panelFactory->setCreateWidgetFunction(
[this, panelFactory] (ProjectExplorer::Project *project) -> QWidget * {
if (!project) return nullptr;
// auto panel = new ProjectExplorer::PropertiesPanel;
// panel->setDisplayName(tr("Remote Mappings"));
auto widget = new ProjectSettingsWidget(*project, *this->m_mapManager);
widget->setDevicesModel(m_devManager->devices());
// panel->setWidget(widget);
auto panelsWidget = new ProjectExplorer::PanelsWidget();
panelsWidget->addPropertiesPanel(tr("RemoteDev"), QIcon(), widget /* panel*/);
// panelsWidget->addPropertiesPanel(panel);
panelsWidget->setFocusProxy(widget);
return panelsWidget;
}
);
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
}
void RemoteDevPlugin::createFileMenus()
{
const Core::Context projectTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE);
// TODO: opened files context?
auto fileContextMenu = Core::ActionManager::actionContainer(
ProjectExplorer::Constants::M_FILECONTEXT);
// "Upload File" menu
m_uploadFile = new QAction(tr("Upload File"), this);
auto *uploadFileCmd = Core::ActionManager::registerAction(m_uploadFile,
Constants::UPLOAD_FILE,
projectTreeContext);
fileContextMenu->addAction(uploadFileCmd, ProjectExplorer::Constants::G_FILE_OTHER);
connect(m_uploadFile, &QAction::triggered,
this, &RemoteDevPlugin::uploadCurrentNode);
// "Download File" menu
m_downloadFile = new QAction(tr("Download File"), this);
auto *downloadFileCmd = Core::ActionManager::registerAction(m_downloadFile,
Constants::DOWNLOAD_FILE,
projectTreeContext);
fileContextMenu->addAction(downloadFileCmd, ProjectExplorer::Constants::G_FILE_OTHER);
connect(m_downloadFile, &QAction::triggered,
this, &RemoteDevPlugin::downloadCurrentNodeUsingActiveMapping);
m_downloadMenu = Core::ActionManager::createMenu(Constants::M_DOWNLOAD_SOURCES);
m_downloadMenu->appendGroup(Constants::G_DOWNLOAD_FILE);
m_downloadMenu->addAction(downloadFileCmd, Constants::G_DOWNLOAD_FILE);
m_downloadMenu->addSeparator(Constants::G_DOWNLOAD_FILE);
m_downloadMenu->menu()->setTitle(tr("Download"));
connect(m_downloadMenu->menu(), &QMenu::aboutToShow,
this, &RemoteDevPlugin::onDownloadMenuAboutToShow);
fileContextMenu->addMenu(m_downloadMenu, ProjectExplorer::Constants::G_FILE_OTHER);
// "Upload Directory" menu
auto folderContextMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FOLDERCONTEXT);
auto projectContextMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
auto subProjectContextMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
m_uploadDirectory = new QAction(tr("Upload Directory"), this);
auto *uploadDirectoryCmd = Core::ActionManager::registerAction(m_uploadDirectory,
Constants::UPLOAD_DIRECTORY,
projectTreeContext);
folderContextMenu->addAction(uploadDirectoryCmd, ProjectExplorer::Constants::G_FOLDER_OTHER);
projectContextMenu->addAction(uploadDirectoryCmd, ProjectExplorer::Constants::G_PROJECT_FILES);
subProjectContextMenu->addAction(uploadDirectoryCmd, ProjectExplorer::Constants::G_PROJECT_FILES);
connect(m_uploadDirectory, &QAction::triggered,
this, &RemoteDevPlugin::uploadCurrentNode);
}
void RemoteDevPlugin::showDebug(const QString &string) const
{
auto messageManager = Core::MessageManager::instance();
messageManager->write(string, Core::MessageManager::Silent);
}