Added DeviceInfo and GeoCode for Qt5
authorMohammed Sameer <msameer@foolab.org>
Sat, 27 Jul 2013 23:53:59 +0000 (02:53 +0300)
committerMohammed Sameer <msameer@foolab.org>
Sat, 27 Jul 2013 23:53:59 +0000 (02:53 +0300)
src/nemo/deviceinfo.cpp [new file with mode: 0644]
src/nemo/deviceinfo.h [new file with mode: 0644]
src/nemo/geocode.cpp [new file with mode: 0644]
src/nemo/geocode.h [new file with mode: 0644]
src/nemo/nemo.pri

diff --git a/src/nemo/deviceinfo.cpp b/src/nemo/deviceinfo.cpp
new file mode 100644 (file)
index 0000000..36c6d23
--- /dev/null
@@ -0,0 +1,48 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "deviceinfo.h"
+#include <QDeviceInfo>
+
+DeviceInfo::DeviceInfo(QObject *parent) :
+  QObject(parent),
+  m_info(new QDeviceInfo(this)) {
+
+}
+
+DeviceInfo::~DeviceInfo() {
+
+}
+
+QString DeviceInfo::manufacturer() {
+  if (m_manufacturer.isEmpty()) {
+    m_manufacturer = m_info->manufacturer();
+  }
+
+  return m_manufacturer;
+}
+
+QString DeviceInfo::model() {
+  if (m_model.isEmpty()) {
+    m_model = m_info->model();
+  }
+
+  return m_model;
+}
diff --git a/src/nemo/deviceinfo.h b/src/nemo/deviceinfo.h
new file mode 100644 (file)
index 0000000..5f14698
--- /dev/null
@@ -0,0 +1,50 @@
+// -*- c++ -*-
+
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef DEVICE_INFO_H
+#define DEVICE_INFO_H
+
+#include <QObject>
+
+class QDeviceInfo;
+
+class DeviceInfo : public QObject {
+  Q_OBJECT
+
+  Q_PROPERTY(QString manufacturer READ manufacturer CONSTANT);
+  Q_PROPERTY(QString model READ model CONSTANT);
+
+public:
+  DeviceInfo(QObject *parent = 0);
+  ~DeviceInfo();
+
+  QString manufacturer();
+  QString model();
+
+private:
+  QDeviceInfo *m_info;
+
+  QString m_manufacturer;
+  QString m_model;
+};
+
+#endif /* DEVICE_INFO_H */
diff --git a/src/nemo/geocode.cpp b/src/nemo/geocode.cpp
new file mode 100644 (file)
index 0000000..5b7a658
--- /dev/null
@@ -0,0 +1,190 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "geocode.h"
+#include <QDebug>
+#include <QStringList>
+#include <QGeoAddress>
+
+Geocode::Geocode(QObject *parent) :
+  QObject(parent),
+  m_provider(0),
+  m_manager(0),
+  m_reply(0),
+  m_active(false) {
+
+  QStringList providers = QGeoServiceProvider::availableServiceProviders();
+
+  if (!providers.isEmpty()) {
+    m_provider = new QGeoServiceProvider(providers.at(0));
+  }
+  else {
+    qCritical() << "Cannot find any geo-service providers";
+    return;
+  }
+
+  if (!m_provider->geocodingFeatures().testFlag(QGeoServiceProvider::ReverseGeocodingFeature)) {
+    qCritical() << "geo-search manager does not support reverse geocoding";
+
+    m_manager = 0;
+    return;
+  }
+
+  m_manager = m_provider->geocodingManager();
+  if (!m_manager) {
+    qCritical() << "Cannot get hold of the geocode manager" << m_provider->errorString();
+    return;
+  }
+
+  QObject::connect(m_manager, SIGNAL(finished(QGeocodeReply *)),
+                  this, SLOT(finished(QGeocodeReply *)));
+  QObject::connect(m_manager, SIGNAL(error(QGeocodeReply *, const QGeocodeReply::Error&,
+                                          const QString&)),
+                  this, SLOT(error(QGeocodeReply *, const QGeocodeReply::Error&,
+                                   const QString&)));
+}
+
+Geocode::~Geocode() {
+  setActive(false);
+
+  delete m_provider; m_provider = 0;
+}
+
+bool Geocode::isActive() const {
+  return m_active;
+}
+
+void Geocode::setActive(bool active) {
+  if (active != m_active) {
+    m_active = active;
+
+    if (!m_active && m_reply) {
+      m_reply->deleteLater(); m_reply = 0;
+    }
+
+    clear();
+
+    emit activeChanged();
+  }
+}
+
+QString Geocode::country() const {
+  return m_country;
+}
+
+QString Geocode::city() const {
+  return m_city;
+}
+
+QString Geocode::suburb() const {
+  return m_suburb;
+}
+
+void Geocode::search(double longitude, double latitude) {
+  if (!m_active || !m_provider) {
+    return;
+  }
+
+  if (m_reply) {
+    m_reply->abort();
+    delete m_reply;
+  }
+
+  m_reply = m_manager->reverseGeocode(QGeoCoordinate(latitude, longitude));
+  if (!m_reply) {
+    qCritical() << "geo-search manager provided a null reply!";
+    return;
+  }
+}
+
+void Geocode::clear() {
+  if (!m_country.isEmpty()) {
+    m_country.clear();
+    emit countryChanged();
+  }
+
+  if (!m_city.isEmpty()) {
+    m_city.clear();
+    emit cityChanged();
+  }
+
+  if (!m_suburb.isEmpty()) {
+    m_suburb.clear();
+    emit suburbChanged();
+  }
+}
+
+void Geocode::finished(QGeocodeReply *reply) {
+  if (reply->error() != QGeocodeReply::NoError) {
+    qWarning() << "Error while geocoding" << reply->error() << reply->errorString();
+
+    reply->deleteLater();
+
+    if (reply == m_reply) {
+      m_reply = 0;
+    }
+
+    clear();
+
+    return;
+  }
+
+  QList<QGeoLocation> places = reply->locations();
+  if (!places.isEmpty()) {
+    QGeoAddress address = places.at(0).address();
+    if (m_country != address.country()) {
+      m_country = address.country();
+      emit countryChanged();
+    }
+
+    if (m_city != address.city()) {
+      m_city = address.city();
+      emit cityChanged();
+    }
+
+    if (m_suburb != address.district()) {
+      m_suburb = address.district();
+      emit suburbChanged();
+    }
+  }
+  else {
+    qWarning() << "No places found";
+    clear();
+  }
+
+  reply->deleteLater();
+
+  if (reply == m_reply) {
+    m_reply = 0;
+  }
+}
+
+void Geocode::error(QGeocodeReply *reply, const QGeocodeReply::Error& error,
+                   const QString& errorString) {
+
+  qWarning() << "Error while geocoding" << error << reply->errorString() << errorString;
+
+  reply->deleteLater();
+
+  if (reply == m_reply) {
+    m_reply = 0;
+    clear();
+  }
+}
diff --git a/src/nemo/geocode.h b/src/nemo/geocode.h
new file mode 100644 (file)
index 0000000..f9284dd
--- /dev/null
@@ -0,0 +1,76 @@
+// -*- c++ -*-
+
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef GEOCODE_H
+#define GEOCODE_H
+
+#include <QObject>
+#include <QGeocodeReply>
+#include <QGeoServiceProvider>
+#include <QGeocodingManager>
+
+class Geocode : public QObject {
+  Q_OBJECT
+  Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged);
+  Q_PROPERTY(QString country READ country NOTIFY countryChanged);
+  Q_PROPERTY(QString city READ city NOTIFY cityChanged);
+  Q_PROPERTY(QString suburb READ suburb NOTIFY suburbChanged);
+
+public:
+  Geocode(QObject *parent = 0);
+  ~Geocode();
+
+  bool isActive() const;
+  void setActive(bool active);
+
+  QString country() const;
+  QString city() const;
+  QString suburb() const;
+
+public slots:
+  void search(double longitude, double latitude);
+
+signals:
+  void activeChanged();
+  void countryChanged();
+  void cityChanged();
+  void suburbChanged();
+
+private slots:
+  void finished(QGeocodeReply *reply);
+  void error(QGeocodeReply *reply, const QGeocodeReply::Error& error,
+            const QString& errorString = QString());
+
+private:
+  void clear();
+
+  QGeoServiceProvider *m_provider;
+  QGeocodingManager *m_manager;
+  QGeocodeReply *m_reply;
+
+  bool m_active;
+  QString m_country;
+  QString m_city;
+  QString m_suburb;
+};
+
+#endif /* GEOCODE_H */
index 5fc04a6..c79b35f 100644 (file)
@@ -1,7 +1,7 @@
 DEPENDPATH += nemo .
 INCLUDEPATH += nemo .
 
-PKGCONFIG += 
+PKGCONFIG += Qt5SystemInfo Qt5Location
 
-HEADERS += nemo/soundvolumecontrol.h
-SOURCES += nemo/soundvolumecontrol.cpp
+HEADERS += nemo/soundvolumecontrol.h nemo/deviceinfo.h nemo/geocode.h
+SOURCES += nemo/soundvolumecontrol.cpp nemo/deviceinfo.cpp nemo/geocode.cpp