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