-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathskype-dbus.cpp
206 lines (166 loc) · 5.96 KB
/
skype-dbus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Skype Call Recorder
Copyright 2008-2010, 2013, 2015 by jlh (jlh at gmx dot ch)
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, version 3 of
the License, or (at your option) any later version.
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, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The GNU General Public License version 2 is included with the source of
this program under the file name COPYING. You can also get a copy on
http://www.fsf.org/
*/
#include <QList>
#include <QVariant>
#include <QTimer>
#include <QMessageBox>
#include <QtDBus>
#include "skype-dbus.h"
#include "common.h"
namespace {
const QString skypeServiceName("com.Skype.API");
const QString skypeInterfaceName("com.Skype.API");
}
SkypeDBus::SkypeDBus(QObject *parent) : Skype(parent), dbus("SkypeRecorder") {
timer = new QTimer(this);
timer->setInterval(5000);
connect(timer, SIGNAL(timeout()), this, SLOT(poll()));
dbus = QDBusConnection::connectToBus(QDBusConnection::SessionBus, "SkypeRecorder");
if (!dbus.isConnected()) {
debug("Error: Cannot connect to DBus");
QMessageBox::critical(NULL, PROGRAM_NAME " - Error",
QString("The connection to DBus failed! This is a fatal error."));
return;
}
// export our object
exported = new SkypeExport(this);
if (!dbus.registerObject("/com/Skype/Client", this)) {
debug("Error: Cannot register object /com/Skype/Client");
QMessageBox::critical(NULL, PROGRAM_NAME " - Error",
QString("Cannot register object on DBus! This is a fatal error."));
return;
}
connect(dbus.interface(), SIGNAL(serviceOwnerChanged(const QString &, const QString &, const QString &)),
this, SLOT(serviceOwnerChanged(const QString &, const QString &, const QString &)));
QTimer::singleShot(0, this, SLOT(connectToSkype()));
}
void SkypeDBus::connectToSkype() {
if (connectionState)
return;
QDBusReply<bool> exists = dbus.interface()->isServiceRegistered(skypeServiceName);
if (!exists.isValid() || !exists.value()) {
timer->stop();
debug(QString("Service %1 not found on DBus").arg(skypeServiceName));
return;
}
if (!timer->isActive())
timer->start();
sendWithAsyncReply("NAME SkypeCallRecorder");
connectionState = 1;
}
void SkypeDBus::serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner) {
if (name != skypeServiceName)
return;
if (oldOwner.isEmpty()) {
debug(QString("DBUS: Skype API service appeared as %1").arg(newOwner));
if (connectionState != 3)
connectToSkype();
} else if (newOwner.isEmpty()) {
debug("DBUS: Skype API service disappeared");
if (connectionState == 3)
emit connected(false);
timer->stop();
connectionState = 0;
}
}
void SkypeDBus::sendWithAsyncReply(const QString &s) {
debug(QString("SKYPE --> %1 (async reply)").arg(s));
QDBusMessage msg = QDBusMessage::createMethodCall(skypeServiceName, "/com/Skype", skypeInterfaceName, "Invoke");
QList<QVariant> args;
args.append(s);
msg.setArguments(args);
dbus.callWithCallback(msg, this, SLOT(methodCallback(const QDBusMessage &)), SLOT(methodError(const QDBusError &, const QDBusMessage &)), 3600000);
}
QString SkypeDBus::sendWithReply(const QString &s, int timeout) {
debug(QString("SKYPE --> %1 (sync reply)").arg(s));
QDBusMessage msg = QDBusMessage::createMethodCall(skypeServiceName, "/com/Skype", skypeInterfaceName, "Invoke");
QList<QVariant> args;
args.append(s);
msg.setArguments(args);
msg = dbus.call(msg, QDBus::Block, timeout);
if (msg.type() != QDBusMessage::ReplyMessage) {
debug(QString("SKYPE <R- (failed)"));
return QString();
}
QString ret = msg.arguments().value(0).toString();
debug(QString("SKYPE <R- %1").arg(ret));
return ret;
}
void SkypeDBus::send(const QString &s) {
debug(QString("SKYPE --> %1 (no reply)").arg(s));
QDBusMessage msg = QDBusMessage::createMethodCall(skypeServiceName, "/com/Skype", skypeInterfaceName, "Invoke");
QList<QVariant> args;
args.append(s);
msg.setArguments(args);
dbus.call(msg, QDBus::NoBlock);
}
void SkypeDBus::methodCallback(const QDBusMessage &msg) {
if (msg.type() != QDBusMessage::ReplyMessage) {
connectionState = 0;
emit connectionFailed("Cannot communicate with Skype");
return;
}
QString s = msg.arguments().value(0).toString();
debug(QString("SKYPE <R- %1").arg(s));
if (connectionState == 1) {
if (s == "OK") {
connectionState = 2;
sendWithAsyncReply("PROTOCOL 5");
} else if (s == "CONNSTATUS OFFLINE") {
// no user logged in, cannot connect now. from now on,
// we have no way of knowing when the user has logged
// in and we may again try to connect. this is an
// annoying limitation of the Skype API which we work
// around be polling
connectionState = 0;
} else {
connectionState = 0;
emit connectionFailed("Skype denied access");
}
} else if (connectionState == 2) {
if (s == "PROTOCOL 5") {
connectionState = 3;
emit connected(true);
} else {
connectionState = 0;
emit connectionFailed("Skype handshake error");
}
}
}
void SkypeDBus::methodError(const QDBusError &error, const QDBusMessage &) {
connectionState = 0;
emit connectionFailed(error.message());
}
void SkypeDBus::poll() {
if (connectionState == 0) {
connectToSkype();
} else if (connectionState == 3) {
if (sendWithReply("PING", 2000) != "PONG") {
debug("Skype didn't reply with PONG to our PING");
connectionState = 0;
emit connected(false);
}
}
}
// ---- SkypeExport ----
SkypeExport::SkypeExport(SkypeDBus *p) : QDBusAbstractAdaptor(p), parent(p) {
}
void SkypeExport::Notify(const QString &s) {
parent->doNotify(s);
}