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 00021 #include "metainfojob.h" 00022 00023 #include <kfileitem.h> 00024 #include <kdebug.h> 00025 #include <kfilemetainfo.h> 00026 #include <kservicetypetrader.h> 00027 00028 #include <QtCore/QTimer> 00029 00030 #include "jobuidelegate.h" 00031 #include "job_p.h" 00032 00033 using namespace KIO; 00034 00035 class KIO::MetaInfoJobPrivate: public KIO::JobPrivate 00036 { 00037 public: 00038 KFileItemList items; // all the items we got 00039 int currentItem; 00040 bool succeeded; // if the current item is ok 00041 00042 Q_DECLARE_PUBLIC(MetaInfoJob) 00043 }; 00044 00045 MetaInfoJob::MetaInfoJob(const KFileItemList& items, KFileMetaInfo::WhatFlags, 00046 int, int, const QStringList&, const QStringList&) 00047 : KIO::Job(*new MetaInfoJobPrivate) 00048 { 00049 Q_D(MetaInfoJob); 00050 d->succeeded = false; 00051 d->items = items; 00052 d->currentItem = 0; 00053 00054 if (d->items.isEmpty()) 00055 { 00056 kDebug(7007) << "nothing to do for the MetaInfoJob\n"; 00057 emitResult(); 00058 return; 00059 } 00060 00061 kDebug(7007) << "starting MetaInfoJob\n"; 00062 00063 // Return to event loop first, determineNextFile() might delete this; 00064 // (no idea what that means, it comes from previewjob) 00065 QTimer::singleShot(0, this, SLOT(start())); 00066 } 00067 00068 MetaInfoJob::~MetaInfoJob() 00069 { 00070 } 00071 00072 void MetaInfoJob::start() 00073 { 00074 getMetaInfo(); 00075 } 00076 00077 void MetaInfoJob::removeItem(const KFileItem& item) 00078 { 00079 Q_D(MetaInfoJob); 00080 if (d->items.at( d->currentItem ) == item) 00081 { 00082 KJob* job = subjobs().first(); 00083 job->kill(); 00084 removeSubjob( job ); 00085 determineNextFile(); 00086 } 00087 00088 d->items.removeAll(item); 00089 } 00090 00091 void MetaInfoJob::determineNextFile() 00092 { 00093 Q_D(MetaInfoJob); 00094 if (d->currentItem >= d->items.count() - 1) 00095 { 00096 kDebug(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 KFileItem item = d->items.at( d->currentItem ); 00106 if (item.metaInfo(false).isValid()) 00107 { 00108 // kDebug(7007) << "Is already valid *************************\n"; 00109 emit gotMetaInfo(item); 00110 determineNextFile(); 00111 return; 00112 } 00113 00114 getMetaInfo(); 00115 } 00116 00117 void MetaInfoJob::slotResult( KJob *job ) 00118 { 00119 removeSubjob(job); 00120 Q_ASSERT(!hasSubjobs()); // We should have only one job at a time ... 00121 00122 determineNextFile(); 00123 } 00124 00125 void MetaInfoJob::getMetaInfo() 00126 { 00127 Q_D(MetaInfoJob); 00128 KFileItem item = d->items.at( d->currentItem ); 00129 Q_ASSERT(!item.isNull()); 00130 00131 KUrl URL; 00132 URL.setProtocol("metainfo"); 00133 URL.setPath(item.url().path()); 00134 00135 KIO::TransferJob* job = KIO::get(URL, NoReload, HideProgressInfo); 00136 addSubjob(job); 00137 00138 connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), 00139 this, SLOT(slotMetaInfo(KIO::Job *, const QByteArray &))); 00140 00141 job->addMetaData("mimeType", item.mimetype()); 00142 } 00143 00144 00145 void MetaInfoJob::slotMetaInfo(KIO::Job*, const QByteArray &data) 00146 { 00147 Q_D(MetaInfoJob); 00148 KFileMetaInfo info; 00149 QDataStream s(data); 00150 00151 s >> info; 00152 00153 KFileItem item = d->items.at( d->currentItem ); 00154 item.setMetaInfo(info); 00155 emit gotMetaInfo(item); 00156 d->succeeded = true; 00157 } 00158 00159 KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KFileItemList& items) 00160 { 00161 return new MetaInfoJob(items); 00162 } 00163 00164 KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KUrl::List &items) 00165 { 00166 KFileItemList fileItems; 00167 foreach (const KUrl& url, items) { 00168 fileItems.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, url, 00169 true)); 00170 } 00171 MetaInfoJob *job = new MetaInfoJob(fileItems); 00172 job->setUiDelegate(new JobUiDelegate()); 00173 return job; 00174 } 00175 00176 #include "metainfojob.moc"
KDE 4.6 API Reference