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