299decb36e7a4f47e2ca29adc0d9c309a218ee08
[profile/ivi/ofono-qt.git] / lib / ofonomodemmanager.cpp
1 /*
2  * This file is part of ofono-qt
3  *
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Alexander Kanavin <alexander.kanavin@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <QtDBus/QtDBus>
25 #include <QtCore/QObject>
26
27 #include "ofonomodemmanager.h"
28 #include "ofonointerface.h"
29
30 struct OfonoModemStruct {
31     QDBusObjectPath path;
32     QVariantMap properties;
33 };
34 typedef QList<OfonoModemStruct> OfonoModemList;
35 Q_DECLARE_METATYPE(OfonoModemStruct)
36 Q_DECLARE_METATYPE(OfonoModemList)
37
38 QDBusArgument &operator<<(QDBusArgument &argument, const OfonoModemStruct &modem)
39 {
40     argument.beginStructure();
41     argument << modem.path << modem.properties;
42     argument.endStructure();
43     return argument;
44 }   
45
46 const QDBusArgument &operator>>(const QDBusArgument &argument, OfonoModemStruct &modem)
47 {   
48     argument.beginStructure();
49     argument >> modem.path >> modem.properties; 
50     argument.endStructure();
51     return argument;
52 }
53
54 OfonoModemManager::OfonoModemManager(QObject *parent)
55     : QObject(parent)
56 {
57     QDBusReply<OfonoModemList> reply;
58     OfonoModemList modems;
59
60     QDBusMessage request;
61
62     qDBusRegisterMetaType<OfonoModemStruct>();
63     qDBusRegisterMetaType<OfonoModemList>();
64
65     request = QDBusMessage::createMethodCall("org.ofono",
66                                              "/", "org.ofono.Manager",
67                                              "GetModems");
68     reply = QDBusConnection::systemBus().call(request);
69
70     modems = reply;
71     foreach(OfonoModemStruct modem, modems) {
72         m_modems << modem.path.path();
73     }
74
75     QDBusConnection::systemBus().connect("org.ofono","/","org.ofono.Manager",
76                                          "ModemAdded", this, 
77                                          SLOT(onModemAdded(const QDBusObjectPath&, const QVariantMap&)));
78     QDBusConnection::systemBus().connect("org.ofono","/","org.ofono.Manager",
79                                          "ModemRemoved", this, 
80                                          SLOT(onModemRemoved(const QDBusObjectPath&)));
81
82 }
83
84 OfonoModemManager::~OfonoModemManager()
85 {
86 }
87
88 QStringList OfonoModemManager::modems() const
89 {
90     return m_modems;
91 }
92
93 void OfonoModemManager::onModemAdded(const QDBusObjectPath& path, const QVariantMap& /*map*/)
94 {
95     m_modems << path.path();
96     emit modemAdded(path.path());
97 }
98
99 void OfonoModemManager::onModemRemoved(const QDBusObjectPath& path)
100 {
101     m_modems.removeAll(path.path()); 
102     emit modemRemoved(path.path());
103 }
104
105