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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/***************************************************************************
* Copyright (C) 2004-2007 by Georgy Yunaev, gyunaev@ulduzsoft.com *
* Please do not use email address above for bug reports; see *
* the README file *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kinstance.h>
#include <kurl.h>
#include <kmimemagic.h>
#include <tqfile.h>
#include <tqbitarray.h>
#include <tqvaluevector.h>
#include "msits.h"
#include "libchmurlfactory.h"
using namespace TDEIO;
extern "C"
{
int kdemain( int argc, char **argv )
{
kdDebug() << "*** tdeio_msits Init" << endl;
TDEInstance instance( "tdeio_msits" );
if ( argc != 4 )
{
kdDebug() << "Usage: tdeio_msits protocol domain-socket1 domain-socket2" << endl;
exit (-1);
}
ProtocolMSITS slave ( argv[2], argv[3] );
slave.dispatchLoop();
kdDebug() << "*** tdeio_msits Done" << endl;
return 0;
}
}
ProtocolMSITS::ProtocolMSITS (const TQCString &pool_socket, const TQCString &app_socket)
: SlaveBase ("tdeio_msits", pool_socket, app_socket)
{
m_chmFile = 0;
}
ProtocolMSITS::~ProtocolMSITS()
{
if ( !m_chmFile )
return;
chm_close (m_chmFile);
m_chmFile = 0;
}
// A simple stat() wrapper
static bool isDirectory ( const TQString & filename )
{
return filename[filename.length() - 1] == '/';
}
void ProtocolMSITS::get( const KURL& url )
{
TQString htmdata, fileName;
chmUnitInfo ui;
TQByteArray buf;
kdDebug() << "tdeio_msits::get() " << url.path() << endl;
if ( !parseLoadAndLookup ( url, fileName ) )
return; // error() has been called by parseLoadAndLookup
kdDebug() << "tdeio_msits::get: parseLoadAndLookup returned " << fileName << endl;
if ( LCHMUrlFactory::handleFileType( url.path(), htmdata ) )
{
buf = htmdata.utf8();
kdDebug() << "Using special handling for image pages: " << htmdata << endl;
}
else
{
if ( isDirectory (fileName) )
{
error( TDEIO::ERR_IS_DIRECTORY, url.prettyURL() );
return;
}
if ( !ResolveObject ( fileName, &ui) )
{
kdDebug() << "tdeio_msits::get: could not resolve filename " << fileName << endl;
error( TDEIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
return;
}
buf.resize( ui.length );
if ( RetrieveObject (&ui, (unsigned char*) buf.data(), 0, ui.length) == 0 )
{
kdDebug() << "tdeio_msits::get: could not retrieve filename " << fileName << endl;
error( TDEIO::ERR_NO_CONTENT, url.prettyURL() );
return;
}
}
totalSize( buf.size() );
KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( buf, fileName );
kdDebug() << "Emitting mimetype " << result->mimeType() << endl;
mimeType( result->mimeType() );
data( buf );
processedSize( buf.size() );
finished();
}
bool ProtocolMSITS::parseLoadAndLookup ( const KURL& url, TQString& abspath )
{
kdDebug() << "ProtocolMSITS::parseLoadAndLookup (const KURL&) " << url.path() << endl;
int pos = url.path().find ("::");
if ( pos == -1 )
{
error( TDEIO::ERR_MALFORMED_URL, url.prettyURL() );
return false;
}
TQString filename = url.path().left (pos);
abspath = url.path().mid (pos + 2); // skip ::
// Some buggy apps add ms-its:/ to the path as well
if ( abspath.startsWith( "ms-its:" ) )
abspath = abspath.mid( 7 );
kdDebug() << "ProtocolMSITS::parseLoadAndLookup: filename " << filename << ", path " << abspath << endl;
if ( filename.isEmpty() )
{
error( TDEIO::ERR_MALFORMED_URL, url.prettyURL() );
return false;
}
// If the file has been already loaded, nothing to do.
if ( m_chmFile && filename == m_openedFile )
return true;
kdDebug() << "Opening a new CHM file " << filename << endl;
// First try to open a temporary file
chmFile * tmpchm;
if ( (tmpchm = chm_open ( TQFile::encodeName (filename))) == 0 )
{
error( TDEIO::ERR_COULD_NOT_READ, url.prettyURL() );
return false;
}
// Replace an existing file by a new one
if ( m_chmFile )
chm_close (m_chmFile);
m_chmFile = tmpchm;
m_openedFile = filename;
kdDebug() << "A CHM file " << filename << " has beed opened successfully" << endl;
return true;
}
/*
* Shamelessly stolen from a KDE KIO tutorial
*/
static void app_entry(UDSEntry& e, unsigned int uds, const TQString& str)
{
UDSAtom a;
a.m_uds = uds;
a.m_str = str;
e.append(a);
}
// appends an int with the UDS-ID uds
static void app_entry(UDSEntry& e, unsigned int uds, long l)
{
UDSAtom a;
a.m_uds = uds;
a.m_long = l;
e.append(a);
}
// internal function
// fills a directory item with its name and size
static void app_dir(UDSEntry& e, const TQString & name)
{
e.clear();
app_entry(e, TDEIO::UDS_NAME, name);
app_entry(e, TDEIO::UDS_FILE_TYPE, S_IFDIR);
app_entry(e, TDEIO::UDS_SIZE, 1);
}
// internal function
// fills a file item with its name and size
static void app_file(UDSEntry& e, const TQString & name, size_t size)
{
e.clear();
app_entry(e, TDEIO::UDS_NAME, name);
app_entry(e, TDEIO::UDS_FILE_TYPE, S_IFREG);
app_entry(e, TDEIO::UDS_SIZE, size);
}
void ProtocolMSITS::stat (const KURL & url)
{
TQString fileName;
chmUnitInfo ui;
kdDebug() << "tdeio_msits::stat (const KURL& url) " << url.path() << endl;
if ( !parseLoadAndLookup ( url, fileName ) )
return; // error() has been called by parseLoadAndLookup
if ( !ResolveObject ( fileName, &ui ) )
{
error( TDEIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
return;
}
kdDebug() << "tdeio_msits::stat: adding an entry for " << fileName << endl;
UDSEntry entry;
if ( isDirectory ( fileName ) )
app_dir(entry, fileName);
else
app_file(entry, fileName, ui.length);
statEntry (entry);
finished();
}
// A local CHMLIB enumerator
static int chmlib_enumerator (struct chmFile *, struct chmUnitInfo *ui, void *context)
{
((TQValueVector<TQString> *) context)->push_back (TQString::fromLocal8Bit (ui->path));
return CHM_ENUMERATOR_CONTINUE;
}
void ProtocolMSITS::listDir (const KURL & url)
{
TQString filepath;
kdDebug() << "tdeio_msits::listDir (const KURL& url) " << url.path() << endl;
if ( !parseLoadAndLookup ( url, filepath ) )
return; // error() has been called by parseLoadAndLookup
filepath += "/";
if ( !isDirectory (filepath) )
{
error(TDEIO::ERR_CANNOT_ENTER_DIRECTORY, url.path());
return;
}
kdDebug() << "tdeio_msits::listDir: enumerating directory " << filepath << endl;
TQValueVector<TQString> listing;
if ( chm_enumerate_dir ( m_chmFile,
filepath.local8Bit(),
CHM_ENUMERATE_NORMAL | CHM_ENUMERATE_FILES | CHM_ENUMERATE_DIRS,
chmlib_enumerator,
&listing ) != 1 )
{
error(TDEIO::ERR_CANNOT_ENTER_DIRECTORY, url.path());
return;
}
UDSEntry entry;
unsigned int striplength = filepath.length();
for ( unsigned int i = 0; i < listing.size(); i++ )
{
// Strip the direcroty name
TQString ename = listing[i].mid (striplength);
if ( isDirectory ( ename ) )
app_dir(entry, ename);
else
app_file(entry, ename, 0);
listEntry(entry, false);
}
listEntry(entry, true);
finished();
}
|