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

« back to all changes in this revision

Viewing changes to kioslave/devices/kio_devices.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
/* This file is part of the KDE project
 
2
   Copyright (C) 2002,2003 Joseph Wenninger <jowenn@kde.org>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
   #include <kio/slavebase.h>
 
21
   #include <kinstance.h>
 
22
   #include <kdebug.h>
 
23
   #include <stdlib.h>
 
24
   #include <qtextstream.h>
 
25
   #include <klocale.h>
 
26
   #include <sys/stat.h>
 
27
   #include <dcopclient.h>
 
28
   #include <qdatastream.h>
 
29
   #include <time.h>
 
30
   #include <kprocess.h>
 
31
 
 
32
   class HelloProtocol : public KIO::SlaveBase
 
33
   {
 
34
   public:
 
35
      HelloProtocol( const QCString& protocol ,const QCString &pool, const QCString &app);
 
36
      virtual ~HelloProtocol();
 
37
#if 0
 
38
      virtual void get( const KURL& url );
 
39
#endif
 
40
      virtual void stat(const KURL& url);
 
41
      virtual void listDir(const KURL& url);
 
42
      void listRoot();
 
43
   private:
 
44
        DCOPClient *m_dcopClient;
 
45
        uint mountpointMappingCount();
 
46
        bool fullMode;
 
47
        QString deviceNode(uint id);
 
48
        bool deviceMounted(const QString dev);
 
49
        bool deviceMounted(int);
 
50
        QString mountPoint(const QString dev);
 
51
        QString mountPoint(int);
 
52
        QString deviceType(int);
 
53
        QStringList deviceList();
 
54
        QStringList deviceInfo(const QString name);
 
55
 
 
56
        QStringList kmobile_list( QString deviceName );
 
57
  };
 
58
 
 
59
  extern "C" {
 
60
      int kdemain( int, char **argv )
 
61
      {
 
62
          kdDebug()<<"kdemain for devices"<<endl;
 
63
          KInstance instance( "kio_devices" );
 
64
          HelloProtocol slave(argv[1],argv[2], argv[3]);
 
65
          slave.dispatchLoop();
 
66
          return 0;
 
67
      }
 
68
  }
 
69
 
 
70
 
 
71
 
 
72
static void createFileEntry(KIO::UDSEntry& entry, const QString& name, const QString& url, const QString& mime);
 
73
static void createDirEntry(KIO::UDSEntry& entry, const QString& name, const QString& url, const QString& mime);
 
74
 
 
75
HelloProtocol::HelloProtocol( const QCString& protocol, const QCString &pool, const QCString &app): 
 
76
                SlaveBase(protocol,  pool, app )
 
77
{
 
78
        kdDebug()<<"HelloProtocol: Called with slavename:"<<protocol<<endl;
 
79
        if (protocol=="system") fullMode=true; else fullMode=false;
 
80
        m_dcopClient=new DCOPClient();
 
81
        if (!m_dcopClient->attach())
 
82
        {
 
83
                kdDebug()<<"ERROR WHILE CONNECTING TO DCOPSERVER"<<endl;
 
84
        }
 
85
}
 
86
 
 
87
HelloProtocol::~HelloProtocol()
 
88
{
 
89
        delete m_dcopClient;
 
90
}
 
91
 
 
92
void HelloProtocol::stat(const KURL& url)
 
93
{
 
94
        QStringList     path = QStringList::split('/', url.encodedPathAndQuery(-1), false);
 
95
        KIO::UDSEntry   entry;
 
96
        QString mime;
 
97
        QString mp;
 
98
 
 
99
        switch (path.count())
 
100
        {
 
101
                case 0:
 
102
                        if (fullMode)
 
103
                                createDirEntry(entry, i18n("System"), "system:/", "inode/directory");
 
104
                        else
 
105
                                createDirEntry(entry, i18n("Devices"), "devices:/", "inode/directory");
 
106
                        statEntry(entry);
 
107
                        finished();
 
108
                        break;
 
109
                default:
 
110
 
 
111
                QStringList info=deviceInfo(url.fileName());
 
112
 
 
113
                if (info.empty())
 
114
                {
 
115
                        error(KIO::ERR_SLAVE_DEFINED,i18n("Unknown device: %1").arg(url.fileName()));
 
116
                        return;
 
117
                }
 
118
 
 
119
 
 
120
                QStringList::Iterator it=info.begin();
 
121
                if (it!=info.end())
 
122
                {
 
123
                        QString device=*it; ++it;
 
124
                        if (it!=info.end())
 
125
                        {
 
126
                                ++it;
 
127
                                if (it!=info.end())
 
128
                                {
 
129
                                        QString mp=*it; ++it;++it;
 
130
                                        if (it!=info.end())
 
131
                                        {
 
132
                                                bool mounted=((*it)=="true");
 
133
                                                if (mounted)
 
134
                                                {
 
135
//                                                      if (mp=="/") mp="";
 
136
                                                        redirection(mp);
 
137
                                                        finished();
 
138
                                                }
 
139
                                                else
 
140
                                                {
 
141
                                                        if (mp.startsWith("file:/"))
 
142
                                                        {
 
143
                                                                KProcess *proc = new KProcess;
 
144
                                                                *proc << "kio_devices_mounthelper";
 
145
                                                                *proc << "-m" << url.url();
 
146
                                                                proc->start(KProcess::Block);
 
147
                                                                delete proc;
 
148
 
 
149
                                                                redirection(mp);
 
150
                                                                finished();
 
151
                                                        }
 
152
                                                        else
 
153
                                                                error(KIO::ERR_SLAVE_DEFINED,i18n("Device not accessible"));
 
154
 
 
155
 
 
156
//                                                      error(KIO::ERR_SLAVE_DEFINED,i18n("Device not mounted"));
 
157
                                                }
 
158
                                                return;
 
159
                                        }
 
160
                                }
 
161
                        }
 
162
                }
 
163
                error(KIO::ERR_SLAVE_DEFINED,i18n("Illegal data received"));
 
164
                return;
 
165
                break;
 
166
        }
 
167
 
 
168
}
 
169
 
 
170
 
 
171
 
 
172
void HelloProtocol::listDir(const KURL& url)
 
173
{
 
174
        kdDebug()<<"HELLO PROTOCOLL::listdir: "<<url.url()<<endl;
 
175
        if ((url==KURL("devices:/")) || (url==KURL("system:/")))
 
176
                listRoot();
 
177
        else
 
178
        {
 
179
                QStringList info=deviceInfo(url.fileName());
 
180
 
 
181
                if (info.empty())
 
182
                {
 
183
                        error(KIO::ERR_SLAVE_DEFINED,i18n("Unknown device %1").arg(url.fileName()));
 
184
                        return;
 
185
                }
 
186
 
 
187
 
 
188
                QStringList::Iterator it=info.begin();
 
189
                if (it!=info.end())
 
190
                {
 
191
                        QString device=*it; ++it;
 
192
                        if (it!=info.end())
 
193
                        {
 
194
                                ++it;
 
195
                                if (it!=info.end())
 
196
                                {
 
197
                                        QString mp=*it; ++it;++it;
 
198
                                        if (it!=info.end())
 
199
                                        {
 
200
                                                bool mounted=((*it)=="true");
 
201
                                                if (mounted)
 
202
                                                {
 
203
//                                                      if (mp=="/") mp="";
 
204
                                                        redirection(mp);
 
205
                                                        finished();
 
206
                                                }
 
207
                                                else
 
208
                                                {
 
209
                                                        if (mp.startsWith("file:/"))
 
210
                                                        {
 
211
                                                                KProcess *proc = new KProcess;
 
212
                                                                *proc << "kio_devices_mounthelper";
 
213
                                                                *proc << "-m" << url.url();
 
214
                                                                proc->start(KProcess::Block);
 
215
                                                                int ec=0;
 
216
                                                                if (proc->normalExit()) ec=proc->exitStatus();
 
217
                                                                delete proc;
 
218
 
 
219
                                                                if (ec)
 
220
                                                                {
 
221
                                                                        error(KIO::ERR_SLAVE_DEFINED,i18n("Device not mounted"));
 
222
                                                                        finished();
 
223
                                                                }
 
224
                                                                else
 
225
                                                                {
 
226
                                                                        redirection(mp);
 
227
                                                                        finished();
 
228
                                                                }
 
229
                                                        }
 
230
                                                        else
 
231
                                                                error(KIO::ERR_SLAVE_DEFINED,i18n("Device not accessible"));
 
232
                                                }
 
233
                                                return;
 
234
                                        }
 
235
                                }
 
236
                        }
 
237
                }
 
238
                error(KIO::ERR_SLAVE_DEFINED,i18n("Illegal data received"));
 
239
        }
 
240
}
 
241
 
 
242
uint HelloProtocol::mountpointMappingCount()
 
243
{
 
244
        QByteArray data;
 
245
        QByteArray param;
 
246
        QCString retType;
 
247
        uint count=0;
 
248
      if ( m_dcopClient->call( "kded",
 
249
                 "mountwatcher", "mountpointMappingCount()", param,retType,data,false ) )
 
250
      {
 
251
        QDataStream stream1(data,IO_ReadOnly);
 
252
        stream1>>count;
 
253
      }
 
254
      return count;
 
255
}
 
256
 
 
257
QString HelloProtocol::deviceNode(uint id)
 
258
{
 
259
        QByteArray data;
 
260
        QByteArray param;
 
261
        QCString retType;
 
262
        QString retVal;
 
263
        QDataStream streamout(param,IO_WriteOnly);
 
264
        streamout<<id;
 
265
        if ( m_dcopClient->call( "kded",
 
266
                 "mountwatcher", "devicenode(int)", param,retType,data,false ) )
 
267
      {
 
268
        QDataStream streamin(data,IO_ReadOnly);
 
269
        streamin>>retVal;
 
270
      }
 
271
      return retVal;
 
272
 
 
273
}
 
274
 
 
275
bool HelloProtocol::deviceMounted(const QString dev)
 
276
{
 
277
        QByteArray data;
 
278
        QByteArray param;
 
279
        QCString retType;
 
280
        bool retVal=false;
 
281
        QDataStream streamout(param,IO_WriteOnly);
 
282
        streamout<<dev;
 
283
        if ( m_dcopClient->call( "kded",
 
284
                 "mountwatcher", "mounted(QString)", param,retType,data,false ) )
 
285
      {
 
286
        QDataStream streamin(data,IO_ReadOnly);
 
287
        streamin>>retVal;
 
288
      }
 
289
      return retVal;
 
290
}
 
291
 
 
292
 
 
293
QStringList HelloProtocol::kmobile_list(const QString deviceName)
 
294
{
 
295
        QByteArray data;
 
296
        QByteArray param;
 
297
        QCString retType;
 
298
        QStringList retVal;
 
299
        QDataStream streamout(param,IO_WriteOnly);
 
300
        streamout<<deviceName;
 
301
        if ( m_dcopClient->call( "kmobile",
 
302
                 "kmobileIface", "kio_devices_deviceInfo(QString)", param,retType,data,false ) )
 
303
      {
 
304
        QDataStream streamin(data,IO_ReadOnly);
 
305
        streamin>>retVal;
 
306
      }
 
307
      return retVal;
 
308
}
 
309
 
 
310
 
 
311
QStringList HelloProtocol::deviceInfo(QString name)
 
312
{
 
313
        QByteArray data;
 
314
        QByteArray param;
 
315
        QCString retType;
 
316
        QStringList retVal;
 
317
        QDataStream streamout(param,IO_WriteOnly);
 
318
        streamout<<name;
 
319
        if ( m_dcopClient->call( "kded",
 
320
                 "mountwatcher", "basicDeviceInfo(QString)", param,retType,data,false ) )
 
321
        {
 
322
          QDataStream streamin(data,IO_ReadOnly);
 
323
          streamin>>retVal;
 
324
        }
 
325
        // kmobile support
 
326
        if (retVal.isEmpty())
 
327
                retVal = kmobile_list(name);
 
328
 
 
329
        return retVal;
 
330
}
 
331
 
 
332
 
 
333
bool HelloProtocol::deviceMounted(int id)
 
334
{
 
335
        QByteArray data;
 
336
        QByteArray param;
 
337
        QCString retType;
 
338
        bool retVal=false;
 
339
        QDataStream streamout(param,IO_WriteOnly);
 
340
        streamout<<id;
 
341
        if ( m_dcopClient->call( "kded",
 
342
                 "mountwatcher", "mounted(int)", param,retType,data,false ) )
 
343
      {
 
344
        QDataStream streamin(data,IO_ReadOnly);
 
345
        streamin>>retVal;
 
346
      }
 
347
      return retVal;
 
348
}
 
349
 
 
350
 
 
351
QStringList HelloProtocol::deviceList()
 
352
{
 
353
        QByteArray data;
 
354
        QByteArray param;
 
355
        QCString retType;
 
356
        QStringList retVal;
 
357
        QDataStream streamout(param,IO_WriteOnly);
 
358
        
 
359
        kdDebug()<<"list dir: Fullmode=="<<fullMode<<endl;
 
360
        QString dcopFun=fullMode?"basicSystemList()":"basicList()";
 
361
        if ( m_dcopClient->call( "kded",
 
362
                 "mountwatcher", dcopFun.utf8(), param,retType,data,false ) )
 
363
        {
 
364
          QDataStream streamin(data,IO_ReadOnly);
 
365
          streamin>>retVal;
 
366
        }
 
367
        else
 
368
        {
 
369
                retVal.append(QString::fromLatin1("!!!ERROR!!!"));
 
370
        }
 
371
        // add mobile devices info (kmobile)
 
372
        retVal += kmobile_list(QString::null);
 
373
 
 
374
      return retVal;
 
375
}
 
376
 
 
377
QString HelloProtocol::mountPoint(const QString dev)
 
378
{
 
379
        QByteArray data;
 
380
        QByteArray param;
 
381
        QCString retType;
 
382
        QString retVal;
 
383
        QDataStream streamout(param,IO_WriteOnly);
 
384
        streamout<<dev;
 
385
        if ( m_dcopClient->call( "kded",
 
386
                 "mountwatcher", "mountpoint(QString)", param,retType,data,false ) )
 
387
      {
 
388
        QDataStream streamin(data,IO_ReadOnly);
 
389
        streamin>>retVal;
 
390
      }
 
391
      return retVal;
 
392
}
 
393
 
 
394
QString HelloProtocol::mountPoint(int id)
 
395
{
 
396
        QByteArray data;
 
397
        QByteArray param;
 
398
        QCString retType;
 
399
        QString retVal;
 
400
        QDataStream streamout(param,IO_WriteOnly);
 
401
        streamout<<id;
 
402
        if ( m_dcopClient->call( "kded",
 
403
                 "mountwatcher", "mountpoint(int)", param,retType,data,false ) )
 
404
      {
 
405
        QDataStream streamin(data,IO_ReadOnly);
 
406
        streamin>>retVal;
 
407
      }
 
408
      return retVal;
 
409
}
 
410
 
 
411
 
 
412
 
 
413
QString HelloProtocol::deviceType(int id)
 
414
{
 
415
        QByteArray data;
 
416
        QByteArray param;
 
417
        QCString retType;
 
418
        QString retVal;
 
419
        QDataStream streamout(param,IO_WriteOnly);
 
420
        streamout<<id;
 
421
        if ( m_dcopClient->call( "kded",
 
422
                 "mountwatcher", "type(int)", param,retType,data,false ) )
 
423
      {
 
424
        QDataStream streamin(data,IO_ReadOnly);
 
425
        streamin>>retVal;
 
426
      }
 
427
      return retVal;
 
428
}
 
429
 
 
430
 
 
431
 
 
432
void HelloProtocol::listRoot()
 
433
{
 
434
        KIO::UDSEntry   entry;
 
435
        uint count;
 
436
 
 
437
        QStringList list=deviceList();
 
438
        count=0;
 
439
        for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
 
440
        {
 
441
                if ((*it)=="!!!ERROR!!!")
 
442
                {
 
443
                        error(KIO::ERR_SLAVE_DEFINED,i18n("The KDE mountwatcher is not running. Please activate it in Control Center->KDE Components->Service Manager, if you want to use the devices:/ protocol"));
 
444
                        return;
 
445
                }
 
446
// FIXME: look for the real ending
 
447
                QString url="devices:/"+(*it); ++it;
 
448
                QString name=*it; ++it;
 
449
                ++it; ++it;
 
450
                QString type=*it; ++it; ++it;
 
451
                createFileEntry(entry,name,url,type);
 
452
                listEntry(entry,false);
 
453
                count++;
 
454
        }
 
455
        totalSize(count);
 
456
        listEntry(entry, true);
 
457
 
 
458
 
 
459
        // Jobs entry
 
460
 
 
461
        // finish
 
462
        finished();
 
463
}
 
464
 
 
465
#if 0
 
466
 void HelloProtocol::get( const KURL& url )
 
467
 {
 
468
/*      mimeType("application/x-desktop");
 
469
        QCString output;
 
470
        output.sprintf("[Desktop Action Format]\n"
 
471
                        "Exec=kfloppy\n"
 
472
                        "Name=Format\n"
 
473
                        "[Desktop Entry]\n"
 
474
                        "Actions=Format\n"
 
475
                        "Dev=/dev/fd0\n"
 
476
                        "Encoding=UTF-8\n"
 
477
                        "Icon=3floppy_mount\n"
 
478
                        "MountPoint=/media/floppy\n"
 
479
                        "ReadOnly=false\n"
 
480
                        "Type=FSDevice\n"
 
481
                        "UnmountIcon=3floppy_unmount\n"
 
482
                        );
 
483
     data(output);
 
484
     finished();
 
485
 */
 
