File: orb_get.cpp

package info (click to toggle)
aethera 0.9.3-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,588 kB
  • ctags: 7,282
  • sloc: cpp: 64,544; sh: 9,913; perl: 1,756; makefile: 1,680; python: 258
file content (677 lines) | stat: -rw-r--r-- 17,488 bytes parent folder | download
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 * This file is part of Magellan <http://www.kAlliance.org/Magellan>
 *
 * Copyright (c) 1998-2000 Teodor Mihai <teddy@ireland.com>
 * Copyright (c) 1998-2000 Laur Ivan <laur.ivan@ul.ie>
 * Copyright (c) 1999-2000 Virgil Palanciuc <vv@ulise.cs.pub.ro>
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.troll.no/
 *
 * Also requires the KDE libraries, available at no cost at
 * http://www.kde.org/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <orb.h>
#include <magellan.h>
#include <mailclasses.h>
#include <indexclass.h>
#include <transactionserver.h>
#include <attributemarshall.h>
#include <conversions.h>
#include <qfileinfo.h>
#include <messagedevice.h>
#include <messagedescriptor.h>
#include <messagefactory.h>
#include <imap4handler.h>

bool ObjectRequestBroker::getObjectData(const QString &url, QByteArray &data)
{
  // debug
  // printf("orb: entering getdata(%s)\n", (const char *)url);
	
  if(!exists(url))
  {
    err="Requested object does not exist";
    return false;
  }
  if(isFolder(url))
  {
    err="Can't apply getObjectData() on a folder";
		
		// debug
		
    return false;
  }
	
  int t=subtype(url);
	if(t==Magellan::FolderCache)
	{
		return getFolderCacheData(url, data);
	}
  if(t==Magellan::FolderAttribute)
  {
		return getFolderAttributeData(url, data);
  }
  if(t==Magellan::MailAttribute)
  {
		return getMailAttributeData(url, data);
  }
  if(t==Magellan::PartAttribute)
  {
		return getPartAttributeData(url, data);
  }
  if(t==Magellan::BlockObjectAttribute)
  {
		return getBlockObjectAttributeData(url, data);
  }
  if(t==Magellan::Attribute)
  {
		return getAttributeData(url, data);
  }
  if(t==Magellan::Message)
  {
		return getMailData(url, data);
  }
  if(t==Magellan::Part)
  {
		return getPartData(url, data);
  }
	if(t==Magellan::BlockObject)
	{
		return getBlockObjectData(url,data);
	}
	if(t==Magellan::FileObject)
	{
		return getFileObjectData(url,data);
	}
	err="Unknown error";
	return false;
}

bool ObjectRequestBroker::getObjectData(const QStringList &urls, QByteArray &data)
{
  QDataStream tempStream(data, IO_WriteOnly);
  QByteArray  tempData;
  bool bRet=true;

  // First insert the number of objects
  tempStream << urls.count();
  // Insert the urls
  if( !urls.count() )
  {
    tempStream << 0;
    printf("\nNothing to sent!");
    fflush(stdout);
    return true;
  }
  tempStream << urls;
  for(QStringList::ConstIterator urlIt=urls.begin(); (urlIt != urls.end()) && bRet; ++urlIt)
  {
  // debug
//   printf("orb: entering getObjectsData(%s)\n", (const char *)(*urlIt));

#if 0
    if(!exists(*urlIt))
    {
      err="Requested object does not exist";
      bRet=false;
      break;
    }
    if(isFolder(*urlIt))
    {
      err="Can't apply getObjectsData() on a folder";
      bRet=false;
      break;
    }
#endif
  	
    int t=subtype((*urlIt));
    tempData.resize(0);
    switch(t)
    {
      case Magellan::FolderCache :
  		  bRet=getFolderCacheData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::FolderAttribute :
  		  bRet=getFolderAttributeData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::MailAttribute :
  		  bRet=getMailAttributeData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::PartAttribute :
  		  bRet=getPartAttributeData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::BlockObjectAttribute :
  		  bRet=getBlockObjectAttributeData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::Attribute :
  		  bRet=getAttributeData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::Message :
  		  bRet=getMailData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::Part :
  		  bRet=getPartData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::BlockObject :
  		  bRet=getBlockObjectData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      case Magellan::FileObject :
  		  bRet=getFileObjectData((*urlIt), tempData);
  		  tempStream<<tempData;
        break;
      default:
        err="Unknown error";
        bRet=false;
        break;
    }
  }

//  printf("\nGetObjectsData----- Done !");
//  fflush(stdout);
	return bRet;
}

