-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofileswitcher.cpp
84 lines (72 loc) · 3.28 KB
/
profileswitcher.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
#include "profileswitcher.h"
#include <QtDBus>
#define PHONE_PROFILE_SILENT "silent"
/*!
The constructor switches the phone profile to silent if activated.
*/
ProfileSwitcher::ProfileSwitcher(const Settings *settings, QObject *parent) :
QObject(parent)
{
// default status is silent, then no switching back at exit will appear
itsInitialProfile = PHONE_PROFILE_SILENT;
// perform profile switching
if (settings->itsSwitchProfile) {
// determine current phone profile
QDBusMessage msg = QDBusMessage::createMethodCall(
"com.nokia.profiled", // --dest
"/com/nokia/profiled", // destination object path
"com.nokia.profiled", // message name (w/o method)
"get_profile" // method
);
QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
if (reply.type() != QDBusMessage::ErrorMessage) {
itsInitialProfile = reply.arguments()[0].toString();
// switch to silent profile
if (itsInitialProfile != PHONE_PROFILE_SILENT) {
QDBusMessage msg = QDBusMessage::createMethodCall(
"com.nokia.profiled", // --dest
"/com/nokia/profiled", // destination object path
"com.nokia.profiled", // message name (w/o method)
"set_profile" // method
);
msg << PHONE_PROFILE_SILENT;
QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
if (reply.type() != QDBusMessage::ErrorMessage) {
qDebug() << "Switched phone profile from" << itsInitialProfile
<< "to" << PHONE_PROFILE_SILENT;
}
else {
qWarning() << "Switching current phone profile failed:" << QDBusConnection::sessionBus().lastError();
// we did not switch profile, therefore we also do not need to switch it back
// this is achieved by faking the initial profile to silent
itsInitialProfile = PHONE_PROFILE_SILENT;
}
}
}
else
qWarning() << "Determining current phone profile failed:" << QDBusConnection::sessionBus().lastError();
}
}
/*!
The destructor restores the initially set phone profile, if applicable.
*/
ProfileSwitcher::~ProfileSwitcher()
{
// switch back to initial profile
if (itsInitialProfile != PHONE_PROFILE_SILENT) {
QDBusMessage msg = QDBusMessage::createMethodCall(
"com.nokia.profiled", // --dest
"/com/nokia/profiled", // destination object path
"com.nokia.profiled", // message name (w/o method)
"set_profile" // method
);
msg << itsInitialProfile;
QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
if (reply.type() != QDBusMessage::ErrorMessage) {
qDebug() << "Switched phone profile back to" << itsInitialProfile;
}
else {
qWarning() << "Switching current phone profile failed:" << QDBusConnection::sessionBus().lastError();
}
}
}