486
  redirection("file:/");
 
487
  //finished();
 
488
}
 
489
#endif
 
490
 
 
491
void addAtom(KIO::UDSEntry& entry, unsigned int ID, long l, const QString& s = QString::null)
 
492
{
 
493
        KIO::UDSAtom    atom;
 
494
        atom.m_uds = ID;
 
495
        atom.m_long = l;
 
496
        atom.m_str = s;
 
497
        entry.append(atom);
 
498
}
 
499
 
 
500
static void createFileEntry(KIO::UDSEntry& entry, const QString& name, const QString& url, const QString& mime)
 
501
{
 
502
        entry.clear();
 
503
        addAtom(entry, KIO::UDS_NAME, 0, name);
 
504
        addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR);//REG);
 
505
        addAtom(entry, KIO::UDS_URL, 0, url);
 
506
        addAtom(entry, KIO::UDS_ACCESS, 0500);
 
507
       if (mime.startsWith("icon:")) {
 
508
                kdDebug()<<"setting prefered icon:"<<mime.right(mime.length()-5)<<endl;
 
509
                addAtom(entry,KIO::UDS_ICON_NAME,0,mime.right(mime.length()-5));
 
510
                addAtom(entry,KIO::UDS_MIME_TYPE,0,"inode/directory");
 
511
        }
 
