KIO
kfileitemlistproperties.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE project 00002 Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> 00003 Copyright (C) 2008 by George Goldberg <grundleborg@googlemail.com> 00004 Copyright 2009 David Faure <faure@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU Library General Public License as published 00008 by the Free Software Foundation; either version 2 of the License or 00009 ( at your option ) version 3 or, at the discretion of KDE e.V. 00010 ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "kfileitemlistproperties.h" 00024 00025 #include <kfileitem.h> 00026 #include <kprotocolmanager.h> 00027 00028 #include <QFileInfo> 00029 00030 class KFileItemListPropertiesPrivate : public QSharedData 00031 { 00032 public: 00033 KFileItemListPropertiesPrivate() 00034 : m_isDirectory(false), 00035 m_supportsReading(false), 00036 m_supportsDeleting(false), 00037 m_supportsWriting(false), 00038 m_supportsMoving(false), 00039 m_isLocal(true) 00040 { } 00041 void setItems(const KFileItemList& items); 00042 00043 void determineMimeTypeAndGroup() const; 00044 00045 KFileItemList m_items; 00046 KUrl::List m_urlList; 00047 mutable QString m_mimeType; 00048 mutable QString m_mimeGroup; 00049 bool m_isDirectory : 1; 00050 bool m_supportsReading : 1; 00051 bool m_supportsDeleting : 1; 00052 bool m_supportsWriting : 1; 00053 bool m_supportsMoving : 1; 00054 bool m_isLocal : 1; 00055 }; 00056 00057 00058 KFileItemListProperties::KFileItemListProperties() 00059 : d(new KFileItemListPropertiesPrivate) 00060 { 00061 } 00062 00063 KFileItemListProperties::KFileItemListProperties(const KFileItemList& items) 00064 : d(new KFileItemListPropertiesPrivate) 00065 { 00066 setItems(items); 00067 } 00068 00069 void KFileItemListProperties::setItems(const KFileItemList& items) 00070 { 00071 d->setItems(items); 00072 } 00073 00074 void KFileItemListPropertiesPrivate::setItems(const KFileItemList& items) 00075 { 00076 const bool initialValue = !items.isEmpty(); 00077 m_items = items; 00078 m_urlList = items.targetUrlList(); 00079 m_supportsReading = initialValue; 00080 m_supportsDeleting = initialValue; 00081 m_supportsWriting = initialValue; 00082 m_supportsMoving = initialValue; 00083 m_isDirectory = initialValue; 00084 m_isLocal = true; 00085 m_mimeType.clear(); 00086 m_mimeGroup.clear(); 00087 00088 QFileInfo parentDirInfo; 00089 foreach (const KFileItem &item, items) { 00090 const KUrl url = item.url(); 00091 m_isLocal = m_isLocal && url.isLocalFile(); 00092 m_supportsReading = m_supportsReading && KProtocolManager::supportsReading(url); 00093 m_supportsDeleting = m_supportsDeleting && KProtocolManager::supportsDeleting(url); 00094 m_supportsWriting = m_supportsWriting && KProtocolManager::supportsWriting(url) && item.isWritable(); 00095 m_supportsMoving = m_supportsMoving && KProtocolManager::supportsMoving(url); 00096 00097 // For local files we can do better: check if we have write permission in parent directory 00098 if (m_isLocal && (m_supportsDeleting || m_supportsMoving)) { 00099 const QString directory = url.directory(); 00100 if (parentDirInfo.filePath() != directory) { 00101 parentDirInfo.setFile(directory); 00102 } 00103 if (!parentDirInfo.isWritable()) { 00104 m_supportsDeleting = false; 00105 m_supportsMoving = false; 00106 } 00107 } 00108 if (m_isDirectory && !item.isDir()) { 00109 m_isDirectory = false; 00110 } 00111 } 00112 } 00113 00114 KFileItemListProperties::KFileItemListProperties(const KFileItemListProperties& other) 00115 : d(other.d) 00116 { } 00117 00118 00119 KFileItemListProperties& KFileItemListProperties::operator=(const KFileItemListProperties& other) 00120 { 00121 d = other.d; 00122 return *this; 00123 } 00124 00125 KFileItemListProperties::~KFileItemListProperties() 00126 { 00127 } 00128 00129 bool KFileItemListProperties::supportsReading() const 00130 { 00131 return d->m_supportsReading; 00132 } 00133 00134 bool KFileItemListProperties::supportsDeleting() const 00135 { 00136 return d->m_supportsDeleting; 00137 } 00138 00139 bool KFileItemListProperties::supportsWriting() const 00140 { 00141 return d->m_supportsWriting; 00142 } 00143 00144 bool KFileItemListProperties::supportsMoving() const 00145 { 00146 return d->m_supportsMoving && d->m_supportsDeleting; 00147 } 00148 00149 bool KFileItemListProperties::isLocal() const 00150 { 00151 return d->m_isLocal; 00152 } 00153 00154 KFileItemList KFileItemListProperties::items() const 00155 { 00156 return d->m_items; 00157 } 00158 00159 KUrl::List KFileItemListProperties::urlList() const 00160 { 00161 return d->m_urlList; 00162 } 00163 00164 bool KFileItemListProperties::isDirectory() const 00165 { 00166 return d->m_isDirectory; 00167 } 00168 00169 QString KFileItemListProperties::mimeType() const 00170 { 00171 if (d->m_mimeType.isEmpty()) 00172 d->determineMimeTypeAndGroup(); 00173 return d->m_mimeType; 00174 } 00175 00176 QString KFileItemListProperties::mimeGroup() const 00177 { 00178 if (d->m_mimeType.isEmpty()) 00179 d->determineMimeTypeAndGroup(); 00180 return d->m_mimeGroup; 00181 } 00182 00183 void KFileItemListPropertiesPrivate::determineMimeTypeAndGroup() const 00184 { 00185 if (!m_items.isEmpty()) { 00186 m_mimeType = m_items.first().mimetype(); 00187 m_mimeGroup = m_mimeType.left(m_mimeType.indexOf('/')); 00188 } 00189 foreach (const KFileItem &item, m_items) { 00190 const QString itemMimeType = item.mimetype(); 00191 // Determine if common mimetype among all items 00192 if (m_mimeType != itemMimeType) { 00193 m_mimeType.clear(); 00194 if (m_mimeGroup != itemMimeType.left(itemMimeType.indexOf('/'))) { 00195 m_mimeGroup.clear(); // mimetype groups are different as well! 00196 } 00197 } 00198 } 00199 }