kio
metainfojob.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 #include <kdatastream.h> 
00024 #include <kfileitem.h>
00025 #include <kdebug.h>
00026 #include <kfilemetainfo.h>
00027 #include <kio/kservice.h>
00028 #include <kparts/componentfactory.h>
00029 
00030 #include <qtimer.h>
00031 
00032 #include "metainfojob.moc"
00033 
00034 using namespace KIO;
00035 
00036 struct KIO::MetaInfoJobPrivate
00037 {
00038     KFileItemList          items;       
00039     KFileItemListIterator* currentItem; 
00040     bool                   deleteItems; 
00041     bool                   succeeded;   
00042 };
00043 
00044 MetaInfoJob::MetaInfoJob(const KFileItemList &items, bool deleteItems)
00045     : KIO::Job(false )
00046 {
00047     d               = new MetaInfoJobPrivate;
00048     d->deleteItems  = deleteItems;
00049     d->succeeded    = false;
00050     d->items        = items;
00051     d->currentItem  = new KFileItemListIterator(d->items);
00052 
00053     d->items.setAutoDelete(deleteItems);
00054 
00055     if (d->currentItem->isEmpty())
00056     {
00057         kdDebug(7007) << "nothing to do for the MetaInfoJob\n";
00058         emitResult();
00059         return;
00060     }
00061 
00062     kdDebug(7007) << "starting MetaInfoJob\n";
00063 
00064     
00065     
00066     QTimer::singleShot(0, this, SLOT(start()));
00067 }
00068 
00069 MetaInfoJob::~MetaInfoJob()
00070 {
00071     delete d->currentItem;
00072     delete d;
00073 }
00074 
00075 void MetaInfoJob::start()
00076 {
00077     getMetaInfo();
00078 }
00079 
00080 void MetaInfoJob::removeItem(const KFileItem* item)
00081 {
00082     if (d->currentItem->current() == item)
00083     {
00084         subjobs.first()->kill();
00085         subjobs.removeFirst();
00086         determineNextFile();
00087     }
00088 
00089     d->items.remove(d->items.find(item));
00090 }
00091 
00092 void MetaInfoJob::determineNextFile()
00093 {
00094     if (d->currentItem->atLast())
00095     {
00096         kdDebug(7007) << "finished MetaInfoJob\n";
00097         emitResult();
00098         return;
00099     }
00100 
00101     ++(*d->currentItem);
00102     d->succeeded = false;
00103 
00104     
00105     if (d->currentItem->current()->metaInfo(false).isValid())
00106     {
00107 
00108         emit gotMetaInfo(d->currentItem->current());
00109         determineNextFile();
00110         return;
00111     }
00112 
00113     getMetaInfo();
00114 }
00115 
00116 void MetaInfoJob::slotResult( KIO::Job *job )
00117 {
00118     subjobs.remove(job);
00119     Q_ASSERT(subjobs.isEmpty()); 
00120 
00121     determineNextFile();
00122 }
00123 
00124 void MetaInfoJob::getMetaInfo()
00125 {
00126     Q_ASSERT(!d->currentItem->isEmpty());
00127 
00128     KURL URL;
00129     URL.setProtocol("metainfo");
00130     URL.setPath(d->currentItem->current()->url().path());
00131 
00132     KIO::TransferJob* job = KIO::get(URL, false, false);
00133     addSubjob(job);
00134 
00135     connect(job,  SIGNAL(data(KIO::Job *, const QByteArray &)),
00136             this, SLOT(slotMetaInfo(KIO::Job *, const QByteArray &)));
00137 
00138     job->addMetaData("mimeType", d->currentItem->current()->mimetype());
00139 }
00140 
00141 
00142 void MetaInfoJob::slotMetaInfo(KIO::Job*, const QByteArray &data)
00143 {
00144     KFileMetaInfo info;
00145     QDataStream s(data, IO_ReadOnly);
00146 
00147     s >> info;
00148 
00149     d->currentItem->current()->setMetaInfo(info);
00150     emit gotMetaInfo(d->currentItem->current());
00151     d->succeeded = true;
00152 }
00153 
00154 QStringList MetaInfoJob::availablePlugins()
00155 {
00156     QStringList result;
00157     KTrader::OfferList plugins = KTrader::self()->query("KFilePlugin");
00158     for (KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
00159         result.append((*it)->desktopEntryName());
00160     return result;
00161 }
00162 
00163 QStringList MetaInfoJob::supportedMimeTypes()
00164 {
00165     QStringList result;
00166     KTrader::OfferList plugins = KTrader::self()->query("KFilePlugin");
00167     for (KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
00168         result += (*it)->property("MimeTypes").toStringList();
00169     return result;
00170 }
00171 
00172 KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KFileItemList &items)
00173 {
00174     return new MetaInfoJob(items, false);
00175 }
00176 
00177 KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KURL::List &items)
00178 {
00179     KFileItemList fileItems;
00180     for (KURL::List::ConstIterator it = items.begin(); it != items.end(); ++it)
00181         fileItems.append(new KFileItem(KFileItem::Unknown, KFileItem::Unknown, *it, true));
00182     return new MetaInfoJob(fileItems, true);
00183 }
00184