512
        else
 
513
                addAtom(entry, KIO::UDS_MIME_TYPE, 0, mime);
 
514
        addAtom(entry, KIO::UDS_SIZE, 0);
 
515
        addAtom(entry, KIO::UDS_GUESSED_MIME_TYPE, 0, "inode/directory");
 
516
        addAtom(entry, KIO::UDS_CREATION_TIME,1);
 
517
        addAtom(entry, KIO::UDS_MODIFICATION_TIME,time(0));
 
518
}
 
519
 
 
520
 
 
521
static void createDirEntry(KIO::UDSEntry& entry, const QString& name, const QString& url, const QString& mime)
 
522
{
 
523
        entry.clear();
 
524
        addAtom(entry, KIO::UDS_NAME, 0, name);
 
525
        addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR);
 
526
        addAtom(entry, KIO::UDS_ACCESS, 0500);
 
527
        kdDebug()<<"DEVICES: "<<mime<<endl;
 
528
        if (mime.startsWith("icon:")) {
 
529
                kdDebug()<<"setting prefered icon:"<<mime.right(mime.length()-5)<<endl;
 
530
                addAtom(entry,KIO::UDS_ICON_NAME,0,mime.right(mime.length()-5));
 
531
                addAtom(entry,KIO::UDS_MIME_TYPE,0,"inode/directory");  
 
532
        }
 
533
        else {
 
534
                addAtom(entry, KIO::UDS_MIME_TYPE, 0, mime);
 
535
        }
 
536
        addAtom(entry, KIO::UDS_URL, 0, url);
 
537
        addAtom(entry, KIO::UDS_SIZE, 0);
 
538
        addAtom(entry, KIO::UDS_GUESSED_MIME_TYPE, 0, "inode/directory");
 
539
 
 
540
//        addAtom(entry, KIO::UDS_GUESSED_MIME_TYPE, 0, "application/x-desktop");
 
541
}