KDELibs4Support

metainfojob.cpp
1 // -*- c++ -*-
2 /* This file is part of the KDE libraries
3  Copyright (C) 2002 Rolf Magnus <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation version 2.0.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18  */
19 
20 #include "metainfojob.h"
21 
22 
23 #include <QTimer>
24 
25 #include "kio/jobuidelegate.h"
26 
27 using namespace KIO;
28 
29 class KIO::MetaInfoJobPrivate
30 {
31 public:
32  KFileItemList items; // all the items we got
33  int currentItem;
34  bool succeeded; // if the current item is ok
35 
36  MetaInfoJob *q_ptr;
37  Q_DECLARE_PUBLIC(MetaInfoJob)
38 };
39 
41  int, int, const QStringList &, const QStringList &)
42  : KIO::Job()
43  , d(new MetaInfoJobPrivate)
44 {
46  d->succeeded = false;
47  d->items = items;
48  d->currentItem = 0;
49 
50  if (d->items.isEmpty()) {
51  //qDebug() << "nothing to do for the MetaInfoJob";
52  emitResult();
53  return;
54  }
55 
56  //qDebug() << "starting MetaInfoJob";
57 
58  // Return to event loop first, determineNextFile() might delete this;
59  // (no idea what that means, it comes from previewjob)
60  QTimer::singleShot(0, this, SLOT(start()));
61  d->q_ptr = this;
62 }
63 
64 MetaInfoJob::~MetaInfoJob()
65 {
66 }
67 
68 void MetaInfoJob::start()
69 {
70  getMetaInfo();
71 }
72 
74 {
76  if (d->items.at(d->currentItem) == item) {
77  KJob *job = subjobs().first();
78  job->kill();
79  removeSubjob(job);
80  determineNextFile();
81  }
82 
83  d->items.removeAll(item);
84 }
85 
86 void MetaInfoJob::determineNextFile()
87 {
89  if (d->currentItem >= d->items.count() - 1) {
90  //qDebug() << "finished MetaInfoJob";
91  emitResult();
92  return;
93  }
94 
95  ++d->currentItem;
96  d->succeeded = false;
97 
98 #if 0 // HACK DISABLED
99  // does the file item already have the needed info? Then shortcut
100  KFileItem item = d->items.at(d->currentItem);
101  if (item.metaInfo(false).isValid()) {
102 //qDebug() << "Is already valid *************************";
103  emit gotMetaInfo(item);
104  determineNextFile();
105  return;
106  }
107 #endif
108 
109  getMetaInfo();
110 }
111 
112 void MetaInfoJob::slotResult(KJob *job)
113 {
114  removeSubjob(job);
115  Q_ASSERT(!hasSubjobs()); // We should have only one job at a time ...
116 
117  determineNextFile();
118 }
119 
120 void MetaInfoJob::getMetaInfo()
121 {
122  Q_D(MetaInfoJob);
123  KFileItem item = d->items.at(d->currentItem);
124  Q_ASSERT(!item.isNull());
125 
126  QUrl URL;
127  URL.setScheme("metainfo");
128  URL.setPath(item.url().path());
129 
130  KIO::TransferJob *job = KIO::get(URL, NoReload, HideProgressInfo);
131  addSubjob(job);
132 
133  connect(job, SIGNAL(data(KIO::Job*,QByteArray)),
134  this, SLOT(slotMetaInfo(KIO::Job*,QByteArray)));
135 
136  job->addMetaData("mimeType", item.mimetype());
137 }
138 
139 void MetaInfoJob::slotMetaInfo(KIO::Job *, const QByteArray &data)
140 {
141  Q_D(MetaInfoJob);
142  KFileMetaInfo info;
143  QDataStream s(data);
144 
145  s >> info;
146 
147  KFileItem item = d->items.at(d->currentItem);
148 #if 0 // HACK DISABLED
149  item.setMetaInfo(info);
150 #endif
151  emit gotMetaInfo(item);
152  d->succeeded = true;
153 }
154 
155 KDELIBS4SUPPORT_DEPRECATED_EXPORT MetaInfoJob *KIO::fileMetaInfo(const KFileItemList &items)
156 {
157  return new MetaInfoJob(items);
158 }
159 
160 KDELIBS4SUPPORT_DEPRECATED_EXPORT MetaInfoJob *KIO::fileMetaInfo(const QList<QUrl> &items)
161 {
162  KFileItemList fileItems;
163  foreach (const QUrl &url, items) {
164  fileItems.append(KFileItem(url));
165  }
166  MetaInfoJob *job = new MetaInfoJob(fileItems);
167  job->setUiDelegate(new JobUiDelegate());
168  return job;
169 }
170 
171 #include "moc_metainfojob.cpp"
void append(const T &value)
bool kill(KillVerbosity verbosity=Quietly)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setScheme(const QString &scheme)
const QList< KJob * > & subjobs() const
bool isNull() const
void gotMetaInfo(const KFileItem &item)
Emitted when the meta info for item has been successfully retrieved.
bool addSubjob(KJob *job) override
QUrl url() const
QString path(QUrl::ComponentFormattingOptions options) const const
void removeItem(const KFileItem &item)
Removes an item from metainfo extraction.
Definition: metainfojob.cpp:73
MetaInfoJob(const KFileItemList &items, KFileMetaInfo::WhatFlags w=KFileMetaInfo::Everything, int iocost=3, int cpucost=6, const QStringList &requiredfields=QStringList(), const QStringList &requestedfields=QStringList())
Creates a new MetaInfoJob.
Definition: metainfojob.cpp:40
void setPath(const QString &path, QUrl::ParsingMode mode)
void setUiDelegate(KJobUiDelegate *delegate)
void emitResult()
QString mimetype() const
bool hasSubjobs() const
void addMetaData(const QMap< QString, QString > &values)
KFileMetaInfo provides metadata extracted from a file or other resource.
Definition: kfilemetainfo.h:58
bool removeSubjob(KJob *job) override
MetaInfoJob is a KIO Job to retrieve meta information from files.
Definition: metainfojob.h:40
Q_D(Todo)
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 03:57:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.