Akonadi Contacts

qsflphonedialer.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2023 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 
6 */
7 
8 #include "qsflphonedialer.h"
9 
10 #include <KLocalizedString>
11 
12 #include <QDBusConnection>
13 #include <QDBusConnectionInterface>
14 #include <QProcess>
15 #include <QStandardPaths>
16 
17 #if !defined(Q_OS_WIN)
18 #include <unistd.h>
19 #else
20 #include <windows.h>
21 #endif
22 
23 static bool isSflPhoneServiceRegistered()
24 {
25  const QString service(QStringLiteral("org.sflphone.SFLphone"));
26 
28  if (interface->isServiceRegistered(service)) {
29  return true;
30  }
31 
32  interface = QDBusConnection::sessionBus().interface();
33  if (interface->isServiceRegistered(service)) {
34  return true;
35  }
36  return false;
37 }
38 
39 QSflPhoneDialer::QSflPhoneDialer(const QString &applicationName)
40  : QDialer(applicationName)
41 {
42 }
43 
44 QSflPhoneDialer::~QSflPhoneDialer() = default;
45 
46 bool QSflPhoneDialer::initializeSflPhone()
47 {
48  // first check whether dbus interface is available yet
49  if (!isSflPhoneServiceRegistered()) {
50  // it could be skype is not running yet, so start it now
51  const QString progFullPath = QStandardPaths::findExecutable(QStringLiteral("sflphone-client-kde"));
52  if (progFullPath.isEmpty() || !QProcess::startDetached(QStringLiteral("sflphone-client-kde"), QStringList())) {
53  mErrorMessage = i18n("Unable to start sflphone-client-kde process, check that sflphone-client-kde executable is in your PATH variable.");
54  return false;
55  }
56 
57  const int runs = 100;
58  for (int i = 0; i < runs; ++i) {
59  if (!isSflPhoneServiceRegistered()) {
60 #if !defined(Q_OS_WIN)
61  ::sleep(2);
62 #else
63  Sleep(2000);
64 #endif
65  } else {
66  return true;
67  }
68  }
69  }
70  return true;
71 }
72 
73 bool QSflPhoneDialer::dialNumber(const QString &number)
74 {
75  if (!initializeSflPhone()) {
76  return false;
77  }
78 
79  QStringList arguments;
80  arguments << QStringLiteral("--place-call");
81  arguments << number;
82  const QString progFullPath = QStandardPaths::findExecutable(QStringLiteral("sflphone-client-kde"));
83  if (progFullPath.isEmpty() || !QProcess::startDetached(QStringLiteral("sflphone-client-kde"), arguments)) {
84  return false;
85  }
86 
87  return true;
88 }
89 
90 bool QSflPhoneDialer::sendSms(const QString &number, const QString &text)
91 {
92  if (!initializeSflPhone()) {
93  return false;
94  }
95 
96  QStringList arguments;
97  arguments << QStringLiteral("--send-text");
98  arguments << number;
99  arguments << QStringLiteral("--message");
100  arguments << text;
101  const QString progFullPath = QStandardPaths::findExecutable(QStringLiteral("sflphone-client-kde"));
102  if (progFullPath.isEmpty() || !QProcess::startDetached(QStringLiteral("sflphone-client-kde"), arguments)) {
103  return false;
104  }
105  return true;
106 }
QDBusReply< bool > isServiceRegistered(const QString &serviceName) const const
QString findExecutable(const QString &executableName, const QStringList &paths)
QString i18n(const char *text, const TYPE &arg...)
QDBusConnection sessionBus()
bool isEmpty() const const
QDBusConnectionInterface * interface() const const
bool startDetached(qint64 *pid)
KIOCORE_EXPORT QString number(KIO::filesize_t size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.