fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kio / kio / metainfojob.cpp
blob6fc6712bcea8a7c37e571378ca707fce3119fdf4
1 // -*- c++ -*-
2 // vim: ts=4 sw=4 et
3 /* This file is part of the KDE libraries
4 Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation version 2.0.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "metainfojob.h"
23 #include <kfileitem.h>
24 #include <kdebug.h>
25 #include <kfilemetainfo.h>
26 #include <kservicetypetrader.h>
28 #include <QtCore/QTimer>
30 #include "jobuidelegate.h"
31 #include "job_p.h"
33 using namespace KIO;
35 class KIO::MetaInfoJobPrivate: public KIO::JobPrivate
37 public:
38 KFileItemList items; // all the items we got
39 int currentItem;
40 bool succeeded; // if the current item is ok
42 Q_DECLARE_PUBLIC(MetaInfoJob)
45 MetaInfoJob::MetaInfoJob(const KFileItemList& items, KFileMetaInfo::WhatFlags,
46 int, int, const QStringList&, const QStringList&)
47 : KIO::Job(*new MetaInfoJobPrivate)
49 Q_D(MetaInfoJob);
50 d->succeeded = false;
51 d->items = items;
52 d->currentItem = 0;
54 if (d->items.isEmpty())
56 kDebug(7007) << "nothing to do for the MetaInfoJob\n";
57 emitResult();
58 return;
61 kDebug(7007) << "starting MetaInfoJob\n";
63 // Return to event loop first, determineNextFile() might delete this;
64 // (no idea what that means, it comes from previewjob)
65 QTimer::singleShot(0, this, SLOT(start()));
68 MetaInfoJob::~MetaInfoJob()
72 void MetaInfoJob::start()
74 getMetaInfo();
77 void MetaInfoJob::removeItem(const KFileItem& item)
79 Q_D(MetaInfoJob);
80 if (d->items.at( d->currentItem ) == item)
82 KJob* job = subjobs().first();
83 job->kill();
84 removeSubjob( job );
85 determineNextFile();
88 d->items.removeAll(item);
91 void MetaInfoJob::determineNextFile()
93 Q_D(MetaInfoJob);
94 if (d->currentItem >= d->items.count() - 1)
96 kDebug(7007) << "finished MetaInfoJob\n";
97 emitResult();
98 return;
101 ++d->currentItem;
102 d->succeeded = false;
104 // does the file item already have the needed info? Then shortcut
105 KFileItem item = d->items.at( d->currentItem );
106 if (item.metaInfo(false).isValid())
108 // kDebug(7007) << "Is already valid *************************\n";
109 emit gotMetaInfo(item);
110 determineNextFile();
111 return;
114 getMetaInfo();
117 void MetaInfoJob::slotResult( KJob *job )
119 removeSubjob(job);
120 Q_ASSERT(!hasSubjobs()); // We should have only one job at a time ...
122 determineNextFile();
125 void MetaInfoJob::getMetaInfo()
127 Q_D(MetaInfoJob);
128 KFileItem item = d->items.at( d->currentItem );
129 Q_ASSERT(!item.isNull());
131 KUrl URL;
132 URL.setProtocol("metainfo");
133 URL.setPath(item.url().path());
135 KIO::TransferJob* job = KIO::get(URL, NoReload, HideProgressInfo);
136 addSubjob(job);
138 connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)),
139 this, SLOT(slotMetaInfo(KIO::Job *, const QByteArray &)));
141 job->addMetaData("mimeType", item.mimetype());
145 void MetaInfoJob::slotMetaInfo(KIO::Job*, const QByteArray &data)
147 Q_D(MetaInfoJob);
148 KFileMetaInfo info;
149 QDataStream s(data);
151 s >> info;
153 KFileItem item = d->items.at( d->currentItem );
154 item.setMetaInfo(info);
155 emit gotMetaInfo(item);
156 d->succeeded = true;
159 KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KFileItemList& items)
161 return new MetaInfoJob(items);
164 KIO_EXPORT MetaInfoJob *KIO::fileMetaInfo( const KUrl::List &items)
166 KFileItemList fileItems;
167 foreach (const KUrl& url, items) {
168 fileItems.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, url,
169 true));
171 MetaInfoJob *job = new MetaInfoJob(fileItems);
172 job->setUiDelegate(new JobUiDelegate());
173 return job;
176 #include "metainfojob.moc"