Added TrackerStore to be used for creating a virtual file before image/video gets...
[harmattan/cameraplus] / src / trackerstore.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 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 #include <QDeclarativeInfo>
26 #include <QSparqlResult>
27 #include <QScopedPointer>
28 #include <QSparqlError>
29 #include <QDebug>
30 #include <QDateTime>
31
32 #define BEGIN_IMAGE "INSERT { _:x a nfo:Image, nmm:Photo"
33 #define BEGIN_VIDEO "INSERT { _:x a nfo:Video, nmm:Video"
34 #define QUERY_END ", nie:DataObject, nie:InformationElement, nfo:Media, nfo:Visual ; nie:url ?:file_url ; nfo:equipment ?:equipment^^xsd:string ; nie:contentCreated ?:contentCreated . }"
35
36 #define IMAGE_QUERY BEGIN_IMAGE QUERY_END
37 #define VIDEO_QUERY BEGIN_VIDEO QUERY_END
38
39 // TODO: mime type
40
41 TrackerStore::TrackerStore(QObject *parent) :
42   QObject(parent),
43   m_connection(0) {
44
45 }
46
47 TrackerStore::~TrackerStore() {
48
49 }
50
51 bool TrackerStore::isActive() const {
52   return m_connection != 0;
53 }
54
55 void TrackerStore::setActive(bool active) {
56   if (isActive() == active) {
57     return;
58   }
59
60   m_connection = new QSparqlConnection("QTRACKER_DIRECT", QSparqlConnectionOptions(), this);
61
62   emit activeChanged();
63 }
64
65 QString TrackerStore::manufacturer() const {
66   return m_manufacturer;
67 }
68
69 void TrackerStore::setManufacturer(const QString& manufacturer) {
70   if (m_manufacturer != manufacturer) {
71     m_manufacturer = manufacturer;
72     emit manufacturerChanged();
73   }
74 }
75
76 QString TrackerStore::model() const {
77   return m_model;
78 }
79
80 void TrackerStore::setModel(const QString& model) {
81   if (m_model != model) {
82     m_model = model;
83     emit modelChanged();
84   }
85 }
86
87 bool TrackerStore::storeImage(const QString& path) {
88   return execQuery(IMAGE_QUERY, path);
89 }
90
91 bool TrackerStore::storeVideo(const QString& path) {
92   return execQuery(VIDEO_QUERY, path);
93 }
94
95 bool TrackerStore::execQuery(const QString& query, const QString& path) {
96   QDateTime dateTime = QDateTime::currentDateTime();
97
98   if (!isActive()) {
99     qmlInfo(this) << "TrackerStore is not active";
100     return false;
101   }
102
103   QString equipment = QString("urn:equipment:%1:%2:").arg(m_manufacturer).arg(m_model);
104
105   QSparqlQuery q(query, QSparqlQuery::InsertStatement);
106   q.bindValue("file_url", QUrl::fromLocalFile(path));
107   q.bindValue("equipment", equipment);
108   q.bindValue("contentCreated", dateTime.toString(Qt::ISODate) +
109                   "." + QString().sprintf("%.3d", dateTime.time().msec()));
110
111   QScopedPointer<QSparqlResult> r(m_connection->syncExec(q));
112
113   if (!r->hasError()) {
114     return true;
115   }
116
117   while (r->next()) {
118     // Nothing
119   }
120
121   qmlInfo(this) << "QtSparql error:" << r->lastError().message();
122
123   return false;
124 }