2
Copyright (C) 2010 by Sebastian Trueg <trueg at kde.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2, or (at your option)
9
This program 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
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#ifndef _NEPOMUK_USER_QUERIES_H_
20
#define _NEPOMUK_USER_QUERIES_H_
22
#include "nepomuksearchurltools.h"
25
#include <KConfigGroup>
30
#include <QtCore/QStringList>
34
/// convert a normal query URL which can be listed to a URL which is used as location for user queries
35
inline KUrl queryUrlToUserQueryUrl( const KUrl& url )
37
// the query URL is NOT the URL under which the query is listed!
39
queryListUrl.setProtocol( QLatin1String( "nepomuksearch" ) );
40
queryListUrl.setFileName( Nepomuk2::resourceUriToUdsName( url ) );
44
inline KUrl queryUrlFromUserQueryUrl( const KUrl& url )
46
return Nepomuk2::udsNameToResourceUri( url.fileName() );
49
class UserQueryUrlList : public KUrl::List
57
static KUrl cleanupQueryUrl( const KUrl& url )
60
newUrl.removeQueryItem( QLatin1String( "title" ) );
61
newUrl.removeQueryItem( QLatin1String( "userquery" ) );
62
// strangly enough removing all query items leaves an empty query
63
// instead of no query
64
if ( newUrl.queryItems().isEmpty() )
65
newUrl.setEncodedQuery( QByteArray() );
69
QList<KUrl>::iterator findQueryUrl( const KUrl& url ) {
70
const KUrl normalizedUrl = cleanupQueryUrl( url );
71
QList<KUrl>::iterator it = begin();
72
while ( it != end() ) {
73
if ( cleanupQueryUrl( *it ) == normalizedUrl )
80
QList<KUrl>::const_iterator findQueryUrl( const KUrl& url ) const {
81
const KUrl normalizedUrl = cleanupQueryUrl( url );
82
QList<KUrl>::const_iterator it = constBegin();
83
while ( it != end() ) {
84
if ( cleanupQueryUrl( *it ) == normalizedUrl )
91
bool containsQueryUrl( const KUrl& url ) const {
92
return findQueryUrl( url ) != constEnd();
95
void addUserQuery( const KUrl& url, const QString& title )
97
// create new query url
98
KUrl queryUrl = cleanupQueryUrl( url );
99
queryUrl.addQueryItem( QLatin1String( "title" ), title );
101
// remove old duplicates
102
removeUserQuery( url );
108
// tell KIO about the new user query
109
org::kde::KDirNotify::emitFilesAdded( QLatin1String( "nepomuksearch:/" ) );
112
void removeUserQuery( const KUrl& url ) {
113
// we clean out all duplicates
114
QStringList oldUserQueryUrls;
115
QList<KUrl>::iterator it = findQueryUrl( url );
116
while ( it != end() ) {
117
oldUserQueryUrls << Nepomuk2::queryUrlToUserQueryUrl( *it ).url();
119
it = findQueryUrl( url );
122
// FIXME: sadly this does not work due to us setting UDS_URL, but emitting that url
123
// does not work either... I think we need help here.
124
org::kde::KDirNotify::emitFilesRemoved( oldUserQueryUrls );
129
KUrl::List::operator=( KConfig("kio_nepomuksearchrc" ).group( "User Queries" ).readEntry( "User queries", QStringList() ) );
133
KConfig("kio_nepomuksearchrc" ).group( "User Queries" ).writeEntry( "User queries", toStringList() );