~ubuntu-branches/ubuntu/raring/kde-runtime/raring

« back to all changes in this revision

Viewing changes to nepomuk/kioslaves/search/userqueries.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-12-07 17:43:09 UTC
  • mfrom: (1.1.19)
  • Revision ID: package-import@ubuntu.com-20121207174309-21o5e6dsbo9judu8
Tags: 4:4.9.90-0ubuntu1
* New upstream beta release
* Update .install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   Copyright (C) 2010 by Sebastian Trueg <trueg at kde.org>
3
 
 
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)
7
 
   any later version.
8
 
 
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.
13
 
 
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.
17
 
 */
18
 
 
19
 
#ifndef _NEPOMUK_USER_QUERIES_H_
20
 
#define _NEPOMUK_USER_QUERIES_H_
21
 
 
22
 
#include "nepomuksearchurltools.h"
23
 
 
24
 
#include <KConfig>
25
 
#include <KConfigGroup>
26
 
#include <KUrl>
27
 
#include <KDirNotify>
28
 
#include <KDebug>
29
 
 
30
 
#include <QtCore/QStringList>
31
 
 
32
 
 
33
 
namespace Nepomuk2 {
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 )
36
 
    {
37
 
        // the query URL is NOT the URL under which the query is listed!
38
 
        KUrl queryListUrl;
39
 
        queryListUrl.setProtocol( QLatin1String( "nepomuksearch" ) );
40
 
        queryListUrl.setFileName( Nepomuk2::resourceUriToUdsName( url ) );
41
 
        return queryListUrl;
42
 
    }
43
 
 
44
 
    inline KUrl queryUrlFromUserQueryUrl( const KUrl& url )
45
 
    {
46
 
        return Nepomuk2::udsNameToResourceUri( url.fileName() );
47
 
    }
48
 
 
49
 
    class UserQueryUrlList : public KUrl::List
50
 
    {
51
 
    public:
52
 
        UserQueryUrlList()
53
 
            : KUrl::List() {
54
 
            read();
55
 
        }
56
 
 
57
 
        static KUrl cleanupQueryUrl( const KUrl& url )
58
 
        {
59
 
            KUrl newUrl( 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() );
66
 
            return newUrl;
67
 
        }
68
 
 
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 )
74
 
                    return it;
75
 
                ++it;
76
 
            }
77
 
            return end();
78
 
        }
79
 
 
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 )
85
 
                    return it;
86
 
                ++it;
87
 
            }
88
 
            return constEnd();
89
 
        }
90
 
 
91
 
        bool containsQueryUrl( const KUrl& url ) const {
92
 
            return findQueryUrl( url ) != constEnd();
93
 
        }
94
 
 
95
 
        void addUserQuery( const KUrl& url, const QString& title )
96
 
        {
97
 
            // create new query url
98
 
            KUrl queryUrl = cleanupQueryUrl( url );
99
 
            queryUrl.addQueryItem( QLatin1String( "title" ), title );
100
 
 
101
 
            // remove old duplicates
102
 
            removeUserQuery( url );
103
 
 
104
 
            // save the new url
105
 
            append( queryUrl );
106
 
            save();
107
 
 
108
 
            // tell KIO about the new user query
109
 
            org::kde::KDirNotify::emitFilesAdded( QLatin1String( "nepomuksearch:/" ) );
110
 
        }
111
 
 
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();
118
 
                erase( it );
119
 
                it = findQueryUrl( url );
120
 
            }
121
 
            save();
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 );
125
 
        }
126
 
 
127
 
    private:
128
 
        void read() {
129
 
            KUrl::List::operator=( KConfig("kio_nepomuksearchrc" ).group( "User Queries" ).readEntry( "User queries", QStringList() ) );
130
 
        }
131
 
 
132
 
        void save() const {
133
 
            KConfig("kio_nepomuksearchrc" ).group( "User Queries" ).writeEntry( "User queries", toStringList() );
134
 
        }
135
 
    };
136
 
}
137
 
 
138
 
#endif