2 This file is part of KDE
4 Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org)
5 Copyright 2008 David Faure <faure@kde.org>
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <kcomponentdata.h>
31 #include <kmimetype.h>
32 #include <kfilterbase.h>
35 extern "C" { KDE_EXPORT
int kdemain(int argc
, char **argv
); }
37 int kdemain( int argc
, char ** argv
)
39 KComponentData
componentData( "kio_filter" );
41 kDebug(7110) << "Starting";
45 fprintf(stderr
, "Usage: kio_filter protocol domain-socket1 domain-socket2\n");
49 FilterProtocol
slave(argv
[1], argv
[2], argv
[3]);
52 kDebug(7110) << "Done";
56 FilterProtocol::FilterProtocol( const QByteArray
& protocol
, const QByteArray
&pool
, const QByteArray
&app
)
57 : KIO::SlaveBase( protocol
, pool
, app
)
59 QString mimetype
= QString::fromLatin1("application/x-") + QString::fromLatin1(protocol
);
60 filter
= KFilterBase::findFilterByMimeType( mimetype
);
64 void FilterProtocol::get(const KUrl
& url
)
66 // In the old solution, subURL would be set by setSubURL.
67 // KDE4: now I simply assume bzip2:/localpath/file.bz2 and set subURL to the local path.
69 subURL
.setProtocol("file");
71 if (subURL
.isEmpty()) {
72 error( KIO::ERR_NO_SOURCE_PROTOCOL
, mProtocol
);
76 QFile
localFile(url
.path());
77 if (!localFile
.open(QIODevice::ReadOnly
)) {
78 error( KIO::ERR_COULD_NOT_READ
, mProtocol
);
83 // TODO better error message
84 error( KIO::ERR_INTERNAL
, mProtocol
);
92 filter
->init(QIODevice::ReadOnly
);
94 bool bNeedHeader
= true;
95 bool bNeedMimetype
= true;
99 QByteArray inputBuffer
;
100 inputBuffer
.resize(8*1024);
101 QByteArray outputBuffer
;
102 outputBuffer
.resize(8*1024); // Start with a modest buffer
103 filter
->setOutBuffer( outputBuffer
.data(), outputBuffer
.size() );
106 if (filter
->inBufferEmpty())
109 dataReq(); // Request data
110 result
= readData( inputBuffer
);
112 result
= localFile
.read(inputBuffer
.data(), inputBuffer
.size());
114 kDebug(7110) << "requestData: got " << result
;
118 break; // Unexpected EOF.
120 filter
->setInBuffer( inputBuffer
.data(), inputBuffer
.size() );
124 bError
= !filter
->readHeader();
129 result
= filter
->uncompress();
130 if ((filter
->outBufferAvailable() == 0) || (result
== KFilterBase::End
))
132 kDebug(7110) << "avail_out = " << filter
->outBufferAvailable();
133 if (filter
->outBufferAvailable() != 0)
135 // Discard unused space :-)
136 outputBuffer
.resize(outputBuffer
.size() - filter
->outBufferAvailable());
139 // Can we use the "base" filename? E.g. foo.txt.bz2
140 const QString extension
= QFileInfo(subURL
.path()).suffix();
142 if (extension
== "gz" || extension
== "bz" || extension
== "bz2") {
143 QString baseName
= subURL
.path();
144 baseName
.truncate(baseName
.length() - extension
.length() - 1 /*the dot*/);
145 kDebug(7110) << "baseName=" << baseName
;
146 mime
= KMimeType::findByNameAndContent(baseName
, outputBuffer
);
148 mime
= KMimeType::findByContent(outputBuffer
);
150 kDebug(7110) << "Emitting mimetype " << mime
->name();
151 mimeType( mime
->name() );
152 bNeedMimetype
= false;
154 data( outputBuffer
); // Send data
155 filter
->setOutBuffer( outputBuffer
.data(), outputBuffer
.size() );
156 if (result
== KFilterBase::End
)
159 if (result
!= KFilterBase::Ok
)
168 dataReq(); // Request data
169 result
= readData( inputBuffer
);
171 result
= localFile
.read(inputBuffer
.data(), inputBuffer
.size());
173 kDebug(7110) << "requestData: got" << result
<< "(expecting 0)";
174 data(QByteArray()); // Send EOF
180 error(KIO::ERR_COULD_NOT_READ
, subURL
.url());
184 subURL
= KUrl(); // Clear subURL
188 void FilterProtocol::put( const KUrl
&/*url*/, int, KIO::JobFlags
/* _flags */ )
190 error( KIO::ERR_UNSUPPORTED_ACTION
, QString::fromLatin1("put"));
193 void FilterProtocol::setSubURL(const KUrl
&url
)