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

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/share/shareengine.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 2010 Artur Duque de Souza <asouza@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 of the License, 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 General Public License     *
 
15
 *   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 <KDebug>
 
21
#include <KSycoca>
 
22
#include <KServiceTypeTrader>
 
23
 
 
24
#include <Plasma/DataContainer>
 
25
 
 
26
#include "shareengine.h"
 
27
#include "shareservice.h"
 
28
 
 
29
 
 
30
ShareEngine::ShareEngine(QObject *parent, const QVariantList &args)
 
31
    : Plasma::DataEngine(parent, args)
 
32
{
 
33
    Q_UNUSED(args);
 
34
}
 
35
 
 
36
void ShareEngine::init()
 
37
{
 
38
    connect(KSycoca::self(), SIGNAL(databaseChanged(QStringList)),
 
39
            this, SLOT(updatePlugins(QStringList)));
 
40
    updatePlugins(QStringList() << "services");
 
41
}
 
42
 
 
43
void ShareEngine::updatePlugins(const QStringList &changes)
 
44
{
 
45
    if (!changes.contains("services")) {
 
46
        return;
 
47
    }
 
48
 
 
49
    removeAllSources();
 
50
 
 
51
    KService::List services = KServiceTypeTrader::self()->query("Plasma/ShareProvider");
 
52
    QMultiMap<int, KService::Ptr> sortedServices;
 
53
    foreach (KService::Ptr service, services) {
 
54
        sortedServices.insert(service->property("X-KDE-Priority").toInt(), service);
 
55
    }
 
56
 
 
57
    QMapIterator<int, KService::Ptr> it(sortedServices);
 
58
    it.toBack();
 
59
    QHash<QString, QStringList> mimetypes;
 
60
    while (it.hasPrevious()) {
 
61
        it.previous();
 
62
        KService::Ptr service = it.value();
 
63
        const QString pluginName =
 
64
            service->property("X-KDE-PluginInfo-Name", QVariant::String).toString();
 
65
 
 
66
        const QStringList pluginMimeTypes =
 
67
            service->property("X-KDE-PlasmaShareProvider-MimeType", QVariant::StringList).toStringList();
 
68
 
 
69
        const QString storageId = service->storageId();
 
70
 
 
71
        if (pluginName.isEmpty() || pluginMimeTypes.isEmpty()) {
 
72
            continue;
 
73
        }
 
74
 
 
75
        // create the list of providers
 
76
        Plasma::DataEngine::Data data;
 
77
        data.insert("Name", service->name());
 
78
        data.insert("Service Id", service->storageId());
 
79
        data.insert("Mimetypes", pluginMimeTypes);
 
80
        setData(pluginName, data);
 
81
 
 
82
        // create the list of providers by type
 
83
        foreach (const QString &pluginMimeType, pluginMimeTypes) {
 
84
            mimetypes[pluginMimeType].append(pluginName);
 
85
        }
 
86
    }
 
87
 
 
88
 
 
89
    QHashIterator<QString, QStringList> it2(mimetypes);
 
90
    while (it2.hasNext()) {
 
91
        it2.next();
 
92
        setData("Mimetypes", it2.key(), it2.value());
 
93
    }
 
94
}
 
95
 
 
96
Plasma::Service *ShareEngine::serviceForSource(const QString &source)
 
97
{
 
98
    Plasma::DataContainer *data = containerForSource(source);
 
99
 
 
100
    if (!data) {
 
101
        return Plasma::DataEngine::serviceForSource(source);
 
102
    }
 
103
 
 
104
    if (source.compare("mimetype", Qt::CaseInsensitive) == 0) {
 
105
        return Plasma::DataEngine::serviceForSource(source);
 
106
    }
 
107
 
 
108
    const QString id = data->data().value("Service Id").toString();
 
109
    if (id.isEmpty()) {
 
110
        return Plasma::DataEngine::serviceForSource(source);
 
111
    }
 
112
 
 
113
    ShareService *service = new ShareService(this);
 
114
    service->setDestination(id);
 
115
    return service;
 
116
}
 
117
 
 
118
#include "shareengine.moc"
 
119
 
 
120
K_EXPORT_PLASMA_DATAENGINE(share, ShareEngine)