delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / nepomuk / libnepomukquery / term.cpp
blob0a030f5bc24bf2f6c7937e0dd7b6c200ebc275f6
1 /*
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007 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 "term.h"
22 #include <QtCore/QString>
23 #include <QtCore/QList>
24 #include <QtCore/QSharedData>
25 #include <QtCore/QDebug>
28 class Nepomuk::Search::Term::Private : public QSharedData
30 public:
31 Private( Type t = InvalidTerm, Comparator c = Equal )
32 : type( t ),
33 comparator( c ) {
36 Type type;
37 Comparator comparator;
38 Soprano::LiteralValue value;
39 QUrl resource;
40 QString field;
41 QUrl property;
42 QList<Term> subTerms;
46 Nepomuk::Search::Term::Term()
47 : d( new Private() )
52 Nepomuk::Search::Term::Term( const Term& other )
54 d = other.d;
58 Nepomuk::Search::Term::Term( const Soprano::LiteralValue& value )
59 : d( new Private( LiteralTerm ) )
61 d->value = value;
65 Nepomuk::Search::Term::Term( const QUrl& value )
66 : d( new Private( ResourceTerm ) )
68 d->resource = value;
72 Nepomuk::Search::Term::Term( const QString& field, const Soprano::LiteralValue& value, Comparator c )
73 : d( new Private( ComparisonTerm, c ) )
75 d->field = field;
76 d->subTerms.append( Term( value ) );
80 Nepomuk::Search::Term::Term( const QUrl& field, const Soprano::LiteralValue& value, Comparator c )
81 : d( new Private( ComparisonTerm, c ) )
83 d->property = field;
84 d->subTerms.append( Term( value ) );
88 Nepomuk::Search::Term::Term( const QUrl& field, const QUrl& resource )
89 : d( new Private( ComparisonTerm ) )
91 d->property = field;
92 d->subTerms.append( Term( resource ) );
96 Nepomuk::Search::Term::~Term()
101 Nepomuk::Search::Term& Nepomuk::Search::Term::operator=( const Term& other )
103 d = other.d;
104 return *this;
108 Nepomuk::Search::Term& Nepomuk::Search::Term::operator=( const Soprano::LiteralValue& literal )
110 d->value = literal;
111 d->type = LiteralTerm;
112 d->subTerms.clear();
113 d->field = QString();
114 return *this;
118 bool Nepomuk::Search::Term::isValid() const
120 switch ( d->type ) {
121 case InvalidTerm:
122 return false;
124 case LiteralTerm:
125 return d->value.isValid() && d->subTerms.isEmpty();
127 case ResourceTerm:
128 return d->resource.isValid() && d->subTerms.isEmpty();
130 case AndTerm:
131 case OrTerm:
132 return !d->subTerms.isEmpty();
134 case ComparisonTerm:
135 return ( !d->field.isEmpty() || !d->property.isEmpty() ) && ( d->subTerms.count() == 1 );
137 // make gcc happy
138 return false;
142 Nepomuk::Search::Term::Type Nepomuk::Search::Term::type() const
144 return d->type;
148 Soprano::LiteralValue Nepomuk::Search::Term::value() const
150 return d->value;
154 QUrl Nepomuk::Search::Term::resource() const
156 return d->resource;
160 Nepomuk::Search::Term::Comparator Nepomuk::Search::Term::comparator() const
162 return d->comparator;
166 QString Nepomuk::Search::Term::field() const
168 return d->field;
172 QUrl Nepomuk::Search::Term::property() const
174 return d->property;
178 QList<Nepomuk::Search::Term> Nepomuk::Search::Term::subTerms() const
180 return d->subTerms;
184 void Nepomuk::Search::Term::setType( Type type )
186 d->type = type;
190 void Nepomuk::Search::Term::setValue( const Soprano::LiteralValue& v )
192 d->value = v;
193 d->resource = QUrl();
197 void Nepomuk::Search::Term::setResource( const QUrl& res )
199 d->resource = res;
200 d->value = Soprano::LiteralValue();
204 void Nepomuk::Search::Term::setComparator( Comparator c )
206 d->comparator = c;
210 void Nepomuk::Search::Term::setField( const QString& f )
212 d->field = f;
213 d->property = QUrl();
217 void Nepomuk::Search::Term::setSubTerms( const QList<Term>& terms )
219 d->subTerms = terms;
223 void Nepomuk::Search::Term::setProperty( const QUrl& p )
225 d->property = p;
226 d->field = QString();
230 void Nepomuk::Search::Term::addSubTerm( const Term& term )
232 d->subTerms.append( term );
236 namespace {
237 bool compareLists( const QList<Nepomuk::Search::Term>& t1, const QList<Nepomuk::Search::Term>& t2 ) {
238 // brute-force
239 foreach( const Nepomuk::Search::Term& t, t1 ) {
240 if ( !t2.contains( t ) ) {
241 return false;
244 foreach( const Nepomuk::Search::Term& t, t2 ) {
245 if ( !t1.contains( t ) ) {
246 return false;
249 return true;
253 bool Nepomuk::Search::Term::operator==( const Term& other ) const
255 if ( d->type == other.d->type ) {
256 if ( d->type == ComparisonTerm ) {
257 return ( d->comparator == other.d->comparator &&
258 compareLists( d->subTerms, other.d->subTerms ) );
260 else {
261 return d->value == other.d->value &&
262 d->resource == other.d->resource &&
263 d->field == other.d->field &&
264 d->property == other.d->property &&
265 compareLists( d->subTerms, other.d->subTerms );
269 return false;
273 QDebug operator<<( QDebug dbg, const Nepomuk::Search::Term& term )
275 if ( term.isValid() ) {
276 dbg << "(Term";
277 switch( term.type() ) {
278 case Nepomuk::Search::Term::LiteralTerm:
279 dbg << "literal" << term.value();
280 break;
281 case Nepomuk::Search::Term::ResourceTerm:
282 dbg << "resource" << term.resource();
283 break;
284 case Nepomuk::Search::Term::AndTerm:
285 dbg << "and";
286 break;
287 case Nepomuk::Search::Term::OrTerm:
288 dbg << "or";
289 break;
290 case Nepomuk::Search::Term::ComparisonTerm:
291 dbg << "compare";
292 switch( term.comparator() ) {
293 case Nepomuk::Search::Term::Contains:
294 dbg << ":";
295 break;
296 case Nepomuk::Search::Term::Equal:
297 dbg << "=";
298 break;
299 case Nepomuk::Search::Term::Greater:
300 dbg << ">";
301 break;
302 case Nepomuk::Search::Term::Smaller:
303 dbg << "<";
304 break;
305 case Nepomuk::Search::Term::GreaterOrEqual:
306 dbg << ">=";
307 break;
308 case Nepomuk::Search::Term::SmallerOrEqual:
309 dbg << "<=";
310 break;
312 default:
313 break;
315 if ( term.type() == Nepomuk::Search::Term::ComparisonTerm ) {
316 if ( term.property().isValid() ) {
317 dbg << "Property" << term.property();
319 else {
320 dbg << "Field:" << term.field();
322 dbg << term.subTerms().first();
324 if ( term.type() == Nepomuk::Search::Term::AndTerm ||
325 term.type() == Nepomuk::Search::Term::OrTerm ) {
326 dbg << "Subterms: [";
327 foreach( const Nepomuk::Search::Term &t, term.subTerms() ) {
328 dbg << t;
330 dbg << "]";
332 dbg << ")";
335 return dbg;
339 uint Nepomuk::Search::qHash( const Nepomuk::Search::Term& term )
341 switch( term.type() ) {
342 case Nepomuk::Search::Term::LiteralTerm:
343 return qHash( term.value().toString() );
345 case Nepomuk::Search::Term::ComparisonTerm:
346 return( qHash( term.property().isValid() ? term.property().toString() : term.field() )<<16 |
347 qHash( term.subTerms().first() )<<8 |
348 ( uint )term.comparator() );
350 case Nepomuk::Search::Term::AndTerm:
351 case Nepomuk::Search::Term::OrTerm: {
352 uint h = ( uint )term.type();
353 QList<Nepomuk::Search::Term> subTerms = term.subTerms();
354 for ( int i = 0; i < subTerms.count(); ++i ) {
355 h |= ( qHash( subTerms[i] )<<i );
357 return h;
360 default:
361 return 0;