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.
22 #include <QtCore/QString>
23 #include <QtCore/QList>
24 #include <QtCore/QSharedData>
25 #include <QtCore/QDebug>
28 class Nepomuk::Search::Term::Private
: public QSharedData
31 Private( Type t
= InvalidTerm
, Comparator c
= Equal
)
37 Comparator comparator
;
38 Soprano::LiteralValue value
;
46 Nepomuk::Search::Term::Term()
52 Nepomuk::Search::Term::Term( const Term
& other
)
58 Nepomuk::Search::Term::Term( const Soprano::LiteralValue
& value
)
59 : d( new Private( LiteralTerm
) )
65 Nepomuk::Search::Term::Term( const QUrl
& value
)
66 : d( new Private( ResourceTerm
) )
72 Nepomuk::Search::Term::Term( const QString
& field
, const Soprano::LiteralValue
& value
, Comparator c
)
73 : d( new Private( ComparisonTerm
, c
) )
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
) )
84 d
->subTerms
.append( Term( value
) );
88 Nepomuk::Search::Term::Term( const QUrl
& field
, const QUrl
& resource
)
89 : d( new Private( ComparisonTerm
) )
92 d
->subTerms
.append( Term( resource
) );
96 Nepomuk::Search::Term::~Term()
101 Nepomuk::Search::Term
& Nepomuk::Search::Term::operator=( const Term
& other
)
108 Nepomuk::Search::Term
& Nepomuk::Search::Term::operator=( const Soprano::LiteralValue
& literal
)
111 d
->type
= LiteralTerm
;
113 d
->field
= QString();
118 bool Nepomuk::Search::Term::isValid() const
125 return d
->value
.isValid() && d
->subTerms
.isEmpty();
128 return d
->resource
.isValid() && d
->subTerms
.isEmpty();
132 return !d
->subTerms
.isEmpty();
135 return ( !d
->field
.isEmpty() || !d
->property
.isEmpty() ) && ( d
->subTerms
.count() == 1 );
142 Nepomuk::Search::Term::Type
Nepomuk::Search::Term::type() const
148 Soprano::LiteralValue
Nepomuk::Search::Term::value() const
154 QUrl
Nepomuk::Search::Term::resource() const
160 Nepomuk::Search::Term::Comparator
Nepomuk::Search::Term::comparator() const
162 return d
->comparator
;
166 QString
Nepomuk::Search::Term::field() const
172 QUrl
Nepomuk::Search::Term::property() const
178 QList
<Nepomuk::Search::Term
> Nepomuk::Search::Term::subTerms() const
184 void Nepomuk::Search::Term::setType( Type type
)
190 void Nepomuk::Search::Term::setValue( const Soprano::LiteralValue
& v
)
193 d
->resource
= QUrl();
197 void Nepomuk::Search::Term::setResource( const QUrl
& res
)
200 d
->value
= Soprano::LiteralValue();
204 void Nepomuk::Search::Term::setComparator( Comparator c
)
210 void Nepomuk::Search::Term::setField( const QString
& f
)
213 d
->property
= QUrl();
217 void Nepomuk::Search::Term::setSubTerms( const QList
<Term
>& terms
)
223 void Nepomuk::Search::Term::setProperty( const QUrl
& p
)
226 d
->field
= QString();
230 void Nepomuk::Search::Term::addSubTerm( const Term
& term
)
232 d
->subTerms
.append( term
);
237 bool compareLists( const QList
<Nepomuk::Search::Term
>& t1
, const QList
<Nepomuk::Search::Term
>& t2
) {
239 foreach( const Nepomuk::Search::Term
& t
, t1
) {
240 if ( !t2
.contains( t
) ) {
244 foreach( const Nepomuk::Search::Term
& t
, t2
) {
245 if ( !t1
.contains( t
) ) {
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
) );
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
);
273 QDebug
operator<<( QDebug dbg
, const Nepomuk::Search::Term
& term
)
275 if ( term
.isValid() ) {
277 switch( term
.type() ) {
278 case Nepomuk::Search::Term::LiteralTerm
:
279 dbg
<< "literal" << term
.value();
281 case Nepomuk::Search::Term::ResourceTerm
:
282 dbg
<< "resource" << term
.resource();
284 case Nepomuk::Search::Term::AndTerm
:
287 case Nepomuk::Search::Term::OrTerm
:
290 case Nepomuk::Search::Term::ComparisonTerm
:
292 switch( term
.comparator() ) {
293 case Nepomuk::Search::Term::Contains
:
296 case Nepomuk::Search::Term::Equal
:
299 case Nepomuk::Search::Term::Greater
:
302 case Nepomuk::Search::Term::Smaller
:
305 case Nepomuk::Search::Term::GreaterOrEqual
:
308 case Nepomuk::Search::Term::SmallerOrEqual
:
315 if ( term
.type() == Nepomuk::Search::Term::ComparisonTerm
) {
316 if ( term
.property().isValid() ) {
317 dbg
<< "Property" << term
.property();
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() ) {
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
);