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/>.
19 #include <QtCore/QDebug>
20 #include <QtGui/QApplication>
21 #include <QtDBus/QDBusConnection>
22 #include <QtDBus/QDBusMessage>
23 #include <QtDBus/QDBusReply>
24 #include <QtDBus/QDBusArgument>
25 #include <QtDBus/QDBusMetaType>
30 QString const MAPS_SERVICE = "com.nokia.Navigation.NokiaMapsProvider";
31 QString const MAPS_PATH = "/Provider";
32 QString const MAPS_INTERFACE = "com.nokia.Navigation.MapProvider";
33 QString const MAPS_ADDRESS_TO_LOCATIONS = "AddressToLocations";
34 QString const MAPS_ADDRESS_TO_LOCATIONS_REPLY = "AddressToLocationsReply";
35 QString const MAPS_LOCATION_TO_ADDRESSES = "LocationToAddresses";
36 QString const MAPS_LOCATION_TO_ADDESSES_REPLY = "LocationToAddressReply";
37 QString const MAPS_START = "ShowPlaceGeo";
41 inline QDBusArgument& operator<<(QDBusArgument& argument,
42 const OviMaps::Location& location)
44 argument.beginStructure();
45 argument << location.latitude << location.longitude;
46 argument.endStructure();
50 inline const QDBusArgument& operator>>(const QDBusArgument& argument,
51 OviMaps::Location& location)
53 argument.beginStructure();
54 argument >> location.latitude >> location.longitude;
55 argument.endStructure();
59 Q_DECLARE_METATYPE(OviMaps::Location);
60 Q_DECLARE_METATYPE(QList<OviMaps::Location>);
61 Q_DECLARE_METATYPE(QList<QStringList>);
63 OviMaps::OviMaps(QObject* parent): QObject(parent),
64 locationsTarget_(0), addressesTarget_(0), ready_(false), timer_(0)
66 qDBusRegisterMetaType<OviMaps::Location>();
67 qDBusRegisterMetaType< QList<OviMaps::Location> >();
68 qDBusRegisterMetaType< QList<QStringList> >();
71 bool OviMaps::addressToLocations(OviMaps::Address const& address,
72 QList<OviMaps::Location>& locations)
74 QDBusMessage msg = QDBusMessage::createMethodCall(MAPS_SERVICE,
77 MAPS_ADDRESS_TO_LOCATIONS);
79 QList<QVariant> arguments;
83 data << address.number << "" << address.street << ""
84 << address.city << "" << "" << address.zipCode << address.country
85 << "" << "" << "" << "" << "" << "";
87 arguments.append(QVariant(data));
88 arguments.append(QVariant(true));
90 msg.setArguments(arguments);
92 QDBusMessage rep = QDBusConnection::sessionBus().call(msg);
94 QDBusReply<QDBusObjectPath> reply(rep);
98 QDBusObjectPath path = reply.value();
99 locationsTarget_ = &locations;
100 QDBusConnection::sessionBus().connect(MAPS_SERVICE,
103 MAPS_ADDRESS_TO_LOCATIONS_REPLY,
105 SLOT(handleAddressToLocationsReply(QList<OviMaps::Location>)));
107 bool ret = waitSignal();
108 locationsTarget_ = 0;
116 bool OviMaps::locationToAddresses(Location const& location, QList<Address>& address)
118 QDBusMessage msg = QDBusMessage::createMethodCall(MAPS_SERVICE,
121 MAPS_LOCATION_TO_ADDRESSES);
123 QList<QVariant> arguments;
125 arguments.append(QVariant(location.latitude));
126 arguments.append(QVariant(location.longitude));
127 arguments.append(QVariant(false));
129 msg.setArguments(arguments);
131 QDBusMessage rep = QDBusConnection::sessionBus().call(msg);
133 QDBusReply<QDBusObjectPath> reply(rep);
137 QDBusObjectPath path = reply.value();
138 addressesTarget_ = &address;
139 QDBusConnection::sessionBus().connect(MAPS_SERVICE,
142 MAPS_LOCATION_TO_ADDESSES_REPLY,
144 SLOT(handleLocationToAddressesReply(QList<QStringList>)));
146 bool ret = waitSignal();
147 addressesTarget_ = 0;
154 bool OviMaps::openMaps(Address const& address)
156 QList<Location> locations;
158 if(!addressToLocations(address, locations) || locations.size() == 0)
163 return openMaps(locations.at(0));
166 bool OviMaps::openMaps(Location const& location)
168 QDBusMessage msg = QDBusMessage::createMethodCall(MAPS_SERVICE,
173 QList<QVariant> arguments;
175 arguments.append(QVariant(location.latitude));
176 arguments.append(QVariant(location.longitude));
180 arguments.append(QVariant(val));
182 msg.setArguments(arguments);
184 QDBusMessage rep = QDBusConnection::sessionBus().call(msg);
186 if(rep.type() == QDBusMessage::ErrorMessage)
194 void OviMaps::timerEvent(QTimerEvent* event)
205 void OviMaps::handleAddressToLocationsReply(QList<OviMaps::Location> locations)
209 *locationsTarget_ = locations;
214 void OviMaps::handleLocationToAddressesReply(QList<QStringList> addresses)
218 for(int i = 0; i < addresses.size(); i++)
221 addr.street = addresses.at(i).at(2);
222 addr.number = addresses.at(i).at(0);
223 addr.zipCode = addresses.at(i).at(7);
224 addr.city = addresses.at(i).at(4);
225 addr.country = addresses.at(i).at(8);
227 addressesTarget_->push_back(addr);
234 bool OviMaps::waitSignal()
237 timer_ = startTimer(TIMEOUT);
239 while(!ready_ && timer_)
241 QApplication::processEvents(QEventLoop::WaitForMoreEvents);