Revert "Implemented per device resolution setting and selection"
[harmattan/cameraplus] / src / trackerstore.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "trackerstore.h"
22 #include <QSparqlConnection>
23 #include <QSparqlQuery>
24 #include <QUrl>
25 #if defined(QT4)
26 #include <QDeclarativeInfo>
27 #elif defined(QT5)
28 #include <QQmlInfo>
29 #endif
30 #include <QSparqlResult>
31 #include <QScopedPointer>
32 #include <QSparqlError>
33 #include <QDebug>
34 #include <QDateTime>
35
36 #define BEGIN_IMAGE "INSERT { _:x a nfo:Image, nmm:Photo"
37 #define BEGIN_VIDEO "INSERT { _:x a nfo:Video, nmm:Video"
38 #define QUERY_END ", nie:DataObject, nie:InformationElement, nfo:Media, nfo:Visual ; nie:url ?:file_url ; nfo:equipment ?:equipment^^xsd:string ; nie:contentCreated ?:contentCreated . }"
39
40 #define IMAGE_QUERY BEGIN_IMAGE QUERY_END
41 #define VIDEO_QUERY BEGIN_VIDEO QUERY_END
42
43 #define ADD_FAVORITE_QUERY "INSERT { ?u nao:hasTag nao:predefined-tag-favorite . } WHERE {?u nie:url <file://%1> . }"
44 #define REMOVE_FAVORITE_QUERY "DELETE { ?u nao:hasTag nao:predefined-tag-favorite . } WHERE {?u nie:url <file://%1> . }"
45
46 TrackerStore::TrackerStore(QObject *parent) :
47   QObject(parent),
48   m_connection(0) {
49
50 }
51
52 TrackerStore::~TrackerStore() {
53
54 }
55
56 bool TrackerStore::isActive() const {
57   return m_connection != 0;
58 }
59
60 void TrackerStore::setActive(bool active) {
61   if (isActive() == active) {
62     return;
63   }
64
65   if (active) {
66     m_connection = new QSparqlConnection("QTRACKER_DIRECT", QSparqlConnectionOptions(), this);
67   }
68   else {
69     m_connection->deleteLater();
70     m_connection = 0;
71   }
72
73   emit activeChanged();
74 }
75
76 QString TrackerStore::manufacturer() const {
77   return m_manufacturer;
78 }
79
80 void TrackerStore::setManufacturer(const QString& manufacturer) {
81   if (m_manufacturer != manufacturer) {
82     m_manufacturer = manufacturer;
83     emit manufacturerChanged();
84   }
85 }
86
87 QString TrackerStore::model() const {
88   return m_model;
89 }
90
91 void TrackerStore::setModel(const QString& model) {
92   if (m_model != model) {
93     m_model = model;
94     emit modelChanged();
95   }
96 }
97
98 bool TrackerStore::storeImage(const QString& path) {
99   return execQuery(IMAGE_QUERY, path);
100 }
101
102 bool TrackerStore::storeVideo(const QString& path) {
103   return execQuery(VIDEO_QUERY, path);
104 }
105
106 bool TrackerStore::execQuery(const QString& query, const QString& path) {
107   QDateTime dateTime = QDateTime::currentDateTime();
108
109   if (!isActive()) {
110     qmlInfo(this) << "TrackerStore is not active";
111     return false;
112   }
113
114   QString equipment = QString("urn:equipment:%1:%2:").arg(m_manufacturer).arg(m_model);
115
116   QSparqlQuery q(query, QSparqlQuery::InsertStatement);
117   q.bindValue("file_url", QUrl::fromLocalFile(path));
118   q.bindValue("equipment", equipment);
119   q.bindValue("contentCreated", dateTime.toString(Qt::ISODate) +
120                   "." + QString().sprintf("%.3d", dateTime.time().msec()));
121
122   return exec(q);
123 }
124
125 bool TrackerStore::addToFavorites(const QUrl& url) {
126   QString query = QString(ADD_FAVORITE_QUERY).arg(url.toLocalFile());
127
128   QSparqlQuery q(query, QSparqlQuery::InsertStatement);
129
130   return exec(q);
131 }
132
133 bool TrackerStore::removeFromFavorites(const QUrl& url) {
134   QString query = QString(REMOVE_FAVORITE_QUERY).arg(url.toLocalFile());
135
136   QSparqlQuery q(query, QSparqlQuery::DeleteStatement);
137
138   return exec(q);
139 }
140
141 bool TrackerStore::exec(QSparqlQuery& q) {
142   QScopedPointer<QSparqlResult> r(m_connection->syncExec(q));
143
144   if (!r->hasError()) {
145     return true;
146   }
147
148   while (r->next()) {
149     // Nothing
150   }
151
152   qmlInfo(this) << "QtSparql error:" << r->lastError().message();
153
154   return false;
155 }