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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/* $Id$
This file is part of the KDE libraries
Copyright (C) 1997 Torben Weis ([email protected])
Copyright (C) 1998 Matthias Ettrich ([email protected])
Copyright (C) 1999 David Faure ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <cstring>
#include <tqstring.h>
#include <tqapplication.h>
#include <tqfile.h>
#include <tqmetaobject.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdetempfile.h>
#include <kdebug.h>
#include <kurl.h>
#include <tdeio/job.h>
#include <tdeio/scheduler.h>
#include "tdeio/netaccess.h"
using namespace TDEIO;
TQString * NetAccess::lastErrorMsg;
int NetAccess::lastErrorCode = 0;
TQStringList* NetAccess::tmpfiles;
bool NetAccess::download(const KURL& u, TQString & target)
{
return NetAccess::download (u, target, 0);
}
bool NetAccess::download(const KURL& u, TQString & target, TQWidget* window)
{
if (u.isLocalFile()) {
// file protocol. We do not need the network
target = u.path();
bool accessible = checkAccess(target, R_OK);
if(!accessible)
{
if(!lastErrorMsg)
lastErrorMsg = new TQString;
*lastErrorMsg = i18n("File '%1' is not readable").arg(target);
lastErrorCode = ERR_COULD_NOT_READ;
}
return accessible;
}
if (target.isEmpty())
{
KTempFile tmpFile;
target = tmpFile.name();
if (!tmpfiles)
tmpfiles = new TQStringList;
tmpfiles->append(target);
}
NetAccess kioNet;
KURL dest;
dest.setPath( target );
return kioNet.filecopyInternal( u, dest, -1, true /*overwrite*/,
false, window, false /*copy*/);
}
bool NetAccess::upload(const TQString& src, const KURL& target)
{
return NetAccess::upload(src, target, 0);
}
bool NetAccess::upload(const TQString& src, const KURL& target, TQWidget* window)
{
if (target.isEmpty())
return false;
// If target is local... well, just copy. This can be useful
// when the client code uses a temp file no matter what.
// Let's make sure it's not the exact same file though
if (target.isLocalFile() && target.path() == src)
return true;
NetAccess kioNet;
KURL s;
s.setPath(src);
return kioNet.filecopyInternal( s, target, -1, true /*overwrite*/,
false, window, false /*copy*/ );
}
bool NetAccess::copy( const KURL & src, const KURL & target )
{
return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, 0L );
}
bool NetAccess::copy( const KURL & src, const KURL & target, TQWidget* window )
{
return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, window );
}
bool NetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
bool overwrite, bool resume, TQWidget* window )
{
NetAccess kioNet;
return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
window, false /*copy*/ );
}
bool NetAccess::file_move( const KURL& src, const KURL& target, int permissions,
bool overwrite, bool resume, TQWidget* window )
{
NetAccess kioNet;
return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
window, true /*move*/ );
}
bool NetAccess::dircopy( const KURL & src, const KURL & target )
{
return NetAccess::dircopy( src, target, 0 );
}
bool NetAccess::dircopy( const KURL & src, const KURL & target, TQWidget* window )
{
KURL::List srcList;
srcList.append( src );
return NetAccess::dircopy( srcList, target, window );
}
bool NetAccess::dircopy( const KURL::List & srcList, const KURL & target, TQWidget* window )
{
NetAccess kioNet;
return kioNet.dircopyInternal( srcList, target, window, false /*copy*/ );
}
bool NetAccess::move( const KURL& src, const KURL& target, TQWidget* window )
{
KURL::List srcList;
srcList.append( src );
return NetAccess::move( srcList, target, window );
}
bool NetAccess::move( const KURL::List& srcList, const KURL& target, TQWidget* window )
{
NetAccess kioNet;
return kioNet.dircopyInternal( srcList, target, window, true /*move*/ );
}
bool NetAccess::exists( const KURL & url )
{
return NetAccess::exists( url, false, 0 );
}
bool NetAccess::exists( const KURL & url, TQWidget* window )
{
return NetAccess::exists( url, false, window );
}
bool NetAccess::exists( const KURL & url, bool source )
{
return NetAccess::exists( url, source, 0 );
}
bool NetAccess::exists( const KURL & url, bool source, TQWidget* window )
{
if ( url.isLocalFile() )
return TQFile::exists( url.path() );
NetAccess kioNet;
return kioNet.statInternal( url, 0 /*no details*/, source, window );
}
KURL NetAccess::localURL(const KURL& url, TQWidget* window)
{
NetAccess kioNet;
return kioNet.localURLInternal( url, window );
}
bool NetAccess::stat( const KURL & url, TDEIO::UDSEntry & entry )
{
return NetAccess::stat( url, entry, 0 );
}
bool NetAccess::stat( const KURL & url, TDEIO::UDSEntry & entry, TQWidget* window )
{
NetAccess kioNet;
bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
if (ret)
entry = kioNet.m_entry;
return ret;
}
KURL NetAccess::mostLocalURL(const KURL & url, TQWidget* window)
{
if ( url.isLocalFile() )
{
return url;
}
TDEIO::UDSEntry entry;
if (!stat(url, entry, window))
{
return url;
}
TQString path;
// Extract the local path from the TDEIO::UDSEntry
TDEIO::UDSEntry::ConstIterator it = entry.begin();
const TDEIO::UDSEntry::ConstIterator end = entry.end();
for ( ; it != end; ++it )
{
if ( (*it).m_uds == TDEIO::UDS_LOCAL_PATH )
{
path = (*it).m_str;
break;
}
}
if ( !path.isEmpty() )
{
KURL new_url;
new_url.setPath(path);
return new_url;
}
return url;
}
bool NetAccess::del( const KURL & url )
{
return NetAccess::del( url, 0 );
}
bool NetAccess::del( const KURL & url, TQWidget* window )
{
NetAccess kioNet;
return kioNet.delInternal( url, window );
}
bool NetAccess::mkdir( const KURL & url, int permissions )
{
return NetAccess::mkdir( url, 0, permissions );
}
bool NetAccess::mkdir( const KURL & url, TQWidget* window, int permissions )
{
NetAccess kioNet;
return kioNet.mkdirInternal( url, permissions, window );
}
TQString NetAccess::fish_execute( const KURL & url, const TQString command, TQWidget* window )
{
NetAccess kioNet;
return kioNet.fish_executeInternal( url, command, window );
}
bool NetAccess::synchronousRun( Job* job, TQWidget* window, TQByteArray* data,
KURL* finalURL, TQMap<TQString, TQString>* metaData )
{
NetAccess kioNet;
return kioNet.synchronousRunInternal( job, window, data, finalURL, metaData );
}
TQString NetAccess::mimetype( const KURL& url )
{
NetAccess kioNet;
return kioNet.mimetypeInternal( url, 0 );
}
TQString NetAccess::mimetype( const KURL& url, TQWidget* window )
{
NetAccess kioNet;
return kioNet.mimetypeInternal( url, window );
}
void NetAccess::removeTempFile(const TQString& name)
{
if (!tmpfiles)
return;
if (tmpfiles->contains(name))
{
unlink(TQFile::encodeName(name));
tmpfiles->remove(name);
}
}
bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
bool overwrite, bool resume, TQWidget* window, bool move)
{
bJobOK = true; // success unless further error occurs
TDEIO::Scheduler::checkSlaveOnHold(true);
TDEIO::Job * job = move
? TDEIO::file_move( src, target, permissions, overwrite, resume )
: TDEIO::file_copy( src, target, permissions, overwrite, resume );
job->setWindow (window);
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
TQWidget* window, bool move)
{
bJobOK = true; // success unless further error occurs
TDEIO::Job * job = move
? TDEIO::move( src, target )
: TDEIO::copy( src, target );
job->setWindow (window);
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::statInternal( const KURL & url, int details, bool source,
TQWidget* window )
{
bJobOK = true; // success unless further error occurs
TDEIO::StatJob * job = TDEIO::stat( url, !url.isLocalFile() );
job->setWindow (window);
job->setDetails( details );
job->setSide( source );
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
enter_loop();
return bJobOK;
}
KURL NetAccess::localURLInternal( const KURL & url, TQWidget* window )
{
m_localURL = url;
TDEIO::LocalURLJob* job = TDEIO::localURL(url);
job->setWindow (window);
connect(job, TQT_SIGNAL( localURL(TDEIO::Job*, const KURL&, bool) ),
this, TQT_SLOT( slotLocalURL(TDEIO::Job*, const KURL&, bool) ));
enter_loop();
return m_localURL;
}
bool NetAccess::delInternal( const KURL & url, TQWidget* window )
{
bJobOK = true; // success unless further error occurs
TDEIO::Job * job = TDEIO::del( url );
job->setWindow (window);
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::mkdirInternal( const KURL & url, int permissions,
TQWidget* window )
{
bJobOK = true; // success unless further error occurs
TDEIO::Job * job = TDEIO::mkdir( url, permissions );
job->setWindow (window);
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
enter_loop();
return bJobOK;
}
TQString NetAccess::mimetypeInternal( const KURL & url, TQWidget* window )
{
bJobOK = true; // success unless further error occurs
m_mimetype = TQString::fromLatin1("unknown");
TDEIO::Job * job = TDEIO::mimetype( url );
job->setWindow (window);
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
connect( job, TQT_SIGNAL( mimetype (TDEIO::Job *, const TQString &) ),
this, TQT_SLOT( slotMimetype (TDEIO::Job *, const TQString &) ) );
enter_loop();
return m_mimetype;
}
void NetAccess::slotMimetype( TDEIO::Job *, const TQString & type )
{
m_mimetype = type;
}
void NetAccess::slotLocalURL(TDEIO::Job*, const KURL & url, bool)
{
m_localURL = url;
tqApp->exit_loop();
}
TQString NetAccess::fish_executeInternal(const KURL & url, const TQString command, TQWidget* window)
{
TQString target, remoteTempFileName, resultData;
KURL tempPathUrl;
KTempFile tmpFile;
tmpFile.setAutoDelete( true );
if( url.protocol() == "fish" )
{
// construct remote temp filename
tempPathUrl = url;
remoteTempFileName = tmpFile.name();
// only need the filename KTempFile adds some KDE specific dirs
// that probably does not exist on the remote side
int pos = remoteTempFileName.findRev('/');
remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
tempPathUrl.setPath( remoteTempFileName );
bJobOK = true; // success unless further error occurs
TQByteArray packedArgs;
TQDataStream stream( packedArgs, IO_WriteOnly );
stream << int('X') << tempPathUrl << command;
TDEIO::Job * job = TDEIO::special( tempPathUrl, packedArgs, true );
job->setWindow( window );
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
enter_loop();
// since the TDEIO::special does not provide feedback we need to download the result
if( NetAccess::download( tempPathUrl, target, window ) )
{
TQFile resultFile( target );
if (resultFile.open( IO_ReadOnly ))
{
TQTextStream ts( &resultFile );
ts.setEncoding( TQTextStream::Locale ); // Locale??
resultData = ts.read();
resultFile.close();
NetAccess::del( tempPathUrl, window );
}
}
}
else
{
resultData = i18n( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
}
return resultData;
}
bool NetAccess::synchronousRunInternal( Job* job, TQWidget* window, TQByteArray* data,
KURL* finalURL, TQMap<TQString,TQString>* metaData )
{
job->setWindow( window );
m_metaData = metaData;
if ( m_metaData ) {
for ( TQMap<TQString, TQString>::iterator it = m_metaData->begin(); it != m_metaData->end(); ++it ) {
job->addMetaData( it.key(), it.data() );
}
}
if ( finalURL ) {
SimpleJob *sj = dynamic_cast<SimpleJob*>( job );
if ( sj ) {
m_url = sj->url();
}
}
connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
TQMetaObject *meta = job->metaObject();
static const char dataSignal[] = "data(TDEIO::Job*,const " "TQByteArray" "&)";
if ( meta->findSignal( dataSignal ) != -1 ) {
connect( job, TQT_SIGNAL(data(TDEIO::Job*,const TQByteArray&)),
this, TQT_SLOT(slotData(TDEIO::Job*,const TQByteArray&)) );
}
static const char redirSignal[] = "redirection(TDEIO::Job*,const KURL&)";
if ( meta->findSignal( redirSignal ) != -1 ) {
connect( job, TQT_SIGNAL(redirection(TDEIO::Job*,const KURL&)),
this, TQT_SLOT(slotRedirection(TDEIO::Job*, const KURL&)) );
}
enter_loop();
if ( finalURL )
*finalURL = m_url;
if ( data )
*data = m_data;
return bJobOK;
}
// If a troll sees this, he kills me
void tqt_enter_modal( TQWidget *widget );
void tqt_leave_modal( TQWidget *widget );
void NetAccess::enter_loop()
{
TQWidget dummy(0,0,(WFlags)(WType_Dialog | WShowModal));
dummy.setFocusPolicy( TQ_NoFocus );
tqt_enter_modal(&dummy);
tqApp->enter_loop();
tqt_leave_modal(&dummy);
}
void NetAccess::slotResult( TDEIO::Job * job )
{
lastErrorCode = job->error();
bJobOK = !job->error();
if ( !bJobOK ) {
if ( !lastErrorMsg ) {
lastErrorMsg = new TQString;
}
*lastErrorMsg = job->errorString();
}
if ( job->isA("TDEIO::StatJob") ) {
m_entry = static_cast<TDEIO::StatJob *>(job)->statResult();
}
if ( m_metaData ) {
*m_metaData = job->metaData();
}
tqApp->exit_loop();
}
void NetAccess::slotData( TDEIO::Job*, const TQByteArray& data )
{
if ( data.isEmpty() ) {
return;
}
unsigned offset = m_data.size();
m_data.resize( offset + data.size() );
std::memcpy( m_data.data() + offset, data.data(), data.size() );
}
void NetAccess::slotRedirection( TDEIO::Job*, const KURL& url )
{
m_url = url;
}
#include "netaccess.moc"
|