Minor formatting tidying up.
[qwerkisync] / Windows / RestoreAccountStateWindow.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 "RestoreAccountStateWindow.h"
20
21 #include "CellularRadio.h"
22 #include "ModeWindow.h"
23
24 #include <QtDBus>
25 #include <QtGui>
26
27 using namespace Windows;
28
29 RestoreAccountStateWindow::RestoreAccountStateWindow(Settings &settings, QWidget *parent) :
30         BaseWindow(settings, parent)
31 {
32         if(CellularRadio::CurrentState() == false && NumEnabledAccounts() == 0)
33                 CreateContents();
34         else
35                 Advance();
36 }
37
38 void RestoreAccountStateWindow::CreateContents()
39 {
40         switch(CurrentSettings().AppMode())
41         {
42                 case Settings::APPMODE_GUI:
43                 {
44                         QVBoxLayout *layout = new QVBoxLayout();
45                         {
46                                 layout->setMargin(40);
47
48                                 QLabel *lblNotice1 = new QLabel(tr("You have no enabled accounts and the cellular modem is disabled. If this was the result of the last crash of this program, please restore them as you wish."));
49                                 lblNotice1->setAlignment(Qt::AlignCenter);
50                                 lblNotice1->setWordWrap(true);
51                                 layout->addWidget(lblNotice1, 1);
52
53                                 QLabel *lblNotice2 = new QLabel(tr("Once you have enabled things as you want them, tap next to proceed."));
54                                 lblNotice2->setWordWrap(true);
55                                 lblNotice2->setAlignment(Qt::AlignCenter);
56                                 layout->addWidget(lblNotice2, 1);
57
58                                 QHBoxLayout *layoutButtons = new QHBoxLayout();
59                                 {
60                                         QPushButton* btnToggleModem = new QPushButton(tr("Toggle Cellular Modem"));
61                                         QObject::connect(btnToggleModem, SIGNAL(clicked()), this, SLOT(ToggleModem()));
62                                         layoutButtons->addWidget(btnToggleModem);
63                                         layoutButtons->setAlignment(btnToggleModem, Qt::AlignHCenter);
64
65                                         QLabel *lblSpacer = new QLabel();
66                                         layoutButtons->addWidget(lblSpacer);
67                                         layoutButtons->setAlignment(lblSpacer, Qt::AlignHCenter | Qt::AlignVCenter);
68
69                                         QPushButton* btnSetAccountStatus = new QPushButton(tr("Set Account Status"));
70                                         QObject::connect(btnSetAccountStatus, SIGNAL(clicked()), this, SLOT(SetAccountStatus()));
71                                         layoutButtons->addWidget(btnSetAccountStatus);
72                                         layoutButtons->setAlignment(btnSetAccountStatus, Qt::AlignHCenter);
73                                 }
74                                 layout->addLayout(layoutButtons);
75
76                                 QPushButton* btnProceed = new QPushButton(tr("Next"));
77                                 QObject::connect(btnProceed, SIGNAL(clicked()), this, SLOT(Proceed()));
78                                 layout->addWidget(btnProceed);
79                                 layout->setAlignment(btnProceed, Qt::AlignHCenter);
80
81                                 centralWidget()->setLayout(layout);
82                         }
83
84                         break;
85                 }
86
87                 case Settings::APPMODE_CONSOLE:
88                 {
89                         // Process args.
90
91                         Advance();
92                         break;
93                 }
94         }
95 }
96
97 void RestoreAccountStateWindow::ToggleModem()
98 {
99         CellularRadio::Toggle();
100 }
101
102 void RestoreAccountStateWindow::SetAccountStatus()
103 {
104         const char * NOKIA_SERVICE_ACCOUNTS_UI("com.nokia.AccountsUI");
105         const char * NOKIA_PATH_ACCOUNTS_UI("/com/nokia/AccountsUI");
106         const char * NOKIA_IFACE_ACCOUNTS_UI("com.nokia.Accounts");
107
108         QDBusMessage msg(QDBusMessage::createMethodCall(NOKIA_SERVICE_ACCOUNTS_UI, NOKIA_PATH_ACCOUNTS_UI, NOKIA_IFACE_ACCOUNTS_UI, "OpenAccountsList"));
109         msg << (uint)winId();
110         QDBusConnection::sessionBus().call(msg);
111 }
112
113 void RestoreAccountStateWindow::Proceed()
114 {
115         Advance();
116 }
117
118 void RestoreAccountStateWindow::Advance()
119 {
120         QWidget* next = new Windows::ModeWindow(CurrentSettings());
121         next->show();
122         close();
123 }
124
125 const uint RestoreAccountStateWindow::NumEnabledAccounts()
126 {
127         const char * TP_ACCOUNT_PHONE("/org/freedesktop/Telepathy/Account/ring/tel/ring");
128         const char * TP_IFACE_ACCOUNT("org.freedesktop.Telepathy.Account");
129         const char * TP_IFACE_ACCOUNT_MANAGER("org.freedesktop.Telepathy.AccountManager");
130         const char * TP_PATH_ACCOUNT_MANAGER("/org/freedesktop/Telepathy/AccountManager");
131         const char * TP_IFACE_DBUS_PROPERTIES("org.freedesktop.DBus.Properties");
132
133         uint numEnabledAccounts(0);
134
135         QDBusInterface tpAccountManager(TP_IFACE_ACCOUNT_MANAGER, TP_PATH_ACCOUNT_MANAGER, TP_IFACE_DBUS_PROPERTIES);
136         if (tpAccountManager.isValid())
137         {
138                 // Grab state
139                 QDBusMessage accountList = tpAccountManager.call("Get", TP_IFACE_ACCOUNT_MANAGER, "ValidAccounts");
140                 foreach (QVariant arg, accountList.arguments())
141                 {
142                         const QDBusArgument argument(qvariant_cast<QDBusVariant>(arg).variant().value<QDBusArgument>());
143                         argument.beginArray();
144                         while (!argument.atEnd())
145                         {
146                                 // Grab the account path
147                                 QDBusObjectPath accountPath;
148                                 argument >> accountPath;
149
150                                 // Ignore the phone account
151                                 if(accountPath.path() == TP_ACCOUNT_PHONE)
152                                         continue;
153                                 else
154                                 {
155                                         // Grab the enabled property of each account
156                                         QDBusInterface tpAccount(TP_IFACE_ACCOUNT_MANAGER, accountPath.path(), TP_IFACE_DBUS_PROPERTIES);
157                                         if (tpAccount.isValid())
158                                         {
159                                                 QDBusReply<QVariant> isEnabledProp = tpAccount.call("Get", TP_IFACE_ACCOUNT, "Enabled");
160
161                                                 if(isEnabledProp.value().toBool())
162                                                         ++numEnabledAccounts;
163                                         }
164                                 }
165                         }
166                         argument.endArray();
167                 }
168         }
169
170         return numEnabledAccounts;
171 }