add more spacing
[personal-kdebase.git] / runtime / nepomuk / libnepomukquery / query.cpp
bloba991865c2e73415e3c94a25ef0bfa71d734a0684
1 /*
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2008 Sebastian Trueg <trueg@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "query.h"
21 #include "term.h"
23 #include <QtCore/QSharedData>
24 #include <QtCore/QDebug>
27 class Nepomuk::Search::Query::Private : public QSharedData
29 public:
30 Private()
31 : type( InvalidQuery ),
32 limit( 0 ) {
35 Type type;
36 Term term;
37 QString sparqlQuery;
38 int limit;
40 QList<RequestProperty> requestProperties;
44 Nepomuk::Search::Query::Query()
45 : d( new Private() )
50 Nepomuk::Search::Query::Query( const Query& other )
52 d = other.d;
56 Nepomuk::Search::Query::Query( const Term& term )
57 : d ( new Private() )
59 d->type = PlainQuery;
60 d->term = term;
64 Nepomuk::Search::Query::Query( const QString& sparqlQuery )
65 : d ( new Private() )
67 d->type = SPARQLQuery;
68 d->sparqlQuery = sparqlQuery;
72 Nepomuk::Search::Query::~Query()
77 Nepomuk::Search::Query& Nepomuk::Search::Query::operator=( const Query& other )
79 d = other.d;
80 return *this;
84 Nepomuk::Search::Query::Type Nepomuk::Search::Query::type() const
86 return d->type;
90 Nepomuk::Search::Term Nepomuk::Search::Query::term() const
92 return d->term;
96 int Nepomuk::Search::Query::limit() const
98 return d->limit;
102 QString Nepomuk::Search::Query::sparqlQuery() const
104 return d->sparqlQuery;
108 void Nepomuk::Search::Query::setTerm( const Term& term )
110 d->term = term;
111 d->type = PlainQuery;
115 void Nepomuk::Search::Query::setLimit( int limit )
117 d->limit = limit;
121 void Nepomuk::Search::Query::setSparqlQuery( const QString& qs )
123 d->sparqlQuery = qs;
124 d->term = Term();
125 d->type = SPARQLQuery;
129 void Nepomuk::Search::Query::addRequestProperty( const QUrl& property, bool optional )
131 d->requestProperties.append( qMakePair( property, optional ) );
135 void Nepomuk::Search::Query::clearRequestProperties()
137 d->requestProperties.clear();
141 QList<Nepomuk::Search::Query::RequestProperty> Nepomuk::Search::Query::requestProperties() const
143 return d->requestProperties;
147 namespace {
148 bool compareRequestProperties( const QList<Nepomuk::Search::Query::RequestProperty>& rp1, const QList<Nepomuk::Search::Query::RequestProperty>& rp2 ) {
149 // brute force
150 foreach( const Nepomuk::Search::Query::RequestProperty& rp, rp1 ) {
151 if ( !rp2.contains( rp ) ) {
152 return false;
155 foreach( const Nepomuk::Search::Query::RequestProperty& rp, rp2 ) {
156 if ( !rp1.contains( rp ) ) {
157 return false;
160 return true;
164 bool Nepomuk::Search::Query::operator==( const Query& other ) const
166 if ( d->type == other.d->type &&
167 d->limit == other.d->limit ) {
168 if ( d->type == SPARQLQuery ) {
169 return( d->sparqlQuery == other.d->sparqlQuery &&
170 compareRequestProperties( d->requestProperties, other.d->requestProperties ) );
172 else {
173 return( d->term == other.d->term &&
174 compareRequestProperties( d->requestProperties, other.d->requestProperties ) );
178 return false;
182 QDebug operator<<( QDebug dbg, const Nepomuk::Search::Query& query )
184 dbg << "(Query" << query.term() << query.limit() << ")";
185 return dbg;
189 uint Nepomuk::Search::qHash( const Nepomuk::Search::Query& query )
191 if ( query.type() == Nepomuk::Search::Query::SPARQLQuery )
192 return qHash( query.sparqlQuery() );
193 else
194 return qHash( query.term() );