bool ObjectRequestBroker::getChildrenData(const QString &url, QByteArray &data)
{
  QDataStream tempStream(data, IO_WriteOnly);

  // debug
   printf("orb: entering getChildrenData(%s)\n", (const char *)url);
   fflush(stdout);
	
  if(!url || url.isNull() || url.isEmpty() || !exists(url))
  {
    err="Requested object does not exist";
    tempStream << 0;
    return false;
  }
  if(!isFolder(url))
  {
    err="Can apply getObjectsData() only for a folder!\n";
    err+=url;
    err+=" isn't a folder.";
    return false;
  }
  // get the url from the id.
  QStringList urls=children(url);
  if( !urls.count() )
  {
    tempStream << 0;
    return(true);
  }
  else
  {
    for(QStringList::Iterator it=urls.begin(); it!=urls.end(); it++)
      (*it).insert(0,url+"/");
    return getObjectData(urls, data);
  }
}

bool ObjectRequestBroker::getFolderCacheData(const QString &url, QByteArray &data)
{
	MailFolder *mailFolder=mailfolderReference(folder(url));
	if(!mailFolder)
	{
		err="Cannot retrieve cache for non-mail folders";
		printf("orb: Warning, folder %s is not a mail folder, cannot retrieve cache\n", (const char *)folder(url));
		return false;
	}
	else
	{
		data=_stream(mailFolder->entryCacheBlock());
		return true;
	}
}

bool ObjectRequestBroker::getFolderAttributeData(const QString &url, QByteArray &data)
{
	// debug
	// printf("orb: %sstatting folder %s\n", url.contains("fattr")?"fast ":"", (const char *)folderpath(url));

  QString folder=folderpath(url);
  _FolderAttribute fattr;
  if(type(folder)==Magellan::Message)
  {
		// debug
		// printf("orb: this is a mail folder, proceeding\n");

    MailFolder *mfolder=mailfolderReference(folder);

		if(url.contains(".attr"))
		{
			QFileInfo finfo(mfolder->getStorageDevice());

      fattr.owner=finfo.owner();
      fattr.device=mfolder->getStorageDevice();
      fattr.rtype="indexed,extended:(data descriptors,multipart storage,uniblock storage,incremental data change,incremental index change,data relocation)";
      fattr.size=finfo.size();
      fattr.lmodified=finfo.lastModified();	
	    fattr.folders=0; // no recursive folders
		}
    fattr.items=mfolder->getMessageCount();
		fattr.recent=mfolder->getUnread();

    data=_stream(fattr);
    return true;
  }
  else
  {
		// debug
		// printf("orb: this is a generic folder, proceeding\n");

    DataCollection *dfolder=folderReference(folder);

		if(url.contains(".attr"))
		{
			QFileInfo finfo(dfolder->storageDevice);

      fattr.owner=finfo.owner();
      fattr.device=dfolder->storageDevice;
      fattr.device=dfolder->type==DataCollection::FileStorage?"file":"folder";
      fattr.rtype="sequential";
      fattr.size=finfo.size();
      fattr.lmodified=finfo.lastModified();
      fattr.folders=0; // no recursive folders
		}

    fattr.items=dfolder->getEntryList().count();
		fattr.recent=0;

    data=_stream(fattr);
    return true;
  }
}

bool ObjectRequestBroker::getMailAttributeData(const QString &url, QByteArray &data)
{
	QString objectUrl=url.left(url.find('.'));
  _MailAttribute attr;
  IndexClass *idx=indexReference(objectUrl);

	if(!idx)
	{
		err="Error getting the mail attribute data: object does not exist";
		
		printf("Warning: %s\n", (const char *)err);
		
		return false;
	}
	
	if(url.contains(".attr"))
	{
		QFileInfo finfo(idx->getParentFolder()->getStorageDevice()+"/messages");

    attr.owner=finfo.owner();
    attr.device=idx->getParentFolder()->getStorageDevice()+"/messages";
    attr.device="file";
    attr.rtype="indexed,extended:(data descriptors,multipart storage,uniblock storage,incremental data change,incremental index change,data relocation)";
    attr.size=finfo.size();
    attr.lmodified=finfo.lastModified();
	}

	attr.uniblockOffset=idx->getUniblockOffset();
	attr.uniblockLength=idx->getUniblockLength();
	attr.descriptorOffset=idx->getDescriptorOffset();
	attr.descriptorLength=idx->getDescriptorLength();
  attr.partCount=idx->getPartCount();
	attr.multipartOnly=idx->isMultipartOnly();

  data=_stream(attr);
  return true;
}

