File indexing completed on 2024-05-12 15:36:35

0001 /*  This file is part of the KDE libraries
0002     Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation version 2.0
0007 
0008     This library is distributed in the hope that it will be useful,
0009     but WITHOUT ANY WARRANTY; without even the implied warranty of
0010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     Library General Public License for more details.
0012 
0013     You should have received a copy of the GNU Library General Public License
0014     along with this library; see the file COPYING.LIB.  If not, write to
0015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016     Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "metainfo.h"
0020 
0021 #include <QCoreApplication>
0022 #include <QUrl>
0023 #include <QDebug>
0024 #include <QDataStream>
0025 #include <kfilemetainfo.h>
0026 #include <klocalizedstring.h>
0027 #include <stdlib.h>
0028 
0029 // Recognized metadata entries:
0030 // mimeType     - the mime type of the file, so we need not extra determine it
0031 // what         - what to load
0032 
0033 // Pseudo plugin class to embed meta data
0034 class KIOPluginForMetaData : public QObject
0035 {
0036     Q_OBJECT
0037     Q_PLUGIN_METADATA(IID "org.kde.kio.slave.metainfo" FILE "metainfo.json")
0038 };
0039 
0040 using namespace KIO;
0041 
0042 extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
0043 {
0044     QCoreApplication::setApplicationName("kio_metainfo");
0045 
0046     QCoreApplication app(argc, argv);
0047 
0048     //KApplication app(argc, argv, "kio_metainfo", false, true);
0049 
0050     if (argc != 4) {
0051         qCritical() << "Usage: kio_metainfo protocol domain-socket1 domain-socket2" << endl;
0052         exit(-1);
0053     }
0054 
0055     MetaInfoProtocol slave(argv[2], argv[3]);
0056     slave.dispatchLoop();
0057 
0058     return 0;
0059 }
0060 
0061 MetaInfoProtocol::MetaInfoProtocol(const QByteArray &pool, const QByteArray &app)
0062     : SlaveBase("metainfo", pool, app)
0063 {
0064 }
0065 
0066 MetaInfoProtocol::~MetaInfoProtocol()
0067 {
0068 }
0069 
0070 void MetaInfoProtocol::get(const QUrl &url)
0071 {
0072     QString mimeType = metaData("mimeType");
0073     KFileMetaInfo info(url.toLocalFile(), mimeType);
0074 
0075     QByteArray arr;
0076     QDataStream stream(&arr, QIODevice::WriteOnly);
0077 
0078     stream << info;
0079 
0080     data(arr);
0081     finished();
0082 }
0083 
0084 void MetaInfoProtocol::put(const QUrl &url, int, KIO::JobFlags)
0085 {
0086     QString mimeType = metaData("mimeType");
0087     KFileMetaInfo info;
0088 
0089     QByteArray arr;
0090     readData(arr);
0091     QDataStream stream(arr);
0092 
0093     stream >> info;
0094 
0095     if (info.isValid()) {
0096         info.applyChanges();
0097     } else {
0098         error(ERR_NO_CONTENT, i18n("No metainfo for %1", url.path()));
0099         return;
0100     }
0101     finished();
0102 }
0103 
0104 #include "metainfo.moc"