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

Skip menu "kdelibs-4.10.2 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