Merge https://vcs.maemo.org/git/movie-schedule
[movie-schedule] / src / utils / connectivitymanager.cpp
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // MovieSchedule is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "connectivitymanager.h"
19
20 #include <QDBusMessage>
21 #include <QDBusInterface>
22 #ifdef MAEMO_SDK
23 #include <icd/dbus_api.h>
24 #endif
25
26 ConnectivityManager::ConnectivityManager(QObject *parent)
27     : QObject(parent),
28     _dbus_connection(QDBusConnection::systemBus()),
29 #ifdef MAEMO_SDK
30     _interface(new QDBusInterface(ICD_DBUS_API_INTERFACE,
31                                   ICD_DBUS_API_PATH,
32                                   ICD_DBUS_API_INTERFACE,
33                                   _dbus_connection))
34 #else
35     _interface(0)
36 #endif
37 {
38 #ifdef MAEMO_SDK
39     _dbus_connection.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
40                              ICD_DBUS_API_STATISTICS_SIG, this, SLOT(StatisticsSentResult(const QDBusMessage&)));
41     _dbus_connection.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
42                              ICD_DBUS_API_STATE_SIG, this, SLOT(ChangeState(const QDBusMessage&)));
43 #endif
44 }
45
46 ConnectivityManager::~ConnectivityManager()
47 {
48 #ifdef MAEMO_SDK
49     _dbus_connection.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
50                                 ICD_DBUS_API_STATE_SIG, this, SLOT(ChangeState(const QDBusMessage&)));
51     _dbus_connection.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
52                                 ICD_DBUS_API_STATISTICS_SIG, this, SLOT(StatisticsSentResult(const QDBusMessage&)));
53 #endif
54 }
55
56 void ConnectivityManager::Connect()
57 {
58 #ifdef MAEMO_SDK
59     _interface->call(ICD_DBUS_API_CONNECT_REQ,
60                      QVariant((unsigned int) ICD_CONNECTION_FLAG_USER_EVENT));
61 #endif
62     UpdateConnectionState();
63 }
64
65 void ConnectivityManager::Disconnect()
66 {
67 #ifdef MAEMO_SDK
68     _interface->call(ICD_DBUS_API_DISCONNECT_REQ,
69                      QVariant((unsigned int) ICD_CONNECTION_FLAG_USER_EVENT));
70 #endif
71 }
72
73 void ConnectivityManager::UpdateConnectionState()
74 {
75 #ifdef MAEMO_SDK
76     _interface->call(ICD_DBUS_API_STATE_REQ);
77 #endif
78     emit Connected();
79 }
80
81 void ConnectivityManager::StatisticsSentResult(const QDBusMessage &rep)
82 {
83     Q_UNUSED(rep);
84 }
85
86 void ConnectivityManager::ChangeState(const QDBusMessage &rep)
87 {
88 #ifdef MAEMO_SDK
89     QString connection_error = rep.arguments().value(6).value<QString>();
90
91     if (!connection_error.isEmpty()) {
92         emit Error();
93         return;
94     }
95
96     unsigned int connection_status = rep.arguments().value(7).value<unsigned int>();
97
98     switch (connection_status) {
99     case ICD_STATE_DISCONNECTED:
100         emit Disconnected();
101         break;
102     case ICD_STATE_CONNECTING:
103         break;
104     case ICD_STATE_CONNECTED:
105         emit Connected();
106         break;
107     case ICD_STATE_DISCONNECTING:
108         break;
109     case ICD_STATE_LIMITED_CONN_ENABLED:
110         break;
111     case ICD_STATE_LIMITED_CONN_DISABLED:
112         break;
113     case ICD_STATE_SEARCH_START:
114         break;
115     case ICD_STATE_SEARCH_STOP:
116         break;
117     case ICD_STATE_INTERNAL_ADDRESS_ACQUIRED:
118         break;
119     }
120 #else
121     Q_UNUSED(rep);
122 #endif
123 }