Fixed some inconsistencies in code indentation
[jenirok] / src / common / connectionmanager.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QDebug>
20 #include <glib-object.h>
21 #include <dbus/dbus-glib-lowlevel.h>
22 #include "connectionmanager.h"
23
24 ConnectionManager* ConnectionManager::instance_ = 0;
25
26 ConnectionManager::ConnectionManager(): connection_(0), connected_(false)
27 {
28     DBusError err;
29     DBusConnection* conn;
30     dbus_error_init(&err);
31     conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
32
33     if(conn)
34     {
35         dbus_connection_setup_with_g_main(conn, NULL);
36     }
37     else
38     {
39         qDebug() << "Unable to connect to DBUS: " << err.message;
40         dbus_error_free(&err);
41     }
42
43     connection_ = con_ic_connection_new();
44
45     g_signal_connect(G_OBJECT(connection_), "connection-event", G_CALLBACK(connectionHandler), NULL);
46
47
48 }
49
50 ConnectionManager& ConnectionManager::instance()
51 {
52     if(!instance_)
53     {
54         instance_ = new ConnectionManager;
55     }
56
57     return *instance_;
58 }
59
60 bool ConnectionManager::connect()
61 {
62     return con_ic_connection_connect(connection_, CON_IC_CONNECT_FLAG_NONE);
63 }
64
65 bool ConnectionManager::disconnect()
66 {
67     return con_ic_connection_disconnect(connection_);
68 }
69
70 bool ConnectionManager::isConnected()
71 {
72     return connected_;
73 }
74 void ConnectionManager::connectionHandler(ConIcConnection *connection,
75                                           ConIcConnectionEvent *event,
76                                           gpointer user_data)
77 {
78     Q_UNUSED(connection);
79     Q_UNUSED(user_data);
80
81     ConIcConnectionStatus status = con_ic_connection_event_get_status(event);
82
83     switch(status)
84     {
85
86     case CON_IC_STATUS_CONNECTED:
87         qDebug() << "Connected";
88         instance_->emit connected();
89         instance_->connected_ = true;
90         break;
91
92     case CON_IC_STATUS_DISCONNECTING:
93     case CON_IC_STATUS_NETWORK_UP:
94         break;
95
96     case CON_IC_STATUS_DISCONNECTED:
97         ConIcConnectionError err = con_ic_connection_event_get_error(event);
98         switch(err)
99         {
100         case CON_IC_CONNECTION_ERROR_NONE:
101         case CON_IC_CONNECTION_ERROR_USER_CANCELED:
102             qDebug() << "Disconnected";
103             instance_->emit disconnected();
104             instance_->connected_ = false;
105             break;
106
107         case CON_IC_CONNECTION_ERROR_INVALID_IAP:
108             qDebug() << "Invalid IAP";
109             instance_->emit error("Invalid IAP");
110             break;
111
112         case CON_IC_CONNECTION_ERROR_CONNECTION_FAILED:
113             qDebug() << "Connection failed";
114             instance_->emit error("Connection failed");
115             break;
116
117         default:
118             break;
119         }
120         break;
121     }
122 }