summaryrefslogtreecommitdiffstats
path: root/kmobile/kioslave/kio_mobile.cpp
blob: 8c4e3853fd146b65abdb2f0da76a3712ffebc513 (plain)
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*  This file is part of the KDE KMobile library
    Copyright (C) 2003 Helge Deller <deller@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    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 <sys/types.h>
#include <sys/stat.h>

#include <sys/types.h>
#include <unistd.h>
#include <time.h>

#include <tqregexp.h>

#include <kdebug.h>
#include <klocale.h>
#include <kinstance.h>
#include <kabc/vcardconverter.h>

#include "kio_mobile.h"

#include <tdepimmacros.h>

using namespace KIO;

#define KIO_MOBILE_DEBUG_AREA 7126
#define PRINT_DEBUG kdDebug(KIO_MOBILE_DEBUG_AREA) << "kio_mobile: "

extern "C" { KDE_EXPORT int kdemain(int argc, char **argv); }

/**
 * The main program.
 */
int kdemain(int argc, char **argv)
{
  TDEInstance instance( "kio_mobile" );

  PRINT_DEBUG << "Starting " << getpid() << endl;

  if (argc != 4) {
	fprintf(stderr, "Usage kio_mobile protocol pool app\n");
	return -1;
  }
  // let the protocol class do its work
  KMobileProtocol slave(argv[2], argv[3]);

  slave.dispatchLoop();

  PRINT_DEBUG << "Done" << endl;
  return 0;
}


/**
 * Initialize the mobile slave
 */
KMobileProtocol::KMobileProtocol(const TQCString &pool, const TQCString &app)
  : SlaveBase( "mobile", pool, app)
{
}

KMobileProtocol::~KMobileProtocol()
{
}

/*
 * getDeviceAndRessource("mobile:/<devicename>/<resource>/...") - split
 */
int KMobileProtocol::getDeviceAndRessource(const TQString &_path,
	TQString &devName, TQString &resource, TQString &devPath, 
	KMobileDevice::Capabilities &devCaps)
{
//  PRINT_DEBUG << TQString("###getDeviceAndRessource### %1\n").arg(_path);
  TQStringList path = TQStringList::split('/', _path, false);

  devName = resource = devPath = TQString();
  devCaps = KMobileDevice::hasNothing;

  if (path.count() >= 1)  { devName = path[0];  path.pop_front(); };
  if (path.count() >= 1)  { resource = path[0]; path.pop_front(); };
  if (path.count() >= 1)  devPath = path.join("/");

  if (devName.isEmpty())
	return 0;

  int _caps = m_dev.capabilities(devName);

  if (resource.isEmpty()) {
	devCaps = (KMobileDevice::Capabilities) _caps;
	return 0;
  }

  for (int i=0; i<31; i++) {
	int cap = 1UL << i;
	if ((_caps & cap) == 0)
		continue;
	TQString capname = m_dev.nameForCap(devName,cap);
	if (capname != resource)
		continue;
	devCaps = (KMobileDevice::Capabilities) cap;
	return 0;
  }

  return KIO::ERR_DOES_NOT_EXIST;
}


static
void addAtom(KIO::UDSEntry& entry, unsigned int ID, long l, const TQString& s = TQString())
{
	KIO::UDSAtom	atom;
	atom.m_uds = ID;
	atom.m_long = l;
	atom.m_str = s;
	entry.append(atom);
}

static
void createDirEntry(KIO::UDSEntry& entry, const TQString& name, const TQString& url, const TQString& mime)
{
	entry.clear();
	addAtom(entry, KIO::UDS_NAME, 0, name);
	addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR);
	addAtom(entry, KIO::UDS_ACCESS, 0500);
	addAtom(entry, KIO::UDS_MIME_TYPE, 0, mime);
	addAtom(entry, KIO::UDS_URL, 0, url);
	addAtom(entry, KIO::UDS_USER, 0, getenv("USER"));
	addAtom(entry, KIO::UDS_GROUP, 0, getenv("USER"));
	PRINT_DEBUG << TQString("createDirEntry: File: %1  MIME: %2  URL: %3\n").arg(name).arg(mime).arg(url);
