File: filter.cc

package info (click to toggle)
kio-extras 4%3A18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,364 kB
  • sloc: cpp: 25,948; ansic: 4,443; perl: 1,056; xml: 460; python: 28; sh: 20; makefile: 6
file content (205 lines) | stat: -rw-r--r-- 6,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
This file is part of KDE

 Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org)
 Copyright 2008 David Faure <faure@kde.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "filter.h"

#include <QCoreApplication>
#include <QFileInfo>
#include <QFile>
#include <QDebug>
#include <QUrl>
#include <QMimeType>
#include <QMimeDatabase>

#include <kfilterbase.h>
#include <kcompressiondevice.h>
#include <kfilterdev.h>

#include "loggingcategory.h"

extern "C" { Q_DECL_EXPORT int kdemain(int argc, char **argv); }

int kdemain( int argc, char ** argv)
{
  QCoreApplication app(argc, argv);
  app.setApplicationName("kio_filter");

  qDebug(KIO_FILTER_DEBUG) << "Starting";

  if (argc != 4)
  {
     fprintf(stderr, "Usage: kio_filter protocol domain-socket1 domain-socket2\n");
     exit(-1);
  }

  FilterProtocol slave(argv[1], argv[2], argv[3]);
  slave.dispatchLoop();

  qDebug(KIO_FILTER_DEBUG) << "Done";
  return 0;
}

FilterProtocol::FilterProtocol( const QByteArray & protocol, const QByteArray &pool, const QByteArray &app )
 : KIO::SlaveBase( protocol, pool, app )
{
    QString mimetype = QString::fromLatin1("application/x-") + QString::fromLatin1(protocol);
    filter = KCompressionDevice::filterForCompressionType(KFilterDev::compressionTypeForMimeType( mimetype ));
    Q_ASSERT(filter);
}

void FilterProtocol::get(const QUrl& url)
{
    // In the old solution, subURL would be set by setSubURL.
    // KDE4: now I simply assume bzip2:/localpath/file.bz2 and set subURL to the local path.
    subURL = url;
    subURL.setScheme("file");

    if (subURL.isEmpty()) {
        error(KIO::ERR_NO_SOURCE_PROTOCOL, QString::fromLatin1(mProtocol));
        return;
    }

    QFile localFile(url.path());
    if (!localFile.open(QIODevice::ReadOnly)) {
        error(KIO::ERR_COULD_NOT_READ, QString::fromLatin1(mProtocol));
        return;
    }

    if (!filter) {
        // TODO better error message
        error(KIO::ERR_INTERNAL, QString::fromLatin1(mProtocol));
        return;
    }

#if 0
  needSubUrlData();
#endif

  filter->init(QIODevice::ReadOnly);

  bool bNeedHeader = true;
  bool bNeedMimetype = true;
  bool bError = true;
  int result;

  QByteArray inputBuffer;
  inputBuffer.resize(8*1024);
  QByteArray outputBuffer;
  outputBuffer.resize(8*1024); // Start with a modest buffer
  filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
  while(true)
  {
     if (filter->inBufferEmpty())
     {
#if 0
        dataReq(); // Request data
        result = readData( inputBuffer);
#else
        result = localFile.read(inputBuffer.data(), inputBuffer.size());
#endif
        qDebug(KIO_FILTER_DEBUG) << "requestData: got " << result;
        if (result <= 0)
        {
          bError = true;
          break; // Unexpected EOF.
        }
        filter->setInBuffer( inputBuffer.data(), inputBuffer.size() );
     }
     if (bNeedHeader)
     {
        bError = !filter->readHeader();
        if (bError)
            break;
        bNeedHeader = false;
     }
     result = filter->uncompress();
     if ((filter->outBufferAvailable() == 0) || (result == KFilterBase::End))
     {
         qDebug(KIO_FILTER_DEBUG) << "avail_out = " << filter->outBufferAvailable();
        if (filter->outBufferAvailable() != 0)
        {
            // Discard unused space :-)
            outputBuffer.resize(outputBuffer.size() - filter->outBufferAvailable());
        }
        if (bNeedMimetype) {
            // Can we use the "base" filename? E.g. foo.txt.bz2
            const QString extension = QFileInfo(subURL.path()).suffix();
            QMimeDatabase db;
            QMimeType mime;
            if (extension == "gz" || extension == "bz" || extension == "bz2") {
                QString baseName = subURL.path();
                baseName.truncate(baseName.length() - extension.length() - 1 /*the dot*/);
                qDebug(KIO_FILTER_DEBUG) << "baseName=" << baseName;
                mime = db.mimeTypeForFileNameAndData(baseName, outputBuffer);
            } else {
                mime = db.mimeTypeForData(outputBuffer);
            }
            qDebug(KIO_FILTER_DEBUG) << "Emitting mimetype " << mime.name();
            mimeType( mime.name() );
            bNeedMimetype = false;
        }
        data( outputBuffer ); // Send data
        filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
        if (result == KFilterBase::End)
           break; // Finished.
     }
     if (result != KFilterBase::Ok)
     {
        bError = true;
        break; // Error
     }
  }

    if (!bError) {
#if 0
        dataReq(); // Request data
        result = readData( inputBuffer);
#else
        result = localFile.read(inputBuffer.data(), inputBuffer.size());
#endif
        qDebug(KIO_FILTER_DEBUG) << "requestData: got" << result << "(expecting 0)";
        data(QByteArray()); // Send EOF
    }

    filter->terminate();

    if (bError) {
        error(KIO::ERR_COULD_NOT_READ, subURL.url());
    } else {
        finished();
    }
    subURL = QUrl(); // Clear subURL
}

#if 0
void FilterProtocol::put( const KUrl &/*url*/, int, KIO::JobFlags /* _flags */ )
{
  error( KIO::ERR_UNSUPPORTED_ACTION, QString::fromLatin1("put"));
}

void FilterProtocol::setSubURL(const QUrl &url)
{
   subURL = url;
}
#endif