Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
18 KB
Referenced Files
None
Subscribers
None
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index b4f16412..f63c939b 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -1,66 +1,67 @@
set(CORE_SRCS
item.cpp
relation.cpp
itemfetchjob.cpp
itemcreatejob.cpp
itemsavejob.cpp
itemremovejob.cpp
relationfetchjob.cpp
relationcreatejob.cpp
relationremovejob.cpp
datastore.cpp
searchstore.cpp
term.cpp
query.cpp
queryrunnable.cpp
resultiterator.cpp
result.cpp
)
kde4_add_library(baloocore SHARED ${CORE_SRCS})
target_link_libraries(baloocore
${QT_QTCORE_LIBRARY}
+ ${QJSON_LIBRARIES}
${KDE4_KDECORE_LIBRARY}
)
set_target_properties(baloocore PROPERTIES
VERSION ${GENERIC_LIB_VERSION}
SOVERSION ${GENERIC_LIB_SOVERSION}
)
install(TARGETS baloocore EXPORT BalooLibraryTargets ${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES
item.h
itemfetchjob.h
itemcreatejob.h
itemsavejob.h
itemremovejob.h
relation.h
relationfetchjob.h
relationremovejob.h
relationcreatejob.h
datastore.h
term.h
query.h
queryrunnable.h
result.h
resultiterator.h
searchstore.h
core_export.h
DESTINATION ${INCLUDE_INSTALL_DIR}/baloo COMPONENT Devel
)
install(FILES baloosearchstore.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR})
add_subdirectory(tests)
diff --git a/src/core/query.cpp b/src/core/query.cpp
index c8bb6945..e50b5183 100644
--- a/src/core/query.cpp
+++ b/src/core/query.cpp
@@ -1,149 +1,176 @@
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 Vishesh Handa <me@vhanda.in>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "query.h"
#include "term.h"
#include "searchstore.h"
#include <QString>
#include <QStringList>
#include <QList>
#include <KDebug>
+#include <qjson/serializer.h>
+#include <qjson/parser.h>
+
using namespace Baloo;
class Baloo::Query::Private {
public:
Private() {
m_limit = 100000;
}
Term m_term;
QStringList m_types;
QString m_searchString;
uint m_limit;
};
Query::Query()
: d(new Private)
{
}
Query::Query(const Term& t)
: d(new Private)
{
d->m_term = t;
}
void Query::setTerm(const Term& t)
{
d->m_term = t;
}
Term Query::term() const
{
return d->m_term;
}
void Query::addType(const QString& type)
{
d->m_types << type.split('/', QString::SkipEmptyParts);
}
void Query::setTypes(const QStringList& types)
{
d->m_types = types;
}
QStringList Query::types() const
{
return d->m_types;
}
QString Query::searchString() const
{
return d->m_searchString;
}
void Query::setSearchString(const QString& str)
{
d->m_searchString = str;
}
void Query::addRelation(const Relation& rel)
{
//TODO:
}
void Query::setRelations(const QList<Relation>& rel)
{
//TODO:
}
QList<Relation> Query::relations() const
{
//TODO:
return QList<Relation>();
}
uint Query::limit() const
{
return d->m_limit;
}
void Query::setLimit(uint limit)
{
d->m_limit = limit;
}
ResultIterator Query::exec()
{
// vHanda: Maybe this should default to allow searches on all search stores?
Q_ASSERT_X(!types().isEmpty(), "Baloo::Query::exec", "A query is being initialized without a type");
if (types().isEmpty())
return ResultIterator();
QList<SearchStore*> stores = SearchStore::searchStores();
SearchStore* storeMatch = 0;
Q_FOREACH (SearchStore* store, stores) {
Q_FOREACH (const QString& type, types()) {
if (store->types().contains(type)) {
storeMatch = store;
break;
}
}
if (storeMatch)
break;
}
if (!storeMatch)
return ResultIterator();
int id = storeMatch->exec(*this);
return ResultIterator(id, storeMatch);
+}
- //FIXME: Massive memory leak. Someone needs to delete all the stores!
+QByteArray Query::toJSON()
+{
+ QVariantMap map;
+ map["type"] = d->m_types;
+ map["limit"] = d->m_limit;
+ map["searchString"] = d->m_searchString;
+ map["term"] = QVariant(d->m_term.toVariantMap());
+
+ QJson::Serializer serializer;
+ return serializer.serialize(map);
}
+// static
+Query Query::fromJSON(const QByteArray& arr)
+{
+ QJson::Parser parser;
+ QVariantMap map = parser.parse(arr).toMap();
+
+ Query query;
+ query.d->m_types = map["type"].toStringList();
+ query.d->m_limit = map["limit"].toInt();
+ query.d->m_searchString = map["searchString"].toString();
+ query.d->m_term = Term::fromVariantMap(map["term"].toMap());
+
+ return query;
+}
diff --git a/src/core/query.h b/src/core/query.h
index 3bc4309e..c437bcf7 100644
--- a/src/core/query.h
+++ b/src/core/query.h
@@ -1,103 +1,106 @@
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 Vishesh Handa <me@vhanda.in>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef QUERY_H
#define QUERY_H
#include "relation.h"
#include "resultiterator.h"
namespace Baloo {
class Term;
class BALOO_CORE_EXPORT Query
{
public:
Query();
Query(const Term& t);
void setTerm(const Term& t);
Term term() const;
/**
* Add a type to the results of the query.
*
* Each Item in the result must contain one of the types.
* This is generally used to filter only Files, Emails, Tags, etc
*
* One can add multiple types in one go by separating individual types
* with a '/'. Eg - "File/Audio"
*/
void addType(const QString& type);
void setTypes(const QStringList& types);
QStringList types() const;
/**
* Every Item in the result must contain the relation \p rel
*/
void addRelation(const Relation& rel);
void setRelations(const QList<Relation>& rel);
QList<Relation> relations() const;
/**
* Set some text which should be used to search for Items. This
* contain a single word or an entire sentence.
*
* Each search backend will interpret it in its own way, and try
* to give the best possible results.
*/
void setSearchString(const QString& str);
QString searchString() const;
/**
* Only a maximum of \p limit results will be returned.
* By default the limit is 100000.
*/
void setLimit(uint limit);
uint limit() const;
// FIXME: Sorting?
/**
* Adds a custom option which any search backend could use
* to configure the query result.
*
* Each backend has their own custom options which should be
* looked up in their corresponding documentation
*/
// TODO:
//void addCustomOption(const QString& option, const QString& value);
//void removeCustomOption(const QString& option);
//QString customOption(const QString& option);
ResultIterator exec();
+ QByteArray toJSON();
+ static Query fromJSON(const QByteArray& arr);
+
private:
class Private;
Private* d;
};
}
#endif // QUERY_H
diff --git a/src/core/term.cpp b/src/core/term.cpp
index 888540f6..b5ad853a 100644
--- a/src/core/term.cpp
+++ b/src/core/term.cpp
@@ -1,183 +1,300 @@
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 Vishesh Handa <me@vhanda.in>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "term.h"
+#include <QVariant>
using namespace Baloo;
class Baloo::Term::Private {
public:
Operation m_op;
Comparator m_comp;
QString m_property;
QVariant m_value;
bool m_isNegated;
QList<Term> m_subTerms;
Private() {
m_op = None;
m_comp = Auto;
m_isNegated = false;
}
};
Term::Term()
: d(new Private)
{
}
Term::Term(const QString& property)
: d(new Private)
{
d->m_property = property;
}
Term::Term(const QString& property, const QVariant& value, Term::Comparator c)
: d(new Private)
{
d->m_property = property;
d->m_value = value;
if (c == Auto) {
if (value.type() == QVariant::String)
d->m_comp = Contains;
else if (value.type() == QVariant::DateTime)
d->m_comp = Contains;
else
d->m_comp = Equal;
}
else {
d->m_comp = c;
}
}
/*
Term::Term(const QString& property, const QVariant& start, const QVariant& end)
: d(new Private)
{
d->m_property = property;
d->m_op = Range;
// FIXME: How to save range queries?
}
*/
Term::Term(Term::Operation op)
: d(new Private)
{
d->m_op = op;
}
Term::Term(Term::Operation op, const Term& t)
: d(new Private)
{
d->m_op = op;
d->m_subTerms << t;
}
Term::Term(Term::Operation op, const QList<Term>& t)
: d(new Private)
{
d->m_op = op;
d->m_subTerms = t;
}
void Term::setNegation(bool isNegated)
{
d->m_isNegated = isNegated;
}
bool Term::isNegated() const
{
return d->m_isNegated;
}
bool Term::negated() const
{
return d->m_isNegated;
}
void Term::addSubTerm(const Term& term)
{
d->m_subTerms << term;
}
void Term::setSubTerms(const QList<Term>& terms)
{
d->m_subTerms = terms;
}
Term Term::subTerm() const
{
if (d->m_subTerms.size())
return d->m_subTerms.first();
return Term();
}
QList<Term> Term::subTerms() const
{
return d->m_subTerms;
}
void Term::setOperation(Term::Operation op)
{
d->m_op = op;
}
Term::Operation Term::operation() const
{
return d->m_op;
}
QString Term::property() const
{
return d->m_property;
}
void Term::setProperty(const QString& property)
{
d->m_property = property;
}
void Term::setValue(const QVariant& value)
{
d->m_value = value;
}
QVariant Term::value() const
{
return d->m_value;
}
Term::Comparator Term::comparator() const
{
return d->m_comp;
}
void Term::setComparator(Term::Comparator c)
{
d->m_comp = c;
}
+
+QVariantMap Term::toVariantMap() const
+{
+ QVariantMap map;
+ if (d->m_op != None) {
+ QVariantList variantList;
+ Q_FOREACH (const Term& term, d->m_subTerms) {
+ variantList << QVariant(term.toVariantMap());
+ }
+
+ if (d->m_op == And)
+ map["$and"] = variantList;
+ else
+ map["$or"] = variantList;
+
+ return map;
+ }
+
+ QString op;
+ switch (d->m_comp) {
+ case Equal:
+ map[d->m_property] = d->m_value;
+ return map;
+
+ case Contains:
+ op = "$ct";
+ break;
+
+ case Greater:
+ op = "$gt";
+ break;
+
+ case GreaterEqual:
+ op = "$gte";
+ break;
+
+ case Less:
+ op = "$lt";
+ break;
+
+ case LessEqual:
+ op = "$lte";
+ break;
+
+ default:
+ return QVariantMap();
+ }
+
+ QVariantMap m;
+ m[op] = d->m_value;
+ map[d->m_property] = QVariant(m);
+ return map;
+}
+
+Term Term::fromVariantMap(const QVariantMap& map)
+{
+ if (map.size() != 1)
+ return Term();
+
+ Term term;
+
+ QString andOrString;
+ if (map.contains("$and")) {
+ andOrString = "$and";
+ term.setOperation(And);
+ }
+ else if (map.contains("$or")) {
+ andOrString = "$or";
+ term.setOperation(Or);
+ }
+
+ if (andOrString.size()) {
+ QList<Term> subTerms;
+
+ QVariantList list = map[andOrString].toList();
+ Q_FOREACH (const QVariant& var, list)
+ subTerms << Term::fromVariantMap(var.toMap());
+
+ term.setSubTerms(subTerms);
+ return term;
+ }
+
+ QString prop = map.keys().first();
+ term.setProperty(prop);
+
+ QVariant value = map.value(prop);
+ if (value.type() == QVariant::Map) {
+ QVariantMap map = value.toMap();
+ if (map.size() != 1)
+ return term;
+
+ QString op = map.keys().first();
+ Term::Comparator com;
+ if (op == "$ct")
+ com = Contains;
+ else if (op == "$gt")
+ com = Greater;
+ else if (op == "$gte")
+ com = GreaterEqual;
+ else if (op == "$lt")
+ com = Less;
+ else if (op == "$lte")
+ com = LessEqual;
+ else
+ return term;
+
+ term.setComparator(com);
+ term.setValue(map.value(op));
+ return term;
+ }
+
+ term.setComparator(Equal);
+ term.setValue(value);
+
+ return term;
+}
diff --git a/src/core/term.h b/src/core/term.h
index 78c9c938..7846729e 100644
--- a/src/core/term.h
+++ b/src/core/term.h
@@ -1,143 +1,145 @@
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 Vishesh Handa <me@vhanda.in>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef TERM_H
#define TERM_H
#include <QString>
#include <QVariant>
#include "core_export.h"
namespace Baloo {
class BALOO_CORE_EXPORT Term
{
public:
enum Comparator {
Auto,
Equal,
Contains,
Greater,
GreaterEqual,
Less,
LessEqual
};
enum Operation {
None,
And,
Or
};
Term();
/**
* The Item must contain the property \p property
*/
Term(const QString& property);
/**
* The Item must contain the property \p property with
* value \value.
*
* The default comparator is Auto which has the following behavior
* For Strings - Contains
* For DateTime - Contains
* For any other type - Equals
*/
Term(const QString& property, const QVariant& value, Comparator c = Auto);
// Removing Range based terms for now
// FIXME: Add range based terms
/**
* The property must fall within the range \p start <= \p value <= \p end
*/
//Term(const QString& property, const QVariant& start, const QVariant& end);
/**
* This term is a combination of other terms
*/
Term(Operation op);
Term(Operation op, const Term& t);
Term(Operation op, const QList<Term>& t);
/**
* Negate this term. Negation only applies for Equal or Contains
* For other Comparators you must invert it yourself
*/
void setNegation(bool isNegated);
bool negated() const;
bool isNegated() const;
void addSubTerm(const Term& term);
void setSubTerms(const QList<Term>& terms);
/**
* Returns the first subTerm in the list of subTerms
*/
Term subTerm() const;
QList<Term> subTerms() const;
void setOperation(Operation op);
Operation operation() const;
/**
* Return the property this term is targetting
*/
QString property() const;
void setProperty(const QString& property);
QVariant value() const;
void setValue(const QVariant& value);
Comparator comparator() const;
void setComparator(Comparator c);
+ QVariantMap toVariantMap() const;
+ static Term fromVariantMap(const QVariantMap& map);
private:
class Private;
Private* d;
};
inline Term operator &&(const Term& lhs, const Term& rhs) {
Term t(Term::And);
t.addSubTerm(lhs);
t.addSubTerm(rhs);
return t;
}
inline Term operator ||(const Term& lhs, const Term& rhs) {
Term t(Term::Or);
t.addSubTerm(lhs);
t.addSubTerm(rhs);
return t;
}
inline Term operator !(const Term& rhs) {
Term t(rhs);
t.setNegation(!rhs.isNegated());
return t;
}
}
#endif // TERM_H

File Metadata

Mime Type
text/x-diff
Expires
Fri, Apr 24, 1:46 PM (3 d, 10 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18896732
Default Alt Text
(18 KB)

Event Timeline