• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

ark

archiveinterface.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
00019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
00021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
00023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         //we loop through all items and find the highest common folder they share
00091         if (files.size() > 1) {
00092             QStringList common = files.first().split("/", QString::SkipEmptyParts);
00093             if (common.size() > 1) {
00094                 common.removeLast(); //We don't need the filename
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(); //take of the filename
00109             return parts.join("/") + "/";
00110         }
00111         return commonBase;
00112     }
00113 
00114     QString ReadOnlyArchiveInterface::findCommonBase(const QVariantList& files)
00115     {
00116         QString commonBase;
00117 
00118         //we loop through all items and find the highest common folder they share
00119         if (files.size() > 1) {
00120             QStringList common = files.first().toString().split("/", QString::SkipEmptyParts);
00121             if (common.size() > 1) {
00122                 common.removeLast(); //We don't need the filename
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(); //take of the filename
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(); // TODO: Should also check if we can create a file in that directory
00200         }
00201     }
00202 
00203 } // namespace Kerfuffle
00204 
00205 #include "archiveinterface.moc"

ark

Skip menu "ark"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • okteta
  • printer-applet
  • superkaramba
  • sweeper
Generated for kdeutils 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