~ubuntu-branches/ubuntu/gutsy/kde4libs/gutsy

« back to all changes in this revision

Viewing changes to kio/kio/slave.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-02-21 11:00:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070221110012-6kw8khr9knv6lmg1
Tags: 3.80.3-0ubuntu1
New upstream unstable release

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <signal.h>
29
29
#include <sys/types.h>
30
30
 
 
31
#include <qglobal.h>
31
32
#include <qfile.h>
32
33
#include <qtimer.h>
33
34
#include <QtDBus/QtDBus>
37
38
#include <kglobal.h>
38
39
#include <kstandarddirs.h>
39
40
#include <kapplication.h>
40
 
#include <ktempfile.h>
 
41
#include <ktemporaryfile.h>
41
42
#include <kprocess.h>
42
43
#include <klibloader.h>
43
44
#include <ktoolinvocation.h>
82
83
   * @internal
83
84
   */
84
85
  class SlavePrivate {
85
 
  public:
86
 
    bool derived;       // true if this instance of Slave is actually an
87
 
                        // instance of a derived class.
88
 
 
89
 
    SlavePrivate(bool derived) : derived(derived) {}
90
86
  };
91
87
}
92
88
 
93
89
void Slave::accept()
94
90
{
95
 
#ifndef Q_WS_WIN
96
91
    KStreamSocket *socket = serv->accept();
97
92
    slaveconn.init(socket);
98
 
#endif
99
93
    serv->deleteLater();
100
94
    serv = 0;
101
95
    slaveconn.connect(this, SLOT(gotInput()));
142
136
}
143
137
 
144
138
Slave::Slave(KServerSocket *socket, const QString &protocol, const QString &socketname)
145
 
  : SlaveInterface(&slaveconn), serv(socket), contacted(false),
146
 
        d(new SlavePrivate(false))
147
 
{
148
 
    m_refCount = 1;
149
 
    m_protocol = protocol;
150
 
    m_slaveProtocol = protocol;
151
 
    m_socket = socketname;
152
 
    dead = false;
153
 
    contact_started = time(0);
154
 
    idle_since = contact_started;
155
 
    m_pid = 0;
156
 
    m_port = 0;
157
 
#ifndef Q_WS_WIN
158
 
    serv->setAcceptBuffered(false);
159
 
    connect(serv, SIGNAL(readyAccept()),
160
 
            SLOT(accept() ) );
161
 
#endif
162
 
}
163
 
 
164
 
Slave::Slave(bool /*derived*/, KServerSocket *socket, const QString &protocol,
165
 
        const QString &socketname)
166
 
  : SlaveInterface(&slaveconn), serv(socket), contacted(false),
167
 
        d(new SlavePrivate(true))
168
 
{
169
 
    // FIXME: hmm, duplicating code here from public ctor, no good (LS)
 
139
  : SlaveInterface(&slaveconn), serv(socket), contacted(false)
 
140
{
170
141
    m_refCount = 1;
171
142
    m_protocol = protocol;
172
143
    m_slaveProtocol = protocol;
177
148
    m_pid = 0;
178
149
    m_port = 0;
179
150
    if (serv != 0) {
180
 
#ifndef Q_WS_WIN
181
 
      serv->setAcceptBuffered(false);
182
 
      connect(serv, SIGNAL(readyAccept()),
183
 
        SLOT(accept() ) );
184
 
#endif
 
151
        serv->setAcceptBuffered(false);
 
152
        connect(serv, SIGNAL(readyAccept()),
 
153
                SLOT(accept() ) );
185
154
    }
186
155
}
187
156
 
194
163
    }
195
164
    unlinkSocket();
196
165
    m_pid = 99999;
197
 
    delete d;
198
 
    d = 0;
199
166
}
200
167
 
201
168
void Slave::setProtocol(const QString & protocol)
220
187
 