//	addAtom(entry, KIO::UDS_SIZE, 0);
	addAtom(entry, KIO::UDS_GUESSED_MIME_TYPE, 0, mime);
}

static
void createFileEntry(KIO::UDSEntry& entry, const TQString& name, const TQString& url, const TQString& mime, 
		const unsigned long size = 0)
{
	entry.clear();
	addAtom(entry, KIO::UDS_NAME, 0, name);
	addAtom(entry, KIO::UDS_FILE_TYPE, S_IFREG);
	addAtom(entry, KIO::UDS_URL, 0, url);
	addAtom(entry, KIO::UDS_ACCESS, 0400);
	addAtom(entry, KIO::UDS_USER, 0, getenv("USER"));
	addAtom(entry, KIO::UDS_GROUP, 0, getenv("USER"));
	addAtom(entry, KIO::UDS_MIME_TYPE, 0, mime);
	if (size) addAtom(entry, KIO::UDS_SIZE, size);
	addAtom(entry, KIO::UDS_GUESSED_MIME_TYPE, 0, mime);
	PRINT_DEBUG << TQString("createFileEntry: File: %1, Size: %2,  MIME: %3\n").arg(name).arg(size).arg(mime);
}


/**
 * Get the information contained in the URL.
 */
void KMobileProtocol::get(const KURL &url)
{
  PRINT_DEBUG << "###############################\n";
  PRINT_DEBUG << TQString("get(%1)\n").arg(url.path());

  KMobileDevice::Capabilities devCaps;
  TQString devName, resource, devPath;

  int err = getDeviceAndRessource(url.path(), devName, resource, devPath, devCaps);
  if (err) {
	error(err, url.path());
	return;
  }

  if (devName.isEmpty() || resource.isEmpty()) {
	error(KIO::ERR_DOES_NOT_EXIST, url.path());
	return;
  }
  
  // collect the result
  TQCString result;
  TQString mime;
  switch (devCaps) {
    case KMobileDevice::hasAddressBook:	err = getVCard(devName, result, mime, devPath);
					break;
    case KMobileDevice::hasCalendar:	err = getCalendar(devName, result, mime, devPath);
					break;
    case KMobileDevice::hasNotes:	err = getNote(devName, result, mime, devPath);
					break;
    case KMobileDevice::hasFileStorage: err = getFileStorage(devName, result, mime, devPath);
					break;
    default:				err = KIO::ERR_CANNOT_ENTER_DIRECTORY; /* TODO */
  }

  if (err) {
	error(err, url.path());
	return;
  }

  // tell the mimetype
  mimeType(mime);

  // tell the length
  KIO::filesize_t processed_size = result.length();
  totalSize(processed_size);

  // tell the contents of the URL
  TQByteArray array;
  array.setRawData( result.data(), result.length() );
  data(array);
  array.resetRawData( result.data(), result.length() );
  processedSize( processed_size );
  // tell we are finished
  data(TQByteArray());

  // tell we are finished
  finished();
}


/*
 * listRoot() - gives listing of all devices
 */
void KMobileProtocol::listRoot(const KURL& url)
{
  PRINT_DEBUG << TQString("########## listRoot(%1) for %2:/\n").arg(url.path()).arg(url.protocol());

  KIO::UDSEntry entry;

  TQStringList deviceNames = m_dev.deviceNames();
  unsigned int dirs = deviceNames.count();
  totalSize(dirs);

  int classMask = KMobileDevice::Unclassified;
  /* handle all possible protocols here and just add a <protocol>.protocol file */
  if (url.protocol() == "cellphone")		// cellphone:/
	classMask = KMobileDevice::Phone;
  if (url.protocol() == "organizer" ||		// organizer:/
      url.protocol() == "pda")			// pda:/
	classMask = KMobileDevice::Organizer;
  if (url.protocol() == "phonecamera")		// camera:/
	classMask = KMobileDevice::Camera;

  for (unsigned int i=0; i<dirs; i++) {

	TQString devName = deviceNames[i];

	if (classMask != KMobileDevice::Unclassified && 
	    m_dev.classType(devName) != classMask)
		continue;	

        createDirEntry(entry, devName, "mobile:/"+devName, 
			KMOBILE_MIMETYPE_DEVICE_KONQUEROR(devName));
        listEntry(entry, false);

	processedSize(i+1);
  }
  listEntry(entry, true);
  finished();
}


