2 * This file is part of Jenirok.
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.
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.
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/>.
20 #include <glib-object.h>
21 #include <dbus/dbus-glib-lowlevel.h>
22 #include "connectionmanager.h"
24 ConnectionManager* ConnectionManager::instance_ = 0;
26 ConnectionManager::ConnectionManager(): connection_(0), connected_(false)
30 dbus_error_init(&err);
31 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
35 dbus_connection_setup_with_g_main(conn, NULL);
39 qDebug() << "Unable to connect to DBUS: " << err.message;
40 dbus_error_free(&err);
43 connection_ = con_ic_connection_new();
45 g_signal_connect(G_OBJECT(connection_), "connection-event", G_CALLBACK(connectionHandler), NULL);
50 ConnectionManager& ConnectionManager::instance()
54 instance_ = new ConnectionManager;
60 bool ConnectionManager::connect()
62 return con_ic_connection_connect(connection_, CON_IC_CONNECT_FLAG_NONE);
65 bool ConnectionManager::disconnect()
67 return con_ic_connection_disconnect(connection_);
70 bool ConnectionManager::isConnected()
74 void ConnectionManager::connectionHandler(ConIcConnection *connection,
75 ConIcConnectionEvent *event,
81 ConIcConnectionStatus status = con_ic_connection_event_get_status(event);
86 case CON_IC_STATUS_CONNECTED:
87 qDebug() << "Connected";
88 instance_->emit connected();
89 instance_->connected_ = true;
92 case CON_IC_STATUS_DISCONNECTING:
93 case CON_IC_STATUS_NETWORK_UP:
96 case CON_IC_STATUS_DISCONNECTED:
97 ConIcConnectionError err = con_ic_connection_event_get_error(event);
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;
107 case CON_IC_CONNECTION_ERROR_INVALID_IAP:
108 qDebug() << "Invalid IAP";
109 instance_->emit error("Invalid IAP");
112 case CON_IC_CONNECTION_ERROR_CONNECTION_FAILED:
113 qDebug() << "Connection failed";
114 instance_->emit error("Connection failed");