Don't reuse file names
[harmattan/cameraplus] / src / postcapturemodel.cpp
index f84f827..fc639c9 100644 (file)
@@ -1,7 +1,7 @@
 /*!
  * This file is part of CameraPlus.
  *
- * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ * 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
 #include <QSparqlQuery>
 #include <QSparqlResult>
 #include <QSparqlError>
+#if defined(QT4)
 #include <QDeclarativeInfo>
+#elif defined(QT5)
+#include <QQmlInfo>
+#endif
 #include <QDateTime>
 #include <QDBusConnection>
 #include <QStringList>
@@ -70,14 +74,16 @@ const QDBusArgument& operator>>(const QDBusArgument& argument, Quad& t) {
 
 PostCaptureModel::PostCaptureModel(QObject *parent) :
   QAbstractListModel(parent),
-  m_connection(0) {
+  m_connection(0),
+  m_connected(false) {
+
+  qDBusRegisterMetaType<Quad>();
+  qDBusRegisterMetaType<QList<Quad> >();
 
   QHash<int, QByteArray> roles;
   roles.insert(Item, "item");
-  setRoleNames(roles);
 
-  qDBusRegisterMetaType<Quad>();
-  qDBusRegisterMetaType<QList<Quad> >();
+  setRoleNames(roles);
 }
 
 PostCaptureModel::~PostCaptureModel() {
@@ -87,14 +93,6 @@ PostCaptureModel::~PostCaptureModel() {
   delete m_connection; m_connection = 0;
 }
 
-void PostCaptureModel::classBegin() {
-
-}
-
-void PostCaptureModel::componentComplete() {
-
-}
-
 void PostCaptureModel::reload() {
   delete m_connection; m_connection = 0;
 
@@ -117,12 +115,20 @@ void PostCaptureModel::reload() {
   q.bindValue("equipment", equipment);
   exec(q);
 
-  QDBusConnection::sessionBus().connect(TRACKER_SERVICE, TRACKER_RESOURCE_PATH,
-                                       TRACKER_RESOURCE_INTERFACE, TRACKER_RESOURCE_SIGNAL,
-                                       TRACKER_RESOURCE_SIGNAL_SIGNATURE,
-                                       this, SLOT(graphUpdated(const QString&,
-                                                               const QList<Quad>&,
-                                                               const QList<Quad>&)));
+  if (!m_connected) {
+    const char *slot = SLOT(graphUpdated(const QString&,
+                                        const QList<Quad>&,
+                                        const QList<Quad>&));
+    m_connected = QDBusConnection::sessionBus().connect(TRACKER_SERVICE, TRACKER_RESOURCE_PATH,
+                                                       TRACKER_RESOURCE_INTERFACE,
+                                                       TRACKER_RESOURCE_SIGNAL,
+                                                       TRACKER_RESOURCE_SIGNAL_SIGNATURE,
+                                                       this, slot);
+  }
+
+  if (!m_connected) {
+    qmlInfo(this) << "Failed to connect to tracker " << TRACKER_RESOURCE_SIGNAL;
+  }
 }
 
 void PostCaptureModel::exec(QSparqlQuery& query) {
@@ -246,14 +252,19 @@ void PostCaptureModel::graphUpdated(const QString& className, const QList<Quad>&
   // We will just assume tracker:available has changed and that's it.
   // We are not really interested in the rest of properties.
 
-  Q_UNUSED(deleted);
-
   if (!(className == QLatin1String(PHOTO_CLASS) || className == QLatin1String(VIDEO_CLASS))) {
     return;
   }
 
   QList<int> items;
 
+  for (int x = 0; x < deleted.size(); x++) {
+    Quad q = deleted[x];
+    if (m_hash.contains(q.subject) && items.indexOf(q.subject) == -1) {
+      items << q.subject;
+    }
+  }
+
   for (int x = 0; x < inserted.size(); x++) {
     Quad q = inserted[x];
     if (m_hash.contains(q.subject) && items.indexOf(q.subject) == -1) {
@@ -269,6 +280,16 @@ void PostCaptureModel::graphUpdated(const QString& className, const QList<Quad>&
   }
 }
 
+#if defined(QT5)
+QHash<int, QByteArray> PostCaptureModel::roleNames() const {
+  return m_roles;
+}
+
+void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {
+  m_roles = roles;
+}
+#endif
+
 PostCaptureModelItem::PostCaptureModelItem(const QSparqlResultRow& row, QObject *parent) :
   QObject(parent) {
 
@@ -315,7 +336,15 @@ unsigned PostCaptureModelItem::trackerId() const {
 }
 
 bool PostCaptureModelItem::favorite() const {
-  return value("favorite").toBool();
+  return value("favorite", false).toBool();
+}
+
+void PostCaptureModelItem::setFavorite(bool add) {
+  if (favorite() != add) {
+    m_data["favorite"] = add;
+
+    emit favoriteChanged();
+  }
 }
 
 QString PostCaptureModelItem::formatDateTime(const QDateTime& dt) const {
@@ -324,6 +353,13 @@ QString PostCaptureModelItem::formatDateTime(const QDateTime& dt) const {
 
 void PostCaptureModelItem::update(PostCaptureModelItem *other) {
   // We will only update available, favorite and title:
+#if 0
+  qDebug() << "i" << trackerId() << other->trackerId()  << "\n"
+          << "a" << available() << other->available() << "\n"
+          << "t" << title() << other->title() << "\n"
+          << "f" << favorite() << other->favorite();
+#endif
+
   if (available() != other->available()) {
     m_data["available"] = other->available();
     emit availableChanged();