1 /* This file is part of the KDE project
2 Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "kfileplacesitem_p.h"
21 #include "kfileplacesmodel.h"
23 #include <QtCore/QDateTime>
25 #include <kbookmarkmanager.h>
26 #include <kiconloader.h>
27 #include <kdirlister.h>
28 #include <solid/opticaldisc.h>
29 #include <solid/storageaccess.h>
30 #include <solid/storagevolume.h>
33 KFilePlacesItem::KFilePlacesItem(KBookmarkManager
*manager
,
34 const QString
&address
,
36 : m_manager(manager
), m_lister(0), m_folderIsEmpty(true)
38 m_bookmark
= m_manager
->findByAddress(address
);
40 if (udi
.isEmpty() && m_bookmark
.metaDataItem("ID").isEmpty()) {
41 m_bookmark
.setMetaDataItem("ID", generateNewId());
42 } else if (udi
.isEmpty()) {
43 if (hasFullIcon(m_bookmark
)) {
44 // TODO if this is only for the trash, it would be much faster to just read trashrc
45 m_lister
= new KDirLister(this);
46 m_lister
->setAutoErrorHandlingEnabled(false, 0); // don't bother the user if trash:/ doesn't exist
47 m_lister
->setDelayedMimeTypes(true); // we don't need the mimetypes, so don't penalize other KDirLister users
48 connect(m_lister
, SIGNAL(completed()),
49 this, SLOT(onListerCompleted()));
50 m_lister
->openUrl(m_bookmark
.url());
52 } else if (!udi
.isEmpty()) {
53 Solid::Device
dev(udi
);
54 Solid::StorageAccess
*access
= dev
.as
<Solid::StorageAccess
>();
56 connect(access
, SIGNAL(accessibilityChanged(bool, const QString
&)),
57 this, SLOT(onAccessibilityChanged()));
62 KFilePlacesItem::~KFilePlacesItem()
66 QString
KFilePlacesItem::id() const
69 return bookmark().metaDataItem("UDI");
71 return bookmark().metaDataItem("ID");
75 bool KFilePlacesItem::isDevice() const
77 return !bookmark().metaDataItem("UDI").isEmpty();
80 KBookmark
KFilePlacesItem::bookmark() const
85 void KFilePlacesItem::setBookmark(const KBookmark
&bookmark
)
87 m_bookmark
= bookmark
;
90 Solid::Device
KFilePlacesItem::device() const
92 return Solid::Device(bookmark().metaDataItem("UDI"));
95 QVariant
KFilePlacesItem::data(int role
) const
99 if (role
!=KFilePlacesModel::HiddenRole
&& role
!=Qt::BackgroundRole
&& isDevice()) {
100 returnData
= deviceData(role
);
102 returnData
= bookmarkData(role
);
108 QVariant
KFilePlacesItem::bookmarkData(int role
) const
110 KBookmark b
= bookmark();
112 if (b
.isNull()) return QVariant();
116 case Qt::DisplayRole
:
118 case Qt::DecorationRole
:
119 return KIcon(iconNameForBookmark(b
));
120 case Qt::BackgroundRole
:
121 if (b
.metaDataItem("IsHidden")=="true") {
122 return Qt::lightGray
;
126 case KFilePlacesModel::UrlRole
:
127 return QUrl(b
.url());
128 case KFilePlacesModel::SetupNeededRole
:
130 case KFilePlacesModel::HiddenRole
:
131 return b
.metaDataItem("IsHidden")=="true";
137 QVariant
KFilePlacesItem::deviceData(int role
) const
139 Solid::Device d
= device();
142 const Solid::StorageAccess
*access
= d
.as
<Solid::StorageAccess
>();
143 const Solid::StorageVolume
*volume
= d
.as
<Solid::StorageVolume
>();
144 const Solid::OpticalDisc
*disc
= d
.as
<Solid::OpticalDisc
>();
145 QStringList overlays
;
149 case Qt::DisplayRole
:
151 case Qt::DecorationRole
:
152 if (access
&& access
->isAccessible()) {
153 overlays
<< "emblem-mounted";
155 overlays
<< QString(); // We have to guarantee the placement of the next emblem
157 if (volume
&& volume
->usage()==Solid::StorageVolume::Encrypted
) {
158 overlays
<< "security-high";
160 return KIcon(d
.icon(), 0, overlays
);
161 case KFilePlacesModel::UrlRole
:
163 return QUrl(KUrl(access
->filePath()));
164 } else if (disc
&& (disc
->availableContent() & Solid::OpticalDisc::Audio
)!=0) {
165 return QUrl("audiocd:/");
169 case KFilePlacesModel::SetupNeededRole
:
171 return !access
->isAccessible();
183 KBookmark
KFilePlacesItem::createBookmark(KBookmarkManager
*manager
,
184 const QString
&label
,
186 const QString
&iconName
)
188 KBookmarkGroup root
= manager
->root();
191 QString empty_icon
= iconName
;
192 if (url
==KUrl("trash:/")) {
193 if (empty_icon
.endsWith("-full")) {
195 } else if (empty_icon
.isEmpty()) {
196 empty_icon
= "user-trash";
199 KBookmark bookmark
= root
.addBookmark(label
, url
, empty_icon
);
200 bookmark
.setMetaDataItem("ID", generateNewId());
205 KBookmark
KFilePlacesItem::createSystemBookmark(KBookmarkManager
*manager
,
206 const QString
&label
,
208 const QString
&iconName
)
210 KBookmark bookmark
= createBookmark(manager
, label
, url
, iconName
);
211 if (!bookmark
.isNull())
212 bookmark
.setMetaDataItem("isSystemItem", "true");
217 KBookmark
KFilePlacesItem::createDeviceBookmark(KBookmarkManager
*manager
,
220 KBookmarkGroup root
= manager
->root();
223 KBookmark bookmark
= root
.createNewSeparator();
224 bookmark
.setMetaDataItem("UDI", udi
);
225 bookmark
.setMetaDataItem("isSystemItem", "true");
229 QString
KFilePlacesItem::generateNewId()
231 static int count
= 0;
233 // return QString::number(count++);
235 return QString::number(QDateTime::currentDateTime().toTime_t())
236 + '/' + QString::number(count
++);
239 // return QString::number(QDateTime::currentDateTime().toTime_t())
240 // + '/' + QString::number(qrand());
243 void KFilePlacesItem::onAccessibilityChanged()
245 emit
itemChanged(id());
248 bool KFilePlacesItem::hasFullIcon(const KBookmark
&bookmark
) const
250 return bookmark
.url()==KUrl("trash:/");
253 QString
KFilePlacesItem::iconNameForBookmark(const KBookmark
&bookmark
) const
255 if (!m_folderIsEmpty
&& hasFullIcon(bookmark
)) {
256 return bookmark
.icon()+"-full";
258 return bookmark
.icon();
262 void KFilePlacesItem::onListerCompleted()
264 m_folderIsEmpty
= m_lister
->items().isEmpty();
265 emit
itemChanged(id());
268 #include "kfileplacesitem_p.moc"