Updated packaging for new 0.0.2 release.
[qwerkisync] / EventPreventer.cpp
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18
19 #include "EventPreventer.h"
20
21 #include "CellularRadio.h"
22 #include "Settings.h"
23
24 #include <QDebug>
25
26 #include <QtCore/QCoreApplication>
27 #include <QtDBus/QtDBus>
28
29 const char * TP_ACCOUNT_PHONE("/org/freedesktop/Telepathy/Account/ring/tel/ring");
30 const char * TP_IFACE_ACCOUNT("org.freedesktop.Telepathy.Account");
31 const char * TP_IFACE_ACCOUNT_MANAGER("org.freedesktop.Telepathy.AccountManager");
32 const char * TP_PATH_ACCOUNT_MANAGER("/org/freedesktop/Telepathy/AccountManager");
33 const char * TP_IFACE_DBUS_PROPERTIES("org.freedesktop.DBus.Properties");
34
35 EventPreventer::EventPreventer(const Settings & settings) : m_Settings(settings)
36 {
37         StoreAccountStates();
38         DisableAccounts();
39 }
40
41 EventPreventer::~EventPreventer()
42 {
43         RestoreAccounts();
44 }
45
46 void EventPreventer::StoreAccountStates()
47 {
48         QDBusInterface tpAccountManager(TP_IFACE_ACCOUNT_MANAGER, TP_PATH_ACCOUNT_MANAGER, TP_IFACE_DBUS_PROPERTIES);
49         if (tpAccountManager.isValid())
50         {
51                 // Grab state
52                 QDBusMessage accountList = tpAccountManager.call("Get", TP_IFACE_ACCOUNT_MANAGER, "ValidAccounts");
53                 foreach (QVariant arg, accountList.arguments())
54                 {
55                         const QDBusArgument argument(qvariant_cast<QDBusVariant>(arg).variant().value<QDBusArgument>());
56                         argument.beginArray();
57                         while (!argument.atEnd())
58                         {
59                                 // Grab the account path
60                                 QDBusObjectPath accountPath;
61                                 argument >> accountPath;
62
63                                 if(accountPath.path() == TP_ACCOUNT_PHONE)
64                                 {
65                                         // ...as we're not allowed to disable the telephone account,
66                                         // track the state of the cellular radio. Heavy-handed, but only known way.
67                                         OriginalAccountStates().insert(accountPath.path(), CellularRadio::CurrentState());
68
69                                 }
70                                 else
71                                 {
72                                         // Grab the enabled property of each account
73                                         QDBusInterface tpAccount(TP_IFACE_ACCOUNT_MANAGER, accountPath.path(), TP_IFACE_DBUS_PROPERTIES);
74                                         if (tpAccount.isValid())
75                                         {
76                                                 QDBusReply<QVariant> isEnabledProp = tpAccount.call("Get", TP_IFACE_ACCOUNT, "Enabled");
77
78                                                 bool isEnabled(isEnabledProp.value().toBool());
79                                                 qDebug() << (isEnabled ? "Enabled:\t\t" : "Not enabled:\t") << accountPath.path();
80                                                 OriginalAccountStates().insert(accountPath.path(), isEnabled);
81                                         }
82                                 }
83                         }
84                         argument.endArray();
85                 }
86         }
87 }
88
89 void EventPreventer::DisableAccounts()
90 {
91         SetAccountsToSameState(false);
92 }
93
94 void EventPreventer::EnableAccounts()
95 {
96         SetAccountsToSameState(true);
97 }
98
99 void EventPreventer::SetAccountsToSameState(const bool state)
100 {
101         // Set all stored accounts to requested state
102         foreach (QString accountPath, OriginalAccountStates().keys())
103                 SetAccountToState(accountPath, state);
104 }
105
106 void EventPreventer::RestoreAccounts()
107 {
108         // Set all stored accounts to stored state
109         foreach (QString accountPath, OriginalAccountStates().keys())
110                 SetAccountToState(accountPath, OriginalAccountStates().value(accountPath));
111 }
112
113 void EventPreventer::SetAccountToState(const QString &accountPath, const bool desiredState)
114 {
115         if(accountPath == TP_ACCOUNT_PHONE)
116         {
117                 if(CurrentSettings().DisableCellular())
118                 {
119                         // ...as we're not allowed to disable the telephone account,
120                         // disable the whole cellular radio. Heavy-handed, but only known way.
121                         CellularRadio::SetState(desiredState);
122                 }
123         }
124         else
125         {
126                 // Set each account to offline
127                 QDBusInterface tpAccountManager(TP_IFACE_ACCOUNT_MANAGER, accountPath, TP_IFACE_DBUS_PROPERTIES);
128                 if (tpAccountManager.isValid())
129                 {
130                         QDBusReply<void> isEnabledProp = tpAccountManager.call("Set", TP_IFACE_ACCOUNT, "Enabled",
131                                 QVariant::fromValue(QDBusVariant(QVariant::fromValue(desiredState))));
132
133                         if(!isEnabledProp.isValid())
134                                 qDebug() << isEnabledProp.error().message();
135                 }
136                 else
137                         qDebug() << tpAccountManager.lastError().message();
138         }
139 }
140
141
142