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

kio

metainfojob.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // vim: ts=4 sw=4 et
00003 /*  This file is part of the KDE libraries
00004     Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation version 2.0.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 
00020     $Id: metainfojob.cpp 465272 2005-09-29 09:47:40Z mueller $
00021 */
00022 
00023 #include <kdatastream.h> // Do not remove, needed for correct bool serialization
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;       // all the items we got
00039     KFileItemListIterator* currentItem; // argh! No default constructor
00040     bool                   deleteItems; // Delete the KFileItems when done?
00041     bool                   succeeded;   // if the current item is ok
00042 };
00043 
00044 MetaInfoJob::MetaInfoJob(const KFileItemList &items, bool deleteItems)
00045     : KIO::Job(false /* no GUI */)
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     // Return to event loop first, determineNextFile() might delete this;
00065     // (no idea what that means, it comes from previewjob)
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     // does the file item already have the needed info? Then shortcut
00105     if (d->currentItem->current()->metaInfo(false).isValid())
00106     {
00107 //        kdDebug(7007) << "Is already valid *************************\n";
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()); // We should have only one job at a time ...
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 

kio

Skip menu "kio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
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