deviceindicator.cpp 4.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21
 */

#include "deviceindicator.h"
22
#include <QFileDialog>
23 24
#include <KLocalizedString>

Nicolas Fella's avatar
Nicolas Fella committed
25 26
#include "interfaces/dbusinterfaces.h"

27 28
#include <dbushelper.h>

29 30 31 32 33
class BatteryAction : public QAction
{
Q_OBJECT
public:
    BatteryAction(DeviceDbusInterface* device)
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
34
        : QAction(nullptr)
35 36 37 38 39 40 41 42
        , 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)));

Nicolas Fella's avatar
Nicolas Fella committed
43 44
        setIcon(QIcon::fromTheme(QStringLiteral("battery")));

45 46 47 48 49 50 51
        update();
    }

    void update() {
        if (m_charge < 0)
            setText(i18n("No Battery"));
        else if (m_charging)
52
            setText(i18n("Battery: %1% (Charging)", m_charge));
53
        else
54
            setText(i18n("Battery: %1%", m_charge));
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    }

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)
{
72
#ifdef Q_OS_WIN
73
    setIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons/hicolor/scalable/status/") + device->iconName() + QStringLiteral(".svg"))));
74
#else
75
    setIcon(QIcon::fromTheme(device->iconName()));
76
#endif
77

78 79
    connect(device, SIGNAL(nameChanged(QString)), this, SLOT(setText(QString)));

Albert Vaca's avatar
Albert Vaca committed
80 81
    auto battery = new BatteryAction(device);
    addAction(battery);
82
    setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_battery")),
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
83
                     [battery](bool available) { battery->setVisible(available);  }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
84
                     , this);
85

Nicolas Fella's avatar
Nicolas Fella committed
86
    auto browse = addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Browse device"));
87 88 89 90 91
    connect(browse, &QAction::triggered, device, [device](){
        SftpDbusInterface* sftpIface = new SftpDbusInterface(device->id(), device);
        sftpIface->startBrowsing();
        sftpIface->deleteLater();
    });
92
    setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_sftp")), [browse](bool available) { browse->setVisible(available); }, this);
93

Nicolas Fella's avatar
Nicolas Fella committed
94
    auto findDevice = addAction(QIcon::fromTheme(QStringLiteral("irc-voice")), i18n("Ring device"));
95 96 97 98 99
    connect(findDevice, &QAction::triggered, device, [device](){
        FindMyPhoneDeviceDbusInterface* iface = new FindMyPhoneDeviceDbusInterface(device->id(), device);
        iface->ring();
        iface->deleteLater();
    });
100
    setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_findmyphone")), [findDevice](bool available) { findDevice->setVisible(available); }, this);
101

Nicolas Fella's avatar
Nicolas Fella committed
102
    auto sendFile = addAction(QIcon::fromTheme(QStringLiteral("document-share")), i18n("Send file"));
103 104 105 106 107
    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;

108
        QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect/devices/") + device->id() + QStringLiteral("/share"), QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl"));
109
        msg.setArguments(QVariantList() << url.toString());
110
        DbusHelper::sessionBus().call(msg);
111
    });
112
    setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_share")), [sendFile](bool available) { sendFile->setVisible(available); }, this);
113 114 115
}

#include "deviceindicator.moc"