~ubuntu-branches/ubuntu/lucid/kmess/lucid

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
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
/***************************************************************************
                          account.cpp  -  description
                             -------------------
    begin                : Sun Jan 5 2003
    copyright            : (C) 2003 by Mike K. Bennett
    email                : mkb137b@hotmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "account.h"

#include "contact/msnobject.h"
#include "utils/kmessconfig.h"
#include "accountsmanager.h"
#include "kmessdebug.h"

#include <QDir>
#include <QRegExp>

#include <KConfigGroup>

#include <KGlobal>
#include <KLocale>
#include <KStandardDirs>


#ifdef KMESSDEBUG_ACCOUNT
  #define KMESSDEBUG_ACCOUNT_GENERAL
//   #define KMESSDEBUG_ACCOUNT_DIRTY
//   #define KMESSDEBUG_ACCOUNT_MSNOBJECT
#endif



// The constructor
Account::Account()
 : autoreplyMessage_( i18n("I am away from my computer") ),
   chatStyle_( "Fresh" ),
   contactFontColor_("#000000"),
   contactListDisplayMode_(VIEW_MIXED),
   deleted_(false),
   dirty_(false),
   emailSupported_(true),  // Assume true (for new accounts), until the msn server proves the opposite
   emoticonStyle_("KMess-new"),
   fontColor_("#000000"),
   friendlyName_( i18n("Your name"), false ), // Do not parse this name, CurrentAccount is not ready when initializing
   guestAccount_(true),
   groupFollowupMessages_(true),
   handle_( i18n("you@hotmail.com") ),
   hideNotificationsWhenBusy_(true),
   idleTime_(5),
   initialStatus_(STATUS_ONLINE),
   listPictureSize_(0),
   notificationDuration_(10),
   saveChats_(false),
   saveChatsToFile_(false),
   saveChatsFormat_(EXPORT_HTML),
   savedChatDirectoryStructure_( 0 ),
   savePassword_(false),
   shakeNudge_(true),
   showAllowedContacts_(false),
   showChatUserPicture_(false),
   showContactEmail_(false),
   showContactListBird_(true),
   showEmail_(true),
   showEmptyGroups_(true),
   showMessageTime_(true),
   showNowListening_(false),
   showOfflineContacts_(true),
   showOtherFolders_(true),
   showRemovedContacts_(false),
   showHistoryBox_(false),
   showSearchBar_(true),
   showSessionInfo_(false),
   showWinks_(true),
   tabbedChatMode_( 0 ),
   timestampShowSeconds_(false),
   timestampShowDate_(false),
   useAutologin_(false),
   useContactFont_(false),
   useEmoticons_(true),
   useFontEffects_(true),
   useIdleTimer_(true),
   useListFormatting_(true),
   useChatFormatting_(true),

   verified_(true),

   showPicture_(true),
   msnObject_(0)
{
  font_.setFamily("Arial");
  font_.setBold(false);
  font_.setItalic(false);
  font_.setUnderline(false);
  font_.setPointSize(12);

  contactFont_.setFamily("Arial");
  contactFont_.setBold(false);
  contactFont_.setItalic(false);
  contactFont_.setUnderline(false);

  saveChatPath_ = QDir::home().absolutePath();
}



// The destructor
Account::~Account()
{
  if ( msnObject_ )
    delete msnObject_;

#ifdef KMESSDEBUG_ACCOUNT_GENERAL
  kDebug() << "DESTROYED. [handle=" << handle_ << "]";
#endif
}



// Copy an account
void Account::copyAccount( const Account *account )
{
#ifdef KMESSDEBUG_ACCOUNT_GENERAL
  kDebug() << "Copying account data...";
#endif

  // This method is used to clone the settings of an Account to the CurrentAccount,
  // or vice-versa.

  // The method calls update all properties for which
  // we need to send signals to the rest of the application.
  setChatInformation            ( account->getUseContactFont(),
                                  account->getUseEmoticons(),
                                  account->getShowWinks(),
                                  account->getUseFontEffects(),
                                  account->getUseChatFormatting(),
                                  account->getShakeNudge(),
                                  account->getShowMessageTime(),
                                  account->getTimestampShowDate(),
                                  account->getTimestampShowSeconds(),
                                  account->getGroupFollowupMessages(),
                                  account->getTabbedChatMode(),
                                  account->getChatStyle()                          );
  setContactListDisplayMode     ( account->getContactListDisplayMode()             );
  setContactListOptions         ( account->getShowNowListening(),
                                  account->getShowContactEmail(),
                                  account->getUseListFormatting()                  );
  setEmailInformation           ( account->getShowEmail(),
                                  account->getShowOtherFolders()                   );
  setFontInformation            ( account->getFont(),
                                  account->getFontColor(),
                                  account->getContactFont(),
                                  account->getContactFontColor()                   );
  setGuestAccount               ( account->isGuestAccount()                        );
  setListPictureSize            ( account->getListPictureSize()                    );
  setLoginInformation           ( account->getHandle(),
                                  account->getFriendlyName( STRING_ORIGINAL ),
                                  account->getPassword()                           );
  setPersonalMessage            ( account->getPersonalMessage( STRING_ORIGINAL )   );
  setShowAllowedContacts        ( account->getShowAllowedContacts()                );
  setShowChatUserPicture        ( account->getShowChatUserPicture()                );
  setShowContactListBird        ( account->getShowContactListBird()                );
  setShowEmptyGroups            ( account->getShowEmptyGroups    ()                );
  setShowOfflineContacts        ( account->getShowOfflineContacts()                );
  setShowRemovedContacts        ( account->getShowRemovedContacts()                );
  setStatusOptions              ( account->getUseIdleTimer(),
                                  account->getIdleTime(),
                                  account->getHideNotificationsWhenBusy()          );
  setEmoticonStyle              ( account->getEmoticonStyle()                      );

  // With direct copying we can quickly duplicate those properties
  // for which we don't need to send updates
  autoreplyMessage_             = account->autoreplyMessage_;
  deleted_                      = account->deleted_;
  dirty_                        = account->dirty_;
  displayPicturePath_           = account->displayPicturePath_;
  emailSupported_               = account->emailSupported_;
  initialStatus_                = account->initialStatus_;
  originalDisplayPicturePath_   = account->originalDisplayPicturePath_;
  saveChats_                    = account->saveChats_;
  saveChatsToFile_              = account->saveChatsToFile_;
  saveChatsFormat_              = account->saveChatsFormat_;
  saveChatPath_                 = account->saveChatPath_;
  savedChatDirectoryStructure_  = account->savedChatDirectoryStructure_;
  savePassword_                 = account->savePassword_;
  showHistoryBox_               = account->showHistoryBox_;
  showPicture_                  = account->showPicture_;
  showSearchBar_                = account->showSearchBar_;
  showSessionInfo_              = account->showSessionInfo_;
  tabbedChatMode_               = account->tabbedChatMode_;
  temporaryPassword_            = account->temporaryPassword_;
  useAutologin_                 = account->useAutologin_;
  verified_                     = account->verified_;

  // The MSN object needs to be replaced
  updateMsnObject();
}



// Set the account as deleted
void Account::setDeleted()
{
  // Can be called twice, avoid warnings.
  if( deleted_ )
  {
    return;
  }

  // Mark as deleted, so other classes avoid saving the settings again.
  deleted_      = true;
  guestAccount_ = true;
}



// Read the auto-reply message.
const QString& Account::getAutoreplyMessage() const
{
  return autoreplyMessage_;
}



// Return the chat style name.
const QString& Account::getChatStyle() const
{
  return chatStyle_;
}



// Return the font used for contact messages, if forced to.
const QFont& Account::getContactFont() const
{
  return contactFont_;
}



// Return the color of the forced contact font.
const QString& Account::getContactFontColor() const
{
  return contactFontColor_;
}



// Read how contacts are shown, by group, by online/offline, or mixed.
Account::ContactListDisplayMode Account::getContactListDisplayMode() const
{
  return contactListDisplayMode_;
}



// Return whether email notifications are supported
bool Account::getEmailSupported() const
{
  return emailSupported_;
}


// Return the user message font.
const QFont& Account::getFont() const
{
  return font_;
}



// Return the color of the user message font.
const QString& Account::getFontColor() const
{
  return fontColor_;
}



// Return the user's friendlyName
QString Account::getFriendlyName( FormattingMode mode ) const
{
  return friendlyName_.getString( mode );
}



// Whether follow-up messages from the contact should be grouped.
bool Account::getGroupFollowupMessages() const
{
  return groupFollowupMessages_;
}



// Return the user's handle
QString Account::getHandle() const
{
  return handle_;
}



// Return the length of time waited before the user goes idle.
const int& Account::getIdleTime() const
{
  return idleTime_;
}



// Return whether now listening information should be displayed.
bool Account::getShowNowListening() const
{
  return showNowListening_;
}



// Return the user's password
QString Account::getPassword() const
{
  return password_;
}



// Return the personal message
QString Account::getPersonalMessage( FormattingMode mode ) const
{
  return personalMessage_.getString( mode );
}



// Return the selected emoticon's style
const QString& Account::getEmoticonStyle() const
{
  return emoticonStyle_;
}



// Return whether to hide notifications when busy
bool Account::getHideNotificationsWhenBusy() const
{
  return hideNotificationsWhenBusy_;
}



// Return the status to set upon connection
Status Account::getInitialStatus() const
{
  return initialStatus_;
}



// Return whether or not to save chats.
bool Account::getSaveChats() const
{
  return saveChats_;
}



// Return whether or not to save chats to external files
bool Account::getSaveChatsToFile() const
{
  return saveChatsToFile_;
}



// Return the format in which chats will be saved
Account::ChatExportFormat Account::getSaveChatsFormat() const
{
  return saveChatsFormat_;
}



// Return the base directory to which chats are saved.
const QString& Account::getSaveChatPath() const
{
  return saveChatPath_;
}



// Return the code of the structure used to save chats.
const int& Account::getSavedChatDirectoryStructure() const
{
  return savedChatDirectoryStructure_;
}



// Return the user's saved password
QString Account::getSavedPassword() const
{
  return savedPassword_;
}



// Read whether the password must be stored
bool Account::getSavePassword() const
{
  return savePassword_;
}



// Read whether the chat window should shake when a nudge is received or sent.
bool Account::getShakeNudge() const
{
  return shakeNudge_;
}



// Read whether or not allowed contacts are shown.
bool Account::getShowAllowedContacts() const
{
  return showAllowedContacts_;
}



// Read whether to show the user picture in chat windows
bool Account::getShowChatUserPicture() const
{
  return showChatUserPicture_;
}



// Read whether or not to show the email of contacts in the contact list instead of their friendly name
bool Account::getShowContactEmail() const
{
  return showContactEmail_;
}



// Get whether to show the contact list background image
bool Account::getShowContactListBird() const
{
  return showContactListBird_;
}



// Read whether the email information should be shown in the main view.
bool Account::getShowEmail() const
{
  return showEmail_;
}



// Read whether empty groups should be shown.
bool Account::getShowEmptyGroups() const
{
  return showEmptyGroups_;
}



// Read whether or not to show the contact list history box
bool Account::getShowHistoryBox() const
{
  return showHistoryBox_;
}



// Read whether message times should be shown.
bool Account::getShowMessageTime() const
{
  return showMessageTime_;
}



// Read whether offline contacts should be shown.
bool Account::getShowOfflineContacts() const
{
  return showOfflineContacts_;
}



// Read property of bool showOtherFolders.
bool Account::getShowOtherFolders() const
{
  return showOtherFolders_;
}



// Read whether or not removed (reverse) contacts are shown.
bool Account::getShowRemovedContacts() const
{
  return showRemovedContacts_;
}



// Read whether or not we need to show a search bar in the contact list
bool Account::getShowSearchBar() const
{
  return showSearchBar_;
}



// Read whether or not to show join/part messages in chat
bool Account::getShowSessionInfo() const
{
  return showSessionInfo_;
}



// Read whether to show winks
bool Account::getShowWinks() const
{
  return showWinks_;
}



// Read the way the chats will be grouped together
int Account::getTabbedChatMode() const
{
  return tabbedChatMode_;
}



// Return the a temporary password for the account for login attempts from the initialview dialog
const QString & Account::getTemporaryPassword() const
{
  return temporaryPassword_;
}



// Returns whether the seconds should be shown in message timestamp
bool Account::getTimestampShowDate() const
{
  return timestampShowDate_;
}



// Returns whether the seconds should be shown in message timestamp
bool Account::getTimestampShowSeconds() const
{
  return timestampShowSeconds_;
}



// Return whether or not to autologin with this profile.
bool Account::getUseAutologin() const
{
  return useAutologin_;
}



// Return whether or not to enable MSN Plus! formatting in chat
bool Account::getUseChatFormatting() const
{
  return useChatFormatting_;
}



// Return whether or not to show contact messages in the stored font.
bool Account::getUseContactFont() const
{
  return useContactFont_;
}



// Return whether or not to show emoticons in chats.
bool Account::getUseEmoticons() const
{
  return useEmoticons_;
}



// Return whether or not to use the bold/italic/underline effects.
bool Account::getUseFontEffects() const
{
  return useFontEffects_;
}



// Return whether the idle timer should be used.
bool Account::getUseIdleTimer() const
{
  return useIdleTimer_;
}



// Return whether or not to enable MSN Plus! formatting in the contact list
bool Account::getUseListFormatting() const
{
  return useListFormatting_;
}



// Returns whether or not to send our display picture
bool Account::getShowPicture() const
{
  return showPicture_;
}



// Returns display mode used to show pictures in contact list.
int Account::getListPictureSize() const
{
  return listPictureSize_;
}



// Return the path to the current display picture
const QString Account::getPicturePath() const
{
  // Return the picture path if the user has selected to show it; else return an empty string
  if( ! showPicture_ )
  {
    return QString();
  }

  // if the user has selected a custom picture, it will be located in
  // ~/.kde/share/apps/kmess/<account@handle>/displaypics/<pictureHash>.png

  // If the file can't be found, fallback to the default picture
  if( displayPicturePath_.isEmpty() || ! QFile::exists( displayPicturePath_ ) )
  {
    return KGlobal::dirs()->findResource( "data", "kmess/pics/kmesspic.png" );
  }

  return displayPicturePath_;
}



// Return location of the display picture original
const QString Account::getOriginalPicturePath() const
{
  return originalDisplayPicturePath_;
}



// Returns the string representation of the MsnObject
const QString Account::getMsnObjectString() const
{
  if ( msnObject_ )
    return msnObject_->objectString();
  else
    return "";
}



// Whether the account is marked to be deleted
bool Account::isDeleted() const
{
  return deleted_;
}



// Whether the account has unsaved settings changes
bool Account::isDirty() const
{
  return dirty_;
}



// Whether the account is a guest account, not permanently saved on the computer
bool Account::isGuestAccount() const
{
  return guestAccount_;
}



// Validate a contact email
bool Account::isValidEmail( QString email )
{
  return email.contains( QRegExp( "^[A-Z0-9._%\\-]+@(?:[A-Z0-9\\-]+\\.)+[A-Z]{2,4}$", Qt::CaseInsensitive ) );
}



// Return whether the account's passport is verified
bool Account::isVerified() const
{
  return verified_;
}



// Read in account properties
void Account::readProperties( const QString &handle )
{
  handle_ = handle;
  QDir    homeDir;
  QString defaultPath;

  homeDir = QDir::home();
  defaultPath = homeDir.absolutePath();

  // Choose the main group
  KConfigGroup profileGroup = KMessConfig::instance()->getAccountConfig( handle_, "Account" );

  autoreplyMessage_              = profileGroup.readEntry( "autoreplymessage",             i18n("I am away from my computer") );
  chatStyle_                     = profileGroup.readEntry( "chatStyle",                    "Fresh"     );
  contactFont_.setFamily         ( profileGroup.readEntry( "contactfontfamily",            "Arial"     ) );
  contactFont_.setBold           ( profileGroup.readEntry( "contactfontbold",              false       ) );
  contactFont_.setItalic         ( profileGroup.readEntry( "contactfontitalic",            false       ) );
  contactFont_.setUnderline      ( profileGroup.readEntry( "contactfontunderline",         false       ) );
  contactFont_.setPointSize      ( profileGroup.readEntry( "contactfontpointsize",         10          ) );
  contactFontColor_              = profileGroup.readEntry( "contactfontcolor" ,            "#000000"   );
  contactListDisplayMode_ = (Account::ContactListDisplayMode)
                                   profileGroup.readEntry( "contactListDisplayMode",       (int)VIEW_MIXED  );
  displayPicturePath_            = profileGroup.readEntry( "displayPicturePath",           ""          );
  originalDisplayPicturePath_    = profileGroup.readEntry( "originalDisplayPicturePath",   ""          );
  emailSupported_                = profileGroup.readEntry( "emailSupported",               true        );
  emoticonStyle_                 = profileGroup.readEntry( "emoticonStyle",                "KMess-new" );
  font_.setFamily                ( profileGroup.readEntry( "fontfamily",                   "Arial"     ) );
  font_.setBold                  ( profileGroup.readEntry( "fontbold",                     false       ) );
  font_.setItalic                ( profileGroup.readEntry( "fontitalic",                   false       ) );
  font_.setPointSize             ( profileGroup.readEntry( "fontpointsize",                12          ) );
  font_.setUnderline             ( profileGroup.readEntry( "fontunderline",                false       ) );
  fontColor_                     = profileGroup.readEntry( "fontColor" ,                   "#000000"   );
  friendlyName_.setString        ( profileGroup.readEntry( "friendlyName",                 handle_     ) );
  groupFollowupMessages_         = profileGroup.readEntry( "groupfollowupmessages",        true        );
  hideNotificationsWhenBusy_     = profileGroup.readEntry( "hideNotificationsWhenBusy",    true        );
  idleTime_                      = profileGroup.readEntry( "idleTime",                     5           );
  initialStatus_         = (Status)profileGroup.readEntry( "initialStatus",          (int) STATUS_ONLINE);
  listPictureSize_           = profileGroup.readEntry( "listPictureSize",          0           );
  notificationDuration_          = profileGroup.readEntry( "notification",                 5           );
  personalMessage_.setString     ( profileGroup.readEntry( "personalMessage",              ""          ) );
  saveChatPath_                  = profileGroup.readEntry( "saveChatPath",                 defaultPath );
  saveChats_                     = profileGroup.readEntry( "saveChats",                    false       );
  saveChatsToFile_               = profileGroup.readEntry( "saveChatsToFile",              false       );
  saveChatsFormat_ = (ChatExportFormat)profileGroup.readEntry( "saveChatsFormat",    (int) EXPORT_HTML );
  savedChatDirectoryStructure_   = profileGroup.readEntry( "savedChatDirectoryStructure",  0           );
  savePassword_                  = profileGroup.readEntry( "savePassword",                 false       );
  shakeNudge_                    = profileGroup.readEntry( "shakeNudge",                   true        );
  showAllowedContacts_           = profileGroup.readEntry( "showAllowed",                  false       );
  showChatUserPicture_           = profileGroup.readEntry( "showChatUserPicture",          false       );
  showContactEmail_              = profileGroup.readEntry( "showContactEmail",             false       );
  showContactListBird_           = profileGroup.readEntry( "showContactListBird",          true        );
  showEmail_                     = profileGroup.readEntry( "showEmail",                    true        );
  showEmptyGroups_               = profileGroup.readEntry( "showEmptyGroups",              true        );
  showHistoryBox_                = profileGroup.readEntry( "showHistoryBox",               false       );
  showPicture_                   = profileGroup.readEntry( "showPicture",                  true        );
  showMessageTime_               = profileGroup.readEntry( "showMessageTime",              true        );
  showNowListening_              = profileGroup.readEntry( "showNowListening",             false       );
  showOfflineContacts_           = profileGroup.readEntry( "showOfflineContacts",          true        );
  showOtherFolders_              = profileGroup.readEntry( "showOtherFolders",             true        );
  showRemovedContacts_           = profileGroup.readEntry( "showRemoved",                  false       );
  showSearchBar_                 = profileGroup.readEntry( "showSearchBar",                true        );
  showSessionInfo_               = profileGroup.readEntry( "showSessionInfo",              false       );
  showWinks_                     = profileGroup.readEntry( "showWinks",                    true        );
  tabbedChatMode_                = profileGroup.readEntry( "tabbedChatMode",               2           );
  timestampShowDate_             = profileGroup.readEntry( "timestampShowDate",            false       );
  timestampShowSeconds_          = profileGroup.readEntry( "timestampShowSeconds",         false       );
  useAutologin_                  = profileGroup.readEntry( "useAutologin",                 false       );
  useChatFormatting_             = profileGroup.readEntry( "useChatFormatting",            true        );
  useContactFont_                = profileGroup.readEntry( "useContactFont",               false       );
  useEmoticons_                  = profileGroup.readEntry( "useEmoticons",                 true        );
  useFontEffects_                = profileGroup.readEntry( "useFontEffects",               true        );
  useIdleTimer_                  = profileGroup.readEntry( "useIdleTimer",                 true        );
  useListFormatting_             = profileGroup.readEntry( "useListFormatting",            true        );

  updateMsnObject();

  // After reading options from file, consider "dirty" to still be false
  guestAccount_ = false;
  dirty_        = false;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Resetting 'dirty' to false.";
#endif

  // If a chat style is removed, fall back to the default.
  // This default is "Fresh" as of 1.5 final, to improve the first impression a user gets.
  // The previous "Default" theme has been renamed to "Classic".
  // This code also makes the migration from 1.5-pre2 to 1.5 (and beyond) easier.
  if( KGlobal::dirs()->findResource( "data", "kmess/styles/" + chatStyle_ + '/' + chatStyle_ + ".xsl" ).isEmpty() )
  {
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Auto-corrected chat & setting, setting 'dirty' to true.";
#endif

    // Fall back to our new default.
    chatStyle_ = "Fresh";
    groupFollowupMessages_ = true;  // Make sure Fresh looks good.
    dirty_ = true;
  }
}



// Save account properties
void Account::saveProperties()
{
  // Protect against unwanted usage
  if( guestAccount_ )
  {
    kWarning() << "Application attempted to save a guest account!";
    return;
  }
  if( deleted_ )
  {
    kWarning() << "Application attempted to save the deleted account again!";
    return;
  }

  // Choose the main group
  KConfigGroup profileGroup = KMessConfig::instance()->getAccountConfig( handle_, "Account" );

  if ( dirty_ )
  {
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Account " << handle_ << " is 'dirty', save properties.";
#endif

    // Write new settings
    profileGroup.writeEntry( "autoreplymessage",             autoreplyMessage_             );
    profileGroup.writeEntry( "chatStyle",                    chatStyle_                    );
    profileGroup.writeEntry( "contactfontfamily",            contactFont_.family()         );
    profileGroup.writeEntry( "contactfontbold",              contactFont_.bold()           );
    profileGroup.writeEntry( "contactfontitalic",            contactFont_.italic()         );
    profileGroup.writeEntry( "contactfontunderline",         contactFont_.underline()      );
    profileGroup.writeEntry( "contactfontpointsize",         contactFont_.pointSize()      );
    profileGroup.writeEntry( "contactfontcolor",             contactFontColor_             );
    profileGroup.writeEntry( "contactListDisplayMode",       (int)contactListDisplayMode_  );
    profileGroup.writeEntry( "displayPicturePath",           displayPicturePath_           );
    profileGroup.writeEntry( "originalDisplayPicturePath",   originalDisplayPicturePath_   );
    profileGroup.writeEntry( "emailSupported",               emailSupported_               );
    profileGroup.writeEntry( "emoticonStyle",                emoticonStyle_                );
    profileGroup.writeEntry( "fontfamily",                   font_.family()                );
    profileGroup.writeEntry( "fontbold",                     font_.bold()                  );
    profileGroup.writeEntry( "fontitalic",                   font_.italic()                );
    profileGroup.writeEntry( "fontunderline",                font_.underline()             );
    profileGroup.writeEntry( "fontpointsize",                font_.pointSize()             );
    profileGroup.writeEntry( "fontColor",                    fontColor_                    );
    profileGroup.writeEntry( "friendlyName",                 friendlyName_.getOriginal()   );
    profileGroup.writeEntry( "groupfollowupmessages",        groupFollowupMessages_        );
    profileGroup.writeEntry( "hideNotificationsWhenBusy",    hideNotificationsWhenBusy_    );
    profileGroup.writeEntry( "idleTime",                     idleTime_                     );
    profileGroup.writeEntry( "listPictureSize",              listPictureSize_              );
    profileGroup.writeEntry( "notification",                 notificationDuration_         );
    profileGroup.writeEntry( "personalMessage",              personalMessage_.getOriginal());
    profileGroup.writeEntry( "saveChatPath",                 saveChatPath_                 );
    profileGroup.writeEntry( "saveChats",                    saveChats_                    );
    profileGroup.writeEntry( "saveChatsToFile",              saveChatsToFile_              );
    profileGroup.writeEntry( "saveChatsFormat",              (int) saveChatsFormat_        );
    profileGroup.writeEntry( "savePassword",                 savePassword_                 );
    profileGroup.writeEntry( "savedChatDirectoryStructure",  savedChatDirectoryStructure_  );
    profileGroup.writeEntry( "shakeNudge",                   shakeNudge_                   );
    profileGroup.writeEntry( "showAllowed",                  showAllowedContacts_          );
    profileGroup.writeEntry( "showChatUserPicture",          showChatUserPicture_          );
    profileGroup.writeEntry( "showContactEmail",             showContactEmail_             );
    profileGroup.writeEntry( "showContactListBird",          showContactListBird_          );
    profileGroup.writeEntry( "showEmail",                    showEmail_                    );
    profileGroup.writeEntry( "showEmptyGroups",              showEmptyGroups_              );
    profileGroup.writeEntry( "showHistoryBox",               showHistoryBox_               );
    profileGroup.writeEntry( "showMessageTime",              showMessageTime_              );
    profileGroup.writeEntry( "showNowListening",             showNowListening_             );
    profileGroup.writeEntry( "showOfflineContacts",          showOfflineContacts_          );
    profileGroup.writeEntry( "showOtherFolders",             showOtherFolders_             );
    profileGroup.writeEntry( "showRemoved",                  showRemovedContacts_          );
    profileGroup.writeEntry( "showSearchBar",                showSearchBar_                );
    profileGroup.writeEntry( "showSessionInfo",              showSessionInfo_              );
    profileGroup.writeEntry( "showWinks",                    showWinks_                    );
    profileGroup.writeEntry( "initialStatus",                (int)initialStatus_           );
    profileGroup.writeEntry( "tabbedChatMode",               tabbedChatMode_               );
    profileGroup.writeEntry( "timestampShowDate",            timestampShowDate_            );
    profileGroup.writeEntry( "timestampShowSeconds",         timestampShowSeconds_         );
    profileGroup.writeEntry( "useAutologin",                 useAutologin_                 );
    profileGroup.writeEntry( "useChatFormatting",            useChatFormatting_            );
    profileGroup.writeEntry( "useContactFont",               useContactFont_               );
    profileGroup.writeEntry( "useEmoticons",                 useEmoticons_                 );
    profileGroup.writeEntry( "useFontEffects",               useFontEffects_               );
    profileGroup.writeEntry( "useListFormatting",            useListFormatting_            );
    profileGroup.writeEntry( "useIdleTimer",                 useIdleTimer_                 );
    profileGroup.writeEntry( "showPicture",                  showPicture_                  );

    if( ! savePassword_ )
    {
      // Remove the cached password
      password_ = QString();
      temporaryPassword_= QString();

      // When storing the passwords, it will be removed from the password manager also
    }
  }
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  else
  {
    kDebug() << "Account " << handle_ << " is NOT 'dirty', don't save properties.";
  }
#endif

  // Save to disk the changes now, to avoid losing data.
  profileGroup.config()->sync();
}



// The login succeeded with the new password. Save the temporary password as new account password.
void Account::saveTemporaryPassword()
{
  if( ! temporaryPassword_.isEmpty() && temporaryPassword_ != password_ )
  {
    password_ = temporaryPassword_;
    temporaryPassword_ = QString();
  }
}



/**
*
* @brief Set contact list options
*
* @param showNowListening whether to show the song played at the moment
* @param useListFormatting whether or not to enable MSN Plus! formatting in the contact list
* @param showContactEmail whether to show contact email instead of friendly name in the contact list
*/
void Account::setContactListOptions( bool showNowListening, bool showContactEmail, bool useListFormatting )
{
  if( useListFormatting_ != useListFormatting
  ||  showContactEmail_  != showContactEmail )
  {
    useListFormatting_ = useListFormatting;
    showContactEmail_  = showContactEmail;

    //let the contact list update to reflect the new changes
    emit changedContactListOptions();
  }

  emit changedTimerSettings();

  if( showNowListening_ != showNowListening )
  {
#ifdef KMESSDEBUG_ACCOUNT_GENERAL
    kDebug() << "Now listening settings changed, notifying listeners.";
#endif

    showNowListening_ = showNowListening;
    emit changedNowListeningSettings();
  }

  dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the auto-reply message.
void Account::setAutoreplyMessage( const QString& autoreplyMessage)
{
  if ( autoreplyMessage_ != autoreplyMessage )
  {
    autoreplyMessage_ = autoreplyMessage;
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



/**
*
* @brief Set chat information
*
* Set options related to chatting
* @param useContactFont whether or not the contact's font should be forced to some value.
* @param useEmoticons whether or not emoticons should be used.
* @param showWinks whether to show winks
* @param useFontEffects whether or not to use bold/italic/underline effects in chats.
* @param useChatFormatting whether to show MSN Plus formatting in chat.
* @param shakeNudge whether the chat window should shake when a nudge is received or sent.
* @param showMessageTime whether or not time information is shown for chat messages.
* @param timestampShowDate whether to show date in message timestamps.
* @param timestampShowSeconds whether to show seconds in message timestamps.
* @param groupFollowupMessages whether follow-up messages from the contact should be grouped.
* @param tabbedChatMode whether or not chats should be tabbed in one window.
* @param chatStyle the chat style to use.
*
*/
  void Account::setChatInformation( bool useContactFont, bool useEmoticons, bool showWinks,
                                    bool useFontEffects, bool useChatFormatting, bool shakeNudge,
                                    bool showMessageTime, bool timestampShowDate,
                                    bool timestampShowSeconds, bool groupFollowupMessages,
                                    int tabbedChatMode, const QString &chatStyle )
{
  bool changedSettings = ( useContactFont_        != useContactFont )
                      || ( useEmoticons_          != useEmoticons )
                      || ( showWinks_             != showWinks )
                      || ( useFontEffects_        != useFontEffects )
                      || ( useChatFormatting_     != useChatFormatting )
                      || ( shakeNudge_            != shakeNudge )
                      || ( showMessageTime_       != showMessageTime )
                      || ( timestampShowDate_     != timestampShowDate )
                      || ( timestampShowSeconds_  != timestampShowSeconds )
                      || ( groupFollowupMessages_ != groupFollowupMessages )
                      || ( tabbedChatMode_        != tabbedChatMode )
                      || ( chatStyle_             != chatStyle );

  // Avoid signals because the chat windows will regenerate all messages.
  if( ! changedSettings )
  {
    return;
  }

  useContactFont_        = useContactFont;
  useEmoticons_          = useEmoticons;
  useFontEffects_        = useFontEffects;
  useChatFormatting_     = useChatFormatting;
  shakeNudge_            = shakeNudge;
  showMessageTime_       = showMessageTime;
  showWinks_             = showWinks;
  timestampShowDate_     = timestampShowDate;
  timestampShowSeconds_  = timestampShowSeconds;
  groupFollowupMessages_ = groupFollowupMessages;
  tabbedChatMode_        = tabbedChatMode;
  chatStyle_             = chatStyle;

  emit changedChatStyleSettings();
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set chat logging information
void Account::setChatLoggingInformation( bool saveChats, bool saveChatsToFile, Account::ChatExportFormat saveChatsFormat,
                                         const QString& saveChatPath, int directoryStructure )
{
  if( saveChats_                   == saveChats
  &&  saveChatsToFile_             == saveChatsToFile
  &&  saveChatsFormat_             == saveChatsFormat
  &&  saveChatPath_                == saveChatPath
  &&  savedChatDirectoryStructure_ == directoryStructure )
  {
    return;
  }

  saveChats_                   = saveChats;
  saveChatsToFile_             = saveChatsToFile;
  saveChatsFormat_             = saveChatsFormat;
  saveChatPath_                = saveChatPath;
  savedChatDirectoryStructure_ = directoryStructure;

  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}




// Set how contacts are shown, by group, by online/offline, or mixed.
void Account::setContactListDisplayMode( ContactListDisplayMode displayMode )
{
  if ( displayMode != contactListDisplayMode_ )
  {
    contactListDisplayMode_ = displayMode;
    emit changedViewMode();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set email information
void Account::setEmailInformation( bool showEmail, bool showOtherFolders )
{
  if( showEmail_        != showEmail
  ||  showOtherFolders_ != showOtherFolders )
  {
    showEmail_        = showEmail;
    showOtherFolders_ = showOtherFolders;

    emit changedEmailDisplaySettings();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Update the email-supported state (protected method)
void Account::setEmailSupported(bool emailSupported)
{
  if( emailSupported_ != emailSupported )
  {
    dirty_ = true;
  }

  emailSupported_ = emailSupported;
}



// Set the font
void Account::setFont( const QFont& font )
{
  if( font != font_ )
  {
    font_ = font;
    emit changedFontSettings();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set the font color
void Account::setFontColor( const QString& fontColor )
{
  if( fontColor != fontColor_ )
  {
    fontColor_ = fontColor;
    emit changedFontSettings();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set the fonts and font colors
void Account::setFontInformation( const QFont& font, const QString &fontColor, const QFont& contactFont, const QString &contactFontColor)
{
  bool changedSettings = (font_ != font)
                      || (fontColor_ != fontColor)
                      || (contactFont_ != contactFont)
                      || (contactFontColor_ != contactFontColor);

  // Avoid signals because the chat windows will regenerate all messages.
  if( ! changedSettings )
  {
    return;
  }

  font_             = font;
  fontColor_        = fontColor;
  contactFont_      = contactFont;
  contactFontColor_ = contactFontColor;
  emit changedFontSettings();
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the emoticon path
void Account::setEmoticonStyle( const QString& emoticonStyle )
{
  if( emoticonStyle != emoticonStyle_ )
  {
    // Set the new style, and signal EmoticonManager about it.
    emoticonStyle_ = emoticonStyle;
    emit changedEmoticonSettings();

    // update friendly name. horrible.
    friendlyName_.setString( friendlyName_.getOriginal() );

    // Tell the account settings changed and need to be saved
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Change the friendly name
void Account::setFriendlyName(const QString& newName)
{
  if( newName.isEmpty() || newName == friendlyName_.getOriginal() )
  {
    return;
  }

  friendlyName_.setString( newName );

  emit changedFriendlyName();
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Whether the account is a guest account, not permanently saved on the computer
void Account::setGuestAccount( bool guestAccount )
{
  guestAccount_ = guestAccount;

  // Undelete when user did restore the account.
  if( ! guestAccount_ )
  {
    deleted_ = false;
  }
}



// Change the status to set upon connection
void Account::setInitialStatus( Status status )
{
  if( initialStatus_ == status )
  {
    return;
  }

  initialStatus_ = status;
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the mode of display list pictures
void Account::setListPictureSize( int mode )
{
  // No need to update
  if( mode == listPictureSize_ )
  {
    return;
  }

  listPictureSize_ = mode;

  emit changedDisplaySettings();
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set basic account data
void Account::setLoginInformation( QString handle, QString friendlyName, QString password )
{
#ifdef KMESSTEST
  KMESS_ASSERT(   handle.contains("@") );
  KMESS_ASSERT( ! handle.isEmpty()     );
#endif

  handle = handle.toLower();

  if( handle_                     == handle
  &&  friendlyName_.getOriginal() == friendlyName
  &&  password                    == password_ )
  {
    return;
  }

  handle_ = handle;

  // Never change the nickname if the account is not verified. We would receive a server error
  if( verified_ )
  {
    friendlyName_.setString( friendlyName );
    emit changedFriendlyName();
  }

  if( ! password.isEmpty() )
  {
    password_ = password;
  }

  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the account's login password
void Account::setPassword( const QString &password )
{
  password_ = password;
}



// Set the personal message
void Account::setPersonalMessage(const QString& newMessage)
{
  if( newMessage == personalMessage_.getOriginal() )
  {
    return;
  }

  personalMessage_.setString( newMessage );
  emit changedPersonalMessage();

  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the display picture file
void Account::setPicturePath( const QString& picturePath )
{
  if( displayPicturePath_ == picturePath )
  {
    return;
  }

  displayPicturePath_ = picturePath;

  // Update the MSN Object linked to this account
  updateMsnObject();

  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Save the location of the display picture original
void Account::setOriginalPicturePath( const QString& picturePath )
{
  if( originalDisplayPicturePath_ == picturePath )
  {
    return;
  }

  originalDisplayPicturePath_ = picturePath;

  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the account's saved password
void Account::setSavedPassword( const QString &password )
{
  savedPassword_ = password;
}



// Set if the password must be stored
void Account::setSavePassword( bool savePassword )
{
  savePassword_ = savePassword;

  if( ! savePassword )
  {
    // Clear the password so the accountsmanager will remove it
    password_.clear();
  }

  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set whether or not allowed contacts are shown.
void Account::setShowAllowedContacts( bool showAllowedContacts )
{
  if( showAllowedContacts != showAllowedContacts_ )
  {
    showAllowedContacts_ = showAllowedContacts;
    emit changedDisplaySettings();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set whether to show the user picture in chat windows
void Account::setShowChatUserPicture( bool showChatUserPicture )
{
  if( showChatUserPicture == showChatUserPicture_ )
  {
    return;
  }

  showChatUserPicture_ = showChatUserPicture;
  emit changedMsnObject();

  dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif

}



// Set whether to show the contact list background image
void Account::setShowContactListBird( bool showBird )
{
  if( showBird == showContactListBird_ )
  {
    return;
  }

  showContactListBird_ = showBird;
  emit changedContactListOptions();

  dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif

}



// Set whether empty groups are shown
void Account::setShowEmptyGroups( bool showEmptyGroups )
{
  if( showEmptyGroups == showEmptyGroups_ )
  {
    return;
  }

  showEmptyGroups_ = showEmptyGroups;
  emit changedViewMode();

  dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set whether or not to show the contact list history box
void Account::setShowHistoryBox( bool showHistoryBox )
{
  if ( showHistoryBox_ == showHistoryBox )
  {
    return;
  }

  showHistoryBox_ = showHistoryBox;
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set whether offline contacts should be shown.
void Account::setShowOfflineContacts( bool showOfflineContacts )
{
  if( showOfflineContacts != showOfflineContacts_ )
  {
    showOfflineContacts_ = showOfflineContacts;
    emit changedDisplaySettings();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set whether or not removed (reverse) contacts are shown.
void Account::setShowRemovedContacts( bool showRemovedContacts )
{
  if( showRemovedContacts != showRemovedContacts_ )
  {
    showRemovedContacts_ = showRemovedContacts;
    emit changedDisplaySettings();
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set whether or not we need to show a search bar in the contact list at login
void Account::setShowSearchBar( bool showSearchBar )
{
  if ( showSearchBar_ == showSearchBar )
  {
    return;
  }

  showSearchBar_ = showSearchBar;
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set whether or not to show join/part messages in chat
void Account::setShowSessionInfo( bool showSessionInfo )
{
  if( showSessionInfo_ == showSessionInfo )
  {
    return;
  }

  showSessionInfo_ = showSessionInfo;
  dirty_ = true;

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



/**
*
* @brief Set the options on the status
*
* @param useIdleTimer whether to set the status as Idle after a while
* @param idleTime the time without activity before being marked as idle
* @param hideNotificationsWhenBusy whether to hide all the notifications when the status is set to Busy
*
*/
void Account::setStatusOptions( bool useIdleTimer, int idleTime, bool hideNotificationsWhenBusy )
{
  if( useIdleTimer_              == useIdleTimer
  &&  idleTime_                  == idleTime
  &&  hideNotificationsWhenBusy_ == hideNotificationsWhenBusy )
  {
    return;
  }

  useIdleTimer_              = useIdleTimer;
  idleTime_                  = idleTime;
  hideNotificationsWhenBusy_ = hideNotificationsWhenBusy;

  emit changedTimerSettings();

  dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Set the way the chats will be grouped together
void Account::setTabbedChatMode( int tabbedChatMode )
{
  tabbedChatMode_ = tabbedChatMode;
}



// Set a temporary password for the account for login attempts from the initialview dialog
void Account::setTemporaryPassword( const QString &temporaryPassword )
{
  temporaryPassword_ = temporaryPassword;
}



// Set whether or not to login with this account.
void Account::setUseAutologin( bool useAutologin)
{
  if( useAutologin != useAutologin_ )
  {
    useAutologin_ = useAutologin;
    dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  }
}



// Set whether or not to show emoticons in chats.
void Account::setUseEmoticons( bool useEmoticons)
{
  useEmoticons_ = useEmoticons;
  dirty_ = true;
#ifdef KMESSDEBUG_ACCOUNT_DIRTY
    kDebug() << "Setting 'dirty' to true.";
#endif
  emit changedChatStyleSettings();
}



// Set whether the account's passport is verified
void Account::setVerified( bool verified )
{
  verified_ = verified;
}



// Set whether or not to send our display picture
void Account::setShowPicture( bool showPicture )
{
  if( showPicture == showPicture_ )
  {
    return;
  }

  showPicture_ = showPicture;
  dirty_ = true;

  updateMsnObject();

#ifdef KMESSDEBUG_ACCOUNT_DIRTY
  kDebug() << "Setting 'dirty' to true.";
#endif
}



// Creates the MsnObject only if the user wants to show a display picture
void Account::updateMsnObject()
{
  QString oldMsnString;
  QString newMsnString;

  // Remove the previous msn object if there is one
  if( msnObject_ )
  {
    oldMsnString = msnObject_->objectString();
    delete msnObject_;
    msnObject_ = 0;
  }

  // Verify if the picture exists: if not, change it to the default one as returned
  // by getPicturePath()
  QString picturePath( getPicturePath() );
  if( picturePath != displayPicturePath_ )
  {
#ifdef KMESSDEBUG_ACCOUNT_MSNOBJECT
    kDebug() << "Replacing picture" << displayPicturePath_ << "with" << picturePath;
#endif
    displayPicturePath_ = picturePath;
  }

#ifdef KMESSDEBUG_ACCOUNT_MSNOBJECT
  kDebug() << "Show the picture?" << showPicture_ << "- Set picture path:" << displayPicturePath_;
#endif

  if ( showPicture_ )
  {
    QFile iFile( displayPicturePath_ );
    if ( iFile.open( QIODevice::ReadOnly ) )
    {
      const QByteArray& data( iFile.readAll() );
      iFile.close();

      msnObject_   = new MsnObject( handle_, "KMess.tmp", "", MsnObject::DISPLAYPIC, data );
      newMsnString = msnObject_->objectString();
    }
    else
    {
      kWarning() << "Picture could not be loaded: " << displayPicturePath_;
    }
  }

  // Detect if there was a change.
  if( oldMsnString != newMsnString )
  {
#ifdef KMESSDEBUG_ACCOUNT_MSNOBJECT
    kDebug() << "result is: '" << newMsnString << "'";
#endif
    emit changedMsnObject();
  }
}



#include "account.moc"