76c09560f32a854b3795f2568d1691d5d24d2481
[profile/ivi/ofono-qt.git] / lib / ofonomodem.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 "ofonomodem.h"
28 #include "ofonointerface.h"
29 #include "ofonomodemmanager.h"
30
31 OfonoModem::OfonoModem(QString modemId, QObject *parent)
32     : OfonoInterface("/", "org.ofono.Modem", OfonoInterface::GetAllOnStartup, parent)
33 {
34     m_mm = new OfonoModemManager(this);
35     connect(m_mm, SIGNAL(modemAdded(QString)), this, SLOT(modemAdded(QString)));
36     connect(m_mm, SIGNAL(modemRemoved(QString)), this, SLOT(modemRemoved(QString)));
37
38     if (modemId.isEmpty())
39         m_autoChoose = true;
40     else
41         m_autoChoose = false;
42
43     if (m_autoChoose == true)    
44         modemId = m_mm->modems().value(0);
45     
46     if (modemId.isEmpty()) {
47         modemId = "/";
48     } 
49     connect(this, SIGNAL(propertyChanged(const QString&, const QVariant&)), 
50             this, SLOT(propertyChanged(const QString&, const QVariant&)));
51     connect(this, SIGNAL(setPropertyFailed(const QString&)), 
52             this, SLOT(setPropertyFailed(const QString&)));
53     setPath(modemId);
54     m_isValid = m_mm->modems().contains(modemId);
55 }
56
57 OfonoModem::~OfonoModem()
58 {
59 }
60
61 void OfonoModem::propertyChanged(const QString& property, const QVariant& value)
62 {
63     if (property == "Online")
64         emit onlineChanged(value.value<bool>());
65     else if (property == "Powered")
66         emit poweredChanged(value.value<bool>());
67     else if (property == "Emergency")
68         emit emergencyChanged(value.value<bool>());
69     else if (property == "Name")
70         emit nameChanged(value.value<QString>());
71     else if (property == "Manufacturer")
72         emit manufacturerChanged(value.value<QString>());
73     else if (property == "Model")
74         emit modelChanged(value.value<QString>());
75     else if (property == "Revision")
76         emit revisionChanged(value.value<QString>());
77     else if (property == "Serial")
78         emit serialChanged(value.value<QString>());
79     else if (property == "Features")
80         emit featuresChanged(value.value<QStringList>());
81     else if (property == "Interfaces")
82         emit interfacesChanged(value.value<QStringList>());
83 }
84
85 void OfonoModem::setPropertyFailed(const QString& property)
86 {
87     if (property == "Online")
88         emit setOnlineFailed();
89     else if (property == "Powered")
90         emit setPoweredFailed();
91 }
92
93 void OfonoModem::modemAdded(QString /*modem*/)
94 {
95     modemsChanged();
96 }
97
98 void OfonoModem::modemRemoved(QString /*modem*/)
99 {
100     modemsChanged();
101 }
102
103 void OfonoModem::modemsChanged()
104 {
105     // validity has changed
106     if (isValid() != m_mm->modems().contains(modemId())) {
107         m_isValid = m_mm->modems().contains(modemId());
108         emit validityChanged(isValid());
109     }
110     if (!m_mm->modems().contains(modemId())) {
111         if (m_autoChoose == true) {
112             QString modemId = m_mm->modems().value(0);
113             if (modemId.isEmpty()) {
114                 modemId = "/";
115             }
116             setPath(modemId);
117             emit modemIdChanged(modemId);
118         }
119     }
120     // validity has changed
121     if (isValid() != m_mm->modems().contains(modemId())) {
122         m_isValid = m_mm->modems().contains(modemId());
123         emit validityChanged(isValid());
124     }
125 }
126
127
128 bool OfonoModem::isValid() const
129 {
130     return m_isValid;
131 }
132
133 QString OfonoModem::modemId() const
134 {
135     return path();
136 }
137
138 bool OfonoModem::powered() const
139 {
140     return properties()["Powered"].value<bool>();
141 }
142
143 void OfonoModem::setPowered(bool powered)
144 {
145     setProperty("Powered", qVariantFromValue(powered));
146 }
147
148 bool OfonoModem::online() const
149 {
150     return properties()["Online"].value<bool>();
151 }
152
153 void OfonoModem::setOnline(bool online)
154 {
155     setProperty("Online", qVariantFromValue(online));
156 }
157
158 bool OfonoModem::emergency() const
159 {
160     return properties()["Emergency"].value<bool>();
161 }
162
163 QString OfonoModem::name() const
164 {
165     return properties()["Name"].value<QString>();
166 }
167
168 QString OfonoModem::manufacturer() const
169 {
170     return properties()["Manufacturer"].value<QString>();
171 }
172
173 QString OfonoModem::model() const
174 {
175     return properties()["Model"].value<QString>();
176 }
177
178 QString OfonoModem::revision() const
179 {
180     return properties()["Revision"].value<QString>();
181 }
182
183 QString OfonoModem::serial() const
184 {
185     return properties()["Serial"].value<QString>();
186 }
187
188 QStringList OfonoModem::features() const
189 {
190     return properties()["Features"].value<QStringList>();
191 }
192
193 QStringList OfonoModem::interfaces() const
194 {
195     return properties()["Interfaces"].value<QStringList>();
196 }
197
198