~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to vcs/compat/netaccess/netaccess.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
    This file is part of the KDE libraries
 
3
    Copyright (C) 1997 Torben Weis (weis@kde.org)
 
4
    Copyright (C) 1998 Matthias Ettrich (ettrich@kde.org)
 
5
    Copyright (C) 1999 David Faure (faure@kde.org)
 
6
 
 
7
    This library is free software; you can redistribute it and/or
 
8
    modify it under the terms of the GNU Library General Public
 
9
    License as published by the Free Software Foundation; either
 
10
    version 2 of the License, or (at your option) any later version.
 
11
 
 
12
    This library is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
    Library General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU Library General Public License
 
18
    along with this library; see the file COPYING.LIB.  If not, write to
 
19
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
    Boston, MA 02111-1307, USA.
 
21
*/
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <signal.h>
 
26
#include <unistd.h>
 
27
 
 
28
#include <cstring>
 
29
 
 
30
#include <qstring.h>
 
31
#include <qapplication.h>
 
32
#include <qfile.h>
 
33
#include <qmetaobject.h>
 
34
 
 
35
#include <kapplication.h>
 
36
#include <klocale.h>
 
37
#include <ktempfile.h>
 
38
#include <kdebug.h>
 
39
#include <kurl.h>
 
40
#include <kio/job.h>
 
41
#include <kio/scheduler.h>
 
42
 
 
43
#include "netaccess.h"
 
44
 
 
45
using namespace KIO_COMPAT;
 
46
 
 
47
QString * NetAccess::lastErrorMsg;
 
48
int NetAccess::lastErrorCode = 0;
 
49
QStringList* NetAccess::tmpfiles;
 
50
 
 
51
bool NetAccess::download(const KURL& u, QString & target)
 
52
{
 
53
  return NetAccess::download (u, target, 0);
 
54
}
 
55
 
 
56
bool NetAccess::download(const KURL& u, QString & target, QWidget* window)
 
57
{
 
58
  if (u.isLocalFile()) {
 
59
    // file protocol. We do not need the network
 
60
    target = u.path();
 
61
    bool accessible = checkAccess(target, R_OK);
 
62
    if(!accessible)
 
63
    {
 
64
        if(!lastErrorMsg)
 
65
            lastErrorMsg = new QString;
 
66
        *lastErrorMsg = i18n("File '%1' is not readable").arg(target);
 
67
        lastErrorCode = KIO::ERR_COULD_NOT_READ;
 
68
    }
 
69
    return accessible;
 
70
  }
 
71
 
 
72
  if (target.isEmpty())
 
73
  {
 
74
      KTempFile tmpFile;
 
75
      target = tmpFile.name();
 
76
      if (!tmpfiles)
 
77
          tmpfiles = new QStringList;
 
78
      tmpfiles->append(target);
 
79
  }
 
80
 
 
81
  NetAccess kioNet;
 
82
  KURL dest;
 
83
  dest.setPath( target );
 
84
  return kioNet.filecopyInternal( u, dest, -1, true /*overwrite*/,
 
85
                                  false, window, false /*copy*/);
 
86
}
 
87
 
 
88
bool NetAccess::upload(const QString& src, const KURL& target)
 
89
{
 
90
  return NetAccess::upload(src, target, 0);
 
91
}
 
92
 
 
93
bool NetAccess::upload(const QString& src, const KURL& target, QWidget* window)
 
94
{
 
95
  if (target.isEmpty())
 
96
    return false;
 
97
 
 
98
  // If target is local... well, just copy. This can be useful
 
99
  // when the client code uses a temp file no matter what.
 
100
  // Let's make sure it's not the exact same file though
 
101
  if (target.isLocalFile() && target.path() == src)
 
102
    return true;
 
103
 
 
104
  NetAccess kioNet;
 
105
  KURL s;
 
106
  s.setPath(src);
 
107
  return kioNet.filecopyInternal( s, target, -1, true /*overwrite*/,
 
108
                                  false, window, false /*copy*/ );
 
109
}
 
