metainfojob.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# metainfojob.cpp

    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., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019
    00020     $Id: metainfojob.cpp,v 1.5 2002/08/27 12:16:30 faure Exp $
    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     // every file should get a valid (but maybe empty) item
    00150     if (!info.isValid()) kdDebug(7007) << info.path() << " is not valid\n";
    00151
    00152     d->currentItem->current()->setMetaInfo(info);
    00153     emit gotMetaInfo(d->currentItem->current());
    00154     d->succeeded = true;
    00155 }
    00156
    00157 QStringList MetaInfoJob::availablePlugins()
    00158 {
    00159     QStringList result;
    00160     KTrader::OfferList plugins = KTrader::self()->query("KFilePlugin");
    00161     for (KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
    00162         result.append((*it)->desktopEntryName());
    00163     return result;
    00164 }
    00165
    00166 QStringList MetaInfoJob::supportedMimeTypes()
    00167 {
    00168     QStringList result;
    00169     KTrader::OfferList plugins = KTrader::self()->query("KFilePlugin");
    00170     for (KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
    00171         result += (*it)->property("MimeTypes").toStringList();
    00172     return result;
    00173 }
    00174
    00175 MetaInfoJob *KIO::fileMetaInfo( const KFileItemList &items)
    00176 {
    00177     return new MetaInfoJob(items, false);
    00178 }
    00179
    00180 MetaInfoJob *KIO::fileMetaInfo( const KURL::List &items)
    00181 {
    00182     KFileItemList fileItems;
    00183     for (KURL::List::ConstIterator it = items.begin(); it != items.end(); ++it)
    00184         fileItems.append(new KFileItem(KFileItem::Unknown, KFileItem::Unknown, *it, true));
    00185     return new MetaInfoJob(fileItems, true);
    00186 }
    00187

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001