metainfojob.cpp
// -*- c++ -*-
// vim: ts=4 sw=4 et
/* This file is part of the KDE libraries
Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation version 2.0.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
$Id: metainfojob.cpp 465272 2005-09-29 09:47:40Z mueller $
*/
#include <kdatastream.h> // Do not remove, needed for correct bool serialization
#include <kfileitem.h>
#include <kdebug.h>
#include <kfilemetainfo.h>
#include <kio/kservice.h>
#include <kparts/componentfactory.h>
#include <qtimer.h>
#include "metainfojob.moc"
using namespace KIO;
struct KIO::MetaInfoJobPrivate
{
KFileItemList items; // all the items we got
KFileItemListIterator* currentItem; // argh! No default constructor
bool deleteItems; // Delete the KFileItems when done?
bool succeeded; // if the current item is ok
};
MetaInfoJob::MetaInfoJob(const KFileItemList &items, bool deleteItems)
: KIO::Job(false /* no GUI */)
{
d = new MetaInfoJobPrivate;
d->deleteItems = deleteItems;
d->succeeded = false;
d->items = items;
d->currentItem = new KFileItemListIterator(d->items);
d->items.setAutoDelete(deleteItems);
if (d->currentItem->isEmpty())
{
kdDebug(7007) << "nothing to do for the MetaInfoJob\n";
emitResult();
return;
}
kdDebug(7007) << "starting MetaInfoJob\n";
// Return to event loop first, determineNextFile() might delete this;
// (no idea what that means, it comes from previewjob)
QTimer::singleShot(0, this, SLOT(start()));
}
MetaInfoJob::~MetaInfoJob()
{
delete d->currentItem;
delete d;
}
void MetaInfoJob::start()
{
getMetaInfo();
}
void MetaInfoJob::removeItem(const KFileItem* item)
{
if (d->currentItem->current() == item)
{
subjobs.first()->kill();
subjobs.removeFirst();
determineNextFile();
}
d->items.remove(d->items.find(item));
}
void MetaInfoJob::determineNextFile()
{
if (d->currentItem->atLast())
{
kdDebug(7007) << "finished MetaInfoJob\n";
emitResult();
return;
}
++(*d->currentItem);
d->succeeded = false;
// does the file item already have the needed info? Then shortcut
if (d->currentItem->current()->metaInfo(false).isValid())
{
// kdDebug(7007) << "Is already valid *************************\n";
emit gotMetaInfo(d->currentItem->current());
determineNextFile();
return;
}
getMetaInfo();
}
void MetaInfoJob::slotResult( KIO::Job *job )
{
subjobs.remove(job);
Q_ASSERT(subjobs.isEmpty()); // We should have only one job at a time ...
determineNextFile();
}
void MetaInfoJob::getMetaInfo()
{
Q_ASSERT(!d->currentItem->isEmpty());
KURL URL;
URL.setProtocol("metainfo");
URL.setPath(d->currentItem->current()->url().path());
KIO::TransferJob* job = KIO::get(URL, false, false);
addSubjob(job);
connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)),
this, SLOT(slotMetaInfo(KIO::Job *, const QByteArray &)));
job->addMetaData("mimeType", d->currentItem->current()->mimetype());
}
void MetaInfoJob::slotMetaInfo(KIO::Job*, const QByteArray &data)
{
KFileMetaInfo info;
QDataStream s(data, IO_ReadOnly);
s >> info;
d->currentItem->current()->setMetaInfo(info);
emit gotMetaInfo(d->currentItem->current());
d->succeeded = true;
}
QStringList MetaInfoJob::availablePlugins()
{
QStringList result;
KTrader::OfferList plugins = KTrader::self()->query("KFilePlugin");
for (KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
result.append((*it)->desktopEntryName());
return result;
}
QStringList MetaInfoJob::supportedMimeTypes()
{
QStringList result;
KTrader::OfferList plugins = KTrader::self()->query("KFilePlugin");
for (KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
result += (*it)->property("MimeTypes").toStringList();
return result;
}
KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KFileItemList &items)
{
return new MetaInfoJob(items, false);
}
KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KURL::List &items)
{
KFileItemList fileItems;
for (KURL::List::ConstIterator it = items.begin(); it != items.end(); ++it)
fileItems.append(new KFileItem(KFileItem::Unknown, KFileItem::Unknown, *it, true));
return new MetaInfoJob(fileItems, true);
}
KIO::MetaInfoJob::MetaInfoJob
MetaInfoJob(const KFileItemList &items, bool deleteItems=false)
Creates a new MetaInfoJob.
Definition: metainfojob.cpp:44
KIO::Job::addMetaData
void addMetaData(const QString &key, const QString &value)
Add key/value pair to the meta data that is sent to the slave.
Definition: job.cpp:405
KIO::MetaInfoJob::gotMetaInfo
void gotMetaInfo(const KFileItem *item)
Emitted when the meta info for item has been successfully retrieved.
KIO::fileMetaInfo
KIO_EXPORT MetaInfoJob * fileMetaInfo(const KFileItemList &items)
Retrieves meta information for the given items.
Definition: metainfojob.cpp:172
KTrader::query
virtual OfferList query(const QString &servicetype, const QString &constraint=QString::null, const QString &preferences=QString::null) const
The main function in the KTrader class.
Definition: ktrader.cpp:106
KIO::Job::addSubjob
virtual void addSubjob(Job *job, bool inheritMetaData=true)
Add a job that has to be finished before a result is emitted.
Definition: job.cpp:162
KFileMetaInfo
Meta Information about a file.
Definition: kfilemetainfo.h:926
KIO::Job::emitResult
void emitResult()
Utility function to emit the result signal, and suicide this job.
Definition: job.cpp:228
KIO::MetaInfoJob::supportedMimeTypes
static QStringList supportedMimeTypes()
Returns a list of all supported MIME types.
Definition: metainfojob.cpp:163
KIO::MetaInfoJob::availablePlugins
static QStringList availablePlugins()
Returns a list of all available metainfo plugins.
Definition: metainfojob.cpp:154
KTrader::OfferList
QValueList< KService::Ptr > OfferList
A list of services.
Definition: ktrader.h:92
KIO::MetaInfoJob::removeItem
void removeItem(const KFileItem *item)
Removes an item from metainfo extraction.
Definition: metainfojob.cpp:80
KTrader::self
static KTrader * self()
This is a static pointer to a KTrader instance.
Definition: ktrader.cpp:90
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:68
KIO::TransferJob
The transfer job pumps data into and/or out of a Slave.
Definition: jobclasses.h:875
KIO::MetaInfoJob
MetaInfoJob is a KIO Job to retrieve meta information from files.
Definition: metainfojob.h:35
KIO::get
KIO_EXPORT TransferJob * get(const KURL &url, bool reload=false, bool showProgressInfo=true)
Get (a.k.a.
Definition: job.cpp:1220
KIO::Job::result
void result(KIO::Job *job)
Emitted when the job is finished, in any case (completed, canceled, failed...).
KFileItem
A KFileItem is a generic class to handle a file, local or remote.
Definition: kfileitem.h:41

KDE Home | KDE Accessibility Home | Description of Access Keys