• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • 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/opticaldisc.h>
00030 #include <solid/storageaccess.h>
00031 #include <solid/storagevolume.h>
00032 
00033 
00034 KFilePlacesItem::KFilePlacesItem(KBookmarkManager *manager,
00035                                  const QString &address,
00036                                  const QString &udi)
00037     : m_manager(manager), m_lister(0), m_folderIsEmpty(true), m_text()
00038 {
00039     setBookmark(m_manager->findByAddress(address));
00040 
00041     if (udi.isEmpty() && m_bookmark.metaDataItem("ID").isEmpty()) {
00042         m_bookmark.setMetaDataItem("ID", generateNewId());
00043     } else if (udi.isEmpty()) {
00044         if (hasFullIcon(m_bookmark)) {
00045             // TODO if this is only for the trash, it would be much faster to just read trashrc
00046             m_lister = new KDirLister(this);
00047             m_lister->setAutoErrorHandlingEnabled(false, 0); // don't bother the user if trash:/ doesn't exist
00048             m_lister->setDelayedMimeTypes(true); // we don't need the mimetypes, so don't penalize other KDirLister users
00049             connect(m_lister, SIGNAL(completed()),
00050                     this, SLOT(onListerCompleted()));
00051             m_lister->openUrl(m_bookmark.url());
00052         }
00053     } else if (!udi.isEmpty()) {
00054         Solid::Device dev(udi);
00055         Solid::StorageAccess *access = dev.as<Solid::StorageAccess>();
00056         if (access!=0) {
00057             connect(access, SIGNAL(accessibilityChanged(bool, const QString &)),
00058                     this, SLOT(onAccessibilityChanged()));
00059         }
00060     }
00061 }
00062 
00063 KFilePlacesItem::~KFilePlacesItem()
00064 {
00065 }
00066 
00067 QString KFilePlacesItem::id() const
00068 {
00069     if (isDevice()) {
00070         return bookmark().metaDataItem("UDI");
00071     } else {
00072         return bookmark().metaDataItem("ID");
00073     }
00074 }
00075 
00076 bool KFilePlacesItem::isDevice() const
00077 {
00078     return !bookmark().metaDataItem("UDI").isEmpty();
00079 }
00080 
00081 KBookmark KFilePlacesItem::bookmark() const
00082 {
00083     return m_bookmark;
00084 }
00085 
00086 void KFilePlacesItem::setBookmark(const KBookmark &bookmark)
00087 {
00088     m_bookmark = bookmark;
00089 
00090     if (bookmark.metaDataItem("isSystemItem") == "true") {
00091         // This context must stay as it is - the translated system bookmark names
00092         // are created with 'KFile System Bookmarks' as their context, so this
00093         // ensures the right string is picked from the catalogue.
00094         // (coles, 13th May 2009)
00095 
00096         m_text = i18nc("KFile System Bookmarks", bookmark.text().toUtf8().data());
00097     } else {
00098         m_text = bookmark.text();
00099     }
00100 }
00101 
00102 Solid::Device KFilePlacesItem::device() const
00103 {
00104     if (m_device.udi().isEmpty()) {
00105         m_device = Solid::Device(bookmark().metaDataItem("UDI"));
00106         if (m_device.isValid()) {
00107             m_access = m_device.as<Solid::StorageAccess>();
00108             m_volume = m_device.as<Solid::StorageVolume>();
00109             m_disc = m_device.as<Solid::OpticalDisc>();
00110         } else {
00111             m_access = 0;
00112             m_volume = 0;
00113             m_disc = 0;
00114         }
00115     }
00116     return m_device;
00117 }
00118 
00119 QVariant KFilePlacesItem::data(int role) const
00120 {
00121     QVariant returnData;
00122 
00123     if (role!=KFilePlacesModel::HiddenRole && role!=Qt::BackgroundRole && isDevice()) {
00124         returnData = deviceData(role);
00125     } else {
00126         returnData = bookmarkData(role);
00127     }
00128 
00129     return returnData;
00130 }
00131 
00132 QVariant KFilePlacesItem::bookmarkData(int role) const
00133 {
00134     KBookmark b = bookmark();
00135 
00136     if (b.isNull()) return QVariant();
00137 
00138     switch (role)
00139     {
00140     case Qt::DisplayRole:
00141         return m_text;
00142     case Qt::DecorationRole:
00143         return KIcon(iconNameForBookmark(b));
00144     case Qt::BackgroundRole:
00145         if (b.metaDataItem("IsHidden")=="true") {
00146             return Qt::lightGray;
00147         } else {
00148             return QVariant();
00149         }
00150     case KFilePlacesModel::UrlRole:
00151         return QUrl(b.url());
00152     case KFilePlacesModel::SetupNeededRole:
00153         return false;
00154     case KFilePlacesModel::HiddenRole:
00155         return b.metaDataItem("IsHidden")=="true";
00156     default:
00157         return QVariant();
00158     }
00159 }
00160 
00161 QVariant KFilePlacesItem::deviceData(int role) const
00162 {
00163     Solid::Device d = device();
00164 
00165     if (d.isValid()) {
00166         QStringList overlays;
00167 
00168         switch (role)
00169         {
00170         case Qt::DisplayRole:
00171             return d.product();
00172         case Qt::DecorationRole:
00173             if (m_access && m_access->isAccessible()) {
00174                 overlays << "emblem-mounted";
00175             } else {
00176                 overlays << QString(); // We have to guarantee the placement of the next emblem
00177             }
00178             if (m_volume && m_volume->usage()==Solid::StorageVolume::Encrypted) {
00179                 overlays << "security-high";
00180             }
00181             return KIcon(d.icon(), 0, overlays);
00182         case KFilePlacesModel::UrlRole:
00183             if (m_access) {
00184                 return QUrl(KUrl(m_access->filePath()));
00185             } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio)!=0) {
00186                 return QUrl("audiocd:/");
00187             } else {
00188                 return QVariant();
00189             }
00190         case KFilePlacesModel::SetupNeededRole:
00191             if (m_access) {
00192                 return !m_access->isAccessible();
00193             } else {
00194                 return QVariant();
00195             }
00196         default:
00197             return QVariant();
00198         }
00199     } else {
00200         return QVariant();
00201     }
00202 }
00203 
00204 KBookmark KFilePlacesItem::createBookmark(KBookmarkManager *manager,
00205                                           const QString &label,
00206                                           const KUrl &url,
00207                                           const QString &iconName)
00208 {
00209     KBookmarkGroup root = manager->root();
00210     if (root.isNull())
00211         return KBookmark();
00212     QString empty_icon = iconName;
00213     if (url==KUrl("trash:/")) {
00214         if (empty_icon.endsWith("-full")) {
00215             empty_icon.chop(5);
00216         } else if (empty_icon.isEmpty()) {
00217             empty_icon = "user-trash";
00218         }
00219     }
00220     KBookmark bookmark = root.addBookmark(label, url, empty_icon);
00221     bookmark.setMetaDataItem("ID", generateNewId());
00222 
00223     return bookmark;
00224 }
00225 
00226 KBookmark KFilePlacesItem::createSystemBookmark(KBookmarkManager *manager,
00227                                                 const QString &untranslatedLabel,
00228                                                 const QString &translatedLabel,
00229                                                 const KUrl &url,
00230                                                 const QString &iconName)
00231 {
00232     Q_UNUSED(translatedLabel); // parameter is only necessary to force the caller
00233                                // providing a translated string for the label
00234 
00235     KBookmark bookmark = createBookmark(manager, untranslatedLabel, url, iconName);
00236     if (!bookmark.isNull())
00237         bookmark.setMetaDataItem("isSystemItem", "true");
00238     return bookmark;
00239 }
00240 
00241 
00242 KBookmark KFilePlacesItem::createDeviceBookmark(KBookmarkManager *manager,
00243                                                 const QString &udi)
00244 {
00245     KBookmarkGroup root = manager->root();
00246     if (root.isNull())
00247         return KBookmark();
00248     KBookmark bookmark = root.createNewSeparator();
00249     bookmark.setMetaDataItem("UDI", udi);
00250     bookmark.setMetaDataItem("isSystemItem", "true");
00251     return bookmark;
00252 }
00253 
00254 QString KFilePlacesItem::generateNewId()
00255 {
00256     static int count = 0;
00257 
00258 //    return QString::number(count++);
00259 
00260     return QString::number(QDateTime::currentDateTime().toTime_t())
00261       + '/' + QString::number(count++);
00262 
00263 
00264 //    return QString::number(QDateTime::currentDateTime().toTime_t())
00265 //         + '/' + QString::number(qrand());
00266 }
00267 
00268 void KFilePlacesItem::onAccessibilityChanged()
00269 {
00270     emit itemChanged(id());
00271 }
00272 
00273 bool KFilePlacesItem::hasFullIcon(const KBookmark &bookmark) const
00274 {
00275     return bookmark.url()==KUrl("trash:/");
00276 }
00277 
00278 QString KFilePlacesItem::iconNameForBookmark(const KBookmark &bookmark) const
00279 {
00280     if (!m_folderIsEmpty && hasFullIcon(bookmark)) {
00281         return bookmark.icon()+"-full";
00282     } else {
00283         return bookmark.icon();
00284     }
00285 }
00286 
00287 void KFilePlacesItem::onListerCompleted()
00288 {
00289     m_folderIsEmpty = m_lister->items().isEmpty();
00290     emit itemChanged(id());
00291 }
00292 
00293 #include "kfileplacesitem_p.moc"

KFile

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal