• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

KFile

  • kfile
kfileplacesitem.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
3 
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.
7 
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.
12 
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.
17 
18 */
19 
20 #include "kfileplacesitem_p.h"
21 #include "kfileplacesmodel.h"
22 
23 #include <QtCore/QDateTime>
24 
25 #include <kbookmarkmanager.h>
26 #include <kiconloader.h>
27 #include <kdirlister.h>
28 #include <klocale.h>
29 #include <solid/block.h>
30 #include <solid/opticaldisc.h>
31 #include <solid/storageaccess.h>
32 #include <solid/storagevolume.h>
33 #include <solid/storagedrive.h>
34 
35 
36 KFilePlacesItem::KFilePlacesItem(KBookmarkManager *manager,
37  const QString &address,
38  const QString &udi)
39  : m_manager(manager), m_lister(0), m_folderIsEmpty(true), m_device(udi)
40 {
41  setBookmark(m_manager->findByAddress(address));
42 
43  if (udi.isEmpty() && m_bookmark.metaDataItem("ID").isEmpty()) {
44  m_bookmark.setMetaDataItem("ID", generateNewId());
45  } else if (udi.isEmpty()) {
46  if (hasFullIcon(m_bookmark)) {
47  // TODO if this is only for the trash, it would be much faster to just read trashrc
48  m_lister = new KDirLister(this);
49  m_lister->setAutoErrorHandlingEnabled(false, 0); // don't bother the user if trash:/ doesn't exist
50  m_lister->setDelayedMimeTypes(true); // we don't need the mimetypes, so don't penalize other KDirLister users
51  connect(m_lister, SIGNAL(completed()),
52  this, SLOT(onListerCompleted()));
53  m_lister->openUrl(m_bookmark.url());
54  }
55  } else if (!udi.isEmpty() && m_device.isValid()) {
56  m_access = m_device.as<Solid::StorageAccess>();
57  m_volume = m_device.as<Solid::StorageVolume>();
58  m_disc = m_device.as<Solid::OpticalDisc>();
59  if (m_access) {
60  connect(m_access, SIGNAL(accessibilityChanged(bool,QString)),
61  this, SLOT(onAccessibilityChanged()));
62  }
63  }
64 }
65 
66 KFilePlacesItem::~KFilePlacesItem()
67 {
68 }
69 
70 QString KFilePlacesItem::id() const
71 {
72  if (isDevice()) {
73  return bookmark().metaDataItem("UDI");
74  } else {
75  return bookmark().metaDataItem("ID");
76  }
77 }
78 
79 bool KFilePlacesItem::isDevice() const
80 {
81  return !bookmark().metaDataItem("UDI").isEmpty();
82 }
83 
84 KBookmark KFilePlacesItem::bookmark() const
85 {
86  return m_bookmark;
87 }
88 
89 void KFilePlacesItem::setBookmark(const KBookmark &bookmark)
90 {
91  m_bookmark = bookmark;
92 
93  if (bookmark.metaDataItem("isSystemItem") == "true") {
94  // This context must stay as it is - the translated system bookmark names
95  // are created with 'KFile System Bookmarks' as their context, so this
96  // ensures the right string is picked from the catalog.
97  // (coles, 13th May 2009)
98 
99  m_text = i18nc("KFile System Bookmarks", bookmark.text().toUtf8().data());
100  } else {
101  m_text = bookmark.text();
102  }
103 }
104 
105 Solid::Device KFilePlacesItem::device() const
106 {
107  if (m_device.udi().isEmpty()) {
108  m_device = Solid::Device(bookmark().metaDataItem("UDI"));
109  if (m_device.isValid()) {
110  m_access = m_device.as<Solid::StorageAccess>();
111  m_volume = m_device.as<Solid::StorageVolume>();
112  m_disc = m_device.as<Solid::OpticalDisc>();
113  } else {
114  m_access = 0;
115  m_volume = 0;
116  m_disc = 0;
117  }
118  }
119  return m_device;
120 }
121 
122 QVariant KFilePlacesItem::data(int role) const
123 {
124  QVariant returnData;
125 
126  if (role!=KFilePlacesModel::HiddenRole && role!=Qt::BackgroundRole && isDevice()) {
127  returnData = deviceData(role);
128  } else {
129  returnData = bookmarkData(role);
130  }
131 
132  return returnData;
133 }
134 
135 QVariant KFilePlacesItem::bookmarkData(int role) const
136 {
137  KBookmark b = bookmark();
138 
139  if (b.isNull()) return QVariant();
140 
141  switch (role)
142  {
143  case Qt::DisplayRole:
144  return m_text;
145  case Qt::DecorationRole:
146  return KIcon(iconNameForBookmark(b));
147  case Qt::BackgroundRole:
148  if (b.metaDataItem("IsHidden")=="true") {
149  return Qt::lightGray;
150  } else {
151  return QVariant();
152  }
153  case KFilePlacesModel::UrlRole:
154  return QUrl(b.url());
155  case KFilePlacesModel::SetupNeededRole:
156  return false;
157  case KFilePlacesModel::HiddenRole:
158  return b.metaDataItem("IsHidden")=="true";
159  default:
160  return QVariant();
161  }
162 }
163 
164 QVariant KFilePlacesItem::deviceData(int role) const
165 {
166  Solid::Device d = device();
167 
168  if (d.isValid()) {
169  switch (role)
170  {
171  case Qt::DisplayRole:
172  return d.description();
173  case Qt::DecorationRole:
174  return KIcon(d.icon(), 0, d.emblems());
175  case KFilePlacesModel::UrlRole:
176  if (m_access) {
177  return QUrl(KUrl(m_access->filePath()));
178  } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio)!=0) {
179  QString device = d.as<Solid::Block>()->device();
180  return QUrl(QString("audiocd:/?device=%1").arg(device));
181  } else {
182  return QVariant();
183  }
184  case KFilePlacesModel::SetupNeededRole:
185  if (m_access) {
186  return !m_access->isAccessible();
187  } else {
188  return QVariant();
189  }
190 
191  case KFilePlacesModel::FixedDeviceRole:
192  {
193  Solid::StorageDrive *drive = 0;
194  Solid::Device parentDevice = m_device;
195  while (parentDevice.isValid() && !drive) {
196  drive = parentDevice.as<Solid::StorageDrive>();
197  parentDevice = parentDevice.parent();
198  }
199  if (drive!=0) {
200  return !drive->isHotpluggable() && !drive->isRemovable();
201  }
202  return true;
203  }
204 
205  case KFilePlacesModel::CapacityBarRecommendedRole:
206  {
207  bool accessible = m_access && m_access->isAccessible();
208  bool isCdrom =
209  ((m_device.is<Solid::StorageDrive>()
210  && m_device.as<Solid::StorageDrive>()->driveType() == Solid::StorageDrive::CdromDrive)
211  || (m_device.parent().is<Solid::StorageDrive>()
212  && m_device.parent().as<Solid::StorageDrive>()->driveType() == Solid::StorageDrive::CdromDrive));
213 
214  return accessible && !isCdrom;
215  }
216 
217  default:
218  return QVariant();
219  }
220  } else {
221  return QVariant();
222  }
223 }
224 
225 KBookmark KFilePlacesItem::createBookmark(KBookmarkManager *manager,
226  const QString &label,
227  const KUrl &url,
228  const QString &iconName,
229  KFilePlacesItem *after)
230 {
231  KBookmarkGroup root = manager->root();
232  if (root.isNull())
233  return KBookmark();
234  QString empty_icon = iconName;
235  if (url==KUrl("trash:/")) {
236  if (empty_icon.endsWith(QLatin1String("-full"))) {
237  empty_icon.chop(5);
238  } else if (empty_icon.isEmpty()) {
239  empty_icon = "user-trash";
240  }
241  }
242  KBookmark bookmark = root.addBookmark(label, url, empty_icon);
243  bookmark.setMetaDataItem("ID", generateNewId());
244 
245  if (after) {
246  root.moveBookmark(bookmark, after->bookmark());
247  }
248 
249  return bookmark;
250 }
251 
252 KBookmark KFilePlacesItem::createSystemBookmark(KBookmarkManager *manager,
253  const QString &untranslatedLabel,
254  const QString &translatedLabel,
255  const KUrl &url,
256  const QString &iconName)
257 {
258  Q_UNUSED(translatedLabel); // parameter is only necessary to force the caller
259  // providing a translated string for the label
260 
261  KBookmark bookmark = createBookmark(manager, untranslatedLabel, url, iconName);
262  if (!bookmark.isNull())
263  bookmark.setMetaDataItem("isSystemItem", "true");
264  return bookmark;
265 }
266 
267 
268 KBookmark KFilePlacesItem::createDeviceBookmark(KBookmarkManager *manager,
269  const QString &udi)
270 {
271  KBookmarkGroup root = manager->root();
272  if (root.isNull())
273  return KBookmark();
274  KBookmark bookmark = root.createNewSeparator();
275  bookmark.setMetaDataItem("UDI", udi);
276  bookmark.setMetaDataItem("isSystemItem", "true");
277  return bookmark;
278 }
279 
280 QString KFilePlacesItem::generateNewId()
281 {
282  static int count = 0;
283 
284 // return QString::number(count++);
285 
286  return QString::number(QDateTime::currentDateTime().toTime_t())
287  + '/' + QString::number(count++);
288 
289 
290 // return QString::number(QDateTime::currentDateTime().toTime_t())
291 // + '/' + QString::number(qrand());
292 }
293 
294 void KFilePlacesItem::onAccessibilityChanged()
295 {
296  emit itemChanged(id());
297 }
298 
299 bool KFilePlacesItem::hasFullIcon(const KBookmark &bookmark) const
300 {
301  return bookmark.url()==KUrl("trash:/");
302 }
303 
304 QString KFilePlacesItem::iconNameForBookmark(const KBookmark &bookmark) const
305 {
306  if (!m_folderIsEmpty && hasFullIcon(bookmark)) {
307  return bookmark.icon()+"-full";
308  } else {
309  return bookmark.icon();
310  }
311 }
312 
313 void KFilePlacesItem::onListerCompleted()
314 {
315  m_folderIsEmpty = m_lister->items().isEmpty();
316  emit itemChanged(id());
317 }
318 
319 #include "kfileplacesitem_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Thu Feb 21 2013 11:20:52 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KFile

Skip menu "KFile"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal