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

KFile

kfileplacesitem.cpp
Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 
00018 */
00019 
00020 #include "kfileplacesitem_p.h"
00021 #include "kfileplacesmodel.h"
00022 
00023 #include <QtCore/QDateTime>
00024 
00025 #include <kbookmarkmanager.h>
00026 #include <kiconloader.h>
00027 #include <kdirlister.h>
00028 #include <klocale.h>
00029 #include <solid/block.h>
00030 #include <solid/opticaldisc.h>
00031 #include <solid/opticaldrive.h>
00032 #include <solid/storageaccess.h>
00033 #include <solid/storagevolume.h>
00034 #include <solid/storagedrive.h>
00035 #include <solid/portablemediaplayer.h>
00036 
00037 
00038 KFilePlacesItem::KFilePlacesItem(KBookmarkManager *manager,
00039                                  const QString &address,
00040                                  const QString &udi)
00041     : m_manager(manager), m_lister(0), m_folderIsEmpty(true), m_isCdrom(false),
00042       m_isAccessible(false), m_device(udi)
00043 {
00044     setBookmark(m_manager->findByAddress(address));
00045 
00046     if (udi.isEmpty() && m_bookmark.metaDataItem("ID").isEmpty()) {
00047         m_bookmark.setMetaDataItem("ID", generateNewId());
00048     } else if (udi.isEmpty()) {
00049         if (hasFullIcon(m_bookmark)) {
00050             // TODO if this is only for the trash, it would be much faster to just read trashrc
00051             m_lister = new KDirLister(this);
00052             m_lister->setAutoErrorHandlingEnabled(false, 0); // don't bother the user if trash:/ doesn't exist
00053             m_lister->setDelayedMimeTypes(true); // we don't need the mimetypes, so don't penalize other KDirLister users
00054             connect(m_lister, SIGNAL(completed()),
00055                     this, SLOT(onListerCompleted()));
00056             m_lister->openUrl(m_bookmark.url());
00057         }
00058     } else if (!udi.isEmpty() && m_device.isValid()) {
00059         m_access = m_device.as<Solid::StorageAccess>();
00060         m_volume = m_device.as<Solid::StorageVolume>();
00061         m_disc = m_device.as<Solid::OpticalDisc>();
00062         m_mtp = m_device.as<Solid::PortableMediaPlayer>();
00063         if (m_access) {
00064             connect(m_access, SIGNAL(accessibilityChanged(bool,QString)),
00065                     this, SLOT(onAccessibilityChanged(bool)));
00066         onAccessibilityChanged(m_access->isAccessible());
00067         }
00068         m_iconPath = m_device.icon();
00069     m_emblems = m_device.emblems();
00070     }
00071 }
00072 
00073 KFilePlacesItem::~KFilePlacesItem()
00074 {
00075 }
00076 
00077 QString KFilePlacesItem::id() const
00078 {
00079     if (isDevice()) {
00080         return bookmark().metaDataItem("UDI");
00081     } else {
00082         return bookmark().metaDataItem("ID");
00083     }
00084 }
00085 
00086 bool KFilePlacesItem::isDevice() const
00087 {
00088     return !bookmark().metaDataItem("UDI").isEmpty();
00089 }
00090 
00091 KBookmark KFilePlacesItem::bookmark() const
00092 {
00093     return m_bookmark;
00094 }
00095 
00096 void KFilePlacesItem::setBookmark(const KBookmark &bookmark)
00097 {
00098     m_bookmark = bookmark;
00099 
00100     if (bookmark.metaDataItem("isSystemItem") == "true") {
00101         // This context must stay as it is - the translated system bookmark names
00102         // are created with 'KFile System Bookmarks' as their context, so this
00103         // ensures the right string is picked from the catalog.
00104         // (coles, 13th May 2009)
00105 
00106         m_text = i18nc("KFile System Bookmarks", bookmark.text().toUtf8().data());
00107     } else {
00108         m_text = bookmark.text();
00109     }
00110 }
00111 
00112 Solid::Device KFilePlacesItem::device() const
00113 {
00114     if (m_device.udi().isEmpty()) {
00115         m_device = Solid::Device(bookmark().metaDataItem("UDI"));
00116         if (m_device.isValid()) {
00117             m_access = m_device.as<Solid::StorageAccess>();
00118             m_volume = m_device.as<Solid::StorageVolume>();
00119             m_disc = m_device.as<Solid::OpticalDisc>();
00120             m_mtp = m_device.as<Solid::PortableMediaPlayer>();
00121         } else {
00122             m_access = 0;
00123             m_volume = 0;
00124             m_disc = 0;
00125             m_mtp = 0;
00126         }
00127     }
00128     return m_device;
00129 }
00130 
00131 QVariant KFilePlacesItem::data(int role) const
00132 {
00133     QVariant returnData;
00134 
00135     if (role!=KFilePlacesModel::HiddenRole && role!=Qt::BackgroundRole && isDevice()) {
00136         returnData = deviceData(role);
00137     } else {
00138         returnData = bookmarkData(role);
00139     }
00140 
00141     return returnData;
00142 }
00143 
00144 QVariant KFilePlacesItem::bookmarkData(int role) const
00145 {
00146     KBookmark b = bookmark();
00147 
00148     if (b.isNull()) return QVariant();
00149 
00150     switch (role)
00151     {
00152     case Qt::DisplayRole:
00153         return m_text;
00154     case Qt::DecorationRole:
00155         return KIcon(iconNameForBookmark(b));
00156     case Qt::BackgroundRole:
00157         if (b.metaDataItem("IsHidden")=="true") {
00158             return Qt::lightGray;
00159         } else {
00160             return QVariant();
00161         }
00162     case KFilePlacesModel::UrlRole:
00163         return QUrl(b.url());
00164     case KFilePlacesModel::SetupNeededRole:
00165         return false;
00166     case KFilePlacesModel::HiddenRole:
00167         return b.metaDataItem("IsHidden")=="true";
00168     default:
00169         return QVariant();
00170     }
00171 }
00172 
00173 QVariant KFilePlacesItem::deviceData(int role) const
00174 {
00175     Solid::Device d = device();
00176 
00177     if (d.isValid()) {
00178         switch (role)
00179         {
00180         case Qt::DisplayRole:
00181             return d.description();
00182         case Qt::DecorationRole:
00183             return KIcon(m_iconPath, 0, m_emblems);
00184         case KFilePlacesModel::UrlRole:
00185             if (m_access) {
00186                 return QUrl(KUrl(m_access->filePath()));
00187             } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio)!=0) {
00188                 QString device = d.as<Solid::Block>()->device();
00189                 return QUrl(QString("audiocd:/?device=%1").arg(device));
00190             } else if (m_mtp) {
00191                 return QUrl(QString("mtp:udi=%1").arg(d.udi()));
00192             } else {
00193                 return QVariant();
00194             }
00195         case KFilePlacesModel::SetupNeededRole:
00196             if (m_access) {
00197                 return !m_isAccessible;
00198             } else {
00199                 return QVariant();
00200             }
00201 
00202         case KFilePlacesModel::FixedDeviceRole:
00203             {
00204                 Solid::StorageDrive *drive = 0;
00205                 Solid::Device parentDevice = m_device;
00206                 while (parentDevice.isValid() && !drive) {
00207                     drive = parentDevice.as<Solid::StorageDrive>();
00208                     parentDevice = parentDevice.parent();
00209                 }
00210                 if (drive!=0) {
00211                     return !drive->isHotpluggable() && !drive->isRemovable();
00212                 }
00213                 return true;
00214             }
00215 
00216         case KFilePlacesModel::CapacityBarRecommendedRole:
00217         return m_isAccessible && !m_isCdrom;
00218 
00219         default:
00220             return QVariant();
00221         }
00222     } else {
00223         return QVariant();
00224     }
00225 }
00226 
00227 KBookmark KFilePlacesItem::createBookmark(KBookmarkManager *manager,
00228                                           const QString &label,
00229                                           const KUrl &url,
00230                                           const QString &iconName,
00231                                           KFilePlacesItem *after)
00232 {
00233     KBookmarkGroup root = manager->root();
00234     if (root.isNull())
00235         return KBookmark();
00236     QString empty_icon = iconName;
00237     if (url==KUrl("trash:/")) {
00238         if (empty_icon.endsWith(QLatin1String("-full"))) {
00239             empty_icon.chop(5);
00240         } else if (empty_icon.isEmpty()) {
00241             empty_icon = "user-trash";
00242         }
00243     }
00244     KBookmark bookmark = root.addBookmark(label, url, empty_icon);
00245     bookmark.setMetaDataItem("ID", generateNewId());
00246 
00247     if (after) {
00248         root.moveBookmark(bookmark, after->bookmark());
00249     }
00250 
00251     return bookmark;
00252 }
00253 
00254 KBookmark KFilePlacesItem::createSystemBookmark(KBookmarkManager *manager,
00255                                                 const QString &untranslatedLabel,
00256                                                 const QString &translatedLabel,
00257                                                 const KUrl &url,
00258                                                 const QString &iconName)
00259 {
00260     Q_UNUSED(translatedLabel); // parameter is only necessary to force the caller
00261                                // providing a translated string for the label
00262 
00263     KBookmark bookmark = createBookmark(manager, untranslatedLabel, url, iconName);
00264     if (!bookmark.isNull())
00265         bookmark.setMetaDataItem("isSystemItem", "true");
00266     return bookmark;
00267 }
00268 
00269 
00270 KBookmark KFilePlacesItem::createDeviceBookmark(KBookmarkManager *manager,
00271                                                 const QString &udi)
00272 {
00273     KBookmarkGroup root = manager->root();
00274     if (root.isNull())
00275         return KBookmark();
00276     KBookmark bookmark = root.createNewSeparator();
00277     bookmark.setMetaDataItem("UDI", udi);
00278     bookmark.setMetaDataItem("isSystemItem", "true");
00279     return bookmark;
00280 }
00281 
00282 QString KFilePlacesItem::generateNewId()
00283 {
00284     static int count = 0;
00285 
00286 //    return QString::number(count++);
00287 
00288     return QString::number(QDateTime::currentDateTime().toTime_t())
00289       + '/' + QString::number(count++);
00290 
00291 
00292 //    return QString::number(QDateTime::currentDateTime().toTime_t())
00293 //         + '/' + QString::number(qrand());
00294 }
00295 
00296 void KFilePlacesItem::onAccessibilityChanged(bool isAccessible)
00297 {
00298     m_isAccessible = isAccessible;
00299     m_isCdrom = m_device.is<Solid::OpticalDrive>() || m_device.parent().is<Solid::OpticalDrive>();
00300     m_emblems = m_device.emblems();
00301 
00302     emit itemChanged(id());
00303 }
00304 
00305 bool KFilePlacesItem::hasFullIcon(const KBookmark &bookmark) const
00306 {
00307     return bookmark.url()==KUrl("trash:/");
00308 }
00309 
00310 QString KFilePlacesItem::iconNameForBookmark(const KBookmark &bookmark) const
00311 {
00312     if (!m_folderIsEmpty && hasFullIcon(bookmark)) {
00313         return bookmark.icon()+"-full";
00314     } else {
00315         return bookmark.icon();
00316     }
00317 }
00318 
00319 void KFilePlacesItem::onListerCompleted()
00320 {
00321     m_folderIsEmpty = m_lister->items().isEmpty();
00322     emit itemChanged(id());
00323 }
00324 
00325 #include "kfileplacesitem_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:40:57 by doxygen 1.7.5.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.9.5 API Reference

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