summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/ocontact.cpp
blob: be4ce0a92dcc0a55d68b10b2624bbb26e4db14a4 (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
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
** Copyright (C) 2002-2003 by Stefan Eilers (eilers.stefan@epost.de)
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#define QTOPIA_INTERNAL_CONTACT_MRE

#include "ocontact.h"
#include "opimresolver.h"

#include <qpe/stringutil.h>
#include "oconversion.h"
#include <qpe/timestring.h>

#include <qobject.h>
#include <qregexp.h>
#include <qstylesheet.h>
#include <qfileinfo.h>
#include <qmap.h>

#include <stdio.h>

/*!
  \class Contact contact.h
  \brief The Contact class holds the data of an address book entry.

  This data includes information the name of the person, contact
  information, and business information such as deparment and job title.

  \ingroup qtopiaemb
  \ingroup qtopiadesktop
*/


/*!
  Creates a new, empty contact.
*/
OContact::OContact()
    : OPimRecord(), mMap(), d( 0 )
{
}

/*!
  \internal
  Creates a new contact.  The properties of the contact are
  set from \a fromMap.
*/
OContact::OContact( const QMap<int, QString> &fromMap ) :
    OPimRecord(), mMap( fromMap ), d( 0 )
{
    QString cats = mMap[ Qtopia::AddressCategory ];
    if ( !cats.isEmpty() )
	setCategories( idsFromString( cats ) );

    QString uidStr = find( Qtopia::AddressUid );

    if ( uidStr.isEmpty() || (uidStr.toInt() == 0) ){
	    qWarning( "Invalid UID found. Generate new one.." );
	setUid( uidGen().generate() );
    }else
	setUid( uidStr.toInt() );

//     if ( !uidStr.isEmpty() )
// 	setUid( uidStr.toInt() );
}

/*!
  Destroys a contact.
*/
OContact::~OContact()
{
}

/*! \fn void OContact::setTitle( const QString &str )
  Sets the title of the contact to \a str.
*/

/*! \fn void OContact::setFirstName( const QString &str )
  Sets the first name of the contact to \a str.
*/

/*! \fn void OContact::setMiddleName( const QString &str )
  Sets the middle name of the contact to \a str.
*/

/*! \fn void OContact::setLastName( const QString &str )
  Sets the last name of the contact to \a str.
*/

/*! \fn void OContact::setSuffix( const QString &str )
  Sets the suffix of the contact to \a str.
*/

/*! \fn void OContact::setFileAs( const QString &str )
  Sets the contact to filed as \a str.
*/

/*! \fn void OContact::setDefaultEmail( const QString &str )
  Sets the default email of the contact to \a str.
*/

/*! \fn void OContact::setHomeStreet( const QString &str )
  Sets the home street address of the contact to \a str.
*/

/*! \fn void OContact::setHomeCity( const QString &str )
  Sets the home city of the contact to \a str.
*/

/*! \fn void OContact::setHomeState( const QString &str )
  Sets the home state of the contact to \a str.
*/

/*! \fn void OContact::setHomeZip( const QString &str )
  Sets the home zip code of the contact to \a str.
*/

/*! \fn void OContact::setHomeCountry( const QString &str )
  Sets the home country of the contact to \a str.
*/

/*! \fn void OContact::setHomePhone( const QString &str )
  Sets the home phone number of the contact to \a str.
*/

/*! \fn void OContact::setHomeFax( const QString &str )
  Sets the home fax number of the contact to \a str.
*/

/*! \fn void OContact::setHomeMobile( const QString &str )
  Sets the home mobile phone number of the contact to \a str.
*/

/*! \fn void OContact::setHomeWebpage( const QString &str )
  Sets the home webpage of the contact to \a str.
*/

/*! \fn void OContact::setCompany( const QString &str )
  Sets the company for contact to \a str.
*/

/*! \fn void OContact::setJobTitle( const QString &str )
  Sets the job title of the contact to \a str.
*/

/*! \fn void OContact::setDepartment( const QString &str )
  Sets the department for contact to \a str.
*/

/*! \fn void OContact::setOffice( const QString &str )
  Sets the office for contact to \a str.
*/

/*! \fn void OContact::setBusinessStreet( const QString &str )
  Sets the business street address of the contact to \a str.
*/

/*! \fn void OContact::setBusinessCity( const QString &str )
  Sets the business city of the contact to \a str.
*/

/*! \fn void OContact::setBusinessState( const QString &str )
  Sets the business state of the contact to \a str.
*/

/*! \fn void OContact::setBusinessZip( const QString &str )
  Sets the business zip code of the contact to \a str.
*/

/*! \fn void OContact::setBusinessCountry( const QString &str )
  Sets the business country of the contact to \a str.
*/

/*! \fn void OContact::setBusinessPhone( const QString &str )
  Sets the business phone number of the contact to \a str.
*/

/*! \fn void OContact::setBusinessFax( const QString &str )
  Sets the business fax number of the contact to \a str.
*/

/*! \fn void OContact::setBusinessMobile( const QString &str )
  Sets the business mobile phone number of the contact to \a str.
*/

/*! \fn void OContact::setBusinessPager( const QString &str )
  Sets the business pager number of the contact to \a str.
*/

/*! \fn void OContact::setBusinessWebpage( const QString &str )
  Sets the business webpage of the contact to \a str.
*/

/*! \fn void OContact::setProfession( const QString &str )
  Sets the profession of the contact to \a str.
*/

/*! \fn void OContact::setAssistant( const QString &str )
  Sets the assistant of the contact to \a str.
*/

/*! \fn void OContact::setManager( const QString &str )
  Sets the manager of the contact to \a str.
*/

/*! \fn void OContact::setSpouse( const QString &str )
  Sets the spouse of the contact to \a str.
*/

/*! \fn void OContact::setGender( const QString &str )
  Sets the gender of the contact to \a str.
*/

/*! \fn void OContact::setNickname( const QString &str )
  Sets the nickname of the contact to \a str.
*/

/*! \fn void OContact::setNotes( const QString &str )
  Sets the notes about the contact to \a str.
*/

/*! \fn QString OContact::title() const
  Returns the title of the contact.
*/

/*! \fn QString OContact::firstName() const
  Returns the first name of the contact.
*/

/*! \fn QString OContact::middleName() const
  Returns the middle name of the contact.
*/

/*! \fn QString OContact::lastName() const
  Returns the last name of the contact.
*/

/*! \fn QString OContact::suffix() const
  Returns the suffix of the contact.
*/

/*! \fn QString OContact::fileAs() const
  Returns the string the contact is filed as.
*/

/*! \fn QString OContact::defaultEmail() const
  Returns the default email address of the contact.
*/

/*! \fn QString OContact::emails() const
  Returns the list of email address for a contact separated by ';'s in a single
  string.
*/

/*! \fn QString OContact::homeStreet() const
  Returns the home street address of the contact.
*/

/*! \fn QString OContact::homeCity() const
  Returns the home city of the contact.
*/

/*! \fn QString OContact::homeState() const
  Returns the home state of the contact.
*/

/*! \fn QString OContact::homeZip() const
  Returns the home zip of the contact.
*/

/*! \fn QString OContact::homeCountry() const
  Returns the home country of the contact.
*/

/*! \fn QString OContact::homePhone() const
  Returns the home phone number of the contact.
*/

/*! \fn QString OContact::homeFax() const
  Returns the home fax number of the contact.
*/

/*! \fn QString OContact::homeMobile() const
  Returns the home mobile number of the contact.
*/

/*! \fn QString OContact::homeWebpage() const
  Returns the home webpage of the contact.
*/

/*! \fn QString OContact::company() const
  Returns the company for the contact.
*/

/*! \fn QString OContact::department() const
  Returns the department for the contact.
*/

/*! \fn QString OContact::office() const
  Returns the office for the contact.
*/

/*! \fn QString OContact::jobTitle() const
  Returns the job title of the contact.
*/

/*! \fn QString OContact::profession() const
  Returns the profession of the contact.
*/

/*! \fn QString OContact::assistant() const
  Returns the assistant of the contact.
*/

/*! \fn QString OContact::manager() const
  Returns the manager of the contact.
*/

/*! \fn QString OContact::businessStreet() const
  Returns the business street address of the contact.
*/

/*! \fn QString OContact::businessCity() const
  Returns the business city of the contact.
*/

/*! \fn QString OContact::businessState() const
  Returns the business state of the contact.
*/

/*! \fn QString OContact::businessZip() const
  Returns the business zip of the contact.
*/

/*! \fn QString OContact::businessCountry() const
  Returns the business country of the contact.
*/

/*! \fn QString OContact::businessPhone() const
  Returns the business phone number of the contact.
*/

/*! \fn QString OContact::businessFax() const
  Returns the business fax number of the contact.
*/

/*! \fn QString OContact::businessMobile() const
  Returns the business mobile number of the contact.
*/

/*! \fn QString OContact::businessPager() const
  Returns the business pager number of the contact.
*/

/*! \fn QString OContact::businessWebpage() const
  Returns the business webpage of the contact.
*/

/*! \fn QString OContact::spouse() const
  Returns the spouse of the contact.
*/

/*! \fn QString OContact::gender() const
  Returns the gender of the contact.
*/

/*! \fn QString OContact::nickname() const
  Returns the nickname of the contact.
*/

/*! \fn QString OContact::children() const
  Returns the children of the contact.
*/

/*! \fn QString OContact::notes() const
  Returns the notes relating to the the contact.
*/

/*! \fn QString OContact::groups() const
  \internal
  Returns the groups for the contact.
*/

/*! \fn QStringList OContact::groupList() const
  \internal
*/

/*! \fn QString OContact::field(int) const
  \internal
*/

/*! \fn void OContact::saveJournal( journal_action, const QString & = QString::null )
  \internal
*/

/*! \fn void OContact::setUid( int id )
  \internal
  Sets the uid for this record to \a id.
*/

/*! \enum OContact::journal_action
  \internal
*/

/*!
  \internal
*/
QMap<int, QString> OContact::toMap() const
{
    QMap<int, QString> map = mMap;
    QString cats = idsToString( categories() );
    if ( !cats.isEmpty() )
	map.insert( Qtopia::AddressCategory, cats );
    return map;
}

/*!
  Returns a rich text formatted QString representing the contents the contact.
*/
QString OContact::toRichText() const
{
    QString text;
    QString value, comp, state;
    QString str;
    bool marker = false;

    // name, jobtitle and company
    if ( !(value = fullName()).isEmpty() )
	text += "<b><h3><img src=\"addressbook/AddressBook\">" + Qtopia::escapeString(value) + "</h3></b>";

    if ( !(value = jobTitle()).isEmpty() )
	text += Qtopia::escapeString(value) + "<br>";

    comp = company();
    if ( !(value = department()).isEmpty() ) {
	text += Qtopia::escapeString(value);
	if ( comp )
	    text += ", ";
	else
	    text += "<br>";
    }
    if ( !comp.isEmpty() )
	text += Qtopia::escapeString(comp) + "<br>";

    text += "<hr><br>";

    // defailt email
    QString defEmail = defaultEmail();
    if ( !defEmail.isEmpty() )
	text += "<b><img src=\"addressbook/email\">" + QObject::tr("Default Email: ") + "</b>"
		+ Qtopia::escapeString(defEmail) + "<br>";

    text += "<br>";

    // business address
    if ( !businessStreet().isEmpty() || !businessCity().isEmpty() ||
	 !businessZip().isEmpty() || !businessCountry().isEmpty() ) {
	text += QObject::tr( "<b>Work Address:</b>" );
	text +=  "<br>";
	marker = true;
    }

    if ( !(value = businessStreet()).isEmpty() )
	text += Qtopia::escapeString(value) + "<br>";
    state =  businessState();
    if ( !(value = businessZip()).isEmpty() )
	text += Qtopia::escapeString(value) + " ";
    if ( !(value = businessCity()).isEmpty() ) {
	text += Qtopia::escapeString(value);
	if ( state )
	    text += ", " + Qtopia::escapeString(state);
	text += "<br>";
    } else if ( !state.isEmpty() )
	text += Qtopia::escapeString(state) + "<br>";

    if ( !(value = businessCountry()).isEmpty() )
	text += Qtopia::escapeString(value) + "<br>";

    // rest of Business data
    str = office();
    if ( !str.isEmpty() ){
	text += "<b>" + QObject::tr("Office: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = businessWebpage();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/webpagework\">" + QObject::tr("Business Web Page: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = businessPhone();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/phonework\">" + QObject::tr("Business Phone: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = businessFax();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/faxwork\">" + QObject::tr("Business Fax: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = businessMobile();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/mobilework\">" + QObject::tr("Business Mobile: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = businessPager();
    if ( !str.isEmpty() ){
	text += "<b>" + QObject::tr("Business Pager: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }

    text += "<br>";

    // home address
    if ( !homeStreet().isEmpty() || !homeCity().isEmpty() ||
	 !homeZip().isEmpty() || !homeCountry().isEmpty() ) {
	text += QObject::tr( "<b>Home Address:</b>" );
	text +=  "<br>";
    }

    if ( !(value = homeStreet()).isEmpty() )
	text += Qtopia::escapeString(value) + "<br>";
    state =  homeState();
    if ( !(value = homeZip()).isEmpty() )
	text += Qtopia::escapeString(value) + " ";
    if ( !(value = homeCity()).isEmpty() ) {
	text += Qtopia::escapeString(value);
	if ( !state.isEmpty() )
	    text += ", " + Qtopia::escapeString(state);
	text += "<br>";
    } else if (!state.isEmpty())
	text += Qtopia::escapeString(state) + "<br>";
    if ( !(value = homeCountry()).isEmpty() )
	text += Qtopia::escapeString(value) + "<br>";

    // rest of Home data
    str = homeWebpage();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/webpagehome\">" + QObject::tr("Home Web Page: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = homePhone();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/phonehome\">" + QObject::tr("Home Phone: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = homeFax();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/faxhome\">" + QObject::tr("Home Fax: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }
    str = homeMobile();
    if ( !str.isEmpty() ){
	text += "<b><img src=\"addressbook/mobilehome\">" + QObject::tr("Home Mobile: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
	marker = true;
    }

    if ( marker )
	    text += "<br><hr><br>";
    // the others...
    str = emails();
    if ( !str.isEmpty() && ( str != defEmail ) )
	text += "<b>" + QObject::tr("All Emails: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
    str = profession();
    if ( !str.isEmpty() )
	text += "<b>" + QObject::tr("Profession: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
    str = assistant();
    if ( !str.isEmpty() )
	text += "<b>" + QObject::tr("Assistant: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
    str = manager();
    if ( !str.isEmpty() )
	text += "<b>" + QObject::tr("Manager: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
    str = gender();
    if ( !str.isEmpty() && str.toInt() != 0 ) {
	if ( str.toInt() == 1 )
	    str = QObject::tr( "Male" );
	else if ( str.toInt() == 2 )
	    str = QObject::tr( "Female" );
	text += "<b>" + QObject::tr("Gender: ") + "</b>" + str + "<br>";
    }
    str = spouse();
    if ( !str.isEmpty() )
	text += "<b>" + QObject::tr("Spouse: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";
    if ( birthday().isValid() ){
	    str = TimeString::numberDateString( birthday() );
	    text += "<b>" + QObject::tr("Birthday: ") + "</b>"
		    + Qtopia::escapeString(str) + "<br>";
    }
    if ( anniversary().isValid() ){
	    str = TimeString::numberDateString( anniversary() );
	    text += "<b>" + QObject::tr("Anniversary: ") + "</b>"
		    + Qtopia::escapeString(str) + "<br>";
    }
    str = children();
    if ( !str.isEmpty() )
	text += "<b>" + QObject::tr("Children: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";

    str = nickname();
    if ( !str.isEmpty() )
	text += "<b>" + QObject::tr("Nickname: ") + "</b>"
		+ Qtopia::escapeString(str) + "<br>";

    // categories
    if ( categoryNames("Contacts").count() ){
	    text += "<b>" + QObject::tr( "Category:") + "</b> ";
	    text += categoryNames("Contacts").join(", ");
	    text += "<br>";
    }

    // notes last
    if ( !(value = notes()).isEmpty() ) {
	    text += "<br><hr><b>" + QObject::tr( "Notes:") + "</b> ";
	    QRegExp reg("\n");

	    //QString tmp = Qtopia::escapeString(value);
	    QString tmp = QStyleSheet::convertFromPlainText(value);
	    //tmp.replace( reg, "<br>" );
	    text += "<br>" + tmp + "<br>";
    }
    return text;
}

/*!
  \internal
*/
void OContact::insert( int key, const QString &v )
{
    QString value = v.stripWhiteSpace();
    if ( value.isEmpty() )
	mMap.remove( key );
    else
	mMap.insert( key, value );
}

/*!
  \internal
*/
void OContact::replace( int key, const QString & v )
{
    QString value = v.stripWhiteSpace();
    if ( value.isEmpty() )
	mMap.remove( key );
    else
	mMap.replace( key, value );
}

/*!
  \internal
*/
QString OContact::find( int key ) const
{
    return mMap[key];
}

/*!
  \internal
*/
QString OContact::displayAddress( const QString &street,
				 const QString &city,
				 const QString &state,
				 const QString &zip,
				 const QString &country ) const
{
    QString s = street;
    if ( !street.isEmpty() )
	s+= "\n";
    s += city;
    if ( !city.isEmpty() && !state.isEmpty() )
	s += ", ";
    s += state;
    if ( !state.isEmpty() && !zip.isEmpty() )
	s += "  ";
    s += zip;
    if ( !country.isEmpty() && !s.isEmpty() )
	s += "\n";
    s += country;
    return s;
}

/*!
  \internal
*/
QString OContact::displayBusinessAddress() const
{
    return displayAddress( businessStreet(), businessCity(),
			   businessState(), businessZip(),
			   businessCountry() );
}

/*!
  \internal
*/
QString OContact::displayHomeAddress() const
{
    return displayAddress( homeStreet(), homeCity(),
			   homeState(), homeZip(),
			   homeCountry() );
}

/*!
  Returns the full name of the contact
*/
QString OContact::fullName() const
{
    QString title = find( Qtopia::Title );
    QString firstName = find( Qtopia::FirstName );
    QString middleName = find( Qtopia::MiddleName );
    QString lastName = find( Qtopia::LastName );
    QString suffix = find( Qtopia::Suffix );

    QString name = title;
    if ( !firstName.isEmpty() ) {
	if ( !name.isEmpty() )
	    name += " ";
	name += firstName;
    }
    if ( !middleName.isEmpty() ) {
	if ( !name.isEmpty() )
	    name += " ";
	name += middleName;
    }
    if ( !lastName.isEmpty() ) {
	if ( !name.isEmpty() )
	    name += " ";
	name += lastName;
    }
    if ( !suffix.isEmpty() ) {
	if ( !name.isEmpty() )
	    name += " ";
	name += suffix;
    }
    return name.simplifyWhiteSpace();
}

/*!
  Returns a list of the names of the children of the contact.
*/
QStringList OContact::childrenList() const
{
    return QStringList::split( " ", find( Qtopia::Children ) );
}

/*! \fn void OContact::insertEmail( const QString &email )

  Insert \a email into the email list. Ensures \a email can only be added
  once. If there is no default email address set, it sets it to the \a email.
*/

/*! \fn void OContact::removeEmail( const QString &email )

  Removes the \a email from the email list. If the default email was \a email,
  then the default email address is assigned to the first email in the
  email list
*/

/*! \fn void OContact::clearEmails()

  Clears the email list.
 */

/*! \fn void OContact::insertEmails( const QStringList &emailList )

  Appends the \a emailList to the exiting email list
 */

/*!
  Returns a list of email addresses belonging to the contact, including
  the default email address.
*/
QStringList OContact::emailList() const
{
    QString emailStr = emails();

    QStringList r;
    if ( !emailStr.isEmpty() ) {
	qDebug(" emailstr ");
	QStringList l = QStringList::split( emailSeparator(), emailStr );
	for ( QStringList::ConstIterator it = l.begin();it != l.end();++it )
	    r += (*it).simplifyWhiteSpace();
    }

    return r;
}

/*!
  \overload

  Generates the string for the contact to be filed as from the first,
  middle and last name of the contact.
*/
void OContact::setFileAs()
{
    QString lastName, firstName, middleName, fileas;

    lastName = find( Qtopia::LastName );
    firstName = find( Qtopia::FirstName );
    middleName = find( Qtopia::MiddleName );
    if ( !lastName.isEmpty() && !firstName.isEmpty()
	 && !middleName.isEmpty() )
	fileas = lastName + ", " + firstName + " " + middleName;
    else if ( !lastName.isEmpty() && !firstName.isEmpty() )
	fileas = lastName + ", " + firstName;
    else if ( !lastName.isEmpty() || !firstName.isEmpty() ||
	      !middleName.isEmpty() )
	fileas = firstName + ( firstName.isEmpty() ? "" : " " )
		 + middleName + ( middleName.isEmpty() ? "" : " " )
		 + lastName;

    replace( Qtopia::FileAs, fileas );
}

/*!
  \internal
  Appends the contact information to \a buf.
*/
void OContact::save( QString &buf ) const
{
    static const QStringList SLFIELDS = fields();
    // I'm expecting "<Contact " in front of this...
    for ( QMap<int, QString>::ConstIterator it = mMap.begin();
	  it != mMap.end(); ++it ) {
	const QString &value = it.data();
	int key = it.key();
	if ( !value.isEmpty() ) {
	    if ( key == Qtopia::AddressCategory || key == Qtopia::AddressUid)
		continue;

	    key -= Qtopia::AddressCategory+1;
	    buf += SLFIELDS[key];
	    buf += "=\"" + Qtopia::escapeString(value) + "\" ";
	}
    }
    buf += customToXml();
    if ( categories().count() > 0 )
	buf += "Categories=\"" + idsToString( categories() ) + "\" ";
    buf += "Uid=\"" + QString::number( uid() ) + "\" ";
    // You need to close this yourself
}


/*!
  \internal
  Returns the list of fields belonging to a contact
  Never change order of this list ! It has to be regarding
  enum AddressBookFields !!
*/
QStringList OContact::fields()
{
    QStringList list;

    list.append( "Title" );  // Not Used!
    list.append( "FirstName" );
    list.append( "MiddleName" );
    list.append( "LastName" );
    list.append( "Suffix" );
    list.append( "FileAs" );

    list.append( "JobTitle" );
    list.append( "Department" );
    list.append( "Company" );
    list.append( "BusinessPhone" );
    list.append( "BusinessFax" );
    list.append( "BusinessMobile" );

    list.append( "DefaultEmail" );
    list.append( "Emails" );

    list.append( "HomePhone" );
    list.append( "HomeFax" );
    list.append( "HomeMobile" );

    list.append( "BusinessStreet" );
    list.append( "BusinessCity" );
    list.append( "BusinessState" );
    list.append( "BusinessZip" );
    list.append( "BusinessCountry" );
    list.append( "BusinessPager" );
    list.append( "BusinessWebPage" );

    list.append( "Office" );
    list.append( "Profession" );
    list.append( "Assistant" );
    list.append( "Manager" );

    list.append( "HomeStreet" );
    list.append( "HomeCity" );
    list.append( "HomeState" );
    list.append( "HomeZip" );
    list.append( "HomeCountry" );
    list.append( "HomeWebPage" );

    list.append( "Spouse" );
    list.append( "Gender" );
    list.append( "Birthday" );
    list.append( "Anniversary" );
    list.append( "Nickname" );
    list.append( "Children" );

    list.append( "Notes" );
    list.append( "Groups" );

    return list;
}


/*!
  Sets the list of email address for contact to those contained in \a str.
  Email address should be separated by ';'s.
*/
void OContact::setEmails( const QString &str )
{
    replace( Qtopia::Emails, str );
    if ( str.isEmpty() )
        setDefaultEmail( QString::null );
}

/*!
  Sets the list of children for the contact to those contained in \a str.
*/
void OContact::setChildren( const QString &str )
{
    replace( Qtopia::Children, str );
}

/*!
  \overload
  Returns TRUE if the contact matches the regular expression \a regexp.
  Otherwise returns FALSE.
*/
bool OContact::match( const QRegExp &r ) const
{
    setLastHitField( -1 );
    bool match;
    match = false;
    QMap<int, QString>::ConstIterator it;
    for ( it = mMap.begin(); it != mMap.end(); ++it ) {
	if ( (*it).find( r ) > -1 ) {
            setLastHitField( it.key() );
	    match = true;
	    break;
	}
    }
    return match;
}


QString OContact::toShortText() const
{
	return ( fullName() );
}
QString OContact::type() const
{
	return QString::fromLatin1( "OContact" );
}



class QString OContact::recordField( int pos ) const
{
	QStringList SLFIELDS = fields(); // ?? why this ? (se)
	return SLFIELDS[pos];
}

// In future releases, we should store birthday and anniversary
// internally as QDate instead of QString !
// QString is always too complicate to interprete (DD.MM.YY, DD/MM/YY, MM/DD/YY, etc..)(se)

/*! \fn void OContact::setBirthday( const QDate& date )
  Sets the birthday for the contact to \a date. If date is null
  the current stored date will be removed.
*/
void OContact::setBirthday( const QDate &v )
{
	if ( v.isNull() ){
		qWarning( "Remove Birthday");
		replace( Qtopia::Birthday, QString::null );
		return;
	}

	if ( v.isValid() )
		replace( Qtopia::Birthday, OConversion::dateToString( v ) );

}


/*! \fn void OContact::setAnniversary( const QDate &date )
  Sets the anniversary of the contact to \a date. If date is
  null, the current stored date will be removed.
*/
void OContact::setAnniversary( const QDate &v )
{
	if ( v.isNull() ){
		qWarning( "Remove Anniversary");
		replace( Qtopia::Anniversary, QString::null );
		return;
	}

	if ( v.isValid() )
		replace( Qtopia::Anniversary, OConversion::dateToString( v ) );
}

/*! \fn QDate OContact::birthday() const
  Returns the birthday of the contact.
*/
QDate OContact::birthday() const
{
	QString str = find( Qtopia::Birthday );
	qWarning ("Birthday %s", str.latin1() );
	if ( !str.isEmpty() )
		return  OConversion::dateFromString ( str );
	else
		return QDate();
}


/*! \fn QDate OContact::anniversary() const
  Returns the anniversary of the contact.
*/
QDate OContact::anniversary() const
{
	QDate empty;
	QString str = find( Qtopia::Anniversary );
	qWarning ("Anniversary %s", str.latin1() );
	if ( !str.isEmpty() )
		return OConversion::dateFromString ( str );
	else
		return empty;
}


void OContact::insertEmail( const QString &v )
{
    //qDebug("insertEmail %s", v.latin1());
    QString e = v.simplifyWhiteSpace();
    QString def = defaultEmail();

    // if no default, set it as the default email and don't insert
    if ( def.isEmpty() ) {
	setDefaultEmail( e ); // will insert into the list for us
	return;
    }

    // otherwise, insert assuming doesn't already exist
    QString emailsStr = find( Qtopia::Emails );
    if ( emailsStr.contains( e ))
	return;
    if ( !emailsStr.isEmpty() )
	emailsStr += emailSeparator();
    emailsStr += e;
    replace( Qtopia::Emails, emailsStr );
}

void OContact::removeEmail( const QString &v )
{
    QString e = v.simplifyWhiteSpace();
    QString def = defaultEmail();
    QString emailsStr = find( Qtopia::Emails );
    QStringList emails = emailList();

    // otherwise, must first contain it
    if ( !emailsStr.contains( e ) )
	return;

    // remove it
    //qDebug(" removing email from list %s", e.latin1());
    emails.remove( e );
    // reset the string
    emailsStr = emails.join(emailSeparator()); // Sharp's brain dead separator
    replace( Qtopia::Emails, emailsStr );

    // if default, then replace the default email with the first one
    if ( def == e ) {
	//qDebug("removeEmail is default; setting new default");
	if ( !emails.count() )
	    clearEmails();
	else // setDefaultEmail will remove e from the list
	    setDefaultEmail( emails.first() );
    }
}
void OContact::clearEmails()
{
    mMap.remove( Qtopia::DefaultEmail );
    mMap.remove( Qtopia::Emails );
}
void OContact::setDefaultEmail( const QString &v )
{
    QString e = v.simplifyWhiteSpace();

    //qDebug("OContact::setDefaultEmail %s", e.latin1());
    replace( Qtopia::DefaultEmail, e );

    if ( !e.isEmpty() )
	insertEmail( e );

}

void OContact::insertEmails( const QStringList &v )
{
    for ( QStringList::ConstIterator it = v.begin(); it != v.end(); ++it )
	insertEmail( *it );
}
int OContact::rtti() {
    return OPimResolver::AddressBook;
}
void OContact::setUid( int i )
{
	OPimRecord::setUid(i);
	replace( Qtopia::AddressUid , QString::number(i));
}