110
 
 
111
bool NetAccess::copy( const KURL & src, const KURL & target )
 
112
{
 
113
  return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, 0L );
 
114
}
 
115
 
 
116
bool NetAccess::copy( const KURL & src, const KURL & target, QWidget* window )
 
117
{
 
118
  return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, window );
 
119
}
 
120
 
 
121
bool NetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
 
122
                           bool overwrite, bool resume, QWidget* window )
 
123
{
 
124
  NetAccess kioNet;
 
125
  return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
 
126
                                  window, false /*copy*/ );
 
127
}
 
128
 
 
129
 
 
130
bool NetAccess::file_move( const KURL& src, const KURL& target, int permissions,
 
131
                           bool overwrite, bool resume, QWidget* window )
 
132
{
 
133
  NetAccess kioNet;
 
134
  return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
 
135
                                  window, true /*move*/ );
 
136
}
 
137
 
 
138
bool NetAccess::dircopy( const KURL & src, const KURL & target )
 
139
{
 
140
  return NetAccess::dircopy( src, target, 0 );
 
141
}
 
142
 
 
143
bool NetAccess::dircopy( const KURL & src, const KURL & target, QWidget* window )
 
144
{
 
145
  KURL::List srcList;
 
146
  srcList.append( src );
 
147
  return NetAccess::dircopy( srcList, target, window );
 
148
}
 
149
 
 
150
bool NetAccess::dircopy( const KURL::List & srcList, const KURL & target, QWidget* window )
 
151
{
 
152
  NetAccess kioNet;
 
153
  return kioNet.dircopyInternal( srcList, target, window, false /*copy*/ );
 
154
}
 
155
 
 
156
bool NetAccess::move( const KURL& src, const KURL& target, QWidget* window )
 
157
{
 
158
  KURL::List srcList;
 
159
  srcList.append( src );
 
160
  return NetAccess::move( srcList, target, window );
 
161
}
 
162
 
 
163
bool NetAccess::move( const KURL::List& srcList, const KURL& target, QWidget* window )
 
164
{
 
165
  NetAccess kioNet;
 
166
  return kioNet.dircopyInternal( srcList, target, window, true /*move*/ );
 
167
}
 
168
 
 
169
bool NetAccess::exists( const KURL & url )
 
170
{
 
171
  return NetAccess::exists( url, false, 0 );
 
172
}
 
173
 
 
174
bool NetAccess::exists( const KURL & url, QWidget* window )
 
175
{
 
176
  return NetAccess::exists( url, false, window );
 
177
}
 
178
 
 
179
bool NetAccess::exists( const KURL & url, bool source )
 
180
{
 
181
  return NetAccess::exists( url, source, 0 );
 
182
}
 
183
 
 
184
bool NetAccess::exists( const KURL & url, bool source, QWidget* window )
 
185
{
 
186
  if ( url.isLocalFile() )
 
187
    return QFile::exists( url.path() );
 
188
  NetAccess kioNet;
 
189
  return kioNet.statInternal( url, 0 /*no details*/, source, window );
 
190
}
 
191
 
 
192
bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry )
 
193
{
 
194
  return NetAccess::stat( url, entry, 0 );
 
195
}
 
196
 
 
197
bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry, QWidget* window )
 
198
{
 
199
  NetAccess kioNet;
 
200
  bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
 
201
  if (ret)
 
202
    entry = kioNet.m_entry;
 
203
  return ret;
 
204
}
 
205
 
 
206
bool NetAccess::del( const KURL & url )
 
207
{
 
208
  return NetAccess::del( url, 0 );
 
209
}
 
210
 
 
211
bool NetAccess::del( const KURL & url, QWidget* window )
 
212
{
 
213
  NetAccess kioNet;
 
214
  return kioNet.delInternal( url, window );
 
215
}
 
216
 
 
217
bool NetAccess::mkdir( const KURL & url, int permissions )
 
