2 * This file is part of CameraPlus.
4 * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
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.
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.
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
21 #include "trackerstore.h"
22 #include <QSparqlConnection>
23 #include <QSparqlQuery>
25 #include <QDeclarativeInfo>
26 #include <QSparqlResult>
27 #include <QScopedPointer>
28 #include <QSparqlError>
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 . }"
36 #define IMAGE_QUERY BEGIN_IMAGE QUERY_END
37 #define VIDEO_QUERY BEGIN_VIDEO QUERY_END
39 #define ADD_FAVORITE_QUERY "INSERT { ?u nao:hasTag nao:predefined-tag-favorite . } WHERE {?u nie:url <file://%1> . }"
40 #define REMOVE_FAVORITE_QUERY "DELETE { ?u nao:hasTag nao:predefined-tag-favorite . } WHERE {?u nie:url <file://%1> . }"
42 TrackerStore::TrackerStore(QObject *parent) :
48 TrackerStore::~TrackerStore() {
52 bool TrackerStore::isActive() const {
53 return m_connection != 0;
56 void TrackerStore::setActive(bool active) {
57 if (isActive() == active) {
62 m_connection = new QSparqlConnection("QTRACKER_DIRECT", QSparqlConnectionOptions(), this);
65 m_connection->deleteLater();
72 QString TrackerStore::manufacturer() const {
73 return m_manufacturer;
76 void TrackerStore::setManufacturer(const QString& manufacturer) {
77 if (m_manufacturer != manufacturer) {
78 m_manufacturer = manufacturer;
79 emit manufacturerChanged();
83 QString TrackerStore::model() const {
87 void TrackerStore::setModel(const QString& model) {
88 if (m_model != model) {
94 bool TrackerStore::storeImage(const QString& path) {
95 return execQuery(IMAGE_QUERY, path);
98 bool TrackerStore::storeVideo(const QString& path) {
99 return execQuery(VIDEO_QUERY, path);
102 bool TrackerStore::execQuery(const QString& query, const QString& path) {
103 QDateTime dateTime = QDateTime::currentDateTime();
106 qmlInfo(this) << "TrackerStore is not active";
110 QString equipment = QString("urn:equipment:%1:%2:").arg(m_manufacturer).arg(m_model);
112 QSparqlQuery q(query, QSparqlQuery::InsertStatement);
113 q.bindValue("file_url", QUrl::fromLocalFile(path));
114 q.bindValue("equipment", equipment);
115 q.bindValue("contentCreated", dateTime.toString(Qt::ISODate) +
116 "." + QString().sprintf("%.3d", dateTime.time().msec()));
121 bool TrackerStore::addToFavorites(const QUrl& url) {
122 QString query = QString(ADD_FAVORITE_QUERY).arg(url.toLocalFile());
124 QSparqlQuery q(query, QSparqlQuery::InsertStatement);
129 bool TrackerStore::removeFromFavorites(const QUrl& url) {
130 QString query = QString(REMOVE_FAVORITE_QUERY).arg(url.toLocalFile());
132 QSparqlQuery q(query, QSparqlQuery::DeleteStatement);
137 bool TrackerStore::exec(QSparqlQuery& q) {
138 QScopedPointer<QSparqlResult> r(m_connection->syncExec(q));
140 if (!r->hasError()) {
148 qmlInfo(this) << "QtSparql error:" << r->lastError().message();