2014-04-01 02:12:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-06-14 14:22:40 +01:00
|
|
|
#include <interfaces/devicesmodel.h>
|
2014-07-01 00:15:23 +01:00
|
|
|
#include <interfaces/notificationsmodel.h>
|
2014-06-23 17:15:10 +01:00
|
|
|
#include <interfaces/dbusinterfaces.h>
|
2014-04-01 02:12:31 +01:00
|
|
|
#include <iostream>
|
2014-06-14 14:22:40 +01:00
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusConnection>
|
2014-09-22 01:26:06 +01:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <KAboutData>
|
|
|
|
#include <KLocalizedString>
|
2014-04-01 02:12:31 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2014-09-22 01:26:06 +01:00
|
|
|
QGuiApplication app(argc, argv);
|
|
|
|
KAboutData about("kdeconnect-cli", i18n("kdeconnect-cli"), "1.0", i18n("KDE Connect CLI tool"),
|
|
|
|
KAboutLicense::GPL, i18n("(C) 2013 Aleix Pol Gonzalez"));
|
|
|
|
KAboutData::setApplicationData(about);
|
2014-07-01 22:59:38 +01:00
|
|
|
|
2014-09-22 01:26:06 +01:00
|
|
|
about.addAuthor( i18n("Aleix Pol Gonzalez"), QString(), "aleixpol@kde.org" );
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addOption(QCommandLineOption(QStringList("l") << "list-devices", i18n("List all devices")));
|
|
|
|
parser.addOption(QCommandLineOption("share", i18n("Share a file to a said device"), "path"));
|
|
|
|
parser.addOption(QCommandLineOption("pair", i18n("Request pairing to a said device")));
|
|
|
|
parser.addOption(QCommandLineOption("unpair", i18n("Stop pairing to a said device")));
|
|
|
|
parser.addOption(QCommandLineOption("ping", i18n("Sends a ping to said device")));
|
|
|
|
parser.addOption(QCommandLineOption("list-notifications", i18n("Display the notifications on a said device")));
|
2014-11-04 18:12:29 +00:00
|
|
|
parser.addOption(QCommandLineOption("ping-msg <message>", i18n("Same as ping but you can customize the shown message."), i18n("message")));
|
2014-09-22 01:26:06 +01:00
|
|
|
parser.addOption(QCommandLineOption("device", i18n("Device ID"), "dev"));
|
|
|
|
about.setupCommandLine(&parser);
|
|
|
|
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.process(app);
|
|
|
|
about.processCommandLine(&parser);
|
|
|
|
|
|
|
|
if(parser.isSet("l")) {
|
2014-04-01 02:12:31 +01:00
|
|
|
DevicesModel devices;
|
|
|
|
for(int i=0, rows=devices.rowCount(); i<rows; ++i) {
|
|
|
|
QModelIndex idx = devices.index(i);
|
2014-06-27 15:18:43 +01:00
|
|
|
QString statusInfo;
|
|
|
|
switch(idx.data(DevicesModel::StatusModelRole).toInt()) {
|
|
|
|
case DevicesModel::StatusPaired:
|
|
|
|
statusInfo = "(paired)";
|
|
|
|
break;
|
|
|
|
case DevicesModel::StatusReachable:
|
|
|
|
statusInfo = "(reachable)";
|
|
|
|
break;
|
|
|
|
case DevicesModel::StatusReachable | DevicesModel::StatusPaired:
|
|
|
|
statusInfo = "(paired and reachable)";
|
|
|
|
break;
|
|
|
|
}
|
2014-04-01 02:12:31 +01:00
|
|
|
std::cout << "- " << idx.data(Qt::DisplayRole).toString().toStdString()
|
2014-06-27 15:18:43 +01:00
|
|
|
<< ": " << idx.data(DevicesModel::IdModelRole).toString().toStdString() << ' ' << statusInfo.toStdString() << std::endl;
|
2014-04-01 02:12:31 +01:00
|
|
|
}
|
|
|
|
std::cout << devices.rowCount() << " devices found" << std::endl;
|
2014-11-04 18:12:29 +00:00
|
|
|
} else if(parser.isSet("refresh")) {
|
2014-09-26 16:36:39 +01:00
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect", "org.kde.kdeconnect.daemon", "forceOnNetworkChange");
|
|
|
|
QDBusConnection::sessionBus().call(msg);
|
2014-04-01 02:12:31 +01:00
|
|
|
} else {
|
|
|
|
QString device;
|
2014-09-22 01:26:06 +01:00
|
|
|
if(!parser.isSet("device")) {
|
|
|
|
qCritical() << (i18n("No device specified"));
|
2014-04-01 02:12:31 +01:00
|
|
|
}
|
2014-09-22 01:26:06 +01:00
|
|
|
device = parser.value("device");
|
2014-04-01 02:12:31 +01:00
|
|
|
QUrl url;
|
2014-09-22 01:26:06 +01:00
|
|
|
if(parser.isSet("share")) {
|
|
|
|
url = QUrl::fromUserInput(parser.value("share"));
|
|
|
|
parser.clearPositionalArguments();
|
2014-06-23 17:15:10 +01:00
|
|
|
if(!url.isEmpty() && !device.isEmpty()) {
|
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device+"/share", "org.kde.kdeconnect.device.share", "shareUrl");
|
|
|
|
msg.setArguments(QVariantList() << url.toString());
|
|
|
|
QDBusConnection::sessionBus().call(msg);
|
|
|
|
} else
|
2014-09-22 01:26:06 +01:00
|
|
|
qCritical() << (i18n("Couldn't share %1", url.toString()));
|
|
|
|
} else if(parser.isSet("pair")) {
|
2014-06-23 17:15:10 +01:00
|
|
|
DeviceDbusInterface dev(device);
|
|
|
|
if(dev.isPaired())
|
|
|
|
std::cout << "Already paired" << std::endl;
|
|
|
|
else {
|
|
|
|
QDBusPendingReply<void> req = dev.requestPair();
|
|
|
|
req.waitForFinished();
|
|
|
|
}
|
2014-09-22 01:26:06 +01:00
|
|
|
} else if(parser.isSet("unpair")) {
|
2014-06-23 17:15:10 +01:00
|
|
|
DeviceDbusInterface dev(device);
|
|
|
|
if(!dev.isPaired())
|
|
|
|
std::cout << "Already not paired" << std::endl;
|
|
|
|
else {
|
|
|
|
QDBusPendingReply<void> req = dev.unpair();
|
|
|
|
req.waitForFinished();
|
|
|
|
}
|
2014-11-04 18:12:29 +00:00
|
|
|
} else if(parser.isSet("ping") || parser.isSet("ping-msg")) {
|
2014-06-27 17:21:40 +01:00
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device+"/ping", "org.kde.kdeconnect.device.ping", "sendPing");
|
2014-11-04 18:12:29 +00:00
|
|
|
if (parser.isSet("ping-msg")) {
|
|
|
|
QString message = parser.value("ping-msg");
|
2014-09-27 04:39:38 +01:00
|
|
|
msg.setArguments(QVariantList() << message);
|
|
|
|
}
|
2014-04-01 02:12:31 +01:00
|
|
|
QDBusConnection::sessionBus().call(msg);
|
2014-09-22 01:26:06 +01:00
|
|
|
} else if(parser.isSet("list-notifications")) {
|
2014-07-01 00:15:23 +01:00
|
|
|
NotificationsModel notifications;
|
|
|
|
notifications.setDeviceId(device);
|
|
|
|
for(int i=0, rows=notifications.rowCount(); i<rows; ++i) {
|
|
|
|
QModelIndex idx = notifications.index(i);
|
|
|
|
std::cout << "- " << idx.data(NotificationsModel::AppNameModelRole).toString().toStdString()
|
|
|
|
<< ": " << idx.data(NotificationsModel::NameModelRole).toString().toStdString() << std::endl;
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-22 01:26:06 +01:00
|
|
|
qCritical() << i18n("Nothing to be done with the device");
|
2014-07-01 00:15:23 +01:00
|
|
|
}
|
2014-04-01 02:12:31 +01:00
|
|
|
}
|
|
|
|
QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|