bool ObjectRequestBroker::getPartAttributeData(const QString &url, QByteArray &data)
{
	QString objectUrl=url.left(url.find('.'));
  _PartAttribute attr;
  IndexClass *idx=indexReference(objectUrl);

	if(!idx)
	{
		err="Error getting the part attribute data: object does not exist";
		
		printf("Warning: %s\n", (const char *)err);
		
		return false;
	}
	
	if(url.contains(".attr"))
	{
		QFileInfo finfo(idx->getParentFolder()->getStorageDevice()+"/messages");

    attr.owner=finfo.owner();
    attr.device=idx->getParentFolder()->getStorageDevice()+"/messages";
    attr.device="file";
    attr.rtype="indexed,extended:(data descriptors,multipart storage,uniblock storage,incremental data change,incremental index change,data relocation)";
    attr.size=finfo.size();
    attr.lmodified=finfo.lastModified();
	}


  int pBegin=url.find('.')+1, pEnd=url.findRev('.');
  MimePart *part=idx->getPartAt(url.mid(pBegin, pEnd-pBegin).toInt());

  attr.mimetype=part->mimetype;
  attr.name=part->name;
  attr.cid=part->cid;
  attr.encoding=part->encoding;
  attr.charset=part->charset;
  attr.mtype=part->type;
  attr.partOffset=part->offset;
  attr.partLength=part->length;
	attr.embeddedPath=part->embeddedPath;

  data=_stream(attr);
  return true;
}

bool ObjectRequestBroker::getBlockObjectAttributeData(const QString &url, QByteArray &data)
{
	QString objectUrl=url.left(url.find('.'));
  _BlockObjectAttribute attr;
  ObjectReference *ref=objectReference(objectUrl);
  DataCollection *dfolder=folderReference(folderpath(url));

	if(!ref || !dfolder)
	{
		err="Error getting the block object attribute data: object does not exist";
		
		printf("Warning: %s\n", (const char *)err);
		
		return false;
	}
		
	if(url.contains(".attr"))
	{
		QFileInfo finfo(dfolder->storageDevice);

    attr.owner=finfo.owner();
    attr.device=dfolder->storageDevice;
    attr.device="file";
    attr.rtype="sequential";
    attr.size=ref->length;
    attr.lmodified=ref->lastModified;
	}

  data=_stream(attr);
  return true;
}

bool ObjectRequestBroker::getBlockObjectData(const QString &url, QByteArray &data)
{
	QString objectUrl=url.left(url.find('.'));
  ObjectReference *ref=objectReference(objectUrl);
	if(!ref)
		return false;
	data=ref->rawData;
	return true;
}

bool ObjectRequestBroker::getFileObjectData(const QString &url, QByteArray &data)
{
	QString objectUrl=url.left(url.find('.'));
  ObjectReference *ref=objectReference(objectUrl);
	printf("ref %p\n",ref);
	if(!ref)
		return false;
	data=ref->rawData;
	return true;
}

bool ObjectRequestBroker::getAttributeData(const QString &url, QByteArray &data)
{
	// debug
	// printf("orb: attribute query in progress\n");

	QString objectUrl=url.left(url.find('.'));
  _Attribute attr;

	// debug
	// printf("orb: %s is a file attribute, stat follows\n", (const char *)url);

  ObjectReference *ref=objectReference(objectUrl);

	// debug
	// printf("orb: object reference: %p\n", ref);
	// printf("orb: filename: %s\n", (const char *)(folderReference(folderpath(objectUrl))->storageDevice+"/"+ref->filename));

	if(!ref)
	{
		err="Error getting the object attribute data: object does not exist";
		
		printf("Warning: %s\n", (const char *)err);
		
		return false;
	}
	
	if(url.contains(".attr"))
	{
		QFileInfo finfo(folderReference(folderpath(objectUrl))->storageDevice+"/"+ref->filename);

    attr.owner=finfo.owner();
    attr.device=folderReference(folderpath(objectUrl))->storageDevice+"/"+ref->filename;
    attr.dtype="file, extended:(standalone data)";
    attr.rtype="random";
	  attr.size=finfo.size();
	  attr.lmodified=finfo.lastModified();
	}

	// printf("orb: got object attribute; owner:%s\n", (const char *)attr.owner);

  data=_stream(attr);
  return true;
}

