ark
archiveinterface.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "archiveinterface.h"
00027 #include "observer.h"
00028 #include <kdebug.h>
00029
00030 #include <QFileInfo>
00031 #include <QDir>
00032
00033 namespace Kerfuffle
00034 {
00035 ReadOnlyArchiveInterface::ReadOnlyArchiveInterface( const QString & filename, QObject *parent )
00036 : QObject( parent ), m_filename( filename )
00037 {
00038 }
00039
00040 ReadOnlyArchiveInterface::~ReadOnlyArchiveInterface()
00041 {
00042 }
00043
00044 void ReadOnlyArchiveInterface::error( const QString & message, const QString & details )
00045 {
00046 foreach( ArchiveObserver *observer, m_observers )
00047 {
00048 observer->onError( message, details );
00049 }
00050 }
00051
00052 void ReadOnlyArchiveInterface::entry( const ArchiveEntry & archiveEntry )
00053 {
00054 foreach( ArchiveObserver *observer, m_observers )
00055 {
00056 observer->onEntry( archiveEntry );
00057 }
00058 }
00059
00060 void ReadOnlyArchiveInterface::entryRemoved( const QString & path )
00061 {
00062 foreach( ArchiveObserver *observer, m_observers )
00063 {
00064 observer->onEntryRemoved( path );
00065 }
00066 }
00067
00068 void ReadOnlyArchiveInterface::progress( double p )
00069 {
00070 foreach( ArchiveObserver *observer, m_observers )
00071 {
00072 observer->onProgress( p );
00073 }
00074 }
00075
00076 void ReadOnlyArchiveInterface::registerObserver( ArchiveObserver *observer )
00077 {
00078 m_observers.append( observer );
00079 }
00080
00081 void ReadOnlyArchiveInterface::removeObserver( ArchiveObserver *observer )
00082 {
00083 m_observers.removeAll( observer );
00084 }
00085
00086 QString ReadOnlyArchiveInterface::findCommonBase(const QStringList& files)
00087 {
00088 QString commonBase;
00089
00090
00091 if (files.size() > 1) {
00092 QStringList common = files.first().split("/", QString::SkipEmptyParts);
00093 if (common.size() > 1) {
00094 common.removeLast();
00095
00096 foreach(const QString &selectedEntry, files) {
00097 QStringList parts = selectedEntry.split("/", QString::SkipEmptyParts);
00098 for (int i = common.size() - 1; i > -1; --i) {
00099 if (common.at(i) != parts.at(i))
00100 common.removeLast();
00101 }
00102 }
00103 commonBase = common.join("/") + "/";
00104 }
00105 }
00106 else if (files.size() == 1) {
00107 QStringList parts = files.first().split("/", QString::SkipEmptyParts);
00108 parts.removeLast();
00109 return parts.join("/") + "/";
00110 }
00111 return commonBase;
00112 }
00113
00114 QString ReadOnlyArchiveInterface::findCommonBase(const QVariantList& files)
00115 {
00116 QString commonBase;
00117
00118
00119 if (files.size() > 1) {
00120 QStringList common = files.first().toString().split("/", QString::SkipEmptyParts);
00121 if (common.size() > 1) {
00122 common.removeLast();
00123
00124 foreach(const QVariant &selectedEntry, files) {
00125 QStringList parts = selectedEntry.toString().split("/", QString::SkipEmptyParts);
00126 for (int i = common.size() - 1; i > -1; --i) {
00127 if (common.at(i) != parts.at(i))
00128 common.removeLast();
00129 }
00130 }
00131 commonBase = common.join("/") + "/";
00132 }
00133 }
00134 else if (files.size() == 1) {
00135 QStringList parts = files.first().toString().split("/", QString::SkipEmptyParts);
00136 parts.removeLast();
00137 return parts.join("/") + "/";
00138 }
00139 return commonBase;
00140 }
00141
00142 void ReadOnlyArchiveInterface::expandDirectories( QStringList &files )
00143 {
00144 static bool onlyOnce = false;
00145 if (!onlyOnce) {
00146 qRegisterMetaType<KIO::filesize_t>("KIO::filesize_t");
00147 qRegisterMetaType<KIO::UDSEntryList>("KIO::UDSEntryList");
00148 onlyOnce = true;
00149 }
00150
00151 for(int i = 0; i < files.size(); ++i) {
00152 const QString& item = files.at(i);
00153 if (QFileInfo(item).isDir()) {
00154 QString absolutePath = QFileInfo(item).absoluteFilePath();
00155 Q_ASSERT(QFileInfo(absolutePath).exists());
00156 kDebug( 1601 ) << "Calling listRecursive on " << absolutePath;
00157 KIO::ListJob *listJob = KIO::listRecursive(absolutePath, KIO::HideProgressInfo);
00158 RecursiveListHelper helper;
00159 connect(listJob, SIGNAL(entries (KIO::Job *, const KIO::UDSEntryList &)),
00160 &helper, SLOT(entries (KIO::Job *, const KIO::UDSEntryList &)));
00161 listJob->exec();
00162
00163 foreach(const QString& result, helper.results) {
00164 files.insert(i + 1, item + "/" + result);
00165 ++i;
00166 }
00167
00168 }
00169 }
00170 }
00171
00172 void RecursiveListHelper::entries (KIO::Job *job, const KIO::UDSEntryList &list)
00173 {
00174 foreach( const KIO::UDSEntry& entry, list) {
00175 QString value = entry.stringValue(KIO::UDSEntry::UDS_NAME);
00176 if (value == ".." || value == ".") continue;
00177 results.append(value);
00178 }
00179 }
00180
00181 ReadWriteArchiveInterface::ReadWriteArchiveInterface( const QString & filename, QObject *parent )
00182 : ReadOnlyArchiveInterface( filename, parent )
00183 {
00184 }
00185
00186 ReadWriteArchiveInterface::~ReadWriteArchiveInterface()
00187 {
00188 }
00189
00190 bool ReadWriteArchiveInterface::isReadOnly() const
00191 {
00192 QFileInfo fileInfo( filename() );
00193 if ( fileInfo.exists() )
00194 {
00195 return ! fileInfo.isWritable();
00196 }
00197 else
00198 {
00199 return !fileInfo.dir().exists();
00200 }
00201 }
00202
00203 }
00204
00205 #include "archiveinterface.moc"