218
{
 
219
  return NetAccess::mkdir( url, 0, permissions );
 
220
}
 
221
 
 
222
bool NetAccess::mkdir( const KURL & url, QWidget* window, int permissions )
 
223
{
 
224
  NetAccess kioNet;
 
225
  return kioNet.mkdirInternal( url, permissions, window );
 
226
}
 
227
 
 
228
QString NetAccess::fish_execute( const KURL & url, const QString command, QWidget* window )
 
229
{
 
230
  NetAccess kioNet;
 
231
  return kioNet.fish_executeInternal( url, command, window );
 
232
}
 
233
 
 
234
bool NetAccess::synchronousRun( KIO::Job* job, QWidget* window, QByteArray* data,
 
235
                                KURL* finalURL, QMap<QString, QString>* metaData )
 
236
{
 
237
  NetAccess kioNet;
 
238
  return kioNet.synchronousRunInternal( job, window, data, finalURL, metaData );
 
239
}
 
240
 
 
241
QString NetAccess::mimetype( const KURL& url )
 
242
{
 
243
  NetAccess kioNet;
 
244
  return kioNet.mimetypeInternal( url, 0 );
 
245
}
 
246
 
 
247
QString NetAccess::mimetype( const KURL& url, QWidget* window )
 
248
{
 
249
  NetAccess kioNet;
 
250
  return kioNet.mimetypeInternal( url, window );
 
251
}
 
252
 
 
253
void NetAccess::removeTempFile(const QString& name)
 
254
{
 
255
  if (!tmpfiles)
 
256
    return;
 
257
  if (tmpfiles->contains(name))
 
258
  {
 
259
    unlink(QFile::encodeName(name));
 
260
    tmpfiles->remove(name);
 
261
  }
 
262
}
 
263
 
 
264
bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
 
265
                                 bool overwrite, bool resume, QWidget* window, bool move)
 
266
{
 
267
  bJobOK = true; // success unless further error occurs
 
268
 
 
269
  KIO::Scheduler::checkSlaveOnHold(true);
 
270
  KIO::Job * job = move
 
271
                   ? KIO::file_move( src, target, permissions, overwrite, resume )
 
272
                   : KIO::file_copy( src, target, permissions, overwrite, resume );
 
273
  job->setWindow (window);
 
274
  connect( job, SIGNAL( result (KIO::Job *) ),
 
275
           this, SLOT( slotResult (KIO::Job *) ) );
 
276
 
 
277
  enter_loop();
 
278
  return bJobOK;
 
279
}
 
280
 
 
281
bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
 
282
                                QWidget* window, bool move)
 
283
{
 
284
  bJobOK = true; // success unless further error occurs
 
285
 
 
286
  KIO::Job * job = move
 
287
                   ? KIO::move( src, target )
 
288
                   : KIO::copy( src, target );
 
289
  job->setWindow (window);
 
290
  connect( job, SIGNAL( result (KIO::Job *) ),
 
291
           this, SLOT( slotResult (KIO::Job *) ) );
 
292
 
 
293
  enter_loop();
 
294
  return bJobOK;
 
295
}
 
296
 
 
297
bool NetAccess::statInternal( const KURL & url, int details, bool source,
 
298
                              QWidget* window )
 
299
{
 
300
  bJobOK = true; // success unless further error occurs
 
301
  KIO::StatJob * job = KIO::stat( url, !url.isLocalFile() );
 
302
  job->setWindow (window);
 
303
  job->setDetails( details );
 
304
  job->setSide( source );
 
305
  connect( job, SIGNAL( result (KIO::Job *) ),
 
306
           this, SLOT( slotResult (KIO::Job *) ) );
 
307
  enter_loop();
 
308
  return bJobOK;
 
309
}
 
310
 
 
311
bool NetAccess::delInternal( const KURL & url, QWidget* window )
 
