Added the ability to mark and unmark favorites from post capture page
authorMohammed Sameer <msameer@foolab.org>
Sat, 29 Dec 2012 19:10:55 +0000 (21:10 +0200)
committerMohammed Sameer <msameer@foolab.org>
Sat, 29 Dec 2012 19:10:55 +0000 (21:10 +0200)
qml/PostCapturePage.qml
src/postcapturemodel.cpp
src/postcapturemodel.h
src/trackerstore.cpp
src/trackerstore.h

index d105975..10c64da 100644 (file)
@@ -104,6 +104,29 @@ CameraPage {
                 }
         }
 
+        function addOrRemoveFavorite() {
+                if (!available) {
+                        return;
+                }
+
+                if (currentItem.itemData.favorite) {
+                        if (!trackerStore.removeFromFavorites(currentItem.itemData.url)) {
+                                showError(qsTr("Failed to remove favorite"));
+                        }
+                        else {
+                                currentItem.itemData.favorite = false;
+                        }
+                }
+                else {
+                        if (!trackerStore.addToFavorites(currentItem.itemData.url)) {
+                                showError(qsTr("Failed to add favorite"));
+                        }
+                        else {
+                                currentItem.itemData.favorite = true;
+                        }
+                }
+        }
+
         ShareHelper {
                 id: share
         }
@@ -164,7 +187,7 @@ CameraPage {
                 tools: ToolBarLayout {
                         id: layout
                         ToolIcon { iconId: "icon-m-toolbar-back-white"; onClicked: { pageStack.pop(); } }
-                        ToolIcon { iconId: available ? "icon-m-toolbar-favorite-mark-white" : "icon-m-toolbar-favorite-mark-dimmed-white"}
+                        ToolIcon { iconId: !available ? "icon-m-toolbar-favorite-mark-dimmed-white" : currentItem.itemData.favorite ? "icon-m-toolbar-favorite-mark-white" : "icon-m-toolbar-favorite-unmark-white"; onClicked: addOrRemoveFavorite(); }
                         ToolIcon { iconId: available ? "icon-m-toolbar-share-white" : "icon-m-toolbar-share-dimmed-white"; onClicked: shareCurrentItem(); }
                         ToolIcon { iconId: available ? "icon-m-toolbar-delete-white" : "icon-m-toolbar-delete-dimmed-white"; onClicked: deleteCurrentItem(); }
                         ToolIcon { iconId: "icon-m-toolbar-view-menu-white"; onClicked: menu.open(); }
index f84f827..658eed5 100644 (file)
@@ -246,14 +246,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) {
@@ -315,7 +320,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 +337,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();
index be3eb8c..43484cd 100644 (file)
@@ -100,7 +100,7 @@ class PostCaptureModelItem : public QObject {
   Q_PROPERTY(bool available READ available NOTIFY availableChanged);
   Q_PROPERTY(QString lastModified READ lastModified NOTIFY lastModifiedChanged);
   Q_PROPERTY(unsigned trackerId READ trackerId CONSTANT);
-  Q_PROPERTY(bool favorite READ favorite NOTIFY favoriteChanged);
+  Q_PROPERTY(bool favorite READ favorite WRITE setFavorite NOTIFY favoriteChanged);
 
 public:
   PostCaptureModelItem(const QSparqlResultRow& row, QObject *parent = 0);
@@ -120,6 +120,8 @@ public:
   bool available() const;
   QString lastModified() const;
   unsigned trackerId() const;
+
+  void setFavorite(bool add);
   bool favorite() const;
 
 signals:
index db3d8b8..604627d 100644 (file)
@@ -36,7 +36,8 @@
 #define IMAGE_QUERY BEGIN_IMAGE QUERY_END
 #define VIDEO_QUERY BEGIN_VIDEO QUERY_END
 
-// TODO: mime type
+#define ADD_FAVORITE_QUERY "INSERT { ?u nao:hasTag nao:predefined-tag-favorite . } WHERE {?u nie:url <file://%1> . }"
+#define REMOVE_FAVORITE_QUERY "DELETE { ?u nao:hasTag nao:predefined-tag-favorite . } WHERE {?u nie:url <file://%1> . }"
 
 TrackerStore::TrackerStore(QObject *parent) :
   QObject(parent),
@@ -108,6 +109,26 @@ bool TrackerStore::execQuery(const QString& query, const QString& path) {
   q.bindValue("contentCreated", dateTime.toString(Qt::ISODate) +
                  "." + QString().sprintf("%.3d", dateTime.time().msec()));
 
+  return exec(q);
+}
+
+bool TrackerStore::addToFavorites(const QUrl& url) {
+  QString query = QString(ADD_FAVORITE_QUERY).arg(url.toLocalFile());
+
+  QSparqlQuery q(query, QSparqlQuery::InsertStatement);
+
+  return exec(q);
+}
+
+bool TrackerStore::removeFromFavorites(const QUrl& url) {
+  QString query = QString(REMOVE_FAVORITE_QUERY).arg(url.toLocalFile());
+
+  QSparqlQuery q(query, QSparqlQuery::DeleteStatement);
+
+  return exec(q);
+}
+
+bool TrackerStore::exec(QSparqlQuery& q) {
   QScopedPointer<QSparqlResult> r(m_connection->syncExec(q));
 
   if (!r->hasError()) {
index 530ce6c..09bd7af 100644 (file)
@@ -26,6 +26,8 @@
 #include <QObject>
 
 class QSparqlConnection;
+class QUrl;
+class QSparqlQuery;
 
 class TrackerStore : public QObject {
   Q_OBJECT
@@ -50,6 +52,9 @@ public:
   QString model() const;
   void setModel(const QString& model);
 
+  Q_INVOKABLE bool addToFavorites(const QUrl& url);
+  Q_INVOKABLE bool removeFromFavorites(const QUrl& url);
+
 signals:
   void activeChanged();
   void manufacturerChanged();
@@ -57,6 +62,7 @@ signals:
 
 private:
   bool execQuery(const QString& query, const QString& path);
+  bool exec(QSparqlQuery& q);
 
   QSparqlConnection *m_connection;
   QString m_manufacturer;