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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/quicklaunch/launcherdata.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) 2008 by Lukas Appelhans <l.appelhans@gmx.de>            *
 
3
 *   Copyright (C) 2010 - 2011 by Ingomar Wesp <ingomar@wesp.name>         *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 
19
 ***************************************************************************/
 
20
#include "launcherdata.h"
 
21
 
 
22
// Qt
 
23
#include <Qt>
 
24
#include <QtGlobal>
 
25
#include <QtCore/QList>
 
26
#include <QtCore/QString>
 
27
#include <QtXml/QDomDocument>
 
28
 
 
29
// KDE
 
30
#include <KBookmark>
 
31
#include <KBookmarkGroup>
 
32
#include <KDesktopFile>
 
33
#include <KMimeType>
 
34
#include <KUrl>
 
35
 
 
36
namespace Quicklaunch {
 
37
 
 
38
LauncherData::LauncherData(const KUrl& url)
 
39
:
 
40
    // KUrl::url() takes care of improperly escaped characters and
 
41
    // resolves pure paths into file:/// URLs
 
42
    m_url(url.url()),
 
43
    m_name(),
 
44
    m_description(),
 
45
    m_icon()
 
46
{
 
47
    if (m_url.isLocalFile() &&
 
48
        KDesktopFile::isDesktopFile(m_url.toLocalFile())) {
 
49
 
 
50
        KDesktopFile f(m_url.toLocalFile());
 
51
 
 
52
        m_name = f.readName();
 
53
        m_description = f.readGenericName();
 
54
        m_icon = f.readIcon();
 
55
 
 
56
    } else {
 
57
        m_icon = KMimeType::iconNameForUrl(m_url);
 
58
    }
 
59
 
 
60
    if (m_name.isNull()) {
 
61
        m_name = m_url.fileName();
 
62
    }
 
63
 
 
64
    if (m_icon.isNull()) {
 
65
        m_icon = "unknown";
 
66
    }
 
67
}
 
68
 
 
69
LauncherData::LauncherData()
 
70
:
 
71
    m_url(),
 
72
    m_name(),
 
73
    m_description(),
 
74
    m_icon()
 
75
{}
 
76
 
 
77
KUrl LauncherData::url() const
 
78
{
 
79
    return m_url;
 
80
}
 
81
 
 
82
QString LauncherData::name() const
 
83
{
 
84
    return m_name;
 
85
}
 
86
 
 
87
QString LauncherData::description() const
 
88
{
 
89
    return m_description;
 
90
}
 
91
 
 
92
QString LauncherData::icon() const
 
93
{
 
94
    return m_icon;
 
95
}
 
96
 
 
97
bool LauncherData::operator==(const LauncherData& other) const
 
98
{
 
99
    return
 
100
        m_url == other.m_url &&
 
101
        m_name == other.m_name &&
 
102
        m_description == other.m_description &&
 
103
        m_icon == other.m_icon;
 
104
}
 
105
 
 
106
bool LauncherData::operator!=(const LauncherData& other) const
 
107
{
 
108
    return !(*this == other);
 
109
}
 
110
 
 
111
void LauncherData::populateMimeData(QMimeData *mimeData)
 
112
{
 
113
    // Use the bookmarks API to do the heavy lifting
 
114
    KBookmark::List bookmarkList;
 
115
 
 
116
    KBookmark bookmark =
 
117
        KBookmark::standaloneBookmark(m_name, m_url, m_icon);
 
118
 
 
119
    bookmark.setDescription(m_description);
 
120
 
 
121
    bookmarkList.append(bookmark);
 
122
    bookmarkList.populateMimeData(mimeData);
 
123
}
 
124
 
 
125
bool LauncherData::canDecode(const QMimeData *mimeData)
 
126
{
 
127
    if (KBookmark::List::canDecode(mimeData)) {
 
128
 
 
129
        QDomDocument tempDoc;
 
130
        return hasUrls(
 
131
            KBookmark::List::fromMimeData(mimeData, tempDoc));
 
132
    }
 
133
 
 
134
    // TODO: Maybe allow text/plain as well if it looks like a URL
 
135
    return false;
 
136
}
 
137
 
 
138
QList<LauncherData> LauncherData::fromMimeData(const QMimeData *mimeData)
 
139
{
 
140
    QList<LauncherData> data;
 
141
 
 
142
    if (KBookmark::List::canDecode(mimeData)) {
 
143
 
 
144
        QDomDocument tempDoc;
 
145
        QList<KUrl> urlList =
 
146
            extractUrls(KBookmark::List::fromMimeData(mimeData, tempDoc));
 
147
 
 
148
        Q_FOREACH(const KUrl& url, urlList) {
 
149
            data.append(LauncherData(url));
 
150
        }
 
151
    }
 
152
 
 
153
    // TODO: Maybe allow text/plain as well if it looks like a URL
 
154
 
 
155
    return data;
 
156
}
 
157
 
 
158
bool LauncherData::hasUrls(const QList<KBookmark> &bookmarkList)
 
159
{
 
160
    Q_FOREACH(const KBookmark& bookmark, bookmarkList) {
 
161
        if (bookmark.isGroup() && hasUrls(bookmark.toGroup())) {
 
162
            return true;
 
163
        }
 
164
        else if (!bookmark.isSeparator() && !bookmark.isNull()) {
 
165
            return true;
 
166
        }
 
167
    }
 
168
 
 
169
    return false;
 
170
}
 
171
 
 
172
bool LauncherData::hasUrls(const KBookmarkGroup &bookmarkGroup)
 
173
{
 
174
    for (KBookmark bookmark = bookmarkGroup.first();;
 
175
         bookmark = bookmarkGroup.next(bookmark)) {
 
176
 
 
177
        if (bookmark.isNull()) {
 
178
            break;
 
179
        }
 
180
        else if (bookmark.isGroup() && hasUrls(bookmark.toGroup())) {
 
181
            return true;
 
182
        }
 
183
        else if (!bookmark.isSeparator()) {
 
184
            return true;
 
185
        }
 
186
    }
 
187
 
 
188
    return false;
 
189
}
 
190
 
 
191
QList<KUrl> LauncherData::extractUrls(const QList<KBookmark> &bookmarkList)
 
192
{
 
193
    QList<KUrl> urlList;
 
194
 
 
195
    Q_FOREACH(const KBookmark& bookmark, bookmarkList) {
 
196
        if (bookmark.isGroup()) {
 
197
            urlList.append(extractUrls(bookmark.toGroup()));
 
198
        } else if (!bookmark.isSeparator()) {
 
199
            urlList.append(bookmark.url());
 
200
        }
 
201
    }
 
202
 
 
203
    return urlList;
 
204
}
 
205
 
 
206
QList<KUrl> LauncherData::extractUrls(const KBookmarkGroup &bookmarkGroup)
 
207
{
 
208
    QList<KUrl> urlList;
 
209
 
 
210
    for (KBookmark bookmark = bookmarkGroup.first();;
 
211
         bookmark = bookmarkGroup.next(bookmark)) {
 
212
 
 
213
        if (bookmark.isNull()) {
 
214
            break;
 
215
        }
 
216
        else if (bookmark.isGroup()) {
 
217
            urlList.append(extractUrls(bookmark.toGroup()));
 
218
        }
 
219
        else if (!bookmark.isSeparator()) {
 
220
            urlList.append(bookmark.url());
 
221
        }
 
222
    }
 
223
    return urlList;
 
224
}
 
225
}