File indexing completed on 2024-05-05 16:08:28

0001 // -*- c++ -*-
0002 /*  This file is part of the KDE libraries
0003     Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Library General Public
0007     License as published by the Free Software Foundation version 2.0.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "metainfojob.h"
0021 
0022 
0023 #include <QTimer>
0024 
0025 #include "kio/jobuidelegate.h"
0026 
0027 using namespace KIO;
0028 
0029 class KIO::MetaInfoJobPrivate
0030 {
0031 public:
0032     KFileItemList          items;       // all the items we got
0033     int                    currentItem;
0034     bool                   succeeded;   // if the current item is ok
0035 
0036     MetaInfoJob *q_ptr;
0037     Q_DECLARE_PUBLIC(MetaInfoJob)
0038 };
0039 
0040 MetaInfoJob::MetaInfoJob(const KFileItemList &items, KFileMetaInfo::WhatFlags,
0041                          int, int, const QStringList &, const QStringList &)
0042     : KIO::Job()
0043     , d(new MetaInfoJobPrivate)
0044 {
0045     Q_D(MetaInfoJob);
0046     d->succeeded    = false;
0047     d->items        = items;
0048     d->currentItem  = 0;
0049 
0050     if (d->items.isEmpty()) {
0051         //qDebug() << "nothing to do for the MetaInfoJob";
0052         emitResult();
0053         return;
0054     }
0055 
0056     //qDebug() << "starting MetaInfoJob";
0057 
0058     // Return to event loop first, determineNextFile() might delete this;
0059     // (no idea what that means, it comes from previewjob)
0060     QTimer::singleShot(0, this, SLOT(start()));
0061     d->q_ptr = this;
0062 }
0063 
0064 MetaInfoJob::~MetaInfoJob()
0065 {
0066 }
0067 
0068 void MetaInfoJob::start()
0069 {
0070     getMetaInfo();
0071 }
0072 
0073 void MetaInfoJob::removeItem(const KFileItem &item)
0074 {
0075     Q_D(MetaInfoJob);
0076     if (d->items.at(d->currentItem) == item) {
0077         KJob *job = subjobs().first();
0078         job->kill();
0079         removeSubjob(job);
0080         determineNextFile();
0081     }
0082 
0083     d->items.removeAll(item);
0084 }
0085 
0086 void MetaInfoJob::determineNextFile()
0087 {
0088     Q_D(MetaInfoJob);
0089     if (d->currentItem >= d->items.count() - 1) {
0090         //qDebug() << "finished MetaInfoJob";
0091         emitResult();
0092         return;
0093     }
0094 
0095     ++d->currentItem;
0096     d->succeeded = false;
0097 
0098 #if 0 // HACK DISABLED
0099     // does the file item already have the needed info? Then shortcut
0100     KFileItem item = d->items.at(d->currentItem);
0101     if (item.metaInfo(false).isValid()) {
0102 //qDebug() << "Is already valid *************************";
0103         emit gotMetaInfo(item);
0104         determineNextFile();
0105         return;
0106     }
0107 #endif
0108 
0109     getMetaInfo();
0110 }
0111 
0112 void MetaInfoJob::slotResult(KJob *job)
0113 {
0114     removeSubjob(job);
0115     Q_ASSERT(!hasSubjobs()); // We should have only one job at a time ...
0116 
0117     determineNextFile();
0118 }
0119 
0120 void MetaInfoJob::getMetaInfo()
0121 {
0122     Q_D(MetaInfoJob);
0123     KFileItem item = d->items.at(d->currentItem);
0124     Q_ASSERT(!item.isNull());
0125 
0126     QUrl URL;
0127     URL.setScheme("metainfo");
0128     URL.setPath(item.url().path());
0129 
0130     KIO::TransferJob *job = KIO::get(URL, NoReload, HideProgressInfo);
0131     addSubjob(job);
0132 
0133     connect(job,  SIGNAL(data(KIO::Job*,QByteArray)),
0134             this, SLOT(slotMetaInfo(KIO::Job*,QByteArray)));
0135 
0136     job->addMetaData("mimeType", item.mimetype());
0137 }
0138 
0139 void MetaInfoJob::slotMetaInfo(KIO::Job *, const QByteArray &data)
0140 {
0141     Q_D(MetaInfoJob);
0142     KFileMetaInfo info;
0143     QDataStream s(data);
0144 
0145     s >> info;
0146 
0147     KFileItem item = d->items.at(d->currentItem);
0148 #if 0 // HACK DISABLED
0149     item.setMetaInfo(info);
0150 #endif
0151     emit gotMetaInfo(item);
0152     d->succeeded = true;
0153 }
0154 
0155 KDELIBS4SUPPORT_DEPRECATED_EXPORT MetaInfoJob *KIO::fileMetaInfo(const KFileItemList &items)
0156 {
0157     return new MetaInfoJob(items);
0158 }
0159 
0160 KDELIBS4SUPPORT_DEPRECATED_EXPORT MetaInfoJob *KIO::fileMetaInfo(const QList<QUrl> &items)
0161 {
0162     KFileItemList fileItems;
0163     foreach (const QUrl &url, items) {
0164         fileItems.append(KFileItem(url));
0165     }
0166     MetaInfoJob *job = new MetaInfoJob(fileItems);
0167     job->setUiDelegate(new JobUiDelegate());
0168     return job;
0169 }
0170 
0171 #include "moc_metainfojob.cpp"