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