bool ObjectRequestBroker::getMailData(const QString &url, QByteArray &data)
{
	// extract query
	QString property, objectUrl=object(url);
	
	if(url.contains('%'))
	{
		property=url.mid(url.find('%')+1).lower();
		objectUrl=url.left(url.findRev('%'));
		return getMailPropertyData(objectUrl, property, data);
	}

	IndexClass *idx=indexReference(url);
  if(!idx)
  {
    printf("Warning: indexReference() returned null when a vadid pointer was expected, bailing out\n");
    err="Internal error in URLManager::indexReference()";
    return false;
  }

	// debug
	// printf("orb: getting message descriptor for object %s, index %s(%p)...\n", (const char *)url, (const char *)idx->getID(), idx);
	
	QDataStream stream(data, IO_WriteOnly);
	
	// debug
	// printf("orb: index parent name is %s\n", (const char *)idx->getParentFolder()->name());
	// printf("orb: index message file is %s\n", (const char *)idx->getDataFilename());
	
	MessageDevice *dev=MessageFactory::thisInstance()->getMessage(idx);
	if(dev)
	{
		dev->loadDescriptor(false);
		stream<<dev->getDescriptor();
	}
	else
	{
		printf("Warning: MessageFactory::getMessage() returned null when a valid pointer was expected, bailing out\n");
		err="Internal error in MessageFactory::getMessage()";
		return false;
	}
	
	return true;
}

bool ObjectRequestBroker::getMailPropertyData(const QString &path, const QString &property, QByteArray &data)
{
	IndexClass *idx=indexReference(path);
  if(!idx)
  {
    printf("Warning: indexReference() returned null when a vadid pointer was expected, bailing out\n");
    err="Internal error in URLManager::indexReference()";
    return false;
  }

//  printf("\nProperty=%s\n", property.latin1());
//  fflush(stdout);

  // check if it is an IMAP folder
//  if( property!="size" && property!="hasattachments")
  if( property=="text" || property=="html" ||
      property=="alltext" || property=="allhtml" )
    if( MailFolder::IMAP4==idx->getParentFolder()->getFolderProtocol() && !idx->isFullMessage() )
   	  IMAP4Handler::ref()->updateMessage(*idx);
 	
	MessageDevice dev(idx);
 	
	if(property=="text")
	{
		data=dev.text();
	}
	else if(property=="alltext")
	{
		data=dev.text(true);
	}
	else if(property=="html")
	{
		data=dev.html();
	}
	else if(property=="allhtml")
	{
		data=dev.html(true);
	}
	else if(property=="rfc822header")
	{
		data=dev.rfc822Header();
	}
	else if(property=="rfc822message")
	{
		data=dev.rfc822Message();
	}
	else if(property=="textpart")
	{
		data=_stream(dev.textPart());
	}
	else if(property=="messagetext")
	{
		data=_stream(dev.messageText());
	}
	else if(property=="htmlpart")
	{
		data=_stream(dev.htmlPart());
	}
	else if(property=="partcount")
	{
		data=_stream(idx->getPartCount());
	}
	else if(property=="hashtml")
	{
		data=_stream(dev.hasHtml());
	}
	else if(property=="hastext")
	{
		data=_stream(dev.hasText());
	}
	else if(property=="hasattachments")
	{
		data=_stream(dev.hasAttachments());
	}
	else if(property=="size")
	{
		data=_stream(idx->getSize());
	}
	else
	{
		// debug
		printf("orb: warning: unknown message query (%s) on object %s\n", (const char *)property, (const char *)path);
		
		err="Unknown message query: "+property;
		return false;
	}
	
	return true;
}

bool ObjectRequestBroker::getPartData(const QString &url, QByteArray &data)
{
	QString objectUrl=url.left(url.find('.'));
  IndexClass *idx=indexReference(objectUrl);
	
  if(!idx)
  {
    printf("Warning: indexReference() returned null when a vadid pointer was expected, bailing out\n");
    err="Internal error in URLManager::indexReference()";
    return false;
  }

  int k=url.findRev('.');
  int p=url.mid(k+1).toInt();
  if(p<0 || p>idx->getPartCount())
  {
    err="Non-existent part requested";
    return false;
  }
	
	MessageDevice dev(idx);
	dev.getPartData(p, data);
	
	return true;
}

bool ObjectRequestBroker::getCollectionObjectData(const QString &url, QByteArray &data)
{
  ObjectReference *tobj=objectReference(url);
	
  if(tobj)
  {
    // debug
    // printf("orb: narrowed object reference (%p)\n", tobj);
		// printf("orb: object data size %d\n", tobj->rawData.size());

    data=tobj->rawData;
    return true;
  }

  printf("Warning: objectReference() returned null when a valid pointer was expected, bailing out\n");
  err="Internal error in URLManager::objectReference()";
  return false;
}