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

« back to all changes in this revision

Viewing changes to plasma/generic/runners/recentdocuments/recentdocuments.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 2008 Sebastian Kügler <sebas@kde.org>
 
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 as
 
6
 *   published by the Free Software Foundation; either version 2, or
 
7
 *   (at your option) 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 Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "recentdocuments.h"
 
21
 
 
22
#include <QMimeData>
 
23
 
 
24
#include <KConfig>
 
25
#include <KConfigGroup>
 
26
#include <KDebug>
 
27
#include <KDirWatch>
 
28
#include <KIcon>
 
29
#include <KRun>
 
30
#include <KRecentDocument>
 
31
 
 
32
 
 
33
RecentDocuments::RecentDocuments(QObject *parent, const QVariantList& args)
 
34
    : Plasma::AbstractRunner(parent, args)
 
35
{
 
36
    Q_UNUSED(args);
 
37
    setObjectName( QLatin1String("Recent Documents" ));
 
38
    m_icon = KIcon("document-open-recent");
 
39
    loadRecentDocuments();
 
40
    // listen for changes to the list of recent documents
 
41
    KDirWatch *recentDocWatch = new KDirWatch(this);
 
42
    recentDocWatch->addDir(KRecentDocument::recentDocumentDirectory(), KDirWatch::WatchFiles);
 
43
    connect(recentDocWatch,SIGNAL(created(QString)),this,SLOT(loadRecentDocuments()));
 
44
    connect(recentDocWatch,SIGNAL(deleted(QString)),this,SLOT(loadRecentDocuments()));
 
45
    connect(recentDocWatch,SIGNAL(dirty(QString)),this,SLOT(loadRecentDocuments()));
 
46
    addSyntax(Plasma::RunnerSyntax(":q:", i18n("Looks for documents recently used with names matching :q:.")));
 
47
}
 
48
 
 
49
RecentDocuments::~RecentDocuments()
 
50
{
 
51
}
 
52
 
 
53
void RecentDocuments::loadRecentDocuments()
 
54
{
 
55
    //kDebug() << "Refreshing recent documents.";
 
56
    m_recentdocuments = KRecentDocument::recentDocuments();
 
57
}
 
58
 
 
59
 
 
60
void RecentDocuments::match(Plasma::RunnerContext &context)
 
61
{
 
62
    if (m_recentdocuments.isEmpty()) {
 
63
        return;
 
64
    }
 
65
 
 
66
    const QString term = context.query();
 
67
    if (term.length() < 3) {
 
68
        return;
 
69
    }
 
70
 
 
71
    foreach (const QString &document, m_recentdocuments) {
 
72
        if (!context.isValid()) {
 
73
            return;
 
74
        }
 
75
 
 
76
        if (document.contains(term, Qt::CaseInsensitive)) {
 
77
            KConfig _config( document, KConfig::SimpleConfig );
 
78
            KConfigGroup config(&_config, "Desktop Entry" );
 
79
            QString niceName =  config.readEntry( "Name" );
 
80
            Plasma::QueryMatch match(this);
 
81
            match.setType(Plasma::QueryMatch::PossibleMatch);
 
82
            match.setRelevance(1.0);
 
83
            match.setIcon(KIcon(config.readEntry("Icon")));
 
84
            match.setData(document); // TODO: Read URL[$e], or can we just pass the path to the .desktop file?
 
85
            match.setText(niceName);
 
86
            match.setSubtext(i18n("Recent Document"));
 
87
            context.addMatch(term, match);
 
88
        }
 
89
    }
 
90
}
 
91
 
 
92
void RecentDocuments::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
 
93
{
 
94
    Q_UNUSED(context)
 
95
    QString url = match.data().toString();
 
96
    kDebug() << "Opening Recent Document" << url;
 
97
    new KRun(url, 0);
 
98
}
 
99
 
 
100
QMimeData * RecentDocuments::mimeDataForMatch(const Plasma::QueryMatch * match)
 
101
{
 
102
    QMimeData * result = new QMimeData();
 
103
    QList<QUrl> urls;
 
104
    urls << QUrl(match->data().toString());
 
105
    result->setUrls(urls);
 
106
 
 
107
    result->setText(match->data().toString());
 
108
    return result;
 
109
}
 
110
 
 
111
#include "recentdocuments.moc"