/*
 * folderMimeType() - returns mimetype of the folder itself
 */
TQString KMobileProtocol::folderMimeType(int cap)
{
  TQString mimetype;
  switch (cap) {
    case KMobileDevice::hasAddressBook:	mimetype = KMOBILE_MIMETYPE_INODE "addressbook";
					break;
    case KMobileDevice::hasCalendar:	mimetype = KMOBILE_MIMETYPE_INODE "calendar";
					break;
    case KMobileDevice::hasNotes:	mimetype = KMOBILE_MIMETYPE_INODE "notes";
					break;
    case KMobileDevice::hasFileStorage:
    default:				mimetype = "inode/directory";
  }
  return mimetype;
}

/*
 * entryMimeType() - returns mimetype of the entries in the given folder
 */
TQString KMobileProtocol::entryMimeType(int cap)
{
  TQString mimetype;
  switch (cap) {
    case KMobileDevice::hasAddressBook:	mimetype = "text/x-vcard";
					break;
    case KMobileDevice::hasCalendar:	mimetype = "text/x-vcalendar";
					break;
    case KMobileDevice::hasNotes:	mimetype = "text/plain";
					break;
    case KMobileDevice::hasFileStorage:
    default:				mimetype = "text/plain";
  }
  return mimetype;
}

/*
 * listTopDeviceDir("mobile:/<devicename>") - sub-directory of a devices
 */

void KMobileProtocol::listTopDeviceDir(const TQString &devName)
{
  PRINT_DEBUG << TQString("listTopDeviceDir(%1)\n").arg(devName);

  KIO::UDSEntry entry;
  unsigned int caps = m_dev.capabilities(devName);

  for (int i=0; i<31; i++) {
	unsigned int cap = 1UL<<i;
	if ((caps & cap) == 0)
		continue;

	TQString filename = m_dev.nameForCap(devName, cap);
	TQString mimetype = folderMimeType(cap);
	
        createDirEntry(entry, filename, TQString("mobile:/%1/%2/").arg(devName).arg(filename), mimetype);
        listEntry(entry, false);
  }
  listEntry(entry, true);
  finished();
}


/*
 * listEntries("mobile:/<devicename>/<resource>") - resources of a device
 */
void KMobileProtocol::listEntries(const TQString &devName, 
	const TQString &resource, const TQString &devPath,
	const KMobileDevice::Capabilities devCaps)
{
  PRINT_DEBUG << TQString("listEntries(%1,%2,%3)\n").arg(devName).arg(resource).arg(devPath);
  switch (devCaps) {
    case KMobileDevice::hasAddressBook:	listAddressBook(devName, resource);
					break;
    case KMobileDevice::hasCalendar:	listCalendar(devName, resource);
					break;
    case KMobileDevice::hasNotes:	listNotes(devName, resource);
					break;
    case KMobileDevice::hasFileStorage: listFileStorage(devName, resource, devPath);
					break;
    default:				error( ERR_CANNOT_ENTER_DIRECTORY,
					  TQString("/%1/%2").arg(devName).arg(resource) );
  }
}

/*
 * listAddressBook("mobile:/<devicename>/Addressbook) - list the addressbook
 */
