2014-04-01 02:12:31 +01:00
/*
2020-08-17 10:48:10 +01:00
* SPDX - FileCopyrightText : 2015 Aleix Pol Gonzalez < aleixpol @ kde . org >
2014-04-01 02:12:31 +01:00
*
2020-08-17 10:48:10 +01:00
* SPDX - License - Identifier : GPL - 2.0 - only OR GPL - 3.0 - only OR LicenseRef - KDE - Accepted - GPL
2014-04-01 02:12:31 +01:00
*/
2022-09-10 22:23:52 +01:00
# include <QCoreApplication>
2015-07-19 15:55:28 +01:00
# include <QCryptographicHash>
2014-06-14 14:22:40 +01:00
# include <QDBusMessage>
2017-01-23 08:08:27 +00:00
# include <QFile>
2022-09-10 22:23:52 +01:00
# include <QIODevice>
# include <QTextStream>
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
2022-09-10 22:23:52 +01:00
# include "interfaces/conversationmessage.h"
# include "interfaces/dbushelpers.h"
# include "interfaces/dbusinterfaces.h"
2015-03-10 04:59:36 +00:00
# include "interfaces/devicesmodel.h"
# include "interfaces/notificationsmodel.h"
# include "kdeconnect-version.h"
2015-02-05 07:52:07 +00:00
2019-06-09 16:28:49 +01:00
# include <dbushelper.h>
2022-09-10 22:23:52 +01:00
int main ( int argc , char * * argv )
2014-04-01 02:12:31 +01:00
{
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
2022-09-10 22:23:52 +01: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 " ) ) ) ;
2022-09-10 22:23:52 +01:00
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 " ) ) ) ;
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 " ) ) ) ;
2023-06-06 20:39:01 +01:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " send-clipboard " ) , i18n ( " Sends the current clipboard to said device " ) ) ) ;
2020-12-02 14:43:09 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " share " ) , i18n ( " Share a file/URL to a said device " ) , QStringLiteral ( " path or URL " ) ) ) ;
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 " ) ) ) ;
2020-11-14 01:08:04 +00:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " unlock " ) , i18n ( " Unlock the specified device " ) ) ) ;
2016-11-26 14:38:08 +00:00
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 " ) ) ) ;
2022-09-10 22:23:52 +01:00
parser . addOption ( QCommandLineOption ( QStringLiteral ( " attachment " ) ,
i18n ( " File urls to send attachments with the message (can be passed multiple times) " ) ,
i18n ( " file urls " ) ) ) ;
2016-11-26 14:38:08 +00:00
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 " ) ) ) ;
2022-09-10 22:23:52 +01: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
2022-09-10 22:23:52 +01:00
// Hidden because it's an implementation detail
2019-03-13 19:58:28 +00:00
QCommandLineOption deviceAutocomplete ( QStringLiteral ( " shell-device-autocompletion " ) ) ;
2019-07-19 21:11:49 +01:00
deviceAutocomplete . setFlags ( QCommandLineOption : : HiddenFromHelp ) ;
2022-09-10 22:23:52 +01:00
deviceAutocomplete . setDescription (
QStringLiteral ( " Outputs all available devices id's with their name and paired status " ) ) ; // Not visible, so no translation needed
2019-06-10 15:40:28 +01:00
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 . 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 " ) ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < iface . selfId ( ) < < Qt : : endl ;
2017-02-24 21:37:00 +00:00
} else if ( parser . isSet ( QStringLiteral ( " l " ) ) | | parser . isSet ( QStringLiteral ( " a " ) ) ) {
2022-07-06 11:50:31 +01:00
bool available = false ;
2016-11-26 14:38:08 +00:00
if ( parser . isSet ( QStringLiteral ( " a " ) ) ) {
2022-07-06 11:50:31 +01:00
available = true ;
2015-09-09 22:22:59 +01:00
} else {
QThread : : sleep ( 2 ) ;
2015-02-25 06:13:30 +00:00
}
2022-07-06 11:50:31 +01:00
const QStringList devices = blockOnReply < QStringList > ( iface . devices ( available , available ) ) ;
2015-03-16 01:58:22 +00:00
2018-02-26 18:39:34 +00:00
bool displayCount = true ;
2022-09-10 22:23:52 +01:00
for ( const QString & id : devices ) {
2016-11-26 14:38:08 +00:00
if ( parser . isSet ( QStringLiteral ( " id-only " ) ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < id < < Qt : : endl ;
2018-02-26 18:39:34 +00:00
displayCount = false ;
} else if ( parser . isSet ( QStringLiteral ( " name-only " ) ) ) {
DeviceDbusInterface deviceIface ( id ) ;
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < deviceIface . name ( ) < < Qt : : endl ;
2018-02-26 18:39:34 +00:00
displayCount = false ;
} else if ( parser . isSet ( QStringLiteral ( " id-name-only " ) ) ) {
DeviceDbusInterface deviceIface ( id ) ;
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < id < < ' ' < < deviceIface . name ( ) < < Qt : : endl ;
2018-02-26 18:39:34 +00:00
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 ( ) ;
2023-06-01 01:25:37 +01:00
const bool isPaired = deviceIface . isPaired ( ) ;
if ( isReachable & & isPaired ) {
2015-03-16 01:58:22 +00:00
statusInfo = i18n ( " (paired and reachable) " ) ;
} else if ( isReachable ) {
statusInfo = i18n ( " (reachable) " ) ;
2023-06-01 01:25:37 +01:00
} else if ( isPaired ) {
2015-03-16 01:58:22 +00:00
statusInfo = i18n ( " (paired) " ) ;
2016-11-23 16:20:59 +00:00
}
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < " - " < < deviceIface . name ( ) < < " : " < < deviceIface . id ( ) < < ' ' < < statusInfo < < Qt : : 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 ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18np ( " 1 device found " , " %1 devices found " , devices . size ( ) ) < < Qt : : endl ;
2015-03-16 01:58:22 +00:00
} else if ( devices . isEmpty ( ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " No devices found " ) < < Qt : : endl ;
2015-02-25 06:13:30 +00:00
}
2015-09-09 22:22:59 +01:00
2019-03-13 19:58:28 +00:00
} else if ( parser . isSet ( QStringLiteral ( " shell-device-autocompletion " ) ) ) {
2022-09-10 22:23:52 +01:00
// Outputs a list of reachable devices in zsh autocomplete format, with the name as description
2019-03-13 19:58:28 +00:00
const QStringList devices = blockOnReply < QStringList > ( iface . devices ( true , false ) ) ;
for ( const QString & id : devices ) {
DeviceDbusInterface deviceIface ( id ) ;
QString statusInfo ;
2023-06-01 01:25:37 +01:00
const bool isPaired = deviceIface . isPaired ( ) ;
if ( isPaired ) {
2019-03-13 19:58:28 +00:00
statusInfo = i18n ( " (paired) " ) ;
} else {
statusInfo = i18n ( " (unpaired) " ) ;
}
2022-09-10 22:23:52 +01:00
// Description: "device name (paired/unpaired)"
2019-06-10 15:40:28 +01:00
QString description = deviceIface . name ( ) + QLatin1Char ( ' ' ) + statusInfo ;
2022-09-10 22:23:52 +01:00
// Replace characters
2019-06-10 15:40:28 +01:00
description . replace ( QLatin1Char ( ' \\ ' ) , QStringLiteral ( " \\ \\ " ) ) ;
2022-09-10 22:23:52 +01:00
description . replace ( QLatin1Char ( ' [ ' ) , QStringLiteral ( " \\ [ " ) ) ;
description . replace ( QLatin1Char ( ' ] ' ) , QStringLiteral ( " \\ ] " ) ) ;
2019-06-10 15:40:28 +01:00
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
2022-09-10 22:23:52 +01:00
// Output id and description
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < id < < ' [ ' < < description < < ' ] ' < < Qt : : endl ;
2019-03-13 19:58:28 +00:00
}
2019-09-11 20:19:42 +01:00
2022-09-10 22:23:52 +01:00
// Exit with 1 if we didn't find a device
2020-01-07 20:49:03 +00:00
return int ( devices . isEmpty ( ) ) ;
2022-09-10 22:23:52 +01: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 " ) ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2014-04-01 02:12:31 +01:00
} else {
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 ( ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < " Couldn't find device: " < < parser . value ( QStringLiteral ( " name " ) ) < < Qt : : endl ;
2016-06-05 22:31:13 +01:00
return 1 ;
}
}
2022-09-10 22:23:52 +01:00
if ( device . isEmpty ( ) ) {
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 " )
2022-10-17 22:17:49 +01:00
< < Qt : : endl ;
2019-12-21 23:37:29 +00:00
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 ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < " Couldn't find device with id \" " < < device < < " \" . To specify a device by name use -n <devicename> " < < Qt : : endl ;
2019-09-10 17:09:06 +01:00
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 ( ) ;
2022-09-10 22:23:52 +01:00
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
}
2022-09-10 22:23:52 +01:00
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) ,
QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /share " ) ,
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 ) ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2019-09-10 17:08:53 +01:00
2022-09-10 22:23:52 +01:00
for ( const QString & url : qAsConst ( urls ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < i18n ( " Shared %1 " , url ) < < Qt : : endl ;
2015-02-25 06:13:30 +00:00
}
2018-11-29 20:20:56 +00:00
} else if ( parser . isSet ( QStringLiteral ( " share-text " ) ) ) {
2022-09-10 22:23:52 +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 " ) ) ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < i18n ( " Shared text: %1 " , parser . value ( QStringLiteral ( " share-text " ) ) ) < < Qt : : endl ;
2020-11-14 01:08:04 +00:00
} else if ( parser . isSet ( QStringLiteral ( " lock " ) ) | | parser . isSet ( QStringLiteral ( " unlock " ) ) ) {
LockDeviceDbusInterface iface ( device ) ;
iface . setLocked ( parser . isSet ( QStringLiteral ( " lock " ) ) ) ;
DeviceDbusInterface deviceIface ( device ) ;
if ( parser . isSet ( QStringLiteral ( " lock " ) ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < i18nc ( " device has requested to lock peer device " , " Requested to lock %1. " , deviceIface . name ( ) ) < < Qt : : endl ;
2020-11-14 01:08:04 +00:00
} else {
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < i18nc ( " device has requested to unlock peer device " , " Requested to unlock %1. " , deviceIface . name ( ) ) < < Qt : : endl ;
2020-11-14 01:08:04 +00:00
}
2022-09-10 22:23:52 +01: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 ( ) ) {
2022-09-10 22:23:52 +01:00
// Device doesn't exist, go into discovery mode and wait up to 30 seconds for the device to appear
2015-09-12 21:07:05 +01:00
QEventLoop wait ;
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " waiting for device... " ) < < Qt : : endl ;
2015-09-12 21:07:05 +01:00
2022-09-10 22:23:52 +01: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 ( ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Device not found " ) < < Qt : : endl ;
2023-06-01 01:25:37 +01:00
} else if ( blockOnReply < bool > ( dev . isPaired ( ) ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Already paired " ) < < Qt : : endl ;
2015-09-12 21:07:05 +01:00
} else {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Pair requested " ) < < Qt : : endl ;
2023-06-01 01:25:37 +01:00
blockOnReply ( dev . requestPairing ( ) ) ;
2014-06-23 17:15:10 +01:00
}
2022-09-10 22:23:52 +01:00
} else if ( parser . isSet ( QStringLiteral ( " unpair " ) ) ) {
2014-06-23 17:15:10 +01:00
DeviceDbusInterface dev ( device ) ;
2023-06-01 01:25:37 +01:00
if ( ! dev . isPaired ( ) ) {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Already not paired " ) < < Qt : : endl ;
2015-09-12 21:07:05 +01:00
} else {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Unpaired " ) < < Qt : : endl ;
2017-01-19 19:01:11 +00:00
blockOnReply ( dev . unpair ( ) ) ;
2014-06-23 17:15:10 +01:00
}
2023-06-06 20:39:01 +01:00
} else if ( parser . isSet ( QStringLiteral ( " send-clipboard " ) ) ) {
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) ,
QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /clipboard " ) ,
QStringLiteral ( " org.kde.kdeconnect.device.clipboard " ) ,
QStringLiteral ( " sendClipboard " ) ) ;
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2022-09-10 22:23:52 +01:00
} else if ( parser . isSet ( QStringLiteral ( " ping " ) ) | | parser . isSet ( QStringLiteral ( " ping-msg " ) ) ) {
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 ) ;
}
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2022-09-10 22:23:52 +01:00
} else if ( parser . isSet ( QStringLiteral ( " send-sms " ) ) ) {
2016-11-26 14:38:08 +00:00
if ( parser . isSet ( QStringLiteral ( " destination " ) ) ) {
2020-08-28 08:15:49 +01:00
qDBusRegisterMetaType < ConversationAddress > ( ) ;
QVariantList addresses ;
const QStringList addressList = parser . value ( QStringLiteral ( " destination " ) ) . split ( QRegularExpression ( QStringLiteral ( " \\ s+ " ) ) ) ;
2022-09-10 22:23:52 +01:00
for ( const QString & input : addressList ) {
2020-08-28 08:15:49 +01:00
ConversationAddress address ( input ) ;
addresses < < QVariant : : fromValue ( address ) ;
}
2020-10-09 02:39:31 +01:00
const QString message = parser . value ( QStringLiteral ( " send-sms " ) ) ;
2020-08-31 11:05:25 +01:00
2020-10-31 11:21:53 +00:00
const QStringList rawAttachmentUrlsList = parser . values ( QStringLiteral ( " attachment " ) ) ;
2020-10-09 02:39:31 +01:00
QVariantList attachments ;
2022-09-10 22:23:52 +01:00
for ( const QString & attachmentUrl : rawAttachmentUrlsList ) {
2020-10-09 02:39:31 +01:00
// TODO: Construct attachment objects from the list of Urls
Q_UNUSED ( attachmentUrl ) ;
}
DeviceConversationsDbusInterface conversationDbusInterface ( device ) ;
auto reply = conversationDbusInterface . sendWithoutConversation ( addresses , message , attachments ) ;
reply . waitForFinished ( ) ;
2016-11-03 18:15:23 +00:00
} else {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " error: should specify the SMS's recipient by passing --destination <phone number> " ) < < Qt : : endl ;
2016-11-03 18:15:23 +00:00
return 1 ;
}
2022-09-10 22:23:52 +01:00
} else if ( parser . isSet ( QStringLiteral ( " ring " ) ) ) {
QDBusMessage msg = QDBusMessage : : createMethodCall ( QStringLiteral ( " org.kde.kdeconnect " ) ,
QStringLiteral ( " /modules/kdeconnect/devices/ " ) + device + QStringLiteral ( " /findmyphone " ) ,
QStringLiteral ( " org.kde.kdeconnect.device.findmyphone " ) ,
QStringLiteral ( " ring " ) ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2022-09-10 22:23:52 +01: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 ( ) ) {
2022-09-10 22:23:52 +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 " ) ) ;
2022-09-02 18:32:01 +01:00
msg . setArguments ( { QUrl : : fromLocalFile ( fileName ) . toString ( ) } ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2019-09-10 17:08:53 +01:00
} else {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Please specify a filename for the photo " ) < < Qt : : endl ;
2019-09-10 17:08:53 +01:00
}
2022-09-10 22:23:52 +01:00
} else if ( parser . isSet ( QStringLiteral ( " send-keys " ) ) ) {
2019-06-10 15:40:28 +01:00
QString seq = parser . value ( QStringLiteral ( " send-keys " ) ) ;
2022-09-10 22:23:52 +01:00
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 ;
2022-09-10 22:23:52 +01:00
if ( in . open ( stdin , QIODevice : : ReadOnly | QIODevice : : Unbuffered ) ) {
2017-01-23 08:08:27 +00:00
while ( ! in . atEnd ( ) ) {
2022-09-10 22:23:52 +01:00
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 } ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2017-01-23 08:08:27 +00:00
}
in . close ( ) ;
}
} else {
msg . setArguments ( { seq , - 1 , false , false , false } ) ;
2022-04-12 06:40:03 +01:00
blockOnReply ( QDBusConnection : : sessionBus ( ) . asyncCall ( msg ) ) ;
2017-01-23 08:08:27 +00:00
}
2022-09-10 22:23:52 +01:00
} else if ( parser . isSet ( QStringLiteral ( " list-notifications " ) ) ) {
2014-07-01 00:15:23 +01:00
NotificationsModel notifications ;
notifications . setDeviceId ( device ) ;
2022-09-10 22:23:52 +01:00
for ( int i = 0 , rows = notifications . rowCount ( ) ; i < rows ; + + i ) {
2014-07-01 00:15:23 +01:00
QModelIndex idx = notifications . index ( i ) ;
2022-09-10 22:23:52 +01:00
QTextStream ( stdout ) < < " - " < < idx . data ( NotificationsModel : : AppNameModelRole ) . toString ( ) < < " : "
2022-10-17 22:17:49 +01:00
< < idx . data ( NotificationsModel : : NameModelRole ) . toString ( ) < < Qt : : endl ;
2014-07-01 00:15:23 +01:00
}
2022-09-10 22:23:52 +01: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 ( ) ;
2022-09-10 22:23:52 +01:00
for ( auto it = cmds . constBegin ( ) , itEnd = cmds . constEnd ( ) ; it ! = itEnd ; + + it ) {
2016-06-12 19:15:40 +01:00
const QJsonObject cont = it - > toObject ( ) ;
2022-09-10 22:23:52 +01:00
QTextStream ( stdout ) < < it . key ( ) < < " : " < < cont . value ( QStringLiteral ( " name " ) ) . toString ( ) < < " : "
2022-10-17 22:17:49 +01:00
< < cont . value ( QStringLiteral ( " command " ) ) . toString ( ) < < Qt : : endl ;
2016-06-12 19:15:40 +01:00
}
2022-09-10 22:23:52 +01: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 " ) ) ) ) ;
2022-09-10 22:23:52 +01: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
2022-10-17 22:17:49 +01:00
QTextStream ( stdout ) < < info < < Qt : : endl ;
2014-07-01 00:15:23 +01:00
} else {
2022-10-17 22:17:49 +01:00
QTextStream ( stderr ) < < i18n ( " Nothing to be done " ) < < Qt : : 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 ( ) ;
}