312
{
 
313
  bJobOK = true; // success unless further error occurs
 
314
  KIO::Job * job = KIO::del( url );
 
315
  job->setWindow (window);
 
316
  connect( job, SIGNAL( result (KIO::Job *) ),
 
317
           this, SLOT( slotResult (KIO::Job *) ) );
 
318
  enter_loop();
 
319
  return bJobOK;
 
320
}
 
321
 
 
322
bool NetAccess::mkdirInternal( const KURL & url, int permissions,
 
323
                               QWidget* window )
 
324
{
 
325
  bJobOK = true; // success unless further error occurs
 
326
  KIO::Job * job = KIO::mkdir( url, permissions );
 
327
  job->setWindow (window);
 
328
  connect( job, SIGNAL( result (KIO::Job *) ),
 
329
           this, SLOT( slotResult (KIO::Job *) ) );
 
330
  enter_loop();
 
331
  return bJobOK;
 
332
}
 
333
 
 
334
QString NetAccess::mimetypeInternal( const KURL & url, QWidget* window )
 
335
{
 
336
  bJobOK = true; // success unless further error occurs
 
337
  m_mimetype = QString::fromLatin1("unknown");
 
338
  KIO::Job * job = KIO::mimetype( url );
 
339
  job->setWindow (window);
 
340
  connect( job, SIGNAL( result (KIO::Job *) ),
 
341
           this, SLOT( slotResult (KIO::Job *) ) );
 
342
  connect( job, SIGNAL( mimetype (KIO::Job *, const QString &) ),
 
343
           this, SLOT( slotMimetype (KIO::Job *, const QString &) ) );
 
344
  enter_loop();
 
345
  return m_mimetype;
 
346
}
 
347
 
 
348
void NetAccess::slotMimetype( KIO::Job *, const QString & type  )
 
349
{
 
350
  m_mimetype = type;
 
351
}
 
352
 
 
353
QString NetAccess::fish_executeInternal(const KURL & url, const QString command, QWidget* window)
 
