filter.cc 5.63 KB
Newer Older
1 2 3 4
/*
This file is part of KDE

 Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org)
5
 Copyright 2008 David Faure <faure@kde.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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.
*/

Gabriel Anderson's avatar
Gabriel Anderson committed
25
#include "filter.h"
26 27

#include <QCoreApplication>
28 29
#include <QFileInfo>
#include <QFile>
30 31 32 33
#include <QDebug>
#include <QUrl>
#include <QMimeType>
#include <QMimeDatabase>
34 35

#include <kfilterbase.h>
36 37
#include <kcompressiondevice.h>
#include <kfilterdev.h>
38 39

#include "loggingcategory.h"
40

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

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

48
  qDebug(KIO_FILTER_DEBUG) << "Starting";
49 50 51 52 53 54 55 56 57 58

  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();

59
  qDebug(KIO_FILTER_DEBUG) << "Done";
60 61 62
  return 0;
}

Laurent Montel's avatar
Laurent Montel committed
63
FilterProtocol::FilterProtocol( const QByteArray & protocol, const QByteArray &pool, const QByteArray &app )
64 65
 : KIO::SlaveBase( protocol, pool, app )
{
66
    QString mimetype = QString::fromLatin1("application/x-") + QString::fromLatin1(protocol);
67
    filter = KCompressionDevice::filterForCompressionType(KFilterDev::compressionTypeForMimeType( mimetype ));
Dirk Mueller's avatar
compile  
Dirk Mueller committed
68
    Q_ASSERT(filter);
69 70
}

71
void FilterProtocol::get(const QUrl& url)
72
{
73 74 75
    // 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;
76
    subURL.setScheme("file");
77 78

    if (subURL.isEmpty()) {
79
        error(KIO::ERR_NO_SOURCE_PROTOCOL, QString::fromLatin1(mProtocol));
80 81 82 83 84
        return;
    }

    QFile localFile(url.path());
    if (!localFile.open(QIODevice::ReadOnly)) {
Laurent Montel's avatar
Laurent Montel committed
85
        error(KIO::ERR_CANNOT_READ, QString::fromLatin1(mProtocol));
86 87 88 89 90
        return;
    }

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

95
  filter->init(QIODevice::ReadOnly);
96 97 98 99 100 101 102

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

  QByteArray inputBuffer;
103
  inputBuffer.resize(8*1024);
Dirk Mueller's avatar
Dirk Mueller committed
104 105
  QByteArray outputBuffer;
  outputBuffer.resize(8*1024); // Start with a modest buffer
106 107 108 109 110
  filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
  while(true)
  {
     if (filter->inBufferEmpty())
     {
111
        result = localFile.read(inputBuffer.data(), inputBuffer.size());
112
        qDebug(KIO_FILTER_DEBUG) << "requestData: got " << result;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        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();
128
     if ((filter->outBufferAvailable() == 0) || (result == KFilterBase::End))
129
     {
130
         qDebug(KIO_FILTER_DEBUG) << "avail_out = " << filter->outBufferAvailable();
131 132 133 134 135
        if (filter->outBufferAvailable() != 0)
        {
            // Discard unused space :-)
            outputBuffer.resize(outputBuffer.size() - filter->outBufferAvailable());
        }
136 137 138
        if (bNeedMimetype) {
            // Can we use the "base" filename? E.g. foo.txt.bz2
            const QString extension = QFileInfo(subURL.path()).suffix();
139 140
            QMimeDatabase db;
            QMimeType mime;
141 142 143
            if (extension == "gz" || extension == "bz" || extension == "bz2") {
                QString baseName = subURL.path();
                baseName.truncate(baseName.length() - extension.length() - 1 /*the dot*/);
144 145
                qDebug(KIO_FILTER_DEBUG) << "baseName=" << baseName;
                mime = db.mimeTypeForFileNameAndData(baseName, outputBuffer);
146
            } else {
147
                mime = db.mimeTypeForData(outputBuffer);
148
            }
149 150
            qDebug(KIO_FILTER_DEBUG) << "Emitting mimetype " << mime.name();
            mimeType( mime.name() );
151 152 153 154
            bNeedMimetype = false;
        }
        data( outputBuffer ); // Send data
        filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
155
        if (result == KFilterBase::End)
156 157
           break; // Finished.
     }
158
     if (result != KFilterBase::Ok)
159 160 161 162 163 164
     {
        bError = true;
        break; // Error
     }
  }

165 166
    if (!bError) {
        result = localFile.read(inputBuffer.data(), inputBuffer.size());
167
        qDebug(KIO_FILTER_DEBUG) << "requestData: got" << result << "(expecting 0)";
168 169 170 171 172 173
        data(QByteArray()); // Send EOF
    }

    filter->terminate();

    if (bError) {
Laurent Montel's avatar
Laurent Montel committed
174
        error(KIO::ERR_CANNOT_READ, subURL.url());
175 176 177
    } else {
        finished();
    }
178
    subURL = QUrl(); // Clear subURL
179
}