2
This file is part of KDE
4
Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org)
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
// $Id: filter.cc,v 1.6 2001/08/31 13:27:23 mueller Exp $
29
#include <kinstance.h>
31
#include <kmimemagic.h>
32
#include <kfilterbase.h>
36
extern "C" { int kdemain(int argc, char **argv); }
38
int kdemain( int argc, char ** argv)
40
KInstance instance( "kio_filter" );
42
kdDebug(7110) << "Starting " << getpid() << endl;
46
fprintf(stderr, "Usage: kio_filter protocol domain-socket1 domain-socket2\n");
50
FilterProtocol slave(argv[1], argv[2], argv[3]);
53
kdDebug(7110) << "Done" << endl;
57
FilterProtocol::FilterProtocol( const QCString & protocol, const QCString &pool, const QCString &app )
58
: KIO::SlaveBase( protocol, pool, app )
60
QString mimetype = QString::fromLatin1("application/x-") + QString::fromLatin1(protocol);
61
filter = KFilterBase::findFilterByMimeType( mimetype );
65
void FilterProtocol::get( const KURL & )
69
error( KIO::ERR_NO_SOURCE_PROTOCOL, mProtocol );
74
error( KIO::ERR_INTERNAL, mProtocol );
79
filter->init(IO_ReadOnly);
81
bool bNeedHeader = true;
82
bool bNeedMimetype = true;
86
QByteArray inputBuffer;
87
QByteArray outputBuffer(8*1024); // Start with a modest buffer
88
filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
91
if (filter->inBufferEmpty())
93
dataReq(); // Request data
94
result = readData( inputBuffer);
95
kdDebug(7110) << "requestData: got " << result << endl;
99
break; // Unexpected EOF.
101
filter->setInBuffer( inputBuffer.data(), inputBuffer.size() );
105
bError = !filter->readHeader();
110
result = filter->uncompress();
111
if ((filter->outBufferAvailable() == 0) || (result == KFilterBase::END))
113
kdDebug(7110) << "avail_out = " << filter->outBufferAvailable() << endl;
114
if (filter->outBufferAvailable() != 0)
116
// Discard unused space :-)
117
outputBuffer.resize(outputBuffer.size() - filter->outBufferAvailable());
121
KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( outputBuffer, subURL.fileName() );
122
kdDebug(7110) << "Emitting mimetype " << result->mimeType() << endl;
123
mimeType( result->mimeType() );
124
bNeedMimetype = false;
126
data( outputBuffer ); // Send data
127
filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
128
if (result == KFilterBase::END)
131
if (result != KFilterBase::OK)
140
dataReq(); // Request data
141
result = readData( inputBuffer);
142
kdDebug(7110) << "requestData: got " << result << "(expecting 0)" << endl;
143
data( QByteArray() ); // Send EOF
150
error(KIO::ERR_COULD_NOT_READ, subURL.url());
151
subURL = KURL(); // Clear subURL
155
subURL = KURL(); // Clear subURL
159
void FilterProtocol::put( const KURL &/*url*/, int, bool /*_overwrite*/, bool /*_resume*/ )
161
error( KIO::ERR_UNSUPPORTED_ACTION, QString::fromLatin1("put"));
164
void FilterProtocol::setSubURL(const KURL &url)