354
{
 
355
  QString target, remoteTempFileName, resultData;
 
356
  KURL tempPathUrl;
 
357
  KTempFile tmpFile;
 
358
  tmpFile.setAutoDelete( true );
 
359
 
 
360
  if( url.protocol() == "fish" )
 
361
  {
 
362
    // construct remote temp filename
 
363
    tempPathUrl = url;
 
364
    remoteTempFileName = tmpFile.name();
 
365
    // only need the filename KTempFile adds some KDE specific dirs
 
366
    // that probably does not exist on the remote side
 
367
    int pos = remoteTempFileName.findRev('/');
 
368
    remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
 
369
    tempPathUrl.setPath( remoteTempFileName );
 
370
    bJobOK = true; // success unless further error occurs
 
371
    QByteArray packedArgs;
 
372
    QDataStream stream( packedArgs, IO_WriteOnly );
 
373
 
 
374
    stream << int('X') << tempPathUrl << command;
 
375
 
 
376
    KIO::Job * job = KIO::special( tempPathUrl, packedArgs, true );
 
377
    job->setWindow( window );
 
378
    connect( job, SIGNAL( result (KIO::Job *) ),
 
379
             this, SLOT( slotResult (KIO::Job *) ) );
 
380
    enter_loop();
 
381
 
 
382
    // since the KIO::special does not provide feedback we need to download the result
 
383
    if( NetAccess::download( tempPathUrl, target, window ) )
 
384
    {
 
385
      QFile resultFile( target );
 
386
 
 
387
      if (resultFile.open( IO_ReadOnly ))
 
388
      {
 
389
        QTextStream ts( &resultFile );
 
390
        ts.setEncoding( QTextStream::Locale ); // Locale??
 
391
        resultData = ts.read();
 
392
        resultFile.close();
 
393
        NetAccess::del( tempPathUrl, window );
 
394
      }
 
395
    }
 
396
  }
 
397
  else
 
398
  {
 
399
    resultData = QString( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
 
400
  }
 
401
  return resultData;
 
402
}
 
403
 
 
404
bool NetAccess::synchronousRunInternal( KIO::Job* job, QWidget* window, QByteArray* data,
 
405
                                        KURL* finalURL, QMap<QString,QString>* metaData )
 
406
{
 
407
  job->setWindow( window );
 
408
 
 
409
  m_metaData = metaData;
 
410
  if ( m_metaData ) {
 
411
      for ( QMap<QString, QString>::iterator it = m_metaData->begin(); it != m_metaData->end(); ++it ) {
 
412
          job->addMetaData( it.key(), it.data() );
 
413
      }
 
414
  }
 
415
 
 
416
  if ( finalURL ) {
 
417
      KIO::SimpleJob *sj = dynamic_cast<KIO::SimpleJob*>( job );
 
418
      if ( sj ) {
 
419
          m_url = sj->url();
 
420
      }
 
421
  }
 
422
 
 
423
  connect( job, SIGNAL( result (KIO::Job *) ),
 
424
           this, SLOT( slotResult (KIO::Job *) ) );
 
425
 
 
426
  QMetaObject *meta = job->metaObject();
 
427
 
 
428
  static const char dataSignal[] = "data(KIO::Job*,const QByteArray&)";
 
429
  if ( meta->findSignal( dataSignal ) != -1 ) {
 
430
      connect( job, SIGNAL(data(KIO::Job*,const QByteArray&)),
 
431
               this, SLOT(slotData(KIO::Job*,const QByteArray&)) );
 
432
  }
 
433
 
 
434
  static const char redirSignal[] = "redirection(KIO::Job*,const KURL&)";
 
435
  if ( meta->findSignal( redirSignal ) != -1 ) {
 
436
      connect( job, SIGNAL(redirection(KIO::Job*,const KURL&)),
 
437
               this, SLOT(slotRedirection(KIO::Job*, const KURL&)) );
 
438
  }
 
439
 
 
440
  enter_loop();
 
441
 
 
442
  if ( finalURL )
 
443
      *finalURL = m_url;
 
444
  if ( data )
 
445
      *data = m_data;
 
446
 
 
447
  return bJobOK;
 
448
}
 
449
 
 
450
// If a troll sees this, he kills me
 
451
void qt_enter_modal( QWidget *widget );
 
452
void qt_leave_modal( QWidget *widget );
 
453
 
 
454
void NetAccess::enter_loop()
 
455
{
 
456
  QWidget dummy(0,0,WType_Dialog | WShowModal);
 
457
  dummy.setFocusPolicy( QWidget::NoFocus );
 
458
  qt_enter_modal(&dummy);
 
459
  qApp->enter_loop();
 
460
  qt_leave_modal(&dummy);
 
461
}
 
462
 
 
463
void NetAccess::slotResult( KIO::Job * job )
 
464
{
 
465
  lastErrorCode = job->error();
 
466
  bJobOK = !job->error();
 
467
  if ( !bJobOK )
 
468
  {
 
469
    if ( !lastErrorMsg )
 
470
      lastErrorMsg = new QString;
 
471
    *lastErrorMsg = job->errorString();
 
472
  }
 
473
  if ( job->isA("KIO::StatJob") )
 
474
    m_entry = static_cast<KIO::StatJob *>(job)->statResult();
 
475
 
 
476
  if ( m_metaData )
 
477
    *m_metaData = job->metaData();
 
478
 
 
479
  qApp->exit_loop();
 
480
}
 
481
 
 
482
void NetAccess::slotData( KIO::Job*, const QByteArray& data )
 
483
{
 
484
  if ( data.isEmpty() )
 
485
    return;
 
486
 
 
487
  unsigned offset = m_data.size();
 
488
  m_data.resize( offset + data.size() );
 
489
  std::memcpy( m_data.data() + offset, data.data(), data.size() );
 
490
}
 
491
 
 
492
void NetAccess::slotRedirection( KIO::Job*, const KURL& url )
 
493
{
 
494
  m_url = url;
 
495
}
 
496
 
 
497
#include "netaccess.moc"