void KMobileProtocol::listAddressBook(const TQString &devName, const TQString &resource)
{
  PRINT_DEBUG << TQString("listAddressBook(%1)\n").arg(devName);
  
  KIO::UDSEntry entry;

  int fieldwidth;
  int entries = m_dev.numAddresses(devName);
  if (entries>=1000) fieldwidth=4; else 
   if (entries>=100) fieldwidth=3; else
    if (entries>=10) fieldwidth=2; else fieldwidth=1;
  totalSize(entries);
//  TQRegExp rx; rx.setPattern( ".*FN:([\\w\\s]*)[\\n\\r]{2}.*" );
  TQString name;
  for (int i=0; i<entries; i++) {

#if 0
  	TQString content = m_dev.readAddress(devName, i);
        if ( rx.search( content ) < 0 )
		name = TQString();
	else
		name = "_" + rx.cap(1);
#endif

	TQString filename = TQString("%1%2.vcf").arg(i,fieldwidth).arg(name);
	for (int p=0; p<fieldwidth; p++) { 
		if (filename[p]==' ') filename[p]='0'; else break; 
	}
	TQString url = TQString("mobile:/%1/%2/%3").arg(devName).arg(resource).arg(filename);

	createFileEntry(entry, filename, url, entryMimeType(KMobileDevice::hasAddressBook),
			400 /*content.utf8().length()*/ );
        listEntry(entry, false);

	processedSize(i+1);
  }
  listEntry(entry, true);
  finished();
}

/*
 * getVCard() - gives the vCard of the given file
 */
int KMobileProtocol::getVCard( const TQString &devName, TQCString &result, TQString &mime, const TQString &path )
{
  PRINT_DEBUG << TQString("getVCard(%1)\n").arg(path);

  int index = path.find('.');
  if (index>0)
	index = path.left(index).toInt();
  if (index<0 || index>=m_dev.numAddresses(devName))
	return KIO::ERR_DOES_NOT_EXIST;

  TQString str = m_dev.readAddress(devName, index);
  if (str.isEmpty())
	return KIO::ERR_INTERNAL;
  result = str.utf8();
  mime = entryMimeType(KMobileDevice::hasAddressBook);
//  setMetaData("plugin", "const TQString &key, const TQString &value);
  return 0;
}

/*
 * listCalendar("mobile:/<devicename>/Calendar) - list the calendar entries
 */
void KMobileProtocol::listCalendar( const TQString &devName, const TQString &resource)
{
  PRINT_DEBUG << TQString("listCalendar(%1)\n").arg(devName);
  
  KIO::UDSEntry entry;

  int entries = m_dev.numCalendarEntries(devName);
  totalSize(entries);
  for (int i=0; i<entries; i++) {

	TQString filename = TQString("%1_%2.vcs").arg(i).arg(i18n("calendar"));
	TQString url = TQString("mobile:/%1/%2/%3").arg(devName).arg(resource).arg(filename);

	createFileEntry(entry, filename, url, entryMimeType(KMobileDevice::hasCalendar));
        listEntry(entry, false);

	processedSize(i+1);
  }
  listEntry(entry, true);
  finished();
}

/*
 * getCalendar() - reads a calendar entry
 */
int KMobileProtocol::getCalendar( const TQString &devName, TQCString &result, TQString &mime, const TQString &path)
{
  PRINT_DEBUG << TQString("getCalendar(%1, #%2)\n").arg(devName).arg(path);

  /* TODO */
  Q_UNUSED(result);
  Q_UNUSED(mime);
  return KIO::ERR_CANNOT_ENTER_DIRECTORY;
}


/*
 * listNotes("mobile:/<devicename>/Notes) - list the notes
 */
void KMobileProtocol::listNotes( const TQString &devName, const TQString &resource)
{
  PRINT_DEBUG << TQString("listNotes(%1)\n").arg(devName);
  
  KIO::UDSEntry entry;

  int entries = m_dev.numNotes(devName);
  totalSize(entries);
  for (int i=0; i<entries; i++) {

        TQString note /*= m_dev.readNote(devName, i)*/;

	TQString filename = TQString("%1_%2.txt").arg(i).arg(i18n("note"));
	TQString url = TQString("mobile:/%1/%2/%3").arg(devName).arg(resource).arg(filename);

	createFileEntry(entry, filename, url, entryMimeType(KMobileDevice::hasNotes),
			0 /*note.utf8().length()*/);
        listEntry(entry, false);

	processedSize(i+1);
  }
  listEntry(entry, true);
  finished();
}

/*
 * getNote() - gives the Note of the given file
 */
