delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kioslave / filter / filter.cc
blob8866f84f2a41b85e10774707a7d8782967da8a39
1 /*
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.
25 #include "filter.h"
26 #include <QFileInfo>
27 #include <QFile>
29 #include <kcomponentdata.h>
30 #include <kdebug.h>
31 #include <kmimetype.h>
32 #include <kfilterbase.h>
33 #include <kurl.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";
43 if (argc != 4)
45 fprintf(stderr, "Usage: kio_filter protocol domain-socket1 domain-socket2\n");
46 exit(-1);
49 FilterProtocol slave(argv[1], argv[2], argv[3]);
50 slave.dispatchLoop();
52 kDebug(7110) << "Done";
53 return 0;
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 );
61 Q_ASSERT(filter);
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.
68 subURL = url;
69 subURL.setProtocol("file");
71 if (subURL.isEmpty()) {
72 error( KIO::ERR_NO_SOURCE_PROTOCOL, mProtocol );
73 return;
76 QFile localFile(url.path());
77 if (!localFile.open(QIODevice::ReadOnly)) {
78 error( KIO::ERR_COULD_NOT_READ, mProtocol );
79 return;
82 if (!filter) {
83 // TODO better error message
84 error( KIO::ERR_INTERNAL, mProtocol );
85 return;
88 #if 0
89 needSubUrlData();
90 #endif
92 filter->init(QIODevice::ReadOnly);
94 bool bNeedHeader = true;
95 bool bNeedMimetype = true;
96 bool bError = true;
97 int result;
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() );
104 while(true)
106 if (filter->inBufferEmpty())
108 #if 0
109 dataReq(); // Request data
110 result = readData( inputBuffer);
111 #else
112 result = localFile.read(inputBuffer.data(), inputBuffer.size());
113 #endif
114 kDebug(7110) << "requestData: got " << result;
115 if (result <= 0)
117 bError = true;
118 break; // Unexpected EOF.
120 filter->setInBuffer( inputBuffer.data(), inputBuffer.size() );
122 if (bNeedHeader)
124 bError = !filter->readHeader();
125 if (bError)
126 break;
127 bNeedHeader = false;
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());
138 if (bNeedMimetype) {
139 // Can we use the "base" filename? E.g. foo.txt.bz2
140 const QString extension = QFileInfo(subURL.path()).suffix();
141 KMimeType::Ptr mime;
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);
147 } else {
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)
157 break; // Finished.
159 if (result != KFilterBase::Ok)
161 bError = true;
162 break; // Error
166 if (!bError) {
167 #if 0
168 dataReq(); // Request data
169 result = readData( inputBuffer);
170 #else
171 result = localFile.read(inputBuffer.data(), inputBuffer.size());
172 #endif
173 kDebug(7110) << "requestData: got" << result << "(expecting 0)";
174 data(QByteArray()); // Send EOF
177 filter->terminate();
179 if (bError) {
180 error(KIO::ERR_COULD_NOT_READ, subURL.url());
181 } else {
182 finished();
184 subURL = KUrl(); // Clear subURL
187 #if 0
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)
195 subURL = url;
197 #endif