• Skip to content
  • Skip to link menu
KDE 4.1 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 <solid/storageaccess.h>
00029 #include <solid/storagevolume.h>
00030 
00031 
00032 KFilePlacesItem::KFilePlacesItem(KBookmarkManager *manager,
00033                                  const QString &address,
00034                                  const QString &udi)
00035     : m_manager(manager), m_lister(0), m_folderIsEmpty(true)
00036 {
00037     m_bookmark = m_manager->findByAddress(address);
00038 
00039     if (udi.isEmpty() && m_bookmark.metaDataItem("ID").isEmpty()) {
00040         m_bookmark.setMetaDataItem("ID", generateNewId());
00041     } else if (udi.isEmpty()) {
00042         if (hasFullIcon(m_bookmark)) {
00043             // TODO if this is only for the trash, it would be much faster to just read trashrc
00044             m_lister = new KDirLister(this);
00045             m_lister->setAutoErrorHandlingEnabled(false, 0); // don't bother the user if trash:/ doesn't exist
00046             m_lister->setDelayedMimeTypes(true); // we don't need the mimetypes, so don't penalize other KDirLister users
00047             connect(m_lister, SIGNAL(completed()),
00048                     this, SLOT(onListerCompleted()));
00049             m_lister->openUrl(m_bookmark.url());
00050         }
00051     } else if (!udi.isEmpty()) {
00052         Solid::Device dev(udi);
00053         connect(dev.as<Solid::StorageAccess>(), SIGNAL(accessibilityChanged(bool, const QString &)),
00054                 this, SLOT(onAccessibilityChanged()));
00055     }
00056 }
00057 
00058 KFilePlacesItem::~KFilePlacesItem()
00059 {
00060 }
00061 
00062 QString KFilePlacesItem::id() const
00063 {
00064     if (isDevice()) {
00065         return bookmark().metaDataItem("UDI");
00066     } else {
00067         return bookmark().metaDataItem("ID");
00068     }
00069 }
00070 
00071 bool KFilePlacesItem::isDevice() const
00072 {
00073     return !bookmark().metaDataItem("UDI").isEmpty();
00074 }
00075 
00076 KBookmark KFilePlacesItem::bookmark() const
00077 {
00078     return m_bookmark;
00079 }
00080 
00081 void KFilePlacesItem::setBookmark(const KBookmark &bookmark)
00082 {
00083     m_bookmark = bookmark;
00084 }
00085 
00086 Solid::Device KFilePlacesItem::device() const
00087 {
00088     return Solid::Device(bookmark().metaDataItem("UDI"));
00089 }
00090 
00091 QVariant KFilePlacesItem::data(int role) const
00092 {
00093     QVariant returnData;
00094 
00095     if (role!=KFilePlacesModel::HiddenRole && role!=Qt::BackgroundRole && isDevice()) {
00096         returnData = deviceData(role);
00097     } else {
00098         returnData = bookmarkData(role);
00099     }
00100 
00101     return returnData;
00102 }
00103 
00104 QVariant KFilePlacesItem::bookmarkData(int role) const
00105 {
00106     KBookmark b = bookmark();
00107 
00108     if (b.isNull()) return QVariant();
00109 
00110     switch (role)
00111     {
00112     case Qt::DisplayRole:
00113         return b.text();
00114     case Qt::DecorationRole:
00115         return KIcon(iconNameForBookmark(b));
00116     case Qt::BackgroundRole:
00117         if (b.metaDataItem("IsHidden")=="true") {
00118             return Qt::lightGray;
00119         } else {
00120             return QVariant();
00121         }
00122     case KFilePlacesModel::UrlRole:
00123         return QUrl(b.url());
00124     case KFilePlacesModel::SetupNeededRole:
00125         return false;
00126     case KFilePlacesModel::HiddenRole:
00127         return b.metaDataItem("IsHidden")=="true";
00128     default:
00129         return QVariant();
00130     }
00131 }
00132 
00133 QVariant KFilePlacesItem::deviceData(int role) const
00134 {
00135     Solid::Device d = device();
00136 
00137     if (d.isValid()) {
00138         const Solid::StorageAccess *access = d.as<Solid::StorageAccess>();
00139         const Solid::StorageVolume *volume = d.as<Solid::StorageVolume>();
00140         QStringList overlays;
00141 
00142         switch (role)
00143         {
00144         case Qt::DisplayRole:
00145             return d.product();
00146         case Qt::DecorationRole:
00147             if (access->isAccessible()) {
00148                 overlays << "emblem-mounted";
00149             } else {
00150                 overlays << QString(); // We have to guarantee the placement of the next emblem
00151             }
00152             if (volume && volume->usage()==Solid::StorageVolume::Encrypted) {
00153                 overlays << "security-high";
00154             }
00155             return KIcon(d.icon(), 0, overlays);
00156         case KFilePlacesModel::UrlRole:
00157             if (access) {
00158                 return QUrl(KUrl(access->filePath()));
00159             } else {
00160                 return QVariant();
00161             }
00162         case KFilePlacesModel::SetupNeededRole:
00163             if (access) {
00164                 return !access->isAccessible();
00165             } else {
00166                 return QVariant();
00167             }
00168         default:
00169             return QVariant();
00170         }
00171     } else {
00172         return QVariant();
00173     }
00174 }
00175 
00176 KBookmark KFilePlacesItem::createBookmark(KBookmarkManager *manager,
00177                                           const QString &label,
00178                                           const KUrl &url,
00179                                           const QString &iconName)
00180 {
00181     KBookmarkGroup root = manager->root();
00182     if (root.isNull())
00183         return KBookmark();
00184     KBookmark bookmark = root.addBookmark(label, url, iconName);
00185     bookmark.setMetaDataItem("ID", generateNewId());
00186 
00187     return bookmark;
00188 }
00189 
00190 KBookmark KFilePlacesItem::createSystemBookmark(KBookmarkManager *manager,
00191                                           const QString &label,
00192                                           const KUrl &url,
00193                                           const QString &iconName)
00194 {
00195     KBookmark bookmark = createBookmark(manager, label, url, iconName);
00196     if (!bookmark.isNull())
00197         bookmark.setMetaDataItem("isSystemItem", "true");
00198     return bookmark;
00199 }
00200 
00201 
00202 KBookmark KFilePlacesItem::createDeviceBookmark(KBookmarkManager *manager,
00203                                                 const QString &udi)
00204 {
00205     KBookmarkGroup root = manager->root();
00206     if (root.isNull())
00207         return KBookmark();
00208     KBookmark bookmark = root.createNewSeparator();
00209     bookmark.setMetaDataItem("UDI", udi);
00210     bookmark.setMetaDataItem("isSystemItem", "true");    
00211     return bookmark;
00212 }
00213 
00214 QString KFilePlacesItem::generateNewId()
00215 {
00216     static int count = 0;
00217 
00218 //    return QString::number(count++);
00219     
00220     return QString::number(QDateTime::currentDateTime().toTime_t())
00221       + '/' + QString::number(count++);
00222     
00223 
00224 //    return QString::number(QDateTime::currentDateTime().toTime_t())
00225 //         + '/' + QString::number(qrand());
00226 }
00227 
00228 void KFilePlacesItem::onAccessibilityChanged()
00229 {
00230     emit itemChanged(id());
00231 }
00232 
00233 bool KFilePlacesItem::hasFullIcon(const KBookmark &bookmark) const
00234 {
00235     return bookmark.url()==KUrl("trash:/");
00236 }
00237 
00238 QString KFilePlacesItem::iconNameForBookmark(const KBookmark &bookmark) const
00239 {
00240     if (!m_folderIsEmpty && hasFullIcon(bookmark)) {
00241         return bookmark.icon()+"-full";
00242     } else {
00243         return bookmark.icon();
00244     }
00245 }
00246 
00247 void KFilePlacesItem::onListerCompleted()
00248 {
00249     m_folderIsEmpty = m_lister->items().isEmpty();
00250     emit itemChanged(id());
00251 }
00252 
00253 #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
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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