2 * Copyright (C) 2011, Jamie Thompson
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.
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.
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/>.
19 #include "EventPreventer.h"
21 #include "CellularRadio.h"
26 #include <QtCore/QCoreApplication>
27 #include <QtDBus/QtDBus>
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");
35 EventPreventer::EventPreventer(const Settings & settings) : m_Settings(settings)
41 EventPreventer::~EventPreventer()
46 void EventPreventer::StoreAccountStates()
48 QDBusInterface tpAccountManager(TP_IFACE_ACCOUNT_MANAGER, TP_PATH_ACCOUNT_MANAGER, TP_IFACE_DBUS_PROPERTIES);
49 if (tpAccountManager.isValid())
52 QDBusMessage accountList = tpAccountManager.call("Get", TP_IFACE_ACCOUNT_MANAGER, "ValidAccounts");
53 foreach (QVariant arg, accountList.arguments())
55 const QDBusArgument argument(qvariant_cast<QDBusVariant>(arg).variant().value<QDBusArgument>());
56 argument.beginArray();
57 while (!argument.atEnd())
59 // Grab the account path
60 QDBusObjectPath accountPath;
61 argument >> accountPath;
63 if(accountPath.path() == TP_ACCOUNT_PHONE)
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());
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())
76 QDBusReply<QVariant> isEnabledProp = tpAccount.call("Get", TP_IFACE_ACCOUNT, "Enabled");
78 bool isEnabled(isEnabledProp.value().toBool());
79 qDebug() << (isEnabled ? "Enabled:\t\t" : "Not enabled:\t") << accountPath.path();
80 OriginalAccountStates().insert(accountPath.path(), isEnabled);
89 void EventPreventer::DisableAccounts()
91 SetAccountsToSameState(false);
94 void EventPreventer::EnableAccounts()
96 SetAccountsToSameState(true);
99 void EventPreventer::SetAccountsToSameState(const bool state)
101 // Set all stored accounts to requested state
102 foreach (QString accountPath, OriginalAccountStates().keys())
103 SetAccountToState(accountPath, state);
106 void EventPreventer::RestoreAccounts()
108 // Set all stored accounts to stored state
109 foreach (QString accountPath, OriginalAccountStates().keys())
110 SetAccountToState(accountPath, OriginalAccountStates().value(accountPath));
113 void EventPreventer::SetAccountToState(const QString &accountPath, const bool desiredState)
115 if(accountPath == TP_ACCOUNT_PHONE)
117 if(CurrentSettings().DisableCellular())
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);
126 // Set each account to offline
127 QDBusInterface tpAccountManager(TP_IFACE_ACCOUNT_MANAGER, accountPath, TP_IFACE_DBUS_PROPERTIES);
128 if (tpAccountManager.isValid())
130 QDBusReply<void> isEnabledProp = tpAccountManager.call("Set", TP_IFACE_ACCOUNT, "Enabled",
131 QVariant::fromValue(QDBusVariant(QVariant::fromValue(desiredState))));
133 if(!isEnabledProp.isValid())
134 qDebug() << isEnabledProp.error().message();
137 qDebug() << tpAccountManager.lastError().message();