~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/runners/webshortcuts/webshortcutrunner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Teemu Rytilahti <tpr@iki.fi>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License version 2 as
 
6
 *   published by the Free Software Foundation
 
7
 *
 
8
 *   This program is distributed in the hope that it will be useful,
 
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *   GNU General Public License for more details
 
12
 *
 
13
 *   You should have received a copy of the GNU Library General Public
 
14
 *   License along with this program; if not, write to the
 
15
 *   Free Software Foundation, Inc.,
 
16
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "webshortcutrunner.h"
 
20
 
 
21
#include <KDebug>
 
22
#include <KLocale>
 
23
#include <KMimeType>
 
24
#include <KServiceTypeTrader>
 
25
#include <KStandardDirs>
 
26
#include <KSycoca>
 
27
#include <KToolInvocation>
 
28
#include <KUrl>
 
29
#include <KUriFilter>
 
30
 
 
31
#include <QtDBus/QtDBus>
 
32
 
 
33
WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args)
 
34
    : Plasma::AbstractRunner(parent, args),
 
35
      m_match(this), m_filterBeforeRun(false)
 
36
{
 
37
    Q_UNUSED(args);
 
38
    setObjectName( QLatin1String("Web Shortcut" ));
 
39
    setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | Plasma::RunnerContext::Executable);
 
40
 
 
41
    m_icon = KIcon("internet-web-browser");
 
42
 
 
43
    m_match.setType(Plasma::QueryMatch::ExactMatch);
 
44
    m_match.setRelevance(0.9);
 
45
 
 
46
    // Listen for KUriFilter plugin config changes and update state...
 
47
    QDBusConnection sessionDbus = QDBusConnection::sessionBus();
 
48
    sessionDbus.connect(QString(), QString(), "org.kde.KUriFilterPlugin",
 
49
                        "configure", this, SLOT(readFiltersConfig()));
 
50
 
 
51
    connect(this, SIGNAL(teardown()), this, SLOT(resetState()));
 
52
    readFiltersConfig();
 
53
}
 
54
 
 
55
WebshortcutRunner::~WebshortcutRunner()
 
56
{
 
57
}
 
58
 
 
59
void WebshortcutRunner::readFiltersConfig()
 
60
{
 
61
    // Make sure that the searchEngines cache, etc. is refreshed when the config file is changed.
 
62
    loadSyntaxes();
 
63
}
 
64
 
 
65
void WebshortcutRunner::loadSyntaxes()
 
66
{
 
67
    KUriFilterData filterData (QLatin1String(":q"));
 
68
    filterData.setSearchFilteringOptions(KUriFilterData::RetrieveAvailableSearchProvidersOnly);
 
69
    if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::NormalTextFilter)) {
 
70
        m_delimiter = filterData.searchTermSeparator();
 
71
    }
 
72
 
 
73
    //kDebug() << "keyword delimiter:" << m_delimiter;
 
74
    //kDebug() << "search providers:" << filterData.preferredSearchProviders();
 
75
 
 
76
    QList<Plasma::RunnerSyntax> syns;
 
77
    Q_FOREACH (const QString &provider, filterData.preferredSearchProviders()) {
 
78
        //kDebug() << "checking out" << provider;
 
79
        Plasma::RunnerSyntax s(filterData.queryForPreferredSearchProvider(provider), /*":q:",*/
 
80
                              i18n("Opens \"%1\" in a web browser with the query :q:.", provider));
 
81
        syns << s;
 
82
    }
 
83
 
 
84
    setSyntaxes(syns);
 
85
}
 
86
 
 
87
void WebshortcutRunner::resetState()
 
88
{
 
89
    kDebug();
 
90
    m_lastFailedKey.clear();
 
91
    m_lastProvider.clear();
 
92
    m_lastKey.clear();
 
93
}
 
94
 
 
95
void WebshortcutRunner::match(Plasma::RunnerContext &context)
 
96
{
 
97
    const QString term = context.query();
 
98
 
 
99
    if (term.length() < 3 || !term.contains(m_delimiter))
 
100
        return;
 
101
 
 
102
    // kDebug() << "checking term" << term;
 
103
 
 
104
    const int delimIndex = term.indexOf(m_delimiter);
 
105
    if (delimIndex == term.length() - 1)
 
106
        return;
 
107
 
 
108
    const QString key = term.left(delimIndex);
 
109
 
 
110
    if (key == m_lastFailedKey) {
 
111
        return;    // we already know it's going to suck ;)
 
112
    }
 
113
 
 
114
    if (!context.isValid()) {
 
115
        kDebug() << "invalid context";
 
116
        return;
 
117
    }
 
118
 
 
119
    // Do a fake user feedback text update if the keyword has not changed.
 
120
    // There is no point filtering the request on every key stroke.
 
121
    // filtering
 
122
    if (m_lastKey == key) {
 
123
        m_filterBeforeRun = true;
 
124
        m_match.setText(i18n("Search %1 for %2", m_lastProvider, term.mid(delimIndex)));
 
125
        context.addMatch(term, m_match);
 
126
        return;
 
127
    }
 
128
 
 
129
    KUriFilterData filterData(term);
 
130
    if (!KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter)) {
 
131
        m_lastFailedKey = key;
 
132
        return;
 
133
    }
 
134
 
 
135
    m_lastFailedKey.clear();
 
136
    m_lastKey = key;
 
137
    m_lastProvider = filterData.searchProvider();
 
138
 
 
139
    m_match.setData(filterData.uri().url());
 
140
    m_match.setId("WebShortcut:" + key);
 
141
 
 
142
    m_match.setIcon(KIcon(filterData.iconName()));
 
143
    m_match.setText(i18n("Search %1 for %2", m_lastProvider, filterData.searchTerm()));
 
144
    context.addMatch(term, m_match);
 
145
}
 
146
 
 
147
void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
 
148
{
 
149
    QString location;
 
150
 
 
151
    //kDebug() << "filter before run?" << m_filterBeforeRun;
 
152
    if (m_filterBeforeRun) {
 
153
        m_filterBeforeRun = false;
 
154
        //kDebug() << "look up webshortcut:" << context.query();
 
155
        KUriFilterData filterData (context.query());
 
156
        if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter))
 
157
            location = filterData.uri().url();
 
158
    } else {
 
159
        location = match.data().toString();
 
160
    }
 
161
 
 
162
    //kDebug() << location;
 
163
    if (!location.isEmpty()) {
 
164
        KToolInvocation::invokeBrowser(location);
 
165
    }
 
166
}
 
167
 
 
168
#include "webshortcutrunner.moc"