2016-11-23 16:49:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
|
|
|
|
*
|
|
|
|
* 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) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* 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
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-11-23 16:49:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "deviceindicator.h"
|
2017-01-11 17:04:01 +00:00
|
|
|
#include <QFileDialog>
|
2016-11-23 16:49:41 +00:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2019-03-11 17:50:14 +00:00
|
|
|
#include "interfaces/dbusinterfaces.h"
|
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2016-11-23 16:49:41 +00:00
|
|
|
class BatteryAction : public QAction
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
BatteryAction(DeviceDbusInterface* device)
|
2016-11-23 19:27:09 +00:00
|
|
|
: QAction(nullptr)
|
2016-11-23 16:49:41 +00:00
|
|
|
, m_batteryIface(new DeviceBatteryDbusInterface(device->id(), this))
|
|
|
|
{
|
|
|
|
setWhenAvailable(m_batteryIface->charge(), [this](int charge) { setCharge(charge); }, this);
|
|
|
|
setWhenAvailable(m_batteryIface->isCharging(), [this](bool charging) { setCharging(charging); }, this);
|
|
|
|
|
|
|
|
connect(m_batteryIface, SIGNAL(chargeChanged(int)), this, SLOT(setCharge(int)));
|
|
|
|
connect(m_batteryIface, SIGNAL(stateChanged(bool)), this, SLOT(setCharging(bool)));
|
|
|
|
|
2018-12-07 14:53:07 +00:00
|
|
|
setIcon(QIcon::fromTheme(QStringLiteral("battery")));
|
|
|
|
|
2016-11-23 16:49:41 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void update() {
|
|
|
|
if (m_charge < 0)
|
|
|
|
setText(i18n("No Battery"));
|
|
|
|
else if (m_charging)
|
2016-11-23 17:43:17 +00:00
|
|
|
setText(i18n("Battery: %1% (Charging)", m_charge));
|
2016-11-23 16:49:41 +00:00
|
|
|
else
|
2016-11-23 17:43:17 +00:00
|
|
|
setText(i18n("Battery: %1%", m_charge));
|
2016-11-23 16:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void setCharge(int charge) { m_charge = charge; update(); }
|
|
|
|
void setCharging(bool charging) { m_charging = charging; update(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DeviceBatteryDbusInterface* m_batteryIface;
|
|
|
|
int m_charge = -1;
|
|
|
|
bool m_charging = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DeviceIndicator::DeviceIndicator(DeviceDbusInterface* device)
|
|
|
|
: QMenu(device->name(), nullptr)
|
|
|
|
, m_device(device)
|
2019-07-20 09:23:33 +01:00
|
|
|
, m_remoteCommandsInterface(new RemoteCommandsDbusInterface(m_device->id()))
|
2016-11-23 16:49:41 +00:00
|
|
|
{
|
2017-05-24 18:25:48 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2019-06-10 15:40:28 +01:00
|
|
|
setIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons/hicolor/scalable/status/") + device->iconName() + QStringLiteral(".svg"))));
|
2017-05-24 18:25:48 +01:00
|
|
|
#else
|
2016-11-23 16:49:41 +00:00
|
|
|
setIcon(QIcon::fromTheme(device->iconName()));
|
2017-05-24 18:25:48 +01:00
|
|
|
#endif
|
2016-11-23 16:49:41 +00:00
|
|
|
|
2016-11-23 17:43:17 +00:00
|
|
|
connect(device, SIGNAL(nameChanged(QString)), this, SLOT(setText(QString)));
|
|
|
|
|
2017-02-20 23:08:03 +00:00
|
|
|
auto battery = new BatteryAction(device);
|
|
|
|
addAction(battery);
|
2019-06-10 15:40:28 +01:00
|
|
|
setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_battery")),
|
2017-09-18 19:04:46 +01:00
|
|
|
[battery](bool available) { battery->setVisible(available); }
|
2017-02-20 22:27:42 +00:00
|
|
|
, this);
|
2016-11-23 17:43:17 +00:00
|
|
|
|
2018-12-07 14:53:07 +00:00
|
|
|
auto browse = addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Browse device"));
|
2016-11-23 17:43:17 +00:00
|
|
|
connect(browse, &QAction::triggered, device, [device](){
|
|
|
|
SftpDbusInterface* sftpIface = new SftpDbusInterface(device->id(), device);
|
|
|
|
sftpIface->startBrowsing();
|
|
|
|
sftpIface->deleteLater();
|
|
|
|
});
|
2019-06-10 15:40:28 +01:00
|
|
|
setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_sftp")), [browse](bool available) { browse->setVisible(available); }, this);
|
2017-01-11 16:54:52 +00:00
|
|
|
|
2018-12-07 14:53:07 +00:00
|
|
|
auto findDevice = addAction(QIcon::fromTheme(QStringLiteral("irc-voice")), i18n("Ring device"));
|
2016-11-23 17:43:17 +00:00
|
|
|
connect(findDevice, &QAction::triggered, device, [device](){
|
|
|
|
FindMyPhoneDeviceDbusInterface* iface = new FindMyPhoneDeviceDbusInterface(device->id(), device);
|
|
|
|
iface->ring();
|
|
|
|
iface->deleteLater();
|
|
|
|
});
|
2019-06-10 15:40:28 +01:00
|
|
|
setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_findmyphone")), [findDevice](bool available) { findDevice->setVisible(available); }, this);
|
2016-11-23 17:43:17 +00:00
|
|
|
|
2018-12-07 14:53:07 +00:00
|
|
|
auto sendFile = addAction(QIcon::fromTheme(QStringLiteral("document-share")), i18n("Send file"));
|
2017-01-11 17:04:01 +00:00
|
|
|
connect(sendFile, &QAction::triggered, device, [device, this](){
|
|
|
|
const QUrl url = QFileDialog::getOpenFileUrl(parentWidget(), i18n("Select file to send to '%1'", device->name()), QUrl::fromLocalFile(QDir::homePath()));
|
|
|
|
if (url.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect/devices/") + device->id() + QStringLiteral("/share"), QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl"));
|
2017-01-11 17:04:01 +00:00
|
|
|
msg.setArguments(QVariantList() << url.toString());
|
2019-08-14 16:36:19 +01:00
|
|
|
DBusHelper::sessionBus().call(msg);
|
2017-01-11 17:04:01 +00:00
|
|
|
});
|
2019-07-20 09:23:33 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_share")), [sendFile](bool available) { sendFile->setVisible(available); }, this);
|
2019-07-20 09:23:33 +01:00
|
|
|
|
2019-08-19 06:39:05 +01:00
|
|
|
// Search current application path
|
|
|
|
const QString kdeconnectsmsExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-sms"), { QCoreApplication::applicationDirPath() });
|
|
|
|
if (!kdeconnectsmsExecutable.isEmpty()) {
|
2019-07-20 15:02:38 +01:00
|
|
|
auto smsapp = addAction(QIcon::fromTheme(QStringLiteral("message-new")), i18n("SMS Messages..."));
|
2019-08-19 06:39:05 +01:00
|
|
|
QObject::connect(smsapp, &QAction::triggered, device, [device, kdeconnectsmsExecutable] () {
|
|
|
|
QProcess::startDetached(kdeconnectsmsExecutable, { QStringLiteral("--device"), device->id() });
|
2019-07-20 15:02:38 +01:00
|
|
|
});
|
|
|
|
setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_sms")), [smsapp](bool available) { smsapp->setVisible(available); }, this);
|
|
|
|
}
|
|
|
|
|
2019-07-20 09:23:33 +01:00
|
|
|
QMenu* remoteCommandsMenu = new QMenu(i18n("Run command"), this);
|
|
|
|
QAction* menuAction = remoteCommandsMenu->menuAction();
|
|
|
|
QAction* addCommandAction = remoteCommandsMenu->addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add commands"));
|
|
|
|
connect(addCommandAction, &QAction::triggered, m_remoteCommandsInterface, &RemoteCommandsDbusInterface::editCommands);
|
|
|
|
|
|
|
|
addAction(menuAction);
|
|
|
|
setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_remotecommands")), [this, remoteCommandsMenu, menuAction](bool available) {
|
|
|
|
menuAction->setVisible(available);
|
|
|
|
|
|
|
|
if (!available)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto cmds = QJsonDocument::fromJson(m_remoteCommandsInterface->commands()).object();
|
|
|
|
|
|
|
|
for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it!=itEnd; ++it) {
|
|
|
|
const QJsonObject cont = it->toObject();
|
|
|
|
QString key = it.key();
|
|
|
|
QAction* action = remoteCommandsMenu->addAction(cont.value(QStringLiteral("name")).toString());
|
|
|
|
connect(action, &QAction::triggered, [this, key] {
|
|
|
|
m_remoteCommandsInterface->triggerCommand(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, this);
|
2016-11-23 16:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "deviceindicator.moc"
|