First commit
[jenirok] / src / common / connectionmanager.cpp
diff --git a/src/common/connectionmanager.cpp b/src/common/connectionmanager.cpp
new file mode 100644 (file)
index 0000000..2793d83
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * This file is part of Jenirok.
+ *
+ * Jenirok is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Jenirok is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QDebug>
+#include <glib-object.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include "connectionmanager.h"
+
+ConnectionManager* ConnectionManager::instance_ = 0;
+
+ConnectionManager::ConnectionManager(): connection_(0), connected_(false)
+{
+       DBusError err;
+       DBusConnection* conn;
+       dbus_error_init(&err);
+       conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
+
+       if(conn)
+       {
+               dbus_connection_setup_with_g_main(conn, NULL);
+       }
+       else
+       {
+               qDebug() << "Unable to connect to DBUS: " << err.message;
+               dbus_error_free(&err);
+       }
+
+       connection_ = con_ic_connection_new();
+
+       g_signal_connect(G_OBJECT(connection_), "connection-event", G_CALLBACK(connectionHandler), NULL);
+
+
+}
+
+ConnectionManager& ConnectionManager::instance()
+{
+       if(!instance_)
+       {
+               instance_ = new ConnectionManager;
+       }
+
+       return *instance_;
+}
+
+bool ConnectionManager::connect()
+{
+       return con_ic_connection_connect(connection_, CON_IC_CONNECT_FLAG_NONE);
+}
+
+bool ConnectionManager::disconnect()
+{
+       return con_ic_connection_disconnect(connection_);
+}
+
+bool ConnectionManager::isConnected()
+{
+       return connected_;
+}
+void ConnectionManager::connectionHandler(ConIcConnection *connection,
+        ConIcConnectionEvent *event,
+        gpointer user_data)
+{
+       Q_UNUSED(connection);
+       Q_UNUSED(user_data);
+
+       ConIcConnectionStatus status = con_ic_connection_event_get_status(event);
+
+       switch(status)
+       {
+
+         case CON_IC_STATUS_CONNECTED:
+               qDebug() << "Connected";
+               instance_->emit connected();
+               instance_->connected_ = true;
+               break;
+
+         case CON_IC_STATUS_DISCONNECTING:
+         case CON_IC_STATUS_NETWORK_UP:
+               break;
+
+         case CON_IC_STATUS_DISCONNECTED:
+               ConIcConnectionError err = con_ic_connection_event_get_error(event);
+               switch(err)
+               {
+                 case CON_IC_CONNECTION_ERROR_NONE:
+                 case CON_IC_CONNECTION_ERROR_USER_CANCELED:
+                       qDebug() << "Disconnected";
+                       instance_->emit disconnected();
+                       instance_->connected_ = false;
+                       break;
+
+                 case CON_IC_CONNECTION_ERROR_INVALID_IAP:
+                       qDebug() << "Invalid IAP";
+                       instance_->emit error("Invalid IAP");
+                       break;
+
+                 case CON_IC_CONNECTION_ERROR_CONNECTION_FAILED:
+                       qDebug() << "Connection failed";
+                       instance_->emit error("Connection failed");
+                       break;
+
+                 default:
+                         break;
+               }
+               break;
+       }
+}