2014-04-01 02:12:31 +01:00
/*
2015-02-05 07:52:07 +00:00
* Copyright 2015 Aleix Pol Gonzalez < aleixpol @ kde . org >
2014-04-01 02:12:31 +01:00
*
* 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/>.
2014-04-01 02:12:31 +01:00
*/
2015-07-19 15:55:28 +01:00
# include <QCryptographicHash>
2015-09-11 12:35:16 +01:00
# include <QIODevice>
2014-06-14 14:22:40 +01:00
# include <QDBusMessage>
2015-03-10 04:59:36 +00:00
# include <QCoreApplication>
# include <QTextStream>
2017-01-23 08:08:27 +00:00
# include <QFile>
2015-03-10 04:59:36 +00:00
2014-09-22 01:26:06 +01:00
# include <KAboutData>
2014-04-01 02:12:31 +01:00
2015-03-10 04:59:36 +00:00
# include "interfaces/devicesmodel.h"
# include "interfaces/notificationsmodel.h"
# include "interfaces/dbusinterfaces.h"
2017-06-06 16:48:30 +01:00
# include "interfaces/dbushelpers.h"
2015-03-10 04:59:36 +00:00
# include "kdeconnect-version.h"
2015-02-05 07:52:07 +00:00
2019-06-09 16:28:49 +01:00
# include <dbushelper.h>
2014-04-01 02:12:31 +01:00
int main ( int argc , char * * argv )
{
2014-11-28 16:42:53 +00:00
QCoreApplication app ( argc , argv ) ;
2016-11-26 14:38:08 +00:00
KAboutData about ( QStringLiteral ( " kdeconnect-cli " ) ,
QStringLiteral ( " kdeconnect-cli " ) ,
QStringLiteral ( KDECONNECT_VERSION_STRING ) ,
2015-02-05 07:52:07 +00:00
i18n ( " KDE Connect CLI tool " ) ,
2018-09-10 10:28:54 +01:00
KAboutLicense : : GPL ,
2015-02-05 07:52:07 +00:00
i18n ( " (C) 2015 Aleix Pol Gonzalez " ) ) ;
2014-09-22 01:26:06 +01:00
KAboutData : : setApplicationData ( about ) ;
2014-07-01 22:59:38 +01:00
2016-11-26 14:38:08 +00:00
about . addAuthor ( i18n ( " Aleix Pol Gonzalez " ) , QString ( ) , QStringLiteral ( " aleixpol@kde.org " ) ) ;
about . addAuthor ( i18n ( " Albert Vaca Cintora " ) , QString ( ) , QStringLiteral ( " albertvaka@gmail.com " ) ) ;
2014-09-22 01:26:06 +01:00
QCommandLineParser parser ;
2016-11-26 14:38:08 +00:00
parser . addOption ( QCommandLineOption ( QStringList ( QStringLiteral ( " l " ) ) < < QStringLiteral ( " list-devices " ) , i18n ( " List all devices " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringList ( QStringLiteral ( " a " ) ) < < QStringLiteral ( " list-available " ) , i18n ( " List available (paired and reachable) devices " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " id-only " ) , i18n ( " Make --list-devices or --list-available print only the devices id, to ease scripting " ) ) ) ;
2018-02-26 18:39:34 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " name-only " ) , i18n ( " Make --list-devices or --list-available print only the devices name, to ease scripting " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " id-name-only " ) , i18n ( " Make --list-devices or --list-available print only the devices id and name, to ease scripting " ) ) ) ;
2016-11-26 14:38:08 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " refresh " ) , i18n ( " Search for devices in the network and re-establish connections " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " pair " ) , i18n ( " Request pairing to a said device " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " ring " ) , i18n ( " Find the said device by ringing it. " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " unpair " ) , i18n ( " Stop pairing to a said device " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " ping " ) , i18n ( " Sends a ping to said device " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " ping-msg " ) , i18n ( " Same as ping but you can set the message to display " ) , i18n ( " message " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " share " ) , i18n ( " Share a file to a said device " ) , QStringLiteral ( " path " ) ) ) ;
2018-11-29 20:20:56 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " share-text " ) , i18n ( " Share text to a said device " ) , QStringLiteral ( " text " ) ) ) ;
2016-11-26 14:38:08 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " list-notifications " ) , i18n ( " Display the notifications on a said device " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " lock " ) , i18n ( " Lock the specified device " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " send-sms " ) , i18n ( " Sends an SMS. Requires destination " ) , i18n ( " message " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " destination " ) , i18n ( " Phone number to send the message " ) , i18n ( " phone number " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringList ( QStringLiteral ( " device " ) ) < < QStringLiteral ( " d " ) , i18n ( " Device ID " ) , QStringLiteral ( " dev " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringList ( QStringLiteral ( " name " ) ) < < QStringLiteral ( " n " ) , i18n ( " Device Name " ) , QStringLiteral ( " name " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " encryption-info " ) , i18n ( " Get encryption info about said device " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " list-commands " ) , i18n ( " Lists remote commands and their ids " ) ) ) ;
parser . addOption ( QCommandLineOption ( QStringLiteral ( " execute-command " ) , i18n ( " Executes a remote command by id " ) , QStringLiteral ( " id " ) ) ) ;
2019-03-09 23:12:09 +00:00
parser . addOption ( QCommandLineOption ( QStringList { QStringLiteral ( " k " ) , QStringLiteral ( " send-keys " ) } , i18n ( " Sends keys to a said device " ) , QStringLiteral ( " key " ) ) ) ;
2017-02-24 21:37:00 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " my-id " ) , i18n ( " Display this device's id and exit " ) ) ) ;
2019-09-13 17:11:16 +01:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " photo " ) , i18n ( " Open the connected device's camera and transfer the photo " ) , QStringLiteral ( " path " ) ) ) ;
2019-03-13 19:58:28 +00:00
//Hidden because it's an implementation detail
QCommandLineOption deviceAutocomplete ( QStringLiteral ( " shell-device-autocompletion " ) ) ;
2019-07-19 21:11:49 +01:00
deviceAutocomplete . setFlags ( QCommandLineOption : : HiddenFromHelp ) ;
2019-06-10 15:40:28 +01:00
deviceAutocomplete . setDescription ( QStringLiteral ( " Outputs all available devices id's with their name and paired status " ) ) ; //Not visible, so no translation needed
deviceAutocomplete . setValueName ( QStringLiteral ( " shell " ) ) ;
2019-03-13 19:58:28 +00:00
parser . addOption ( deviceAutocomplete ) ;
2014-09-22 01:26:06 +01:00
about . setupCommandLine ( & parser ) ;
parser . addHelpOption ( ) ;
parser . process ( app ) ;
about . processCommandLine ( & parser ) ;
2019-06-10 15:40:28 +01:00
const QString id = QStringLiteral ( " kdeconnect-cli- " ) + QString : : number ( QCoreApplication : : applicationPid ( ) ) ;
2015-09-12 21:07:05 +01:00
DaemonDbusInterface iface ;
2017-02-24 21:37:00 +00:00
if ( parser . isSet ( QStringLiteral ( " my-id " ) ) ) {
QTextStream ( stdout ) < < iface . selfId ( ) < < endl ;
} else if ( parser . isSet ( QStringLiteral ( " l " ) ) | | parser . isSet ( QStringLiteral ( " a " ) ) ) {
2018-12-24 22:40:07 +00:00
bool reachable = false ;
2016-11-26 14:38:08 +00:00
if ( parser . isSet ( QStringLiteral ( " a " ) ) ) {
2015-03-16 01:58:22 +00:00
reachable = true ;
2015-09-09 22:22:59 +01:00
} else {
2017-01-19 19:01:11 +00:00
blockOnReply ( iface . acquireDiscoveryMode ( id ) ) ;
2015-09-09 22:22:59 +01:00
QThread : : sleep ( 2 ) ;
2015-02-25 06:13:30 +00:00
}
2018-12-24 22:40:07 +00:00
const QStringList devices = blockOnReply < QStringList > ( iface . devices ( reachable , false ) ) ;
2015-03-16 01:58:22 +00:00
2018-02-26 18:39:34 +00:00
bool displayCount = true ;
2017-07-20 15:14:07 +01:00
for ( const QString & id : devices ) {
2016-11-26 14:38:08 +00:00
if ( parser . isSet ( QStringLiteral ( " id-only " ) ) ) {
2015-03-16 01:58:22 +00:00
QTextStream ( stdout ) < < id < < endl ;
2018-02-26 18:39:34 +00:00
displayCount = false ;
} else if ( parser . isSet ( QStringLiteral ( " name-only " ) ) ) {
DeviceDbusInterface deviceIface ( id ) ;
QTextStream ( stdout ) < < deviceIface . name ( ) < < endl ;
displayCount = false ;
} else if ( parser . isSet ( QStringLiteral ( " id-name-only " ) ) ) {
DeviceDbusInterface deviceIface ( id ) ;
QTextStream ( stdout ) < < id < < ' ' < < deviceIface . name ( ) < < endl ;
displayCount = false ;
2015-02-25 06:13:30 +00:00
} else {
2015-03-16 01:58:22 +00:00
DeviceDbusInterface deviceIface ( id ) ;
2015-02-25 06:13:30 +00:00
QString statusInfo ;
2016-11-23 16:20:59 +00:00
const bool isReachable = deviceIface . isReachable ( ) ;
const bool isTrusted = deviceIface . isTrusted ( ) ;
if ( isReachable & & isTrusted ) {
2015-03-16 01:58:22 +00:00
statusInfo = i18n ( " (paired and reachable) " ) ;
} else if ( isReachable ) {
statusInfo = i18n ( " (reachable) " ) ;
2016-11-23 16:20:59 +00:00
} else if ( isTrusted ) {
2015-03-16 01:58:22 +00:00
statusInfo = i18n ( " (paired) " ) ;
2016-11-23 16:20:59 +00:00
}
2015-03-16 01:58:22 +00:00
QTextStream ( stdout ) < < " - " < < deviceIface . name ( )
< < " : " < < deviceIface . id ( ) < < ' ' < < statusInfo < < endl ;
2014-06-27 15:18:43 +01:00
}
2014-04-01 02:12:31 +01:00
}
2018-02-26 18:39:34 +00:00
if ( displayCount ) {
2019-03-13 19:58:28 +00:00
QTextStream ( stderr ) < < i18np ( " 1 device found " , " %1 devices found " , devices . size ( ) ) < < endl ;
2015-03-16 01:58:22 +00:00
} else if ( devices . isEmpty ( ) ) {
2015-02-25 06:13:30 +00:00
QTextStream ( stderr ) < < i18n ( " No devices found " ) < < endl ;
}
2015-09-09 22:22:59 +01:00
2017-01-19 19:01:11 +00:00
blockOnReply ( iface . releaseDiscoveryMode ( id ) ) ;
2019-03-13 19:58:28 +00:00
} else if ( parser . isSet ( QStringLiteral ( " shell-device-autocompletion " ) ) ) {
//Outputs a list of reachable devices in zsh autocomplete format, with the name as description
const QStringList devices = blockOnReply < QStringList > ( iface . devices ( true , false ) ) ;
for ( const QString & id : devices ) {
DeviceDbusInterface deviceIface ( id ) ;
QString statusInfo ;
const bool isTrusted = deviceIface . isTrusted ( ) ;
if ( isTrusted ) {
statusInfo = i18n ( " (paired) " ) ;
} else {
statusInfo = i18n ( " (unpaired) " ) ;
}
//Description: "device name (paired/unpaired)"
2019-06-10 15:40:28 +01:00
QString description = deviceIface . name ( ) + QLatin1Char ( ' ' ) + statusInfo ;
2019-03-13 19:58:28 +00:00
//Replace characters
2019-06-10 15:40:28 +01:00
description . replace ( QLatin1Char ( ' \\ ' ) , QStringLiteral ( " \\ \\ " ) ) ;
description . replace ( QLatin1Char ( ' [ ' ) , QStringLiteral ( " \\ [ " ) ) ;
description . replace ( QLatin1Char ( ' ] ' ) , QStringLiteral ( " \\ ] " ) ) ;
description . replace ( QLatin1Char ( ' \' ' ) , QStringLiteral ( " \\ ' " ) ) ;
description . replace ( QLatin1Char ( ' \" ' ) , QStringLiteral ( " \\ \" " ) ) ;
description . replace ( QLatin1Char ( ' \n ' ) , QLatin1Char ( ' ' ) ) ;
description . remove ( QLatin1Char ( ' \0 ' ) ) ;
2019-03-13 19:58:28 +00:00
//Output id and description
QTextStream ( stdout ) < < id < < ' [ ' < < description < < ' ] ' < < endl ;
}
2019-09-11 20:19:42 +01:00
//Exit with 1 if we didn't find a device
return int ( ! devices . isEmpty ( ) ) ;
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " refresh " ) ) ) {
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect " ) , QStringLiteral ( " org.kde.kdeconnect.daemon " ) , QStringLiteral ( " forceOnNetworkChange " ) ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2014-04-01 02:12:31 +01:00
} else {
2016-10-24 11:42:20 +01:00
2016-11-26 14:38:08 +00:00
QString device = parser . value ( QStringLiteral ( " device " ) ) ;
if ( device . isEmpty ( ) & & parser . isSet ( QStringLiteral ( " name " ) ) ) {
2017-01-19 19:01:11 +00:00
device = blockOnReply ( iface . deviceIdByName ( parser . value ( QStringLiteral ( " name " ) ) ) ) ;
2016-06-05 22:31:13 +01:00
if ( device . isEmpty ( ) ) {
2016-11-26 14:38:08 +00:00
QTextStream ( stderr ) < < " Couldn't find device: " < < parser . value ( QStringLiteral ( " name " ) ) < < endl ;
2016-06-05 22:31:13 +01:00
return 1 ;
}
}
if ( device . isEmpty ( ) ) {
2019-12-21 23:37:29 +00:00
QTextStream ( stderr ) < < i18n ( " No device specified: Use -d <Device ID> or -n <Device Name> to specify a device. \n Device ID's and names may be found using \" kdeconnect-cli -l \" \n View complete help with --help option " ) < < endl ;
return 1 ;
2014-04-01 02:12:31 +01:00
}
2016-10-24 11:42:20 +01:00
2019-09-10 17:09:06 +01:00
if ( ! blockOnReply < QStringList > ( iface . devices ( false , false ) ) . contains ( device ) ) {
QTextStream ( stderr ) < < " Couldn't find device with id \" " < < device < < " \" . To specify a device by name use -n <devicename> " < < endl ;
return 1 ;
}
2018-02-17 12:50:13 +00:00
if ( parser . isSet ( QStringLiteral ( " share " ) ) ) {
2018-11-28 18:56:22 +00:00
QStringList urls ;
2019-09-10 17:08:53 +01:00
2018-09-28 14:12:17 +01:00
QUrl url = QUrl : : fromUserInput ( parser . value ( QStringLiteral ( " share " ) ) , QDir : : currentPath ( ) ) ;
2018-11-28 18:56:22 +00:00
urls . append ( url . toString ( ) ) ;
2018-02-17 12:50:13 +00:00
2018-09-28 14:12:57 +01:00
// Check for more arguments
2018-11-25 22:58:24 +00:00
const auto args = parser . positionalArguments ( ) ;
for ( const QString & input : args ) {
2018-09-28 14:12:57 +01:00
QUrl url = QUrl : : fromUserInput ( input , QDir : : currentPath ( ) ) ;
2018-11-28 18:56:22 +00:00
urls . append ( url . toString ( ) ) ;
2018-02-17 12:50:13 +00:00
}
2019-06-10 15:40:28 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /share " ) ,
2018-11-28 18:56:22 +00:00
QStringLiteral ( " org.kde.kdeconnect.device.share " ) , QStringLiteral ( " shareUrls " ) ) ;
2019-09-10 17:08:53 +01:00
2018-11-28 18:56:22 +00:00
msg . setArguments ( QVariantList ( ) < < QVariant ( urls ) ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2019-09-10 17:08:53 +01:00
2018-11-28 18:56:22 +00:00
for ( const QString & url : qAsConst ( urls ) ) {
QTextStream ( stdout ) < < i18n ( " Shared %1 " , url ) < < endl ;
2015-02-25 06:13:30 +00:00
}
2018-11-29 20:20:56 +00:00
} else if ( parser . isSet ( QStringLiteral ( " share-text " ) ) ) {
2019-06-10 15:40:28 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /share " ) , QStringLiteral ( " org.kde.kdeconnect.device.share " ) , QStringLiteral ( " shareText " ) ) ;
2018-11-29 20:20:56 +00:00
msg . setArguments ( QVariantList ( ) < < parser . value ( QStringLiteral ( " share-text " ) ) ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2018-11-29 20:20:56 +00:00
QTextStream ( stdout ) < < i18n ( " Shared text: %1 " , parser . value ( QStringLiteral ( " share-text " ) ) ) < < endl ;
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " pair " ) ) ) {
2014-06-23 17:15:10 +01:00
DeviceDbusInterface dev ( device ) ;
2015-09-12 21:07:05 +01:00
if ( ! dev . isReachable ( ) ) {
//Device doesn't exist, go into discovery mode and wait up to 30 seconds for the device to appear
QEventLoop wait ;
QTextStream ( stderr ) < < i18n ( " waiting for device... " ) < < endl ;
2017-01-19 19:01:11 +00:00
blockOnReply ( iface . acquireDiscoveryMode ( id ) ) ;
2015-09-12 21:07:05 +01:00
2019-02-28 13:18:47 +00:00
QObject : : connect ( & iface , & DaemonDbusInterface : : deviceAdded , & iface , [ & ] ( const QString & deviceAddedId ) {
2015-09-12 21:07:05 +01:00
if ( device = = deviceAddedId ) {
wait . quit ( ) ;
}
} ) ;
QTimer : : singleShot ( 30 * 1000 , & wait , & QEventLoop : : quit ) ;
wait . exec ( ) ;
}
if ( ! dev . isReachable ( ) ) {
QTextStream ( stderr ) < < i18n ( " Device not found " ) < < endl ;
2017-01-19 19:01:11 +00:00
} else if ( blockOnReply < bool > ( dev . isTrusted ( ) ) ) {
2015-02-25 06:13:30 +00:00
QTextStream ( stderr ) < < i18n ( " Already paired " ) < < endl ;
2015-09-12 21:07:05 +01:00
} else {
QTextStream ( stderr ) < < i18n ( " Pair requested " ) < < endl ;
2017-01-19 19:01:11 +00:00
blockOnReply ( dev . requestPair ( ) ) ;
2014-06-23 17:15:10 +01:00
}
2017-01-19 19:01:11 +00:00
blockOnReply ( iface . releaseDiscoveryMode ( id ) ) ;
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " unpair " ) ) ) {
2014-06-23 17:15:10 +01:00
DeviceDbusInterface dev ( device ) ;
2018-09-24 14:58:48 +01:00
if ( ! dev . isTrusted ( ) ) {
2015-02-25 06:13:30 +00:00
QTextStream ( stderr ) < < i18n ( " Already not paired " ) < < endl ;
2015-09-12 21:07:05 +01:00
} else {
QTextStream ( stderr ) < < i18n ( " Unpaired " ) < < endl ;
2017-01-19 19:01:11 +00:00
blockOnReply ( dev . unpair ( ) ) ;
2014-06-23 17:15:10 +01:00
}
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " ping " ) ) | | parser . isSet ( QStringLiteral ( " ping-msg " ) ) ) {
2019-06-10 15:40:28 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /ping " ) , QStringLiteral ( " org.kde.kdeconnect.device.ping " ) , QStringLiteral ( " sendPing " ) ) ;
2016-11-26 14:38:08 +00:00
if ( parser . isSet ( QStringLiteral ( " ping-msg " ) ) ) {
QString message = parser . value ( QStringLiteral ( " ping-msg " ) ) ;
2014-09-27 04:39:38 +01:00
msg . setArguments ( QVariantList ( ) < < message ) ;
}
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " send-sms " ) ) ) {
if ( parser . isSet ( QStringLiteral ( " destination " ) ) ) {
2019-06-10 15:40:28 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /sms " ) , QStringLiteral ( " org.kde.kdeconnect.device.sms " ) , QStringLiteral ( " sendSms " ) ) ;
msg . setArguments ( { parser . value ( QStringLiteral ( " destination " ) ) , parser . value ( QStringLiteral ( " send-sms " ) ) } ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2016-11-03 18:15:23 +00:00
} else {
QTextStream ( stderr ) < < i18n ( " error: should specify the SMS's recipient by passing --destination <phone number> " ) ;
return 1 ;
}
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " ring " ) ) ) {
2019-06-10 15:40:28 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /findmyphone " ) , QStringLiteral ( " org.kde.kdeconnect.device.findmyphone " ) , QStringLiteral ( " ring " ) ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2019-03-04 22:53:30 +00:00
} else if ( parser . isSet ( QStringLiteral ( " photo " ) ) ) {
2019-09-13 17:11:16 +01:00
const QString fileName = parser . value ( QStringLiteral ( " photo " ) ) ;
if ( ! fileName . isEmpty ( ) ) {
2019-09-10 17:08:53 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /photo " ) , QStringLiteral ( " org.kde.kdeconnect.device.photo " ) , QStringLiteral ( " requestPhoto " ) ) ;
msg . setArguments ( { fileName } ) ;
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
} else {
2019-09-13 17:11:16 +01:00
QTextStream ( stderr ) < < i18n ( " Please specify a filename for the photo " ) < < endl ;
2019-09-10 17:08:53 +01:00
}
2019-06-10 15:40:28 +01:00
} else if ( parser . isSet ( QStringLiteral ( " send-keys " ) ) ) {
QString seq = parser . value ( QStringLiteral ( " send-keys " ) ) ;
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) , QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /remotekeyboard " ) , QStringLiteral ( " org.kde.kdeconnect.device.remotekeyboard " ) , QStringLiteral ( " sendKeyPress " ) ) ;
2017-01-23 08:08:27 +00:00
if ( seq . trimmed ( ) = = QLatin1String ( " - " ) ) {
2018-06-08 19:23:18 +01:00
// from stdin
2017-01-23 08:08:27 +00:00
QFile in ;
if ( in . open ( stdin , QIODevice : : ReadOnly | QIODevice : : Unbuffered ) ) {
while ( ! in . atEnd ( ) ) {
QByteArray line = in . readLine ( ) ; // sanitize to ASCII-codes > 31?
2019-06-10 15:40:28 +01:00
msg . setArguments ( { QString : : fromLatin1 ( line ) , - 1 , false , false , false } ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2017-01-23 08:08:27 +00:00
}
in . close ( ) ;
}
} else {
msg . setArguments ( { seq , - 1 , false , false , false } ) ;
2019-08-14 16:36:19 +01:00
blockOnReply ( DBusHelper : : sessionBus ( ) . asyncCall ( msg ) ) ;
2017-01-23 08:08:27 +00:00
}
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " 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 ) ;
2014-11-10 01:14:53 +00:00
QTextStream ( stdout ) < < " - " < < idx . data ( NotificationsModel : : AppNameModelRole ) . toString ( )
2015-02-25 06:13:30 +00:00
< < " : " < < idx . data ( NotificationsModel : : NameModelRole ) . toString ( ) < < endl ;
2014-07-01 00:15:23 +01:00
}
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " list-commands " ) ) ) {
2016-06-12 19:15:40 +01:00
RemoteCommandsDbusInterface iface ( device ) ;
const auto cmds = QJsonDocument : : fromJson ( iface . commands ( ) ) . object ( ) ;
for ( auto it = cmds . constBegin ( ) , itEnd = cmds . constEnd ( ) ; it ! = itEnd ; + + it ) {
const QJsonObject cont = it - > toObject ( ) ;
2016-11-26 14:38:08 +00:00
QTextStream ( stdout ) < < it . key ( ) < < " : " < < cont . value ( QStringLiteral ( " name " ) ) . toString ( ) < < " : " < < cont . value ( QStringLiteral ( " command " ) ) . toString ( ) < < endl ;
2016-06-12 19:15:40 +01:00
}
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " execute-command " ) ) ) {
2016-06-12 19:15:40 +01:00
RemoteCommandsDbusInterface iface ( device ) ;
2017-01-19 19:01:11 +00:00
blockOnReply ( iface . triggerCommand ( parser . value ( QStringLiteral ( " execute-command " ) ) ) ) ;
2016-11-26 14:38:08 +00:00
} else if ( parser . isSet ( QStringLiteral ( " encryption-info " ) ) ) {
2015-09-11 16:25:23 +01:00
DeviceDbusInterface dev ( device ) ;
2017-01-19 19:01:11 +00:00
QString info = blockOnReply < QString > ( dev . encryptionInfo ( ) ) ; // QSsl::Der = 1
QTextStream ( stdout ) < < info < < endl ;
2014-07-01 00:15:23 +01:00
} else {
2015-02-25 06:13:30 +00:00
QTextStream ( stderr ) < < i18n ( " Nothing to be done " ) < < endl ;
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 ( ) ;
}