int KMobileProtocol::getNote( const TQString &devName, TQCString &result, TQString &mime, const TQString &path )
{
  PRINT_DEBUG << TQString("getNote(%1)\n").arg(path);

  int index = path.find('_');
  if (index>0)
	index = path.left(index).toInt();
  if (index<0 || index>=m_dev.numNotes(devName))
	return KIO::ERR_DOES_NOT_EXIST;

  TQString note = m_dev.readNote(devName, index);
  if (note.isEmpty())
	return KIO::ERR_DOES_NOT_EXIST;

  result = note.utf8();
  mime = entryMimeType(KMobileDevice::hasNotes);
  return 0;
}

/*
 * listFileStorage("mobile:/<devicename>/Files) - list the files on the device
 */
void KMobileProtocol::listFileStorage(const TQString &devName, const TQString &resource, const TQString &devPath)
{
  PRINT_DEBUG << TQString("listFileStorage(%1,%2)\n").arg(devName).arg(devPath);
  
  /* TODO */
  error( KIO::ERR_DOES_NOT_EXIST, TQString("/%1/%2/%3").arg(devName).arg(resource).arg(devPath) );
}

/*
 * getFileStorage() - gives the file contents of the given file
 */
int KMobileProtocol::getFileStorage(const TQString &devName, TQCString &result, TQString &mime, const TQString &path)
{
  PRINT_DEBUG << TQString("getFileStorage(%1)\n").arg(path);

  /* TODO */
  Q_UNUSED(devName);
  Q_UNUSED(result);
  Q_UNUSED(mime);
  return KIO::ERR_CANNOT_ENTER_DIRECTORY;
}


/**
 * Test if the url contains a directory or a file.
 */
void KMobileProtocol::stat( const KURL &url )
{
  PRINT_DEBUG << "###############################\n";
  PRINT_DEBUG << TQString("stat(%1)\n").arg(url.path());

  KMobileDevice::Capabilities devCaps;
  TQString devName, resource, devPath;

  int err = getDeviceAndRessource(url.path(), devName, resource, devPath, devCaps);
  if (err) {
	error(err, url.path());
	return;
  }

  TQStringList path = TQStringList::split('/', url.path(), false);
  TQString filename = (path.count()>0) ? path[path.count()-1] : "/";
  TQString fullPath = path.join("/");
  TQString fullUrl = TQString("mobile:/%1").arg(fullPath);

  UDSEntry entry;

  bool isDir = devPath.isEmpty();

  if (isDir) {
     createDirEntry(entry, filename, fullUrl, folderMimeType(devCaps));
  } else {
     createFileEntry(entry, filename, fullUrl, entryMimeType(devCaps));
  }

  statEntry(entry);
  finished();
}

/**
 * Get the mimetype.
 */
void KMobileProtocol::mimetype(const KURL &url)
{
  PRINT_DEBUG << "###############################\n";
  PRINT_DEBUG << TQString("mimetype(%1)\n").arg(url.path());

  KMobileDevice::Capabilities devCaps;
  TQString devName, resource, devPath;

  int err = getDeviceAndRessource(url.path(), devName, resource, devPath, devCaps);
  if (err) {
	error(err, url.path());
	return;
  }

  // tell the mimetype
  mimeType(entryMimeType(devCaps));
  finished();
}

/**
 * List the contents of a directory.
 */
void KMobileProtocol::listDir(const KURL &url)
{
  PRINT_DEBUG << "###############################\n";
  PRINT_DEBUG << TQString("listDir(%1)\n").arg(url.path());

  if (!m_dev.isKMobileAvailable()) {
	error( KIO::ERR_CONNECTION_BROKEN, i18n("TDE Mobile Device Manager") );
	return;
  }

  KMobileDevice::Capabilities devCaps;
  TQString devName, resource, devPath;

  int err = getDeviceAndRessource(url.path(), devName, resource, devPath, devCaps);
  if (err) {
	error(err, url.path());
	return;
  }

  if (devName.isEmpty()) {
	listRoot(url);
	return;
  }

#if 0  
  if (!dev) {
	error( KIO::ERR_DOES_NOT_EXIST, TQString("/%1").arg(devName) );
	return;
  }
#endif

  if (resource.isEmpty()) {
	listTopDeviceDir(devName);
	return;
  }

  listEntries(devName, resource, devPath, devCaps);
}