221
188
void Slave::hold(const KUrl &url)
222
189
{
223
 
   if (d->derived) {            // TODO: clean up before KDE 4
224
 
     HoldParams params;
225
 
     params.url = &url;
226
 
     virtual_hook(VIRTUAL_HOLD, &params);
227
 
     return;
228
 
   }/*end if*/
229
 
 
230
190
   ref();
231
191
   {
232
192
      QByteArray data;
246
206
 
247
207
void Slave::suspend()
248
208
{
249
 
   if (d->derived) {            // TODO: clean up before KDE 4
250
 
     virtual_hook(VIRTUAL_SUSPEND, 0);
251
 
     return;
252
 
   }/*end if*/
253
 
 
254
209
   slaveconn.suspend();
255
210
}
256
211
 
257
212
void Slave::resume()
258
213
{
259
 
   if (d->derived) {            // TODO: clean up before KDE 4
260
 
     virtual_hook(VIRTUAL_RESUME, 0);
261
 
     return;
262
 
   }/*end if*/
263
 
 
264
214
   slaveconn.resume();
265
215
}
266
216
 
267
217
bool Slave::suspended()
268
218
{
269
 
   if (d->derived) {            // TODO: clean up before KDE 4
270
 
     SuspendedParams params;
271
 
     virtual_hook(VIRTUAL_SUSPENDED, &params);
272
 
     return params.retval;
273
 
   }/*end if*/
274
 
 
275
219
   return slaveconn.suspended();
276
220
}
277
221
 
278
222
void Slave::send(int cmd, const QByteArray &arr) {
279
 
   if (d->derived) {            // TODO: clean up before KDE 4
280
 
     SendParams params;
281
 
     params.cmd = cmd;
282
 
     params.arr = &arr;
283
 
     virtual_hook(VIRTUAL_SEND, &params);
284
 
     return;
285
 
   }/*end if*/
286
 
 
287
223
   slaveconn.send(cmd, arr);
288
224
}
289
225
 
347
283
 
348
284
Slave* Slave::createSlave( const QString &protocol, const KUrl& url, int& error, QString& error_text )
349
285
{
350
 
    //kDebug(7002) << "createSlave '" << protocol << "' for " << url.prettyUrl() << endl;
 
286
    kDebug(7002) << "createSlave '" << protocol << "' for " << url.prettyUrl() << endl;
351
287
    // Firstly take into account all special slaves
352
288
    if (protocol == "data")
353
289
        return new DataProtocol();
354
 
 
355
 
    QString prefix = KStandardDirs::locateLocal("socket", KGlobal::instance()->instanceName());
356
 
    KTempFile socketfile(prefix, QLatin1String(".slave-socket"));
357
 
    if ( socketfile.status() != 0 )
 
290
#ifdef Q_WS_WIN
 
291
    // localhost could not resolved yet, this s a bug in kdecore network resolver stuff
 
292
    // autoselect free tcp port 
 
293
    KServerSocket *kss = new KServerSocket(getenv("COMPUTERNAME"),"0");
 
294
    kss->setFamily(KResolver::InetFamily);
 
295
    kss->listen();
 
296
    QString sockname = kss->localAddress().serviceName();
 
297
#else
 
298
    QString prefix = KStandardDirs::locateLocal("socket", KGlobal::mainComponent().componentName());
 
299
    KTemporaryFile *socketfile = new KTemporaryFile();
 
300
    socketfile->setPrefix(prefix);
 
301
    socketfile->setSuffix(QLatin1String(".slave-socket"));
 
302
    if ( !socketfile->open() )
358
303
    {
359
 
        error_text = i18n("Unable to create io-slave: %1", strerror(errno));
360
 
        error = KIO::ERR_CANNOT_LAUNCH_PROCESS;
361
 
        return 0;
 
304
        error_text = i18n("Unable to create io-slave: %1", strerror(errno));
 
305
        error = KIO::ERR_CANNOT_LAUNCH_PROCESS;
 
306
        delete socketfile;
 
307
        return 0;
362
308
    }
363
309
 
364
 
    QString sockname = socketfile.name();
365
 
 
366
 
#ifdef __CYGWIN__
367
 
   socketfile.close();
368
 
#endif
369
 
   socketfile.unlink(); // can't bind if there is such a file
370
 
 
371
 
#ifndef Q_WS_WIN
 
310
    QString sockname = socketfile->fileName();
 
311
    delete socketfile; // can't bind if there is such a file
 
312
 
372
313
    KServerSocket *kss = new KServerSocket(QFile::encodeName(sockname));
373
314
    kss->setFamily(KResolver::LocalFamily);
374
315
    kss->listen();
375
 
 
 
316
#endif
376
317
    Slave *slave = new Slave(kss, protocol, sockname);
377
 
#else
378
 
    Slave *slave = 0;
379
 
#endif
380
318
 
381
319
    // WABA: if the dcopserver is running under another uid we don't ask
382
320
    // klauncher for a slave, because the slave might have that other uid
391
329
    if (!bForkSlaves)
392
330
    {
393
331
       // check the UID of klauncher
394
 
       QDBusReply<uint> reply = QDBus::sessionBus().interface()->serviceUid(KToolInvocation::klauncher()->service());
 
332
       QDBusReply<uint> reply = QDBusConnection::sessionBus().interface()->serviceUid(KToolInvocation::klauncher()->service());
395
333
       if (reply.isValid() && getuid() != reply)
396
334
          bForkSlaves = true;
397
335
    }
398
336
#endif
399
337
 
 
338
#ifdef Q_OS_UNIX
400
339
    if (bForkSlaves)
401
340
    {
402
341
       QString _name = KProtocolInfo::exec(protocol);
428
367
#endif
429
368
       return slave;
430
369
    }
431
 
 
 
370
#endif
432
371
    org::kde::KLauncher* klauncher = KToolInvocation::klauncher();
433
372
    QString errorStr;
 
373
        qDebug() << __FUNCTION__ << protocol  << " " << url.host() << " " << sockname;
434
374
    QDBusReply<int> reply = klauncher->requestSlave(protocol, url.host(), sockname, errorStr);
435
375
    if (!reply.isValid()) {
436
376
        error_text = i18n("Cannot talk to klauncher: %1", klauncher->lastError().message() );
446
386
        delete slave;
447
387
        return 0;
448
388
    }
449
 
#ifndef Q_WS_WIN
450
389
    slave->setPID(pid);
451
390
    QTimer::singleShot(1000*SLAVE_CONNECTION_TIMEOUT_MIN, slave, SLOT(timeout()));
452
 
#endif
453
391
    return slave;
454
392
}
455
393
 
459
397
    // Firstly take into account all special slaves
460
398
    if (protocol == "data")
461
399
        return 0;
462
 
 
463
 
    QString prefix = KStandardDirs::locateLocal("socket", KGlobal::instance()->instanceName());
464
 
    KTempFile socketfile(prefix, QLatin1String(".slave-socket"));
465
 
    if ( socketfile.status() != 0 )
466
 
        return 0;
467
 
 
468
 
#ifdef __CYGWIN__
469
 
   socketfile.close();
470
 
   socketfile.unlink();
471
 
#endif
472
 
 
473
 
#ifndef Q_WS_WIN
474
 
    KServerSocket *kss = new KServerSocket(QFile::encodeName(socketfile.name()));
475
 
 
476
 
    Slave *slave = new Slave(kss, protocol, socketfile.name());
 
400
#ifdef Q_WS_WIN
 
401
    // localhost could not resolved yet, this s a bug in kdecore network resolver stuff
 
402
    // autoselect free tcp port 
 
403
    KServerSocket *kss = new KServerSocket(getenv("COMPUTERNAME"),"0");
 
404
    QString sockname = kss->localAddress().serviceName();
477
405
#else
478
 
    Slave *slave = 0;
 
406
    QString prefix = KStandardDirs::locateLocal("socket", KGlobal::mainComponent().componentName());
 
407
    KTemporaryFile *socketfile = new KTemporaryFile();
 
408
    socketfile->setPrefix(prefix);
 
409
    socketfile->setSuffix(QLatin1String(".slave-socket"));
 
410
    if ( !socketfile->open() ) {
 
411
        delete socketfile;
 
412
        return 0;
 
413
    }
 
414
 
 
415
    QString sockname = socketfile->fileName();
 
416
    delete socketfile; // can't bind if there is such a file
 
417
 
 
418
    KServerSocket *kss = new KServerSocket(QFile::encodeName(sockname));
479
419
#endif
480
 
 
481
 
    QDBusReply<int> reply = KToolInvocation::klauncher()->requestHoldSlave(url.url(), socketfile.name());
 
420
    Slave *slave = new Slave(kss, protocol, sockname);
 
421
    QDBusReply<int> reply = KToolInvocation::klauncher()->requestHoldSlave(url.url(), sockname);
482
422
    if (!reply.isValid()) {
483
423
        delete slave;
484
424
        return 0;
489
429
        delete slave;
490
430
        return 0;
491
431
    }
492
 
#ifndef Q_WS_WIN
493
432
    slave->setPID(pid);
494
433
    QTimer::singleShot(1000*SLAVE_CONNECTION_TIMEOUT_MIN, slave, SLOT(timeout()));
495
 
#endif
496
434
    return slave;
497
435
}
498
436
 
499
 
void Slave::virtual_hook( int id, void* data ) {
500
 
  KIO::SlaveInterface::virtual_hook( id, data );
501
 
}
502
 
 
503
437
#include "slave.moc"