forked from KDE/liquidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceList.cxx
516 lines (402 loc) · 15.6 KB
/
DeviceList.cxx
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
Copyright 2017 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell 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 3 of the License, or
(at your option) any later version.
liquidshell 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 liquidshell. If not, see <http://www.gnu.org/licenses/>.
*/
#include <DeviceList.hxx>
#include <Solid/DeviceNotifier>
#include <Solid/DeviceInterface>
#include <Solid/StorageAccess>
#include <Solid/StorageVolume>
#include <Solid/StorageDrive>
#include <Solid/Block>
#include <QHBoxLayout>
#include <QIcon>
#include <QStandardPaths>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <KLocalizedString>
#include <KDesktopFile>
#include <KDesktopFileActions>
#include <KConfigGroup>
#include <KService>
#include <KRun>
#include <kio/global.h>
//--------------------------------------------------------------------------------
DeviceItem::DeviceItem(Solid::Device dev, const QVector<DeviceAction> &deviceActions)
: device(dev)
{
setFrameShape(QFrame::StyledPanel);
QVBoxLayout *vbox = new QVBoxLayout(this);
QHBoxLayout *hbox = new QHBoxLayout;
vbox->addLayout(hbox);
QLabel *iconLabel = new QLabel;
iconLabel->setPixmap(QIcon::fromTheme(device.icon()).pixmap(32));
iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
textLabel = new QLabel;
textLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
hbox->addWidget(iconLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);
hbox->addWidget(textLabel, 0, Qt::AlignVCenter);
Solid::StorageAccess *storage = device.as<Solid::StorageAccess>();
if ( storage )
{
connect(storage, &Solid::StorageAccess::teardownDone, this, &DeviceItem::teardownDone);
connect(storage, &Solid::StorageAccess::setupDone, this, &DeviceItem::setupDone);
mountButton = new QToolButton;
mountButton->setIconSize(QSize(32, 32));
connect(mountButton, &QToolButton::clicked, mountButton,
[this]()
{
statusLabel->hide();
Solid::StorageAccess *storage = device.as<Solid::StorageAccess>();
if ( storage->isAccessible() ) // mounted -> unmount it
storage->teardown();
else
storage->setup();
}
);
hbox->addWidget(mountButton);
}
statusLabel = new QLabel;
statusLabel->setWordWrap(true);
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
statusLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
vbox->addWidget(statusLabel);
statusLabel->hide();
statusTimer.setSingleShot(true);
statusTimer.setInterval(60000);
connect(&statusTimer, &QTimer::timeout, statusLabel, &QLabel::hide);
fillData();
// append actions
for (const DeviceAction &action : deviceActions)
{
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addSpacing(iconLabel->sizeHint().width());
QToolButton *button = new QToolButton;
button->setAutoRaise(true);
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
button->setIcon(QIcon::fromTheme(action.action.icon()));
button->setText(action.action.text() + " (" + QFileInfo(action.path).baseName() + ")");
connect(button, &QToolButton::clicked, button,
[action, this]()
{
QString command = action.action.exec();
Solid::StorageAccess *storage = device.as<Solid::StorageAccess>();
if ( storage )
{
if ( !storage->isAccessible() )
{
statusLabel->hide();
storage->setup();
return;
}
command.replace("%f", storage->filePath());
}
if ( device.is<Solid::Block>() )
command.replace("%d", device.as<Solid::Block>()->device());
command.replace("%i", device.udi());
KRun::runCommand(command, this);
window()->hide();
}
);
hbox->addWidget(button);
vbox->addLayout(hbox);
}
}
//--------------------------------------------------------------------------------
DeviceItem::DeviceItem(const KdeConnect::Device &dev)
{
setFrameShape(QFrame::StyledPanel);
QVBoxLayout *vbox = new QVBoxLayout(this);
QHBoxLayout *hbox = new QHBoxLayout;
vbox->addLayout(hbox);
QLabel *iconLabel = new QLabel;
iconLabel->setPixmap(dev->icon.pixmap(32));
iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
textLabel = new QLabel;
textLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
hbox->addWidget(iconLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);
hbox->addWidget(textLabel, 0, Qt::AlignVCenter);
statusLabel = new QLabel;
hbox->addWidget(statusLabel, 0, Qt::AlignVCenter);
QLabel *chargeIcon = new QLabel;
hbox->addWidget(chargeIcon, 0, Qt::AlignVCenter);
if ( dev->charge >= 0 )
{
statusLabel->setText(QString::number(dev->charge) + '%');
chargeIcon->setPixmap(dev->chargeIcon.pixmap(22));
}
connect(dev.data(), &KdeConnectDevice::changed, this,
[this, chargeIcon, dev]()
{
textLabel->setText(dev->name);
if ( dev->charge >= 0 )
{
statusLabel->setText(QString::number(dev->charge) + '%');
chargeIcon->setPixmap(dev->chargeIcon.pixmap(22));
}
});
if ( dev->plugins.contains("kdeconnect_findmyphone") )
{
QToolButton *ringButton = new QToolButton;
ringButton->setIcon(QIcon::fromTheme("preferences-desktop-notification-bell"));
connect(ringButton, &QToolButton::clicked, dev.data(), [dev]() { dev->ringPhone(); });
hbox->addWidget(ringButton, 0, Qt::AlignVCenter);
}
QToolButton *configure = new QToolButton;
configure->setIcon(QIcon::fromTheme("configure"));
connect(configure, &QToolButton::clicked, configure,
[this]()
{
if ( !dialog )
{
dialog = new KCMultiDialog(nullptr);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->addModule("kcm_kdeconnect");
dialog->setWindowTitle(i18n("KDE Connect"));
dialog->adjustSize();
}
dialog->show();
});
hbox->addWidget(configure, 0, Qt::AlignVCenter);
textLabel->setText(dev->name);
if ( dev->plugins.contains("kdeconnect_sftp") )
{
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addSpacing(iconLabel->sizeHint().width());
QToolButton *button = new QToolButton;
button->setAutoRaise(true);
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
button->setIcon(QIcon::fromTheme("system-file-manager"));
button->setText(i18n("Open with File Manager"));
connect(button, &QToolButton::clicked,
[dev]() { new KRun(QUrl(QLatin1String("kdeconnect://") + dev->id), nullptr); });
hbox->addWidget(button);
vbox->addLayout(hbox);
}
}
//--------------------------------------------------------------------------------
void DeviceItem::fillData()
{
Solid::StorageAccess *storage = device.as<Solid::StorageAccess>();
QString text = device.description();
if ( !device.product().isEmpty() )
text += " (" + device.product() + ")";
else if ( !device.vendor().isEmpty() )
text += " (" + device.vendor() + ")";
Solid::StorageVolume *volume = device.as<Solid::StorageVolume>();
if ( volume )
text += " " + KIO::convertSize(volume->size());
if ( storage && !storage->filePath().isEmpty() )
text += '\n' + storage->filePath();
textLabel->setText(text);
if ( mountButton )
{
if ( storage && storage->isAccessible() )
mountButton->setIcon(QIcon::fromTheme("media-eject"));
else
{
if ( device.emblems().count() )
mountButton->setIcon(QIcon::fromTheme(device.emblems()[0]));
else
mountButton->setIcon(QIcon::fromTheme("emblem-unmounted"));
}
if ( storage )
{
mountButton->setToolTip(storage->isAccessible() ?
i18n("Device is mounted.\nClick to unmount/eject") :
i18n("Device is unmounted.\nClick to mount"));
}
}
}
//--------------------------------------------------------------------------------
QString DeviceItem::errorToString(Solid::ErrorType error)
{
switch ( error )
{
case Solid::UnauthorizedOperation: return i18n("Unauthorized Operation");
case Solid::DeviceBusy: return i18n("Device Busy");
case Solid::OperationFailed: return i18n("Operation Failed");
case Solid::UserCanceled: return i18n("User Canceled");
case Solid::InvalidOption: return i18n("Invalid Option");
case Solid::MissingDriver: return i18n("Missing Driver");
default: return QString();
}
}
//--------------------------------------------------------------------------------
void DeviceItem::mountDone(Action action, Solid::ErrorType error, QVariant errorData, const QString &udi)
{
Q_UNUSED(udi)
if ( error == Solid::NoError )
{
fillData();
if ( action == Unmount )
{
statusLabel->setText(i18n("The device can now be safely removed"));
statusLabel->show();
statusTimer.start();
}
}
else
{
QString text = (action == Mount) ? i18n("Mount failed:") : i18n("Unmount failed:");
statusLabel->setText("<b>" + text + "</b>" + errorToString(error) + "<br>" + errorData.toString());
statusLabel->show();
statusTimer.start();
}
}
//--------------------------------------------------------------------------------
void DeviceItem::teardownDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
{
mountDone(Unmount, error, errorData, udi);
}
//--------------------------------------------------------------------------------
void DeviceItem::setupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
{
mountDone(Mount, error, errorData, udi);
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
DeviceList::DeviceList(QWidget *parent)
: QFrame(parent)
{
setWindowFlags(windowFlags() | Qt::Tool);
setFrameShape(QFrame::StyledPanel);
setAttribute(Qt::WA_AlwaysShowToolTips);
loadActions();
vbox = new QVBoxLayout(this);
vbox->setContentsMargins(QMargins());
vbox->addStretch();
predicate = Solid::Predicate(Solid::DeviceInterface::StorageAccess);
predicate |= Solid::Predicate(Solid::DeviceInterface::StorageDrive);
predicate |= Solid::Predicate(Solid::DeviceInterface::StorageVolume);
predicate |= Solid::Predicate(Solid::DeviceInterface::OpticalDrive);
predicate |= Solid::Predicate(Solid::DeviceInterface::OpticalDisc);
predicate |= Solid::Predicate(Solid::DeviceInterface::PortableMediaPlayer);
predicate |= Solid::Predicate(Solid::DeviceInterface::Camera);
QList<Solid::Device> devices = Solid::Device::listFromQuery(predicate);
for (Solid::Device device : devices)
addDevice(device);
connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded,
this, &DeviceList::deviceAdded);
connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved,
this, &DeviceList::deviceRemoved);
connect(&kdeConnect, &KdeConnect::deviceAdded, this, &DeviceList::kdeConnectDeviceAdded);
connect(&kdeConnect, &KdeConnect::deviceRemoved, this, &DeviceList::deviceRemoved);
}
//--------------------------------------------------------------------------------
void DeviceList::loadActions()
{
actions.clear();
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "solid/actions", QStandardPaths::LocateDirectory);
for (const QString &dirPath : dirs)
{
QDir dir(dirPath);
for (const QString &file : dir.entryList(QStringList(QLatin1String("*.desktop")), QDir::Files))
{
QString path = dir.absoluteFilePath(file);
KDesktopFile cfg(path);
const QString predicateString = cfg.desktopGroup().readEntry("X-KDE-Solid-Predicate");
QList<KServiceAction> actionList = KDesktopFileActions::userDefinedServices(path, true);
if ( !actionList.isEmpty() && !predicateString.isEmpty() )
actions.append(DeviceAction(path, Solid::Predicate::fromString(predicateString), actionList[0]));
}
}
}
//--------------------------------------------------------------------------------
void DeviceList::addDevice(Solid::Device device)
{
if ( items.contains(device.udi()) )
{
//qDebug() << device.udi() << "already known";
return;
}
QVector<DeviceAction> deviceActions;
for (const DeviceAction &action : actions)
{
if ( action.predicate.matches(device) )
deviceActions.append(action);
}
Solid::StorageVolume *storage = device.as<Solid::StorageVolume>();
if ( !storage ) // storage can at least be mounted; others need some specific actions
{
if ( deviceActions.isEmpty() )
{
//qDebug() << device.udi() << "no action found";
return;
}
}
else if ( storage->usage() != Solid::StorageVolume::FileSystem )
{
//qDebug() << device.udi() << "storage no filesystem";
return;
}
// show only removable devices
if ( device.is<Solid::StorageDrive>() &&
!device.as<Solid::StorageDrive>()->isRemovable() )
{
//qDebug() << device.udi() << "not Removable";
return;
}
// show only removable devices
if ( device.parent().is<Solid::StorageDrive>() &&
!device.parent().as<Solid::StorageDrive>()->isRemovable() )
{
//qDebug() << device.parent().udi() << "parent() not Removable";
return;
}
DeviceItem *item = new DeviceItem(device, deviceActions);
vbox->insertWidget(vbox->count() - 1, item); // insert before stretch
items.insert(device.udi(), item);
}
//--------------------------------------------------------------------------------
void DeviceList::deviceAdded(const QString &dev)
{
int oldCount = items.count();
Solid::Device device(dev);
if ( !predicate.matches(device) )
return;
addDevice(device);
// when we added a new device, make sure the DeviceNotifier shows and places this window
if ( items.count() != oldCount )
emit deviceWasAdded();
}
//--------------------------------------------------------------------------------
void DeviceList::deviceRemoved(const QString &dev)
{
if ( items.contains(dev) )
{
delete items.take(dev);
emit deviceWasRemoved();
}
}
//--------------------------------------------------------------------------------
void DeviceList::kdeConnectDeviceAdded(const KdeConnect::Device &device)
{
if ( items.contains(device->id) )
{
//qDebug() << device->id << "already known";
return;
}
DeviceItem *item = new DeviceItem(device);
vbox->insertWidget(vbox->count() - 1, item); // insert before stretch
items.insert(device->id, item);
// when we added a new device, make sure the DeviceNotifier shows and places this window
emit deviceWasAdded();
}
//--------------------------------------------------------------------------------