1
/***************************************************************************
2
* Copyright (C) 2008 by Lukas Appelhans <l.appelhans@gmx.de> *
3
* Copyright (C) 2010 - 2011 by Ingomar Wesp <ingomar@wesp.name> *
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. *
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. *
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"
25
#include <QtCore/QList>
26
#include <QtCore/QString>
27
#include <QtXml/QDomDocument>
31
#include <KBookmarkGroup>
32
#include <KDesktopFile>
36
namespace Quicklaunch {
38
LauncherData::LauncherData(const KUrl& url)
40
// KUrl::url() takes care of improperly escaped characters and
41
// resolves pure paths into file:/// URLs
47
if (m_url.isLocalFile() &&
48
KDesktopFile::isDesktopFile(m_url.toLocalFile())) {
50
KDesktopFile f(m_url.toLocalFile());
52
m_name = f.readName();
53
m_description = f.readGenericName();
54
m_icon = f.readIcon();
57
m_icon = KMimeType::iconNameForUrl(m_url);
60
if (m_name.isNull()) {
61
m_name = m_url.fileName();
64
if (m_icon.isNull()) {
69
LauncherData::LauncherData()
77
KUrl LauncherData::url() const
82
QString LauncherData::name() const
87
QString LauncherData::description() const
92
QString LauncherData::icon() const
97
bool LauncherData::operator==(const LauncherData& other) const
100
m_url == other.m_url &&
101
m_name == other.m_name &&
102
m_description == other.m_description &&
103
m_icon == other.m_icon;
106
bool LauncherData::operator!=(const LauncherData& other) const
108
return !(*this == other);
111
void LauncherData::populateMimeData(QMimeData *mimeData)
113
// Use the bookmarks API to do the heavy lifting
114
KBookmark::List bookmarkList;
117
KBookmark::standaloneBookmark(m_name, m_url, m_icon);
119
bookmark.setDescription(m_description);
121
bookmarkList.append(bookmark);
122
bookmarkList.populateMimeData(mimeData);
125
bool LauncherData::canDecode(const QMimeData *mimeData)
127
if (KBookmark::List::canDecode(mimeData)) {
129
QDomDocument tempDoc;
131
KBookmark::List::fromMimeData(mimeData, tempDoc));
134
// TODO: Maybe allow text/plain as well if it looks like a URL
138
QList<LauncherData> LauncherData::fromMimeData(const QMimeData *mimeData)
140
QList<LauncherData> data;
142
if (KBookmark::List::canDecode(mimeData)) {
144
QDomDocument tempDoc;
145
QList<KUrl> urlList =
146
extractUrls(KBookmark::List::fromMimeData(mimeData, tempDoc));
148
Q_FOREACH(const KUrl& url, urlList) {
149
data.append(LauncherData(url));
153
// TODO: Maybe allow text/plain as well if it looks like a URL
158
bool LauncherData::hasUrls(const QList<KBookmark> &bookmarkList)
160
Q_FOREACH(const KBookmark& bookmark, bookmarkList) {
161
if (bookmark.isGroup() && hasUrls(bookmark.toGroup())) {
164
else if (!bookmark.isSeparator() && !bookmark.isNull()) {
172
bool LauncherData::hasUrls(const KBookmarkGroup &bookmarkGroup)
174
for (KBookmark bookmark = bookmarkGroup.first();;
175
bookmark = bookmarkGroup.next(bookmark)) {
177
if (bookmark.isNull()) {
180
else if (bookmark.isGroup() && hasUrls(bookmark.toGroup())) {
183
else if (!bookmark.isSeparator()) {
191
QList<KUrl> LauncherData::extractUrls(const QList<KBookmark> &bookmarkList)
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());
206
QList<KUrl> LauncherData::extractUrls(const KBookmarkGroup &bookmarkGroup)
210
for (KBookmark bookmark = bookmarkGroup.first();;
211
bookmark = bookmarkGroup.next(bookmark)) {
213
if (bookmark.isNull()) {
216
else if (bookmark.isGroup()) {
217
urlList.append(extractUrls(bookmark.toGroup()));
219
else if (!bookmark.isSeparator()) {
220
urlList.append(bookmark.url());