• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kfilemetainfo.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002 
00003    Copyright (c) 2001,2002 Carsten Pfeiffer <pfeiffer@kde.org>
00004                  2007 Jos van den Oever <jos@vandenoever.info>
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 (LGPL) as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 #include "kfilemetainfo_p.h"
00022 #include <strigi/bufferedstream.h>
00023 #include <strigi/indexwriter.h>
00024 #include <strigi/analysisresult.h>
00025 #include <strigi/fieldtypes.h>
00026 #include <kurl.h>
00027 #include <kdebug.h>
00028 #include <QFileInfo>
00029 #include <QDateTime>
00030 #include <QStringList>
00031 
00032 using namespace std;
00033 using namespace Strigi;
00034 
00035 class KFileMetaInfoGroupPrivate : public QSharedData {
00036 public:
00037     QString name;
00038 };
00039 KFileMetaInfoGroup::~KFileMetaInfoGroup() {
00040 }
00041 KFileMetaInfoGroup::KFileMetaInfoGroup(KFileMetaInfoGroup const& g) {
00042     p = g.p;
00043 }
00044 
00045 QDataStream& operator >>(QDataStream& s, KFileMetaInfo& ) {
00046     return s;
00047 }
00048 QDataStream& operator <<(QDataStream& s, const KFileMetaInfo&) {
00049     return s;
00050 }
00051 
00055 class QIODeviceInputStream : public BufferedInputStream {
00056 private:
00057     QIODevice& in;
00058     int32_t fillBuffer(char* start, int32_t space);
00059 public:
00060     QIODeviceInputStream(QIODevice& i);
00061 };
00062 
00063 int32_t
00064 QIODeviceInputStream::fillBuffer(char* start, int32_t space) {
00065     if (!in.isOpen() || !in.isReadable()) return -1;
00066     // read into the buffer
00067     int32_t nwritten = in.read(start, space);
00068     // check the file stream status
00069     if (nwritten < 0) {
00070         m_error = "Could not read from QIODevice.";
00071         in.close();
00072         return -1;
00073     }
00074     if (nwritten == 0 || in.atEnd()) {
00075         in.close();
00076     }
00077     return nwritten;
00078 }
00079 
00080 QIODeviceInputStream::QIODeviceInputStream(QIODevice &i) :in(i)
00081 {
00082     // determine if we have a character device, which will likely never eof and thereby
00083     // potentially cause an infinite loop.
00084     if(i.isSequential()) {
00085         in.close(); // cause fillBuffer to return -1
00086     }
00087 }
00088 
00093 class KMetaInfoWriter : public IndexWriter {
00094 public:
00095     // irrelevant for KFileMetaInfo
00096     void startAnalysis(const AnalysisResult*) {
00097     }
00098     // irrelevant for KFileMetaInfo
00099     // we do not store text as metainfo
00100     void addText(const AnalysisResult*, const char* /*s*/, int32_t /*n*/) {
00101     }
00102     void addValue(const AnalysisResult* idx, const RegisteredField* field,
00103             const string& value) {
00104         if (idx->writerData()) {
00105             QString val = QString::fromUtf8(value.c_str(), value.size());
00106             addValue(idx, field, val);
00107         }
00108     }
00109     void addValue(const AnalysisResult* idx, const RegisteredField* field,
00110         const unsigned char* data, uint32_t size) {
00111         if (idx->writerData()) {
00112             QByteArray d((const char*)data, size);
00113             addValue(idx, field, QVariant(d));
00114         }
00115     }
00116     void addValue(const AnalysisResult* idx, const RegisteredField* field,
00117             uint32_t value) {
00118         if (idx->writerData()) {
00119             addValue(idx, field, QVariant((quint32)value));
00120         }
00121     }
00122     void addValue(const AnalysisResult* idx, const RegisteredField* field,
00123             int32_t value) {
00124         if (idx->writerData()) {
00125             addValue(idx, field, QVariant((qint32)value));
00126         }
00127     }
00128     void addValue(const AnalysisResult* idx, const RegisteredField* field,
00129             double value) {
00130         if (idx->writerData()) {
00131             addValue(idx, field, QVariant(value));
00132         }
00133     }
00134     void addValue(const AnalysisResult* idx,
00135             const RegisteredField* field, const QVariant& value) {
00136         QHash<QString, KFileMetaInfoItem>* info
00137             = static_cast<QHash<QString, KFileMetaInfoItem>*>(
00138             idx->writerData());
00139         if (info) {
00140             string name(field->key());
00141             QString key = QString::fromUtf8(name.c_str(), name.size());
00142             QHash<QString, KFileMetaInfoItem>::iterator i = info->find(key);
00143             if (i == info->end()) {
00144                 // retrieve the info describing this field
00145                 PredicateProperties pp(key);
00146                 info->insert(key, KFileMetaInfoItem(pp, value, 0, true));
00147             } else {
00148                 i.value().addValue(value);
00149             }
00150         }
00151     }
00152     void addValue(const Strigi::AnalysisResult* ar,
00153             const RegisteredField* field, const std::string& name,
00154             const std::string& value) {
00155         if (ar->writerData()) {
00156             QVariantMap m;
00157             m.insert(name.c_str(), value.c_str());
00158             addValue(ar, field, m);
00159         }
00160     }
00161     /* irrelevant for KFileMetaInfo: These triples does not convey information
00162      * about this file, so we ignore it
00163      */
00164     void addTriplet(const std::string& /*subject*/,
00165         const std::string& /*predicate*/, const std::string& /*object*/) {
00166     }
00167     // irrelevant for KFileMetaInfo
00168     void finishAnalysis(const AnalysisResult*) {}
00169     // irrelevant for KFileMetaInfo
00170     void deleteEntries(const vector<string>&) {}
00171     // irrelevant for KFileMetaInfo
00172     void deleteAllEntries() {}
00173 };
00174 class KFileMetaInfoPrivate : public QSharedData {
00175 public:
00176     QHash<QString, KFileMetaInfoItem> items;
00177     KUrl kurl;
00178 
00179     //Private() :QSharedData() {qDebug() <<"ok: " << this;}
00180     void init(QIODevice& stream, const KUrl& url, time_t mtime);
00181     void initWriters(const KUrl& /*file*/);
00182     void operator=(const KFileMetaInfoPrivate& k) {
00183         items = k.items;
00184         kurl = k.kurl;
00185     }
00186 };
00187 static const KFileMetaInfoItem nullitem;
00188 
00189 //const KFileMetaInfoItem KFileMetaInfoPrivate::null;
00190 void
00191 KFileMetaInfoPrivate::init(QIODevice& stream, const KUrl& url, time_t mtime) {
00192     // get data from Strigi
00193     kurl = url;
00194     StreamAnalyzer& indexer = PredicatePropertyProvider::self()->indexer();
00195     KMetaInfoWriter writer;
00196     QIODeviceInputStream strigiStream(stream);
00197     kDebug() << url;
00198     AnalysisResult idx(url.url().toUtf8().constData(), mtime, writer, indexer);
00199 
00200     idx.setWriterData(&items);
00201     indexer.analyze(idx, &strigiStream);
00202 
00203     // TODO: get data from Nepomuk
00204 }
00205 void
00206 KFileMetaInfoPrivate::initWriters(const KUrl& file) {
00207     QStringList mimetypes;
00208     QHash<QString, KFileMetaInfoItem>::iterator i;
00209     for (i = items.begin(); i != items.end(); ++i) {
00210         KFileWritePlugin *w =
00211             KFileWriterProvider::self()->loadPlugin(i.key());
00212         if (w && w->canWrite(file, i.key())) {
00213             i.value().p->writer = w;
00214         }
00215     }
00216 }
00217 KFileMetaInfo::KFileMetaInfo(const QString& path, const QString& /*mimetype*/,
00218         KFileMetaInfo::WhatFlags /*w*/) :p(new KFileMetaInfoPrivate()) {
00219     QFileInfo fileinfo(path);
00220     QFile file(path);
00221     file.open(QIODevice::ReadOnly);
00222     KUrl u;
00223     u.setPath(path);
00224     p->init(file, u, fileinfo.lastModified().toTime_t());
00225     if (fileinfo.isWritable()) {
00226         p->initWriters(u);
00227     }
00228 }
00229 KFileMetaInfo::KFileMetaInfo(const KUrl& url) :p(new KFileMetaInfoPrivate()) {
00230     QFileInfo fileinfo(url.path());
00231     QFile file(url.path());
00232     file.open(QIODevice::ReadOnly);
00233     p->init(file, url, fileinfo.lastModified().toTime_t());
00234     if (fileinfo.isWritable()) {
00235         p->initWriters(url);
00236     }
00237 }
00238 KFileMetaInfo::KFileMetaInfo() :p(new KFileMetaInfoPrivate()) {
00239 }
00240 KFileMetaInfo::KFileMetaInfo(const KFileMetaInfo& k) :p(k.p) {
00241 }
00242 const KFileMetaInfo&
00243 KFileMetaInfo::operator=(KFileMetaInfo const& kfmi) {
00244     p = kfmi.p;
00245     return kfmi;
00246 }
00247 KFileMetaInfo::~KFileMetaInfo() {
00248 }
00249 bool
00250 KFileMetaInfo::applyChanges() {
00251     // go through all editable fields and group them by writer
00252     QHash<KFileWritePlugin*, QVariantMap> data;
00253     QHash<QString, KFileMetaInfoItem>::const_iterator i;
00254     for (i = p->items.begin(); i != p->items.end(); ++i) {
00255         if (i.value().isModified() && i.value().p->writer) {
00256             data[i.value().p->writer][i.key()] = i.value().value();
00257         }
00258     }
00259 
00260     // call the writers on the data they can write
00261     bool ok = true;
00262     QHash<KFileWritePlugin*, QVariantMap>::const_iterator j;
00263     QFile file(p->kurl.path());
00264     file.open(QIODevice::ReadOnly);
00265     for (j = data.begin(); j != data.end(); ++j) {
00266         ok &= j.key()->write(p->kurl, j.value());
00267     }
00268     return ok;
00269 }
00270 const KUrl&
00271 KFileMetaInfo::url() const {
00272     return p->kurl;
00273 }
00274 const QHash<QString, KFileMetaInfoItem>&
00275 KFileMetaInfo::items() const {
00276     return p->items;
00277 }
00278 const KFileMetaInfoItem&
00279 KFileMetaInfo::item(const QString& key) const {
00280     QHash<QString, KFileMetaInfoItem>::const_iterator i = p->items.find(key);
00281     return (i == p->items.end()) ?nullitem :i.value();
00282 }
00283 QStringList
00284 KFileMetaInfo::keys() const {
00285     return p->items.keys();
00286 }
00287 KFileMetaInfoItem&
00288 KFileMetaInfo::item(const QString& key) {
00289     return p->items[key];
00290 }
00291 bool
00292 KFileMetaInfo::isValid() const {
00293     return true;
00294 }
00295 QStringList KFileMetaInfo::preferredKeys() const { return QStringList(); }
00296 QStringList KFileMetaInfo::supportedKeys() const { return QStringList(); }
00297 
00298 KFileMetaInfoGroupList
00299 KFileMetaInfo::preferredGroups() const {
00300     return KFileMetaInfoGroupList();
00301 }
00302 KFileMetaInfoGroupList
00303 KFileMetaInfo::supportedGroups() const {
00304     return KFileMetaInfoGroupList();
00305 }
00306 KFileMetaInfoItemList
00307 KFileMetaInfoGroup::items() const {
00308     return KFileMetaInfoItemList();
00309 }
00310 const QString&
00311 KFileMetaInfoGroup::name() const {
00312     return p->name;
00313 }
00314 

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal