File: nsMessengerFreeDesktopIntegration.cpp.orig

package info (click to toggle)
mozilla-traybiff 1.2.3-4.3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 768 kB
  • ctags: 656
  • sloc: cpp: 1,840; ansic: 1,757; sh: 576; makefile: 236; perl: 123
file content (1268 lines) | stat: -rw-r--r-- 39,813 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is 
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Seth Spitzer <sspitzer@netscape.com>
 * Bhuvan Racham <racham@netscape.com>
 * Ilya Konstantinov
 * Portions were taken or adapted from various Mozilla source files.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsMessengerFreeDesktopIntegration.h"
#include <nsCOMPtr.h>
#include <nsIMsgMailSession.h> // Compatible with tb1.0
#include <nsIMsgIncomingServer.h>
#include "nsIMsgIncomingServer10.h" // For compatibility with tb1.0
#include <nsIMsgAccount.h> // Compatible with tb1.0
#include <nsIRDFResource.h> // Compatible with tb1.0
#include <nsIMsgFolder.h>
#include "nsIMsgFolder10.h" // For compatibility with tb1.0
#include <nsIMsgHdr.h> // nsMsgDBHdr compatible with tb1.0
#include <nsMsgBaseCID.h>
#include <nsMsgFolderFlags.h>
#include <nsIProfile.h>
#include <nsIRDFService.h> // Compatible with tb1.0
#include <nsIPrefBranchInternal.h> // Only for NS_PREFBRANCH_PREFCHANGE_TOPIC_ID

#include <nsIWindowMediator.h> // Compatible with tb1.0
#include <nsIDOMWindowInternal.h> // Compatible with tb1.0
#include <nsPIDOMWindow.h>
#ifndef MOZ_TRUNK_FINAL
#include <nsIScriptGlobalObject.h>
#endif
#include "nsIScriptGlobalObject10.h"
#include <nsIDocShell.h>
#include "nsIDocShell10.h"
#include <nsIBaseWindow.h> // Compatible with tb1.0
#include <nsIWidget.h>
#include "nsIWidget10.h"

#include <nsIMessengerWindowService.h> // Compatible with 1.0
#include <nsMsgUtils.h>
#include <prprf.h>
#include <nsIWeakReference.h>
#include <nsIStringBundle.h>
#include <nsIPrefService.h>
#include <nsIPref.h>
#include <nsStringAPI.h>

#include <nsAppDirectoryServiceDefs.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
       
// One day, we should use an icon from chrome.
// Meanwhile, we'll embed the icon:
#include "trayBiffIcon.h"

// Path to the New Mail Led control files
const char* HW_INDICATOR_CONTROL_FILENAMES[] = {
	// ASUS laptop led on Linux
	"/proc/acpi/asus/mled",
	// ACER New Mail led on Linux
	"/proc/driver/acerhk/led"
};

// Prefs in use
const char* PREF_BIFF_SHOW_ICON = "mail.biff.show_icon";
const char* PREF_BIFF_SHOW_ASUS_LED = "mail.biff.show_asus_led";
const char* PREF_BIFF_USE_HW_INDICATOR = "mail.biff.use_hw_indicator";
const char* PREF_BIFF_HW_INDICATOR_FILE = "mail.biff.hw_indicator_file";
const char* PREF_BIFF_ALWAYS_SHOW_ICON = "mail.biff.always_show_icon";
const char* PREF_BIFF_USE_KEYBOARD_LED = "mail.biff.use_keyboard_led";

// String bundles
const char* STRING_BUNDLE_MESSENGER = "chrome://messenger/locale/messenger.properties";
const char* STRING_BUNDLE_TRAYBIFF = "chrome://traybiff/locale/traybiff.properties";

// Useful for debugging.
inline void PrintAtom(nsIAtom* atom)
{
	const char *res;
	atom->GetUTF8String(&res);
	fprintf(stderr, "Atom = %s\n", res);
}

#include "eggstatusicon.h"
#include <gtk/gtk.h>

/**
 * Modifies aFile to point at an icon file with the given name and suffix.  The
 * suffix may correspond to a file extension with leading '.' if appropriate.
 * Returns true if the icon file exists and can be read.
 * @note Taken from nsBaseWidget.cpp
 */
static PRBool
ResolveIconNameHelper(nsILocalFile *aFile,
                      const nsAString &aIconName,
                      const nsAString &aIconSuffix)
{
	aFile->Append(NS_LITERAL_STRING("icons"));
	aFile->Append(NS_LITERAL_STRING("default"));
	aFile->Append(aIconName + aIconSuffix);

	PRBool readable;
	return NS_SUCCEEDED(aFile->IsReadable(&readable)) && readable;
}

/**
 * Resolve the given icon name into a local file object.  This method is
 * intended to be called by subclasses of nsBaseWidget.  aIconSuffix is a
 * platform specific icon file suffix (e.g., ".ico" under Win32).
 *
 * If no file is found matching the given parameters, then null is returned.
 * @note Taken from nsBaseWidget.cpp
 */
void ResolveIconName(const nsAString &aIconName, const nsAString &aIconSuffix, nsILocalFile **aResult)
{ 
  *aResult = nsnull;

  nsCOMPtr<nsIProperties> dirSvc = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
  if (!dirSvc)
    return;

  // first check auxilary chrome directories

  nsCOMPtr<nsISimpleEnumerator> dirs;
  dirSvc->Get(NS_APP_CHROME_DIR, NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(dirs));
  if (dirs) {
    PRBool hasMore;
    while (NS_SUCCEEDED(dirs->HasMoreElements(&hasMore)) && hasMore) {
      nsCOMPtr<nsISupports> element;
      dirs->GetNext(getter_AddRefs(element));
      if (!element)
        continue;
      nsCOMPtr<nsILocalFile> file = do_QueryInterface(element);
      if (!file)
        continue;
      if (ResolveIconNameHelper(file, aIconName, aIconSuffix)) {
        NS_ADDREF(*aResult = file);
        return;
      }
    }
  }

  // then check the main app chrome directory

  nsCOMPtr<nsILocalFile> file;
  dirSvc->Get(NS_APP_CHROME_DIR, NS_GET_IID(nsILocalFile),
              getter_AddRefs(file));
  if (file && ResolveIconNameHelper(file, aIconName, aIconSuffix))
    NS_ADDREF(*aResult = file);
}

GdkWindow* GetGdkWindowForDOMWindow(nsISupports *window)
{
	nsCOMPtr<nsIBaseWindow> ppBaseWindow;
	
#ifndef MOZ_TRUNK_FINAL
	nsCOMPtr<nsIScriptGlobalObject> ppScriptGlobalObj = do_QueryInterface(window);
	if (ppScriptGlobalObj)
	{
		ppBaseWindow = do_QueryInterface( ppScriptGlobalObj->GetDocShell() );
	}
	else
	{
		nsCOMPtr<nsIScriptGlobalObject10> ppScriptGlobalObj10 = do_QueryInterface(window);
		if (ppScriptGlobalObj10)
		ppBaseWindow = do_QueryInterface( ppScriptGlobalObj->GetDocShell() );
	}
#else
	nsCOMPtr<nsPIDOMWindow> win( do_QueryInterface(window) );
	if (win)
		ppBaseWindow = do_QueryInterface( win->GetDocShell() );
#endif
  
	if (!ppBaseWindow)
		return NULL;

	GdkWindow* pWindow = NULL;
	nsCOMPtr<nsIWidget> pWidgetBase;
	ppBaseWindow->GetMainWidget( getter_AddRefs(pWidgetBase) );
	if (pWidgetBase)
	{
		nsCOMPtr<nsIWidget> pWidget;
		nsCOMPtr<nsIWidget10> pWidget10;

		if (pWidget = do_QueryInterface(pWidgetBase))
		{
			pWindow = reinterpret_cast<GdkWindow*>(pWidget->GetNativeData(NS_NATIVE_WIDGET));
		}
		else if (pWidget10 = do_QueryInterface(pWidgetBase))
		{
			pWindow = reinterpret_cast<GdkWindow*>(pWidget10->GetNativeData(NS_NATIVE_WIDGET));
		}
	}
 
	return pWindow;
}

//! @brief Opens a (preferrably existing) window, optionally going to a certain message.
//! @note Refer to mail/base/content/mailCore.js : toMessengerWindow() to see
//! how we raise the mailer window.
//! @param[in] aMailWindowName Name of the mailer window. Usually "mail:3pane".
//! @param[in] aFolderUri Optional parameter specifying the URI of a folder to switch the mail view to. 
//! @param[in] aMessageUri Optional parameter specifying the URI of a folder to switch the mail view to. 
static void openMailWindow(const PRUnichar * aMailWindowName, const char * aFolderUri, const char * aMessageUri)
{
	nsCOMPtr<nsIDOMWindowInternal> domWindow;
	
	nsCOMPtr<nsIWindowMediator> mediator ( do_GetService(NS_WINDOWMEDIATOR_CONTRACTID) );
	NS_ASSERTION(mediator, "no mediator");
	if (aMailWindowName != NULL)
	{
		mediator->GetMostRecentWindow(aMailWindowName, getter_AddRefs(domWindow));
		
		if (domWindow)
		{
			// Jump to the desired folder/message
			nsCOMPtr<nsPIDOMWindow> piDOMWindow(do_QueryInterface(domWindow));
			if (piDOMWindow)
			{
				if ((aFolderUri != NULL) && (aMessageUri != NULL))
				{
					nsCOMPtr<nsISupports> xpConnectObj;
					piDOMWindow->GetObjectProperty(NS_LITERAL_STRING("MsgWindowCommands").get(), getter_AddRefs(xpConnectObj));
					nsCOMPtr<nsIMsgWindowCommands> msgWindowCommands = do_QueryInterface(xpConnectObj);
					if (msgWindowCommands)
					{
						if (aFolderUri != NULL)
						{
							msgWindowCommands->SelectFolder(aFolderUri);
							if (aMessageUri != NULL)
								msgWindowCommands->SelectMessage(aMessageUri);
						}
					}
				}
			}

			// window.focus() on X11 brings the window up but doesn't focus it,
			// so we do some GDK magic:
			GdkWindow* pGdkWindow = GetGdkWindowForDOMWindow(domWindow);
			if (GDK_IS_WINDOW(pGdkWindow))
			{
				GdkWindow * root = gdk_window_get_toplevel(pGdkWindow);
				gdk_window_show(root);
				gdk_window_raise(root);
			}
			domWindow->Focus();
		}
	}
	
	if (!domWindow)
	{
		nsMsgKey msgKey = nsMsgKey_None;
		// If a specific message is needed, go through the hoops required to translate
		// a message URL into a message key.
		if (aMessageUri != NULL)
		{
			nsCOMPtr<nsIMsgDBHdr> hdr;
			GetMsgDBHdrFromURI(aMessageUri, getter_AddRefs(hdr));
			if (hdr)
				hdr->GetMessageKey(&msgKey);
		}
		
		nsCOMPtr <nsIMessengerWindowService> messengerWindowService = do_GetService(NS_MESSENGERWINDOWSERVICE_CONTRACTID);
		if (messengerWindowService) 
			messengerWindowService->OpenMessengerWindowWithUri("mail:3pane", aFolderUri, msgKey);
	}
}

enum MailWindowVisibilityEnum
{
	MAIL_WINDOW_NOT_FOUND,
	MAIL_WINDOW_HIDDEN,
	MAIL_WINDOW_VISIBLE
};

static MailWindowVisibilityEnum isMailWindowVisible(const PRUnichar * aMailWindowName)
{
	nsCOMPtr<nsIDOMWindowInternal> domWindow;
	
	nsCOMPtr<nsIWindowMediator> mediator ( do_GetService(NS_WINDOWMEDIATOR_CONTRACTID) );
	NS_ASSERTION(mediator, "no mediator");
	if (aMailWindowName != NULL)
	{
		mediator->GetMostRecentWindow(aMailWindowName, getter_AddRefs(domWindow));
		if (domWindow)
		{
			GdkWindow* pGdkWindow = GetGdkWindowForDOMWindow(domWindow);
			if (GDK_IS_WINDOW(pGdkWindow))
			{
				GdkWindow * root = gdk_window_get_toplevel(pGdkWindow);
				return gdk_window_is_visible(root) ? MAIL_WINDOW_VISIBLE : MAIL_WINDOW_HIDDEN;
			}
		}
	}
	return MAIL_WINDOW_NOT_FOUND;
}

static void toggleMailWindow(const PRUnichar * aMailWindowName)
{
	nsCOMPtr<nsIDOMWindowInternal> domWindow;
	
	nsCOMPtr<nsIWindowMediator> mediator ( do_GetService(NS_WINDOWMEDIATOR_CONTRACTID) );
	NS_ASSERTION(mediator, "no mediator");
	if (aMailWindowName != NULL)
	{
		mediator->GetMostRecentWindow(aMailWindowName, getter_AddRefs(domWindow));
		if (domWindow)
		{
			// window.focus() on X11 brings the window up but doesn't focus it,
			// so we do some GDK magic:
			GdkWindow* pGdkWindow = GetGdkWindowForDOMWindow(domWindow);
			if (GDK_IS_WINDOW(pGdkWindow))
			{
				GdkWindow * root = gdk_window_get_toplevel(pGdkWindow);
				if(gdk_window_is_visible(root))
				{
					gdk_window_hide(root);
				}
				else
				{
					gdk_window_show(root);
					gdk_window_raise(root);
					domWindow->Focus();
				}
			}
		}
	}
	
	if (!domWindow)
	{
		nsCOMPtr <nsIMessengerWindowService> messengerWindowService = do_GetService(NS_MESSENGERWINDOWSERVICE_CONTRACTID);
		if (messengerWindowService) 
			messengerWindowService->OpenMessengerWindowWithUri("mail:3pane", NULL, nsMsgKey_None);
	}
}

nsMessengerFreeDesktopIntegration::nsMessengerFreeDesktopIntegration() :
	mTrayIcon(NULL),
	mShowBiffIcon(PR_TRUE),
	mHasBiff(PR_FALSE)
{
	mBiffStateAtom = do_GetAtom("BiffState");
	NS_NewISupportsArray(getter_AddRefs(mFoldersWithNewMail));

	nsCOMPtr<nsILocalFile> iconFile;
	ResolveIconName(NS_LITERAL_STRING("messengerWindow16"), NS_LITERAL_STRING(".xpm"), getter_AddRefs(iconFile));
	if (!iconFile)
	{
		ResolveIconName(NS_LITERAL_STRING("mozicon16"), NS_LITERAL_STRING(".xpm"), getter_AddRefs(iconFile));
	}
	if (iconFile)
	{
		iconFile->GetNativePath(mBrandIconPath);
	}
}

nsMessengerFreeDesktopIntegration::~nsMessengerFreeDesktopIntegration()
{
	RemoveBiffIcon();
	HideHwIndicator();
}

NS_IMPL_ADDREF(nsMessengerFreeDesktopIntegration)
NS_IMPL_RELEASE(nsMessengerFreeDesktopIntegration)

NS_INTERFACE_MAP_BEGIN(nsMessengerFreeDesktopIntegration)
	NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMessengerOSIntegration)
	NS_INTERFACE_MAP_ENTRY(nsIMessengerOSIntegration)
	NS_INTERFACE_MAP_ENTRY(nsIMessengerFreeDesktopIntegration)
	NS_INTERFACE_MAP_ENTRY(nsIFolderListener)
	NS_INTERFACE_MAP_ENTRY(nsIFolderListener10)
	NS_INTERFACE_MAP_ENTRY(nsIObserver)
	NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END

nsresult
nsMessengerFreeDesktopIntegration::Init()
{
	nsresult rv;
	
	/*
	 Support for Thunderbird's "Disable Extension" functionality.
	 Even though we don't really need the chrome to function (for now),
	 the user will (rightfully) expect that "Disable Extension" would
	 disable the XPCOM component as well.

	 The following code is ripped from chatzilla-service.js.
	*/
	// Checking if we're disabled in the Chrome Registry.
	// Trunk builds no longer have rdf:chrome, and in addition, they avoid loading the component
	// when the extension is disabled so this check is no longer needed.
	{
		nsCOMPtr<nsIRDFService> rdfSvc = do_GetService("@mozilla.org/rdf/rdf-service;1", &rv);
		if (NS_SUCCEEDED(rv))
		{
			nsCOMPtr<nsIRDFDataSource> rdfDS;
			rv = rdfSvc->GetDataSource("rdf:chrome", getter_AddRefs(rdfDS));
			if (NS_SUCCEEDED(rv))
			{
				nsCOMPtr<nsIRDFResource> resSelf;
				rv = rdfSvc->GetResource(NS_LITERAL_CSTRING("urn:mozilla:package:traybiff"), getter_AddRefs(resSelf));
				if (NS_SUCCEEDED(rv))
				{
					nsCOMPtr<nsIRDFResource> resDisabled;
					rv = rdfSvc->GetResource(NS_LITERAL_CSTRING("http://www.mozilla.org/rdf/chrome#disabled"), getter_AddRefs(resDisabled));
					if (NS_SUCCEEDED(rv))
					{
						nsCOMPtr<nsIRDFNode> resNode;
						rv = rdfDS->GetTarget(resSelf, resDisabled, true, getter_AddRefs(resNode));
						if (NS_SUCCEEDED(rv) && (resNode != NULL))
						{
							// The chrome for this extension is marked as disabled.
							return NS_ERROR_FAILURE;
						}
					}
				}
			}
		}
	}

	// get pref service
	nsCOMPtr<nsIPrefService> prefService;
	prefService = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
	NS_ENSURE_SUCCESS(rv,rv);
	
	// get mail.biff pref branch and watch it for interesting prefs changing
	prefService->GetBranch(nsnull, getter_AddRefs(mPrefBranch));
	nsCOMPtr<nsIPref> pref = do_QueryInterface(mPrefBranch);
	// ... with weak reference (PR_TRUE), to avoid cleaning up at shutdown.
	if (pref)
	{
		rv = pref->AddObserver(PREF_BIFF_SHOW_ICON, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = pref->AddObserver(PREF_BIFF_SHOW_ASUS_LED, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = pref->AddObserver(PREF_BIFF_USE_HW_INDICATOR, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = pref->AddObserver(PREF_BIFF_HW_INDICATOR_FILE, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = pref->AddObserver(PREF_BIFF_ALWAYS_SHOW_ICON, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = pref->AddObserver(PREF_BIFF_USE_KEYBOARD_LED, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
	}
	else
	{
		nsCOMPtr<nsIPrefBranchInternal> prefInternal = do_QueryInterface(mPrefBranch);
		NS_ENSURE_TRUE(prefInternal, NS_OK);
		rv = prefInternal->AddObserver(PREF_BIFF_SHOW_ICON, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = prefInternal->AddObserver(PREF_BIFF_SHOW_ASUS_LED, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = prefInternal->AddObserver(PREF_BIFF_USE_HW_INDICATOR, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = prefInternal->AddObserver(PREF_BIFF_HW_INDICATOR_FILE, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = prefInternal->AddObserver(PREF_BIFF_ALWAYS_SHOW_ICON, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
		rv = prefInternal->AddObserver(PREF_BIFF_USE_KEYBOARD_LED, this, PR_TRUE);
		NS_ENSURE_SUCCESS(rv,rv);
	}

	// because we care about biff notifications
	nsCOMPtr <nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
	NS_ENSURE_SUCCESS(rv,rv);
	rv = mailSession->AddFolderListener(this, nsIFolderListener::propertyFlagChanged | nsIFolderListener::boolPropertyChanged | nsIFolderListener::intPropertyChanged | nsIFolderListener10::propertyFlagChanged | nsIFolderListener10::boolPropertyChanged | nsIFolderListener10::intPropertyChanged);
	NS_ENSURE_SUCCESS(rv,rv);
	// In the future, we might want to add more properties here, to enable us to display
	// the unread message count etc.
	// For reference, see:
	// - nsMessengerWinIntegration.cpp
	// - nsStatusBarBiffManager.cpp

	ApplyPrefs();

	return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
{
	if (nsCRT::strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0)
	{
		nsCAutoString prefName;
		prefName.AppendWithConversion(aData);
		
		if (prefName.Equals(PREF_BIFF_SHOW_ICON) ||
		    prefName.Equals(PREF_BIFF_ALWAYS_SHOW_ICON) ||
		    prefName.Equals(PREF_BIFF_USE_HW_INDICATOR) ||
		    prefName.Equals(PREF_BIFF_HW_INDICATOR_FILE) ||
		    prefName.Equals(PREF_BIFF_USE_KEYBOARD_LED) ||
		    // for backward compatibility:
		    prefName.Equals(PREF_BIFF_SHOW_ASUS_LED))
		{
			ApplyPrefs();
		}
	}
	return NS_OK;
}

void nsMessengerFreeDesktopIntegration::ApplyPrefs()
{
	mPrefBranch->GetBoolPref(PREF_BIFF_ALWAYS_SHOW_ICON, &mAlwaysShowBiffIcon);
	mPrefBranch->GetBoolPref(PREF_BIFF_SHOW_ICON, &mShowBiffIcon);
	mPrefBranch->GetBoolPref(PREF_BIFF_SHOW_ASUS_LED, &mUseHwIndicator);
	mPrefBranch->GetBoolPref(PREF_BIFF_USE_HW_INDICATOR, &mUseHwIndicator);
	mPrefBranch->GetIntPref(PREF_BIFF_USE_KEYBOARD_LED, &mUseKeyboardLed);
	
	// Try to autodetect the hw indicator filename.
	// This still doesn't mean we'll use it; depends on mUseHwIndicator.
	nsXPIDLCString newHwIndicatorFile;
	mPrefBranch->GetCharPref(PREF_BIFF_HW_INDICATOR_FILE, getter_Copies(newHwIndicatorFile));
	if ((newHwIndicatorFile != mHwIndicatorFile) || mHwIndicatorFile.IsEmpty())
	{
		HideHwIndicator();
		if (newHwIndicatorFile.IsEmpty())
		{
			mHwIndicatorFile = EmptyCString();
			// Autodetect
			for (unsigned int i=0;
			     i<sizeof(HW_INDICATOR_CONTROL_FILENAMES)/sizeof(HW_INDICATOR_CONTROL_FILENAMES[0]);
			     ++i)
			{
				const char* filename = HW_INDICATOR_CONTROL_FILENAMES[i];
				if ((access(filename, R_OK | W_OK) == 0) ||
				    ((errno != ENOENT) && (errno != ENOTDIR)))
				{
					mHwIndicatorFile = filename;
					break;
				}
			}
		}
		else
		{
			mHwIndicatorFile = newHwIndicatorFile;
		}
	}

	if (mUseHwIndicator && mHasBiff)
	{
		ShowHwIndicator();
	}
	else
	{
		HideHwIndicator();
	}
	
	if (mUseKeyboardLed != 0)
	{
		XKeyboardControl kbControl;
		kbControl.led = mUseKeyboardLed;
		kbControl.led_mode = mHasBiff ? LedModeOn : LedModeOff;
		XChangeKeyboardControl(GDK_DISPLAY(), KBLed | KBLedMode, &kbControl);
	}
	
	if (mAlwaysShowBiffIcon)
	{
		mShowBiffIcon = true; // force to true
		AddBiffIcon();
		OnBiffChange();
		ClearToolTip();
	}
	else if (!mShowBiffIcon || !mHasBiff)
	{
		RemoveBiffIcon();
	}
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemPropertyChanged(nsIRDFResource *, nsIAtom *, char const *, char const *)
{
  return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemPropertyChanged(nsISupports*, nsIAtom*, const char*, const char*)
{
  return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemUnicharPropertyChanged(nsIRDFResource *, nsIAtom *, const PRUnichar *, const PRUnichar *)
{
  return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemUnicharPropertyChanged(nsISupports*, nsIAtom*, const PRUnichar*, const PRUnichar*)
{
  return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemRemoved(nsIRDFResource *, nsISupports *)
{
  return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemRemoved(nsISupports*, nsISupports*, const char*)
{
  return NS_OK;
}

nsresult nsMessengerFreeDesktopIntegration::GetStringBundle(const char* src, nsIStringBundle **aBundle)
{
  nsresult rv = NS_OK;
  NS_ENSURE_ARG_POINTER(aBundle);
  nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
  nsCOMPtr<nsIStringBundle> bundle;
  if (bundleService && NS_SUCCEEDED(rv))
    bundleService->CreateBundle(src, getter_AddRefs(bundle));
  NS_IF_ADDREF(*aBundle = bundle);
  return rv;
}

void nsMessengerFreeDesktopIntegration::FillToolTipInfo()
{
	// iterate over all the folders in mFoldersWithNewMail
	nsXPIDLString accountName;
	nsXPIDLCString hostName; 
	nsAutoString toolTipText;
	PRInt32 numNewMessages = 0;
	PRInt32 numNewMessagesTotal = 0;

	PRUint32 count = 0;
	mFoldersWithNewMail->Count(&count);

	for (PRUint32 index = 0; index < count; index++)
	{
		nsCOMPtr<nsISupports> supports;
		supports = getter_AddRefs(mFoldersWithNewMail->ElementAt(index));
		nsCOMPtr<nsIWeakReference> weakReference;
		weakReference = do_QueryInterface(supports);
		nsCOMPtr<nsIMsgFolder> folder;
		nsCOMPtr<nsIMsgFolder10> folder10;
		if (folder = do_QueryReferent(weakReference))
		{
			folder->GetPrettiestName(getter_Copies(accountName));
			numNewMessages = 0;   
			folder->GetNumNewMessages(PR_TRUE, &numNewMessages);
			numNewMessagesTotal += numNewMessages;
		}
		else if (folder10 = do_QueryReferent(weakReference))
		{
			folder10->GetPrettiestName(getter_Copies(accountName));
			numNewMessages = 0;   
			folder10->GetNumNewMessages(PR_TRUE, &numNewMessages);
			numNewMessagesTotal += numNewMessages;
		}
	}
	
	nsCOMPtr<nsIStringBundle> bundle; 
	GetStringBundle(STRING_BUNDLE_MESSENGER, getter_AddRefs(bundle));
	if (bundle)
	{ 
		nsAutoString numNewMsgsText;     
		numNewMsgsText.AppendInt(numNewMessagesTotal);

		const PRUnichar *formatStrings[] =
		{
			numNewMsgsText.get()
		};
  
		nsXPIDLString text;
		if (numNewMessages == 1)
			bundle->FormatStringFromName(NS_LITERAL_STRING("biffNotification_message").get(), formatStrings, 2, getter_Copies(text));
		else
			bundle->FormatStringFromName(NS_LITERAL_STRING("biffNotification_messages").get(), formatStrings, 2, getter_Copies(text));
    
		toolTipText = accountName + NS_LITERAL_STRING(" ") + text;
	} // if we got a bundle
	
	SetToolTipString(toolTipText.get());
}

// get the first top level folder which we know has new mail, then enumerate over all the subfolders
// looking for the first real folder with new mail. Return the folderURI for that folder.
nsresult nsMessengerFreeDesktopIntegration::GetFirstFolderWithNewMail(char ** aFolderURI, char ** aMessageURI)
{
	nsresult rv;
	NS_ENSURE_TRUE(mFoldersWithNewMail, NS_ERROR_FAILURE); 

	PRInt32 numNewMessages = 0;

	PRUint32 count = 0;
	mFoldersWithNewMail->Count(&count);

	if (count == 0)  // kick out if we don't have any folders with new mail
		return NS_OK;

	nsCOMPtr<nsISupports> supports = getter_AddRefs(mFoldersWithNewMail->ElementAt(0));
	nsCOMPtr<nsIWeakReference> weakReference = do_QueryInterface(supports);
	nsCOMPtr<nsIMsgFolder> folder;
	nsCOMPtr<nsIMsgFolder10> folder10;

	if (folder = do_QueryReferent(weakReference))
	{
		PRUint32 biffState = nsIMsgFolder::nsMsgBiffState_NoMail; 
		nsString msgURI;
		// enumerate over the folders under this root folder till we find one with new mail....
		nsCOMPtr<nsISupportsArray> allFolders;
		NS_NewISupportsArray(getter_AddRefs(allFolders));
		rv = folder->ListDescendents(allFolders);
		NS_ENSURE_SUCCESS(rv, rv);

		nsCOMPtr<nsIMsgFolder> msgFolder;
		nsCOMPtr<nsIEnumerator> enumerator;
		allFolders->Enumerate(getter_AddRefs(enumerator));
		if (enumerator)
		{
			nsresult more = enumerator->First();
			while (NS_SUCCEEDED(more))
			{
				nsCOMPtr<nsISupports> supports;
				rv = enumerator->CurrentItem(getter_AddRefs(supports));
				if (supports)
				{			
					msgFolder = do_QueryInterface(supports, &rv);
					if (msgFolder)
					{
						numNewMessages = 0;   
						msgFolder->GetNumNewMessages(PR_FALSE, &numNewMessages);
						if (numNewMessages > 0)
						{
							nsCOMPtr<nsIMsgDBHdr> msgHeader;
							msgFolder->GetFirstNewMessage(getter_AddRefs(msgHeader));
							msgFolder->GetUriForMsg(msgHeader, aMessageURI);
							break; // kick out of the while loop
						}
						more = enumerator->Next();
					}
				} // if we have a folder
			}  // if we have more potential folders to enumerate
		}  // if enumerator
	  
		if (msgFolder)
			msgFolder->GetURI(aFolderURI);
	}
	else if (folder10 = do_QueryReferent(weakReference))
	{
		PRUint32 biffState = nsIMsgFolder10::nsMsgBiffState_NoMail; 
		nsString msgURI;
		// enumerate over the folders under this root folder till we find one with new mail....
		nsCOMPtr<nsISupportsArray> allFolders;
		NS_NewISupportsArray(getter_AddRefs(allFolders));
		rv = folder10->ListDescendents(allFolders);
		NS_ENSURE_SUCCESS(rv, rv);

		nsCOMPtr<nsIMsgFolder10> msgFolder;
		nsCOMPtr<nsIEnumerator> enumerator;
		allFolders->Enumerate(getter_AddRefs(enumerator));
		if (enumerator)
		{
			nsresult more = enumerator->First();
			while (NS_SUCCEEDED(more))
			{
				nsCOMPtr<nsISupports> supports;
				rv = enumerator->CurrentItem(getter_AddRefs(supports));
				if (supports)
				{			
					msgFolder = do_QueryInterface(supports, &rv);
					if (msgFolder)
					{
						numNewMessages = 0;   
						msgFolder->GetNumNewMessages(PR_FALSE, &numNewMessages);
						if (numNewMessages > 0)
						{
							nsCOMPtr<nsIMsgDBHdr> msgHeader;
							msgFolder->GetFirstNewMessage(getter_AddRefs(msgHeader));
							msgFolder->GetUriForMsg(msgHeader, aMessageURI);
							break; // kick out of the while loop
						}
						more = enumerator->Next();
					}
				} // if we have a folder
			}  // if we have more potential folders to enumerate
		}  // if enumerator
	  
		if (msgFolder)
			msgFolder->GetURI(aFolderURI);
	}

	return NS_OK;
}

void nsMessengerFreeDesktopIntegration::AddBiffIcon()
{
	if (mTrayIcon == NULL)
	{
		mTrayIcon = egg_status_icon_new();
		if (mTrayIcon != NULL)
		{
			g_signal_connect(G_OBJECT(mTrayIcon), "activate", G_CALLBACK(TrayIconActivate), this);
			g_signal_connect(G_OBJECT(mTrayIcon), "popup-menu", G_CALLBACK(TrayIconPopupMenu), this);
		}
	}
}

void nsMessengerFreeDesktopIntegration::RemoveBiffIcon()
{
	if (mTrayIcon != NULL)
	{
		g_object_unref(G_OBJECT(mTrayIcon));
		mTrayIcon = NULL;
	}
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::GetMailHwIndicatorStatus(PRInt16 *aHwIndicatorStatus)
{
	if (access(mHwIndicatorFile, R_OK | W_OK) == 0)
	{
		*aHwIndicatorStatus = MAIL_HW_INDICATOR_OK;
	}
	else
	{
		switch(errno)
		{
			case ENOENT:
			case ENOTDIR:
				*aHwIndicatorStatus = MAIL_HW_INDICATOR_MISSING;
				break;
			default:
				*aHwIndicatorStatus = MAIL_HW_INDICATOR_INACCESSIBLE;
				break;
		}
	}
	
	return NS_OK;
} 

void nsMessengerFreeDesktopIntegration::ShowHwIndicator()
{
	if (!mHwIndicatorFile.IsEmpty())
	{
		int fd = open(mHwIndicatorFile, O_WRONLY);
		if (fd != -1)
		{
			write(fd, "1", 1);
			close(fd);
		}
	}
}

void nsMessengerFreeDesktopIntegration::HideHwIndicator()
{
	if (!mHwIndicatorFile.IsEmpty())
	{
		int fd = open(mHwIndicatorFile, O_WRONLY);
		if (fd != -1)
		{
			write(fd, "0", 1);
			close(fd);
		}
	}
}

void nsMessengerFreeDesktopIntegration::OnActionDefault()
{
	if (mHasBiff)
	{
		OnActionReadMail();
	}
	else
	{
		OnActionToggleWindow();
	}
}

void nsMessengerFreeDesktopIntegration::OnActionReadMail()
{
	nsXPIDLCString folderURI, messageURI;
	GetFirstFolderWithNewMail(getter_Copies(folderURI), getter_Copies(messageURI));
	openMailWindow(NS_LITERAL_STRING("mail:3pane").get(), folderURI, messageURI);
}

void nsMessengerFreeDesktopIntegration::OnActionToggleWindow()
{
	toggleMailWindow(NS_LITERAL_STRING("mail:3pane").get());
}

void nsMessengerFreeDesktopIntegration::OnActionHideIcon()
{
	if (mHasBiff)
	{
		mPrefBranch->SetBoolPref(PREF_BIFF_SHOW_ICON, PR_FALSE);
	}
	else
	{
		mPrefBranch->SetBoolPref(PREF_BIFF_ALWAYS_SHOW_ICON, PR_FALSE);
	}
	// TODO: This should ultimately popup an informational message box.
}

void nsMessengerFreeDesktopIntegration::OnBiffChange()
{
	if (mHasBiff)
	{
		if (mShowBiffIcon)
		{
			AddBiffIcon();
			FillToolTipInfo();
			GdkPixbuf* pixbuf = gdk_pixbuf_new_from_inline(-1, tray_biff_icon, FALSE, NULL);
			egg_status_icon_set_from_pixbuf(mTrayIcon, pixbuf);
			g_object_unref(G_OBJECT(pixbuf));
		}
	      
		if (mUseHwIndicator)
		{
			ShowHwIndicator();
		}
	}
	else
	{
		if (mAlwaysShowBiffIcon)
		{
			if (!mBrandIconPath.IsEmpty())
			{
				ClearToolTip();
				GError* err = NULL;
				GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(mBrandIconPath.get(), &err);
				if (pixbuf != NULL)
				{
					egg_status_icon_set_from_pixbuf(mTrayIcon, pixbuf);
					g_object_unref(G_OBJECT(pixbuf));
				}
				else
				{
					fprintf (stderr, "mozTrayBiff: Error loading application icon: %s\n", err->message);
					RemoveBiffIcon();
				}
			}
			else
			{
				fprintf(stderr, "mozTrayBiff: No application icon found\n");
				RemoveBiffIcon();
			}
		}
		else
		{
			RemoveBiffIcon();
		}
		HideHwIndicator();
	}
}

void nsMessengerFreeDesktopIntegration::OnActionPopupMenu(unsigned int button, unsigned int activateTime)
{
	GtkWidget* pTrayMenu = gtk_menu_new();
	GtkWidget *entry;

	// Default values for menu item strings
	nsXPIDLString menuItemReadMail;
	menuItemReadMail.Assign(NS_LITERAL_STRING("Read mail..."));
	nsXPIDLString menuItemNewWindow;
	menuItemNewWindow.Assign(NS_LITERAL_STRING("New window"));
	nsXPIDLString menuItemRestoreWindow;
	menuItemRestoreWindow.Assign(NS_LITERAL_STRING("Restore window"));
	nsXPIDLString menuItemHideWindow;
	menuItemHideWindow.Assign(NS_LITERAL_STRING("Hide window"));
	nsXPIDLString menuItemHideIcon;
	menuItemHideIcon.Assign(NS_LITERAL_STRING("Hide icon"));

	// Retrieve menu item localizations from the string bundle, if possible
	nsCOMPtr<nsIStringBundle> bundle; 
	GetStringBundle(STRING_BUNDLE_TRAYBIFF, getter_AddRefs(bundle));
	NS_ASSERTION(bundle, "no string bundle");
	if (bundle)
	{ 
		bundle->GetStringFromName(NS_LITERAL_STRING("trayMenu_ReadMail").get(), getter_Copies(menuItemReadMail));
		bundle->GetStringFromName(NS_LITERAL_STRING("trayMenu_NewWindow").get(), getter_Copies(menuItemNewWindow));
		bundle->GetStringFromName(NS_LITERAL_STRING("trayMenu_RestoreWindow").get(), getter_Copies(menuItemRestoreWindow));
		bundle->GetStringFromName(NS_LITERAL_STRING("trayMenu_HideWindow").get(), getter_Copies(menuItemHideWindow));
		bundle->GetStringFromName(NS_LITERAL_STRING("trayMenu_HideIcon").get(), getter_Copies(menuItemHideIcon));
	}

	if (mHasBiff)
	{
		entry = gtk_menu_item_new_with_label(NS_ConvertUTF16toUTF8(menuItemReadMail).get());
		g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(MenuReadMail), this);
		gtk_menu_shell_append(GTK_MENU_SHELL(pTrayMenu), entry);
	}
	switch (isMailWindowVisible(NS_LITERAL_STRING("mail:3pane").get()))
	{
	case MAIL_WINDOW_NOT_FOUND:
		entry = gtk_menu_item_new_with_label(NS_ConvertUTF16toUTF8(menuItemNewWindow).get());
		break;
	case MAIL_WINDOW_HIDDEN:
		entry = gtk_menu_item_new_with_label(NS_ConvertUTF16toUTF8(menuItemRestoreWindow).get());
		break;
	case MAIL_WINDOW_VISIBLE:
		entry = gtk_menu_item_new_with_label(NS_ConvertUTF16toUTF8(menuItemHideWindow).get());
		break;
	}
	g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(MenuToggleWindow), this);
	gtk_menu_shell_append(GTK_MENU_SHELL(pTrayMenu), entry);
	entry = gtk_menu_item_new_with_label(NS_ConvertUTF16toUTF8(menuItemHideIcon).get());
	g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(MenuHideIcon), this);
	gtk_menu_shell_append(GTK_MENU_SHELL(pTrayMenu), entry);

	gtk_widget_show_all(pTrayMenu);
	g_signal_connect(G_OBJECT(pTrayMenu), "selection-done", G_CALLBACK(TrayIconPopupMenuSelectionDone), this);
	gtk_menu_popup(GTK_MENU(pTrayMenu), NULL, NULL, NULL, NULL, button, activateTime);
}

/*
 Static functions which are GTK+ event handlers; minimal code to cast user-data
 into nsMessengerFreeDesktopIntegration and call the appropriate method.
 */
 
void nsMessengerFreeDesktopIntegration::TrayIconActivate(GtkWidget *trayIcon, void *data)
{
	reinterpret_cast<nsMessengerFreeDesktopIntegration*>(data)->OnActionDefault();
}

void nsMessengerFreeDesktopIntegration::TrayIconPopupMenu(GtkWidget *trayIcon, guint button, guint activateTime, void *data)
{
	reinterpret_cast<nsMessengerFreeDesktopIntegration*>(data)->OnActionPopupMenu(button, activateTime);
}

void nsMessengerFreeDesktopIntegration::TrayIconPopupMenuSelectionDone(GtkMenuShell *menushell, void *data)
{
	gtk_widget_destroy(GTK_WIDGET(menushell));
}

void nsMessengerFreeDesktopIntegration::MenuHideIcon(GtkMenuItem *menuItem, void *data)
{
	reinterpret_cast<nsMessengerFreeDesktopIntegration*>(data)->OnActionHideIcon();
}

void nsMessengerFreeDesktopIntegration::MenuReadMail(GtkMenuItem *menuItem, void *data)
{
	reinterpret_cast<nsMessengerFreeDesktopIntegration*>(data)->OnActionReadMail();
}

void nsMessengerFreeDesktopIntegration::MenuToggleWindow(GtkMenuItem *menuItem, void *data)
{
	reinterpret_cast<nsMessengerFreeDesktopIntegration*>(data)->OnActionToggleWindow();
}

void nsMessengerFreeDesktopIntegration::SetToolTipString(const PRUnichar * aToolTipString)
{
	if (aToolTipString == NULL || mTrayIcon == NULL)
	{
		return;
	}
	NS_ConvertUCS2toUTF8 utf8TooltipString(aToolTipString);
	egg_status_icon_set_tooltip(mTrayIcon, static_cast<const gchar*>(utf8TooltipString.get()), NULL);
}

void nsMessengerFreeDesktopIntegration::ClearToolTip()
{
	if (mTrayIcon == NULL)
	{
		return;
	}
	
	// This hardcoded "Mozilla" should be changed once we move
	// to 1.8 branch and have nsIXULAppInfo.
	egg_status_icon_set_tooltip(mTrayIcon, "Mozilla", NULL);
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemPropertyFlagChanged(nsISupports *item, nsIAtom *aProperty, PRUint32 oldFlag, PRUint32 newFlag)
{
	// if we got new mail show a icon in the system tray
	if (mBiffStateAtom == aProperty && mFoldersWithNewMail)
	{
		nsCOMPtr<nsIMsgFolder10> folder = do_QueryInterface(item);
		NS_ENSURE_TRUE(folder, NS_OK);

		if (newFlag == nsIMsgFolder::nsMsgBiffState_NewMail) 
		{
			// if the icon is not already visible, only show a system tray icon iff 
			// we are performing biff (as opposed to the user getting new mail)
			PRBool performingBiff = PR_FALSE;
			nsCOMPtr<nsIMsgIncomingServer10> server;
			folder->GetServer(getter_AddRefs(server));
			if (server)
				server->GetPerformingBiff(&performingBiff);
			if (!performingBiff) 
				return NS_OK; // kick out right now...
			nsCOMPtr<nsIWeakReference> weakFolder = do_GetWeakReference(folder); 

			// remove the element if it is already in the array....
			PRUint32 count = 0;
			PRUint32 index = 0; 
			mFoldersWithNewMail->Count(&count);
			nsCOMPtr<nsISupports> supports;
			nsCOMPtr<nsIMsgFolder10> oldFolder;
			nsCOMPtr<nsIWeakReference> weakReference;
			for (index = 0; index < count; index++)
			{
				supports = getter_AddRefs(mFoldersWithNewMail->ElementAt(index));
				weakReference = do_QueryInterface(supports);
				oldFolder = do_QueryReferent(weakReference);
				if (oldFolder == folder) // if they point to the same folder
					break;
				oldFolder = nsnull;
			}

			if (oldFolder)
				mFoldersWithNewMail->ReplaceElementAt(weakFolder, index);
			else
				mFoldersWithNewMail->AppendElement(weakFolder);

			// Suggests the icon's default action (which is performed by OnBiffIconActivate)
			mHasBiff = true;
			OnBiffChange();
		}
		else if (newFlag == nsIMsgFolder::nsMsgBiffState_NoMail)
		{
			mFoldersWithNewMail->Clear();
			// Suggests the icon's default action (which is performed by OnBiffIconActivate)
			mHasBiff = false;
			OnBiffChange();
		}
	} // if the biff property changed
	
	return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemPropertyFlagChanged(nsIMsgDBHdr *item, nsIAtom *property, PRUint32 oldFlag, PRUint32 newFlag)
{
	return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemAdded(nsIRDFResource *, nsISupports *)
{
  return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemAdded(nsISupports*, nsISupports*, const char*)
{
  return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemBoolPropertyChanged(nsIRDFResource *aItem, nsIAtom *aProperty, PRBool aOldValue, PRBool aNewValue)
{
  return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemBoolPropertyChanged(nsISupports*, nsIAtom*, int, int)
{
  return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemIntPropertyChanged(nsIRDFResource *aItem, nsIAtom *aProperty, int aOldValue, int aNewValue)
{
	// if we got new mail show a icon in the system tray
	if (mBiffStateAtom == aProperty && mFoldersWithNewMail)
	{
		nsCOMPtr<nsIMsgFolder> folder = do_QueryInterface(aItem);
		NS_ENSURE_TRUE(folder, NS_OK);

		if (aNewValue == nsIMsgFolder::nsMsgBiffState_NewMail) 
		{
			// if the icon is not already visible, only show a system tray icon iff 
			// we are performing biff (as opposed to the user getting new mail)
			PRBool performingBiff = PR_FALSE;
			nsCOMPtr<nsIMsgIncomingServer> server;
			folder->GetServer(getter_AddRefs(server));
			if (server)
				server->GetPerformingBiff(&performingBiff);
			if (!performingBiff) 
				return NS_OK; // kick out right now...
			nsCOMPtr<nsIWeakReference> weakFolder = do_GetWeakReference(folder); 

			// remove the element if it is already in the array....
			PRUint32 count = 0;
			PRUint32 index = 0; 
			mFoldersWithNewMail->Count(&count);
			nsCOMPtr<nsISupports> supports;
			nsCOMPtr<nsIMsgFolder> oldFolder;
			nsCOMPtr<nsIWeakReference> weakReference;
			for (index = 0; index < count; index++)
			{
				supports = getter_AddRefs(mFoldersWithNewMail->ElementAt(index));
				weakReference = do_QueryInterface(supports);
				oldFolder = do_QueryReferent(weakReference);
				if (oldFolder == folder) // if they point to the same folder
					break;
				oldFolder = nsnull;
			}

			if (oldFolder)
				mFoldersWithNewMail->ReplaceElementAt(weakFolder, index);
			else
				mFoldersWithNewMail->AppendElement(weakFolder);

			// Suggests the icon's default action (which is performed by OnBiffIconActivate)
			mHasBiff = true;
			OnBiffChange();
		}
		else if (aNewValue == nsIMsgFolder::nsMsgBiffState_NoMail)
		{
			mFoldersWithNewMail->Clear();
			// Suggests the icon's default action (which is performed by OnBiffIconActivate)
			mHasBiff = false;
			OnBiffChange();
		}
	} // if the biff property changed
	
	return NS_OK;
}

// For compatibility with tb1.0
NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemIntPropertyChanged(nsISupports*, nsIAtom*, int, int)
{
	return NS_OK;
}

NS_IMETHODIMP
nsMessengerFreeDesktopIntegration::OnItemEvent(nsIMsgFolder *, nsIAtom *)
{
  return NS_OK;
}