~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to kioslave/filter/filter.cc

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This file is part of KDE
 
3
 
 
4
 Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org)
 
5
 
 
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:
 
12
 
 
13
The above copyright notice and this permission notice shall be included in
 
14
all copies or substantial portions of the Software.
 
15
 
 
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.
 
22
*/
 
23
// $Id: filter.cc,v 1.6 2001/08/31 13:27:23 mueller Exp $
 
24
 
 
25
#include <unistd.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
 
 
29
#include <kinstance.h>
 
30
#include <kdebug.h>
 
31
#include <kmimemagic.h>
 
32
#include <kfilterbase.h>
 
33
 
 
34
#include "filter.h"
 
35
 
 
36
extern "C" { int kdemain(int argc, char **argv); }
 
37
 
 
38
int kdemain( int argc, char ** argv)
 
39
{
 
40
  KInstance instance( "kio_filter" );
 
41
 
 
42
  kdDebug(7110) << "Starting " << getpid() << endl;
 
43
 
 
44
  if (argc != 4)
 
45
  {
 
46
     fprintf(stderr, "Usage: kio_filter protocol domain-socket1 domain-socket2\n");
 
47
     exit(-1);
 
48
  }
 
49
 
 
50
  FilterProtocol slave(argv[1], argv[2], argv[3]);
 
51
  slave.dispatchLoop();
 
52
 
 
53
  kdDebug(7110) << "Done" << endl;
 
54
  return 0;
 
55
}
 
56
 
 
57
FilterProtocol::FilterProtocol( const QCString & protocol, const QCString &pool, const QCString &app )
 
58
 : KIO::SlaveBase( protocol, pool, app )
 
59
{
 
60
    QString mimetype = QString::fromLatin1("application/x-") + QString::fromLatin1(protocol);
 
61
    filter = KFilterBase::findFilterByMimeType( mimetype );
 
62
    Q_ASSERT(filter);
 
63
}
 
64
 
 
65
void FilterProtocol::get( const KURL & )
 
66
{
 
67
  if (subURL.isEmpty())
 
68
  {
 
69
     error( KIO::ERR_NO_SOURCE_PROTOCOL, mProtocol );
 
70
     return;
 
71
  }
 
72
  if (!filter)
 
73
  {
 
74
      error( KIO::ERR_INTERNAL, mProtocol );
 
75
      return;
 
76
  }
 
77
  needSubURLData();
 
78
 
 
79
  filter->init(IO_ReadOnly);
 
80
 
 
81
  bool bNeedHeader = true;
 
82
  bool bNeedMimetype = true;
 
83
  bool bError = true;
 
84
  int result;
 
85
 
 
86
  QByteArray inputBuffer;
 
87
  QByteArray outputBuffer(8*1024); // Start with a modest buffer
 
88
  filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
 
89
  while(true)
 
90
  {
 
91
     if (filter->inBufferEmpty())
 
92
     {
 
93
        dataReq(); // Request data
 
94
        result = readData( inputBuffer);
 
95
  kdDebug(7110) << "requestData: got " << result << endl;
 
96
        if (result <= 0)
 
97
        {
 
98
          bError = true;
 
99
          break; // Unexpected EOF.
 
100
        }
 
101
        filter->setInBuffer( inputBuffer.data(), inputBuffer.size() );
 
102
     }
 
103
     if (bNeedHeader)
 
104
     {
 
105
        bError = !filter->readHeader();
 
106
        if (bError)
 
107
            break;
 
108
        bNeedHeader = false;
 
109
     }
 
110
     result = filter->uncompress();
 
111
     if ((filter->outBufferAvailable() == 0) || (result == KFilterBase::END))
 
112
     {
 
113
         kdDebug(7110) << "avail_out = " << filter->outBufferAvailable() << endl;
 
114
        if (filter->outBufferAvailable() != 0)
 
115
        {
 
116
            // Discard unused space :-)
 
117
            outputBuffer.resize(outputBuffer.size() - filter->outBufferAvailable());
 
118
        }
 
119
        if (bNeedMimetype)
 
120
        {
 
121
            KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( outputBuffer, subURL.fileName() );
 
122
            kdDebug(7110) << "Emitting mimetype " << result->mimeType() << endl;
 
123
            mimeType( result->mimeType() );
 
124
            bNeedMimetype = false;
 
125
        }
 
126
        data( outputBuffer ); // Send data
 
127
        filter->setOutBuffer( outputBuffer.data(), outputBuffer.size() );
 
128
        if (result == KFilterBase::END)
 
129
           break; // Finished.
 
130
     }
 
131
     if (result != KFilterBase::OK)
 
132
     {
 
133
        bError = true;
 
134
        break; // Error
 
135
     }
 
136
  }
 
137
 
 
138
  if (!bError)
 
139
  {
 
140
     dataReq(); // Request data
 
141
     result = readData( inputBuffer);
 
142
  kdDebug(7110) << "requestData: got " << result << "(expecting 0)" << endl;
 
143
     data( QByteArray() ); // Send EOF
 
144
  }
 
145
 
 
146
  filter->terminate();
 
147
 
 
148
  if (bError)
 
149
  {
 
150
     error(KIO::ERR_COULD_NOT_READ, subURL.url());
 
151
     subURL = KURL(); // Clear subURL
 
152
     return;
 
153
  }
 
154
 
 
155
  subURL = KURL(); // Clear subURL
 
156
  finished();
 
157
}
 
158
 
 
159
void FilterProtocol::put( const KURL &/*url*/, int, bool /*_overwrite*/, bool /*_resume*/ )
 
160
{
 
161
  error( KIO::ERR_UNSUPPORTED_ACTION, QString::fromLatin1("put"));
 
162
}
 
163
 
 
164
void FilterProtocol::setSubURL(const KURL &url)
 
165
{
 
166
   subURL = url;
 
167
}
 
168