1 /*  This file is part of the KDE libraries
2     Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation version 2.0
7 
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11     Library General Public License for more details.
12 
13     You should have received a copy of the GNU Library General Public License
14     along with this library; see the file COPYING.LIB.  If not, write to
15     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16     Boston, MA 02110-1301, USA.
17 */
18 
19 #include "metainfo.h"
20 
21 #include <QCoreApplication>
22 #include <QUrl>
23 #include <QDebug>
24 #include <QDataStream>
25 #include <kfilemetainfo.h>
26 #include <klocalizedstring.h>
27 #include <stdlib.h>
28 
29 // Recognized metadata entries:
30 // mimeType     - the mime type of the file, so we need not extra determine it
31 // what         - what to load
32 
33 // Pseudo plugin class to embed meta data
34 class KIOPluginForMetaData : public QObject
35 {
36     Q_OBJECT
37     Q_PLUGIN_METADATA(IID "org.kde.kio.slave.metainfo" FILE "metainfo.json")
38 };
39 
40 using namespace KIO;
41 
kdemain(int argc,char ** argv)42 extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
43 {
44     QCoreApplication::setApplicationName("kio_metainfo");
45 
46     QCoreApplication app(argc, argv);
47 
48     //KApplication app(argc, argv, "kio_metainfo", false, true);
49 
50     if (argc != 4) {
51         qCritical() << "Usage: kio_metainfo protocol domain-socket1 domain-socket2" << endl;
52         exit(-1);
53     }
54 
55     MetaInfoProtocol slave(argv[2], argv[3]);
56     slave.dispatchLoop();
57 
58     return 0;
59 }
60 
MetaInfoProtocol(const QByteArray & pool,const QByteArray & app)61 MetaInfoProtocol::MetaInfoProtocol(const QByteArray &pool, const QByteArray &app)
62     : SlaveBase("metainfo", pool, app)
63 {
64 }
65 
~MetaInfoProtocol()66 MetaInfoProtocol::~MetaInfoProtocol()
67 {
68 }
69 
get(const QUrl & url)70 void MetaInfoProtocol::get(const QUrl &url)
71 {
72     QString mimeType = metaData("mimeType");
73     KFileMetaInfo info(url.toLocalFile(), mimeType);
74 
75     QByteArray arr;
76     QDataStream stream(&arr, QIODevice::WriteOnly);
77 
78     stream << info;
79 
80     data(arr);
81     finished();
82 }
83 
put(const QUrl & url,int,KIO::JobFlags)84 void MetaInfoProtocol::put(const QUrl &url, int, KIO::JobFlags)
85 {
86     QString mimeType = metaData("mimeType");
87     KFileMetaInfo info;
88 
89     QByteArray arr;
90     readData(arr);
91     QDataStream stream(arr);
92 
93     stream >> info;
94 
95     if (info.isValid()) {
96         info.applyChanges();
97     } else {
98         error(ERR_NO_CONTENT, i18n("No metainfo for %1", url.path()));
99         return;
100     }
101     finished();
102 }
103 
104 #include "metainfo.moc"