~ubuntu-branches/ubuntu/trusty/kdeplasma-addons/trusty

« back to all changes in this revision

Viewing changes to libs/lancelot-datamodels/FavoriteApplications.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2010-05-25 09:50:14 UTC
  • mfrom: (1.1.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100525095014-6mlrm9z9bkws0zkt
Tags: 4:4.4.80-0ubuntu1
* New upstream beta release:
  - Bump kde-sc-dev-latest build-dep version to 4.4.80
  - Refresh kubuntu_04_kimpanel_disable_scim.diff
  - Update various .install files
  - Drop liblancelot0a and liblancelot-dev packages; Upstream has broken ABI
    without an .so version bump, and after discussion with Debian it was
    decided it was not worth it to ship an unstable library.
  - Add liblancelot files to plasma-widget-lancelot, adding appropriate
    Replaces: entries
* Switch to source format 3.0 (quilt):
  - Bump debhelper build-depend version to 7.3.16 or greater

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(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 Lesser/Library General Public License version 2,
 
6
 *   or (at your option) any later version, as published by the Free
 
7
 *   Software Foundation
 
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 Lesser/Library General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Lesser/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 "FavoriteApplications.h"
 
21
 
 
22
#include <QDebug>
 
23
#include <KConfig>
 
24
#include <KIcon>
 
25
#include <KConfigGroup>
 
26
#include <KStandardDirs>
 
27
#include <KTemporaryFile>
 
28
#include "Serializator.h"
 
29
 
 
30
namespace Lancelot {
 
31
namespace Models {
 
32
 
 
33
FavoriteApplications * FavoriteApplications::m_instance = NULL;
 
34
 
 
35
FavoriteApplications::FavoriteApplications()
 
36
    : BaseModel(true)
 
37
{
 
38
    setSelfTitle(i18n("Favorites"));
 
39
    setSelfIcon(KIcon("favorites"));
 
40
    load();
 
41
}
 
42
 
 
43
FavoriteApplications::~FavoriteApplications()
 
44
{
 
45
}
 
46
 
 
47
bool FavoriteApplications::addFavorite(QString url)
 
48
{
 
49
    if (addUrl(url)) {
 
50
        save();
 
51
        return true;
 
52
    }
 
53
    return false;
 
54
}
 
55
 
 
56
FavoriteApplications * FavoriteApplications::self()
 
57
{
 
58
    if (!m_instance) {
 
59
        m_instance = new FavoriteApplications();
 
60
    }
 
61
    return m_instance;
 
62
}
 
63
 
 
64
void FavoriteApplications::load()
 
65
{
 
66
    KConfig cfg(KStandardDirs::locate("config", "lancelotrc"));
 
67
    KConfigGroup favoritesGroup = cfg.group("Favorites");
 
68
 
 
69
    QStringList favoriteList = favoritesGroup.readEntry("FavoriteURLs", QStringList());
 
70
 
 
71
    if (favoriteList.empty()) {
 
72
        loadDefaultApplications();
 
73
        save();
 
74
    } else {
 
75
        addUrls(favoriteList);
 
76
    }
 
77
}
 
78
 
 
79
void FavoriteApplications::save()
 
80
{
 
81
    KConfig cfg(KStandardDirs::locate("config", "lancelotrc"));
 
82
    KConfigGroup favoritesGroup = cfg.group("Favorites");
 
83
 
 
84
    QStringList favoriteList;
 
85
    for (int i = 0; i < size(); i++) {
 
86
        favoriteList << itemAt(i).data.toString();
 
87
    }
 
88
 
 
89
    favoritesGroup.writeEntry("FavoriteURLs", favoriteList);
 
90
    favoritesGroup.sync();
 
91
}
 
92
 
 
93
void FavoriteApplications::loadDefaultApplications()
 
94
{
 
95
    QStringList result;
 
96
 
 
97
    // First, we check whether Kickoff is already set up,
 
98
    // if it is, we are using its Favorites
 
99
    KConfig cfg(KStandardDirs::locate("config", "kickoffrc"));
 
100
    KConfigGroup favoritesGroup = cfg.group("Favorites");
 
101
    result = favoritesGroup.readEntry("FavoriteURLs", QStringList());
 
102
    if (!result.empty()) {
 
103
        addUrls(result);
 
104
        return;
 
105
    }
 
106
 
 
107
    QStringList applications;
 
108
    applications
 
109
        << "firefox|konqbrowser"
 
110
        << "kmail|thunderbird"
 
111
        << "kopete|gaim"
 
112
        << "kate|gvim|kedit"
 
113
        << "konsole|xterm";
 
114
    addServices(applications);
 
115
}
 
116
 
 
117
bool FavoriteApplications::hasContextActions(int index) const
 
118
{
 
119
    Q_UNUSED(index);
 
120
    return true;
 
121
}
 
122
 
 
123
void FavoriteApplications::setContextActions(int index, Lancelot::PopupMenu * menu)
 
124
{
 
125
    Q_UNUSED(index);
 
126
    menu->addAction(KIcon("list-remove"), i18n("Remove From Favorites"))
 
127
        ->setData(QVariant(0));
 
128
}
 
129
 
 
130
void FavoriteApplications::contextActivate(int index, QAction * context)
 
131
{
 
132
    if (!context) {
 
133
        return;
 
134
    }
 
135
 
 
136
    if (context->data().toInt() == 0) {
 
137
        removeAt(index);
 
138
        save();
 
139
    }
 
140
}
 
141
 
 
142
FavoriteApplications::PassagewayViewProxy::PassagewayViewProxy()
 
143
    : ActionTreeModelProxy(Models::FavoriteApplications::self(),
 
144
            i18n("Favorites"), KIcon("favorites"))
 
145
{
 
146
}
 
147
 
 
148
FavoriteApplications::PassagewayViewProxy::~PassagewayViewProxy()
 
149
{
 
150
}
 
151
 
 
152
QMimeData * FavoriteApplications::PassagewayViewProxy::selfMimeData() const
 
153
{
 
154
    QMap < QString , QString > map;
 
155
    map["version"] = "1.0";
 
156
    map["type"]    = "list";
 
157
    map["model"]   = "FavoriteApplications";
 
158
 
 
159
    QMimeData * data = new QMimeData();
 
160
    data->setData("text/x-lancelotpart", Serializator::serialize(map).toAscii());
 
161
    return data;
 
162
}
 
163
 
 
164
bool FavoriteApplications::dataDropAvailable(int where, const QMimeData * mimeData)
 
165
{
 
166
    Q_UNUSED(where);
 
167
    return (mimeData->formats().contains("text/uri-list"));
 
168
}
 
169
 
 
170
void FavoriteApplications::dataDropped(int where, const QMimeData * mimeData)
 
171
{
 
172
    if (mimeData->formats().contains("text/uri-list")) {
 
173
        int from = -1;
 
174
 
 
175
        KUrl url = KUrl(QString(mimeData->data("text/uri-list")));
 
176
 
 
177
        for (int i = 0; i < size(); i++) {
 
178
            if (url.path() == itemAt(i).data) {
 
179
                from = i;
 
180
                break;
 
181
            }
 
182
        }
 
183
 
 
184
        if (from != -1) {
 
185
            removeAt(from);
 
186
        }
 
187
        insertUrl(where, url);
 
188
 
 
189
        save();
 
190
    }
 
191
}
 
192
 
 
193
} // namespace Models
 
194
} // namespace Lancelot