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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <windows.h>
#include <shellapi.h>
#include "nsMessengerWinIntegration.h"
#include "nsIMsgAccountManager.h"
#include "nsIMsgMailSession.h"
#include "nsIMsgIncomingServer.h"
#include "nsIMsgIdentity.h"
#include "nsIMsgAccount.h"
#include "nsIMsgFolder.h"
#include "nsIMsgWindow.h"
#include "nsCOMPtr.h"
#include "nsMsgBaseCID.h"
#include "nsMsgFolderFlags.h"
#include "nsDirectoryServiceDefs.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIDirectoryService.h"
#include "nsIWindowWatcher.h"
#include "nsIWindowMediator.h"
#include "mozIDOMWindow.h"
#include "nsPIDOMWindow.h"
#include "nsIDocShell.h"
#include "nsIBaseWindow.h"
#include "nsIWidget.h"
#include "nsWidgetsCID.h"
#include "MailNewsTypes.h"
#include "nsIMessengerWindowService.h"
#include "prprf.h"
#include "nsIWeakReference.h"
#include "nsIStringBundle.h"
#include "nsIAlertsService.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIProperties.h"
#include "nsISupportsPrimitives.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIWeakReferenceUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsNativeCharsetUtils.h"
#include "nsMsgUtils.h"
#ifdef MOZILLA_INTERNAL_API
#include "mozilla/LookAndFeel.h"
#endif
#include "mozilla/Services.h"
#include "nsIMutableArray.h"
#include "nsArrayUtils.h"
#include "nsToolkitCompsCID.h"
#include <stdlib.h>
#define PROFILE_COMMANDLINE_ARG " -profile "
#define NOTIFICATIONCLASSNAME "MailBiffNotificationMessageWindow"
#define UNREADMAILNODEKEY "Software\\Microsoft\\Windows\\CurrentVersion\\UnreadMail\\"
#define DOUBLE_QUOTE "\""
#define MAIL_COMMANDLINE_ARG " -mail"
#define IDI_MAILBIFF 32576
#define UNREAD_UPDATE_INTERVAL (20 * 1000) // 20 seconds
#define ALERT_CHROME_URL "chrome://messenger/content/newmailalert.xul"
#define NEW_MAIL_ALERT_ICON "chrome://messenger/skin/icons/new-mail-alert.png"
#define SHOW_ALERT_PREF "mail.biff.show_alert"
#define SHOW_TRAY_ICON_PREF "mail.biff.show_tray_icon"
#define SHOW_BALLOON_PREF "mail.biff.show_balloon"
#define SHOW_NEW_ALERT_PREF "mail.biff.show_new_alert"
#define ALERT_ORIGIN_PREF "ui.alertNotificationOrigin"
// since we are including windows.h in this file, undefine get user name....
#ifdef GetUserName
#undef GetUserName
#endif
#ifndef NIIF_USER
#define NIIF_USER 0x00000004
#endif
#ifndef NIIF_NOSOUND
#define NIIF_NOSOUND 0x00000010
#endif
#ifndef NIN_BALOONUSERCLICK
#define NIN_BALLOONUSERCLICK (WM_USER + 5)
#endif
#ifndef MOZILLA_INTERNAL_API
// from LookAndFeel.h
#define NS_ALERT_HORIZONTAL 1
#define NS_ALERT_LEFT 2
#define NS_ALERT_TOP 4
#endif
using namespace mozilla;
// begin shameless copying from nsNativeAppSupportWin
HWND hwndForDOMWindow( mozIDOMWindowProxy *window )
{
if ( !window ) {
return 0;
}
nsCOMPtr<nsPIDOMWindowOuter> pidomwindow = nsPIDOMWindowOuter::From(window);
nsCOMPtr<nsIBaseWindow> ppBaseWindow =
do_QueryInterface( pidomwindow->GetDocShell() );
if (!ppBaseWindow)
return 0;
nsCOMPtr<nsIWidget> ppWidget;
ppBaseWindow->GetMainWidget( getter_AddRefs( ppWidget ) );
return (HWND)( ppWidget->GetNativeData( NS_NATIVE_WIDGET ) );
}
static void activateWindow( mozIDOMWindowProxy *win )
{
// Try to get native window handle.
HWND hwnd = hwndForDOMWindow( win );
if ( hwnd )
{
// Restore the window if it is minimized.
if ( ::IsIconic( hwnd ) )
::ShowWindow( hwnd, SW_RESTORE );
// Use the OS call, if possible.
::SetForegroundWindow( hwnd );
} else {
// Use internal method.
nsCOMPtr<nsPIDOMWindowOuter> privateWindow = nsPIDOMWindowOuter::From(win);
privateWindow->Focus();
}
}
// end shameless copying from nsNativeAppWinSupport.cpp
static void openMailWindow(const nsACString& aFolderUri)
{
nsresult rv;
nsCOMPtr<nsIMsgMailSession> mailSession ( do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv));
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIMsgWindow> topMostMsgWindow;
rv = mailSession->GetTopmostMsgWindow(getter_AddRefs(topMostMsgWindow));
if (topMostMsgWindow)
{
nsCOMPtr<mozIDOMWindowProxy> domWindow;
topMostMsgWindow->GetDomWindow(getter_AddRefs(domWindow));
if (domWindow)
{
if (!aFolderUri.IsEmpty())
{
nsCOMPtr<nsIMsgWindowCommands> windowCommands;
topMostMsgWindow->GetWindowCommands(getter_AddRefs(windowCommands));
if (windowCommands)
windowCommands->SelectFolder(aFolderUri);
}
activateWindow(domWindow);
return;
}
}
{
// the user doesn't have a mail window open already so open one for them...
nsCOMPtr<nsIMessengerWindowService> messengerWindowService =
do_GetService(NS_MESSENGERWINDOWSERVICE_CONTRACTID);
// if we want to preselect the first account with new mail,
// here is where we would try to generate a uri to pass in
// (and add code to the messenger window service to make that work)
if (messengerWindowService)
messengerWindowService->OpenMessengerWindowWithUri(
"mail:3pane", nsCString(aFolderUri).get(), nsMsgKey_None);
}
}
static void CALLBACK delayedSingleClick(HWND msgWindow, UINT msg, INT_PTR idEvent, DWORD dwTime)
{
::KillTimer(msgWindow, idEvent);
// single clicks on the biff icon should re-open the alert notification
nsresult rv = NS_OK;
nsCOMPtr<nsIMessengerOSIntegration> integrationService =
do_GetService(NS_MESSENGEROSINTEGRATION_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
// we know we are dealing with the windows integration object
nsMessengerWinIntegration * winIntegrationService = static_cast<nsMessengerWinIntegration*>
(static_cast<nsIMessengerOSIntegration*>(integrationService.get()));
winIntegrationService->ShowNewAlertNotification(true, EmptyString(), EmptyString());
}
}
// Window proc.
static LRESULT CALLBACK MessageWindowProc( HWND msgWindow, UINT msg, WPARAM wp, LPARAM lp )
{
if (msg == WM_USER)
{
if (lp == WM_LBUTTONDOWN)
{
// the only way to tell a single left click
// from a double left click is to fire a timer which gets cleared if we end up getting
// a WM_LBUTTONDBLK event.
::SetTimer(msgWindow, 1, GetDoubleClickTime(), (TIMERPROC) delayedSingleClick);
}
else if (lp == WM_LBUTTONDBLCLK || lp == NIN_BALLOONUSERCLICK)
{
::KillTimer(msgWindow, 1);
openMailWindow(EmptyCString());
}
}
return TRUE;
}
static HWND msgWindow;
// Create: Register class and create window.
static nsresult Create()
{
if (msgWindow)
return NS_OK;
WNDCLASS classStruct = { 0, // style
&MessageWindowProc, // lpfnWndProc
0, // cbClsExtra
0, // cbWndExtra
0, // hInstance
0, // hIcon
0, // hCursor
0, // hbrBackground
0, // lpszMenuName
NOTIFICATIONCLASSNAME }; // lpszClassName
// Register the window class.
NS_ENSURE_TRUE( ::RegisterClass( &classStruct ), NS_ERROR_FAILURE );
// Create the window.
NS_ENSURE_TRUE( msgWindow = ::CreateWindow( NOTIFICATIONCLASSNAME,
0, // title
WS_CAPTION, // style
0,0,0,0, // x, y, cx, cy
0, // parent
0, // menu
0, // instance
0 ), // create struct
NS_ERROR_FAILURE );
return NS_OK;
}
nsMessengerWinIntegration::nsMessengerWinIntegration()
{
mDefaultServerAtom = MsgGetAtom("DefaultServer");
mTotalUnreadMessagesAtom = MsgGetAtom("TotalUnreadMessages");
mUnreadTimerActive = false;
mBiffStateAtom = MsgGetAtom("BiffState");
mBiffIconVisible = false;
mSuppressBiffIcon = false;
mAlertInProgress = false;
mBiffIconInitialized = false;
mFoldersWithNewMail = do_CreateInstance(NS_ARRAY_CONTRACTID);
}
nsMessengerWinIntegration::~nsMessengerWinIntegration()
{
if (mUnreadCountUpdateTimer) {
mUnreadCountUpdateTimer->Cancel();
mUnreadCountUpdateTimer = nullptr;
}
// one last attempt, update the registry
nsresult rv = UpdateRegistryWithCurrent();
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to update registry on shutdown");
DestroyBiffIcon();
}
NS_IMPL_ADDREF(nsMessengerWinIntegration)
NS_IMPL_RELEASE(nsMessengerWinIntegration)
NS_INTERFACE_MAP_BEGIN(nsMessengerWinIntegration)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMessengerOSIntegration)
NS_INTERFACE_MAP_ENTRY(nsIMessengerOSIntegration)
NS_INTERFACE_MAP_ENTRY(nsIFolderListener)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END
nsresult
nsMessengerWinIntegration::ResetCurrent()
{
mInboxURI.Truncate();
mEmail.Truncate();
mCurrentUnreadCount = -1;
mLastUnreadCountWrittenToRegistry = -1;
mDefaultAccountMightHaveAnInbox = true;
return NS_OK;
}
NOTIFYICONDATAW sBiffIconData = { NOTIFYICONDATAW_V2_SIZE,
0,
2,
NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO,
WM_USER,
0,
L"",
0,
0,
L"",
30000,
L"",
NIIF_USER | NIIF_NOSOUND };
// allow for the null terminator
static const uint32_t kMaxTooltipSize = sizeof(sBiffIconData.szTip) /
sizeof(sBiffIconData.szTip[0]) - 1;
static const uint32_t kMaxBalloonSize = sizeof(sBiffIconData.szInfo) /
sizeof(sBiffIconData.szInfo[0]) - 1;
static const uint32_t kMaxBalloonTitle = sizeof(sBiffIconData.szInfoTitle) /
sizeof(sBiffIconData.szInfoTitle[0]) - 1;
void nsMessengerWinIntegration::InitializeBiffStatusIcon()
{
// initialize our biff status bar icon
Create();
sBiffIconData.hWnd = (HWND) msgWindow;
sBiffIconData.hIcon = ::LoadIcon( ::GetModuleHandle( NULL ), MAKEINTRESOURCE(IDI_MAILBIFF) );
mBiffIconInitialized = true;
}
nsresult
nsMessengerWinIntegration::Init()
{
nsresult rv;
nsCOMPtr <nsIMsgAccountManager> accountManager =
do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
// because we care if the default server changes
rv = accountManager->AddRootFolderListener(this);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
// because we care if the unread total count changes
rv = mailSession->AddFolderListener(this, nsIFolderListener::boolPropertyChanged | nsIFolderListener::intPropertyChanged);
NS_ENSURE_SUCCESS(rv,rv);
// get current profile path for the commandliner
nsCOMPtr<nsIProperties> directoryService =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr<nsIFile> profilePath;
rv = directoryService->Get(NS_APP_USER_PROFILE_50_DIR,
NS_GET_IID(nsIFile),
getter_AddRefs(profilePath));
NS_ENSURE_SUCCESS(rv,rv);
rv = profilePath->GetPath(mProfilePath);
NS_ENSURE_SUCCESS(rv, rv);
// get application path
WCHAR appPath[_MAX_PATH] = {0};
::GetModuleFileNameW(nullptr, appPath, sizeof(appPath));
mAppName.Assign((char16_t *)appPath);
rv = ResetCurrent();
NS_ENSURE_SUCCESS(rv,rv);
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemPropertyChanged(nsIMsgFolder *, nsIAtom *, char const *, char const *)
{
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemUnicharPropertyChanged(nsIMsgFolder *, nsIAtom *, const char16_t *, const char16_t *)
{
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemRemoved(nsIMsgFolder *, nsISupports *)
{
return NS_OK;
}
nsresult nsMessengerWinIntegration::GetStringBundle(nsIStringBundle **aBundle)
{
NS_ENSURE_ARG_POINTER(aBundle);
nsCOMPtr<nsIStringBundleService> bundleService =
mozilla::services::GetStringBundleService();
NS_ENSURE_TRUE(bundleService, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIStringBundle> bundle;
bundleService->CreateBundle("chrome://messenger/locale/messenger.properties",
getter_AddRefs(bundle));
NS_IF_ADDREF(*aBundle = bundle);
return NS_OK;
}
#ifndef MOZ_THUNDERBIRD
nsresult nsMessengerWinIntegration::ShowAlertMessage(const nsString& aAlertTitle,
const nsString& aAlertText,
const nsACString& aFolderURI)
{
nsresult rv;
// if we are already in the process of showing an alert, don't try to show another....
if (mAlertInProgress)
return NS_OK;
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
bool showBalloon = false;
prefBranch->GetBoolPref(SHOW_BALLOON_PREF, &showBalloon);
sBiffIconData.szInfo[0] = '\0';
if (showBalloon) {
::wcsncpy( sBiffIconData.szInfoTitle, aAlertTitle.get(), kMaxBalloonTitle);
::wcsncpy( sBiffIconData.szInfo, aAlertText.get(), kMaxBalloonSize);
}
bool showAlert = true;
prefBranch->GetBoolPref(SHOW_ALERT_PREF, &showAlert);
if (showAlert)
{
nsCOMPtr<nsIAlertsService> alertsService (do_GetService(NS_ALERTSERVICE_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv))
{
rv = alertsService->ShowAlertNotification(NS_LITERAL_STRING(NEW_MAIL_ALERT_ICON), aAlertTitle,
aAlertText, true,
NS_ConvertASCIItoUTF16(aFolderURI), this,
EmptyString(),
NS_LITERAL_STRING("auto"),
EmptyString(), EmptyString(),
nullptr,
false,
false);
mAlertInProgress = true;
}
}
if (!showAlert || NS_FAILED(rv)) // go straight to showing the system tray icon.
AlertFinished();
return rv;
}
#endif
// Opening Thunderbird's new mail alert notification window
// aUserInitiated --> true if we are opening the alert notification in response to a user action
// like clicking on the biff icon
nsresult nsMessengerWinIntegration::ShowNewAlertNotification(bool aUserInitiated, const nsString& aAlertTitle, const nsString& aAlertText)
{
nsresult rv;
// if we are already in the process of showing an alert, don't try to show another....
if (mAlertInProgress)
return NS_OK;
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
bool showBalloon = false;
prefBranch->GetBoolPref(SHOW_BALLOON_PREF, &showBalloon);
sBiffIconData.szInfo[0] = '\0';
if (showBalloon) {
::wcsncpy( sBiffIconData.szInfoTitle, aAlertTitle.get(), kMaxBalloonTitle);
::wcsncpy( sBiffIconData.szInfo, aAlertText.get(), kMaxBalloonSize);
}
bool showAlert = true;
if (prefBranch)
prefBranch->GetBoolPref(SHOW_ALERT_PREF, &showAlert);
// check if we are allowed to show a notification
if (showAlert) {
QUERY_USER_NOTIFICATION_STATE qstate;
if (SUCCEEDED(SHQueryUserNotificationState(&qstate))) {
if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) {
showAlert = false;
}
}
}
if (showAlert)
{
nsCOMPtr<nsIMutableArray> argsArray(do_CreateInstance(NS_ARRAY_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
// pass in the array of folders with unread messages
nsCOMPtr<nsISupportsInterfacePointer> ifptr = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
ifptr->SetData(mFoldersWithNewMail);
ifptr->SetDataIID(&NS_GET_IID(nsIArray));
rv = argsArray->AppendElement(ifptr, false);
NS_ENSURE_SUCCESS(rv, rv);
// pass in the observer
ifptr = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr <nsISupports> supports = do_QueryInterface(static_cast<nsIMessengerOSIntegration*>(this));
ifptr->SetData(supports);
ifptr->SetDataIID(&NS_GET_IID(nsIObserver));
rv = argsArray->AppendElement(ifptr, false);
NS_ENSURE_SUCCESS(rv, rv);
// pass in the animation flag
nsCOMPtr<nsISupportsPRBool> scriptableUserInitiated (do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
scriptableUserInitiated->SetData(aUserInitiated);
rv = argsArray->AppendElement(scriptableUserInitiated, false);
NS_ENSURE_SUCCESS(rv, rv);
// pass in the alert origin
nsCOMPtr<nsISupportsPRUint8> scriptableOrigin (do_CreateInstance(NS_SUPPORTS_PRUINT8_CONTRACTID));
NS_ENSURE_TRUE(scriptableOrigin, NS_ERROR_FAILURE);
scriptableOrigin->SetData(0);
int32_t origin = 0;
#ifdef MOZILLA_INTERNAL_API
origin = LookAndFeel::GetInt(LookAndFeel::eIntID_AlertNotificationOrigin);
#else
// Get task bar window handle
HWND shellWindow = FindWindowW(L"Shell_TrayWnd", NULL);
rv = prefBranch->GetIntPref(ALERT_ORIGIN_PREF, &origin);
if (NS_FAILED(rv) && (shellWindow != NULL))
{
// Determine position
APPBARDATA appBarData;
appBarData.hWnd = shellWindow;
appBarData.cbSize = sizeof(appBarData);
if (SHAppBarMessage(ABM_GETTASKBARPOS, &appBarData))
{
// Set alert origin as a bit field - see LookAndFeel.h
// 0 represents bottom right, sliding vertically.
switch(appBarData.uEdge)
{
case ABE_LEFT:
origin = NS_ALERT_HORIZONTAL | NS_ALERT_LEFT;
break;
case ABE_RIGHT:
origin = NS_ALERT_HORIZONTAL;
break;
case ABE_TOP:
origin = NS_ALERT_TOP;
// fall through for the right-to-left handling.
case ABE_BOTTOM:
// If the task bar is right-to-left,
// move the origin to the left
if (::GetWindowLong(shellWindow, GWL_EXSTYLE) &
WS_EX_LAYOUTRTL)
origin |= NS_ALERT_LEFT;
break;
}
}
}
#endif
scriptableOrigin->SetData(origin);
rv = argsArray->AppendElement(scriptableOrigin, false);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
nsCOMPtr<mozIDOMWindowProxy> newWindow;
rv = wwatch->OpenWindow(0, ALERT_CHROME_URL, "_blank",
"chrome,dialog=yes,titlebar=no,popup=yes", argsArray,
getter_AddRefs(newWindow));
mAlertInProgress = true;
}
// if the user has turned off the mail alert, or openWindow generated an error,
// then go straight to the system tray.
if (!showAlert || NS_FAILED(rv))
AlertFinished();
return rv;
}
nsresult nsMessengerWinIntegration::AlertFinished()
{
// okay, we are done showing the alert
// now put an icon in the system tray, if allowed
bool showTrayIcon = !mSuppressBiffIcon || sBiffIconData.szInfo[0];
if (showTrayIcon)
{
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefBranch)
prefBranch->GetBoolPref(SHOW_TRAY_ICON_PREF, &showTrayIcon);
}
if (showTrayIcon)
{
GenericShellNotify(NIM_ADD);
mBiffIconVisible = true;
}
mSuppressBiffIcon = false;
mAlertInProgress = false;
return NS_OK;
}
nsresult nsMessengerWinIntegration::AlertClicked()
{
nsresult rv;
nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr<nsIMsgWindow> topMostMsgWindow;
rv = mailSession->GetTopmostMsgWindow(getter_AddRefs(topMostMsgWindow));
if (topMostMsgWindow)
{
nsCOMPtr<mozIDOMWindowProxy> domWindow;
topMostMsgWindow->GetDomWindow(getter_AddRefs(domWindow));
if (domWindow)
{
activateWindow(domWindow);
return NS_OK;
}
}
// make sure we don't insert the icon in the system tray since the user clicked on the alert.
mSuppressBiffIcon = true;
nsCString folderURI;
GetFirstFolderWithNewMail(folderURI);
openMailWindow(folderURI);
return NS_OK;
}
#ifdef MOZ_SUITE
nsresult nsMessengerWinIntegration::AlertClickedSimple()
{
mSuppressBiffIcon = true;
return NS_OK;
}
#endif MOZ_SUITE
NS_IMETHODIMP
nsMessengerWinIntegration::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
{
if (strcmp(aTopic, "alertfinished") == 0)
return AlertFinished();
if (strcmp(aTopic, "alertclickcallback") == 0)
return AlertClicked();
#ifdef MOZ_SUITE
// SeaMonkey does most of the GUI work in JS code when clicking on a mail
// notification, so it needs an extra function here
if (strcmp(aTopic, "alertclicksimplecallback") == 0)
return AlertClickedSimple();
#endif
return NS_OK;
}
static void EscapeAmpersands(nsString& aToolTip)
{
// First, check to see whether we have any ampersands.
int32_t pos = aToolTip.FindChar('&');
if (pos == kNotFound)
return;
// Next, see if we only have bare ampersands.
pos = MsgFind(aToolTip, "&&", false, pos);
// Windows tooltip code removes one ampersand from each run,
// then collapses pairs of amperands. This means that in the easy case,
// we need to replace each ampersand with three.
MsgReplaceSubstring(aToolTip, NS_LITERAL_STRING("&"), NS_LITERAL_STRING("&&&"));
if (pos == kNotFound)
return;
// We inserted too many ampersands. Remove some.
for (;;) {
pos = MsgFind(aToolTip, "&&&&&&", false, pos);
if (pos == kNotFound)
return;
aToolTip.Cut(pos, 1);
pos += 2;
}
}
void nsMessengerWinIntegration::FillToolTipInfo()
{
// iterate over all the folders in mFoldersWithNewMail
nsString accountName;
nsCString hostName;
nsString toolTipLine;
nsAutoString toolTipText;
nsAutoString animatedAlertText;
nsCOMPtr<nsIMsgFolder> folder;
nsCOMPtr<nsIWeakReference> weakReference;
int32_t numNewMessages = 0;
uint32_t count = 0;
NS_ENSURE_SUCCESS_VOID(mFoldersWithNewMail->GetLength(&count));
for (uint32_t index = 0; index < count; index++)
{
weakReference = do_QueryElementAt(mFoldersWithNewMail, index);
folder = do_QueryReferent(weakReference);
if (folder)
{
folder->GetPrettiestName(accountName);
numNewMessages = 0;
folder->GetNumNewMessages(true, &numNewMessages);
nsCOMPtr<nsIStringBundle> bundle;
GetStringBundle(getter_AddRefs(bundle));
if (bundle)
{
nsAutoString numNewMsgsText;
numNewMsgsText.AppendInt(numNewMessages);
const char16_t *formatStrings[] =
{
numNewMsgsText.get(),
};
nsString finalText;
if (numNewMessages == 1)
bundle->FormatStringFromName(u"biffNotification_message", formatStrings, 1, getter_Copies(finalText));
else
bundle->FormatStringFromName(u"biffNotification_messages", formatStrings, 1, getter_Copies(finalText));
// the alert message is special...we actually only want to show the first account with
// new mail in the alert.
if (animatedAlertText.IsEmpty()) // if we haven't filled in the animated alert text yet
animatedAlertText = finalText;
toolTipLine.Append(accountName);
toolTipLine.Append(' ');
toolTipLine.Append(finalText);
EscapeAmpersands(toolTipLine);
// only add this new string if it will fit without truncation....
if (toolTipLine.Length() + toolTipText.Length() <= kMaxTooltipSize)
toolTipText.Append(toolTipLine);
// clear out the tooltip line for the next folder
toolTipLine.Assign('\n');
} // if we got a bundle
} // if we got a folder
} // for each folder
::wcsncpy( sBiffIconData.szTip, toolTipText.get(), kMaxTooltipSize);
if (!mBiffIconVisible)
{
#ifndef MOZ_THUNDERBIRD
nsresult rv;
bool showNewAlert = false;
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS_VOID(rv);
prefBranch->GetBoolPref(SHOW_NEW_ALERT_PREF, &showNewAlert);
if (!showNewAlert)
ShowAlertMessage(accountName, animatedAlertText, EmptyCString());
else
#endif
ShowNewAlertNotification(false, accountName, animatedAlertText);
}
else
GenericShellNotify( NIM_MODIFY);
}
// 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 nsMessengerWinIntegration::GetFirstFolderWithNewMail(nsACString& aFolderURI)
{
NS_ENSURE_TRUE(mFoldersWithNewMail, NS_ERROR_FAILURE);
nsCOMPtr<nsIMsgFolder> folder;
nsCOMPtr<nsIWeakReference> weakReference;
int32_t numNewMessages = 0;
uint32_t count = 0;
nsresult rv = mFoldersWithNewMail->GetLength(&count);
if (NS_FAILED(rv) || !count) // kick out if we don't have any folders with new mail
return NS_OK;
weakReference = do_QueryElementAt(mFoldersWithNewMail, 0);
folder = do_QueryReferent(weakReference);
if (folder)
{
nsCOMPtr<nsIMsgFolder> msgFolder;
// enumerate over the folders under this root folder till we find one with new mail....
nsCOMPtr<nsIArray> allFolders;
rv = folder->GetDescendants(getter_AddRefs(allFolders));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISimpleEnumerator> enumerator;
rv = allFolders->Enumerate(getter_AddRefs(enumerator));
if (NS_SUCCEEDED(rv) && enumerator)
{
nsCOMPtr<nsISupports> supports;
bool hasMore = false;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore)
{
rv = enumerator->GetNext(getter_AddRefs(supports));
if (NS_SUCCEEDED(rv) && supports)
{
msgFolder = do_QueryInterface(supports, &rv);
if (msgFolder)
{
numNewMessages = 0;
msgFolder->GetNumNewMessages(false, &numNewMessages);
if (numNewMessages)
break; // kick out of the while loop
}
} // if we have a folder
} // if we have more potential folders to enumerate
} // if enumerator
if (msgFolder)
msgFolder->GetURI(aFolderURI);
}
return NS_OK;
}
void nsMessengerWinIntegration::DestroyBiffIcon()
{
GenericShellNotify(NIM_DELETE);
// Don't call DestroyIcon(). see http://bugzilla.mozilla.org/show_bug.cgi?id=134745
}
void nsMessengerWinIntegration::GenericShellNotify(DWORD aMessage)
{
::Shell_NotifyIconW( aMessage, &sBiffIconData );
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemPropertyFlagChanged(nsIMsgDBHdr *item, nsIAtom *property, uint32_t oldFlag, uint32_t newFlag)
{
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemAdded(nsIMsgFolder *, nsISupports *)
{
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemBoolPropertyChanged(nsIMsgFolder *aItem,
nsIAtom *aProperty,
bool aOldValue,
bool aNewValue)
{
if (aProperty == mDefaultServerAtom) {
nsresult rv;
// this property changes multiple times
// on account deletion or when the user changes their
// default account. ResetCurrent() will set
// mInboxURI to null, so we use that
// to prevent us from attempting to remove
// something from the registry that has already been removed
if (!mInboxURI.IsEmpty() && !mEmail.IsEmpty()) {
rv = RemoveCurrentFromRegistry();
NS_ENSURE_SUCCESS(rv,rv);
}
// reset so we'll go get the new default server next time
rv = ResetCurrent();
NS_ENSURE_SUCCESS(rv,rv);
rv = UpdateUnreadCount();
NS_ENSURE_SUCCESS(rv,rv);
}
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemEvent(nsIMsgFolder *, nsIAtom *)
{
return NS_OK;
}
NS_IMETHODIMP
nsMessengerWinIntegration::OnItemIntPropertyChanged(nsIMsgFolder *aItem, nsIAtom *aProperty, int64_t aOldValue, int64_t aNewValue)
{
// if we got new mail show a icon in the system tray
if (mBiffStateAtom == aProperty && mFoldersWithNewMail)
{
nsCOMPtr<nsIWeakReference> weakFolder = do_GetWeakReference(aItem);
uint32_t indexInNewArray;
nsresult rv = mFoldersWithNewMail->IndexOf(0, weakFolder, &indexInNewArray);
bool folderFound = NS_SUCCEEDED(rv);
if (!mBiffIconInitialized)
InitializeBiffStatusIcon();
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)
if (!mBiffIconVisible)
{
bool performingBiff = false;
nsCOMPtr<nsIMsgIncomingServer> server;
aItem->GetServer(getter_AddRefs(server));
if (server)
server->GetPerformingBiff(&performingBiff);
if (!performingBiff)
return NS_OK; // kick out right now...
}
if (!folderFound)
mFoldersWithNewMail->InsertElementAt(weakFolder, 0, false);
// now regenerate the tooltip
FillToolTipInfo();
}
else if (aNewValue == nsIMsgFolder::nsMsgBiffState_NoMail)
{
// we are always going to remove the icon whenever we get our first no mail
// notification.
// avoid a race condition where we are told to remove the icon before we've actually
// added it to the system tray. This happens when the user reads a new message before
// the animated alert has gone away.
if (mAlertInProgress)
mSuppressBiffIcon = true;
if (folderFound)
mFoldersWithNewMail->RemoveElementAt(indexInNewArray);
if (mBiffIconVisible)
{
mBiffIconVisible = false;
GenericShellNotify(NIM_DELETE);
}
}
} // if the biff property changed
if (aProperty == mTotalUnreadMessagesAtom) {
nsCString itemURI;
nsresult rv;
rv = aItem->GetURI(itemURI);
NS_ENSURE_SUCCESS(rv,rv);
if (mInboxURI.Equals(itemURI))
mCurrentUnreadCount = aNewValue;
// If the timer isn't running yet, then we immediately update the
// registry and then start a one-shot timer. If the Unread counter
// has toggled zero / nonzero, we also update immediately.
// Otherwise, if the timer is running, defer the update. This means
// that all counter updates that occur within the timer interval are
// batched into a single registry update, to avoid hitting the
// registry too frequently. We also do a final update on shutdown,
// regardless of the timer.
if (!mUnreadTimerActive ||
(!mCurrentUnreadCount && mLastUnreadCountWrittenToRegistry) ||
(mCurrentUnreadCount && mLastUnreadCountWrittenToRegistry < 1)) {
rv = UpdateUnreadCount();
NS_ENSURE_SUCCESS(rv,rv);
// If timer wasn't running, start it.
if (!mUnreadTimerActive)
rv = SetupUnreadCountUpdateTimer();
}
}
return NS_OK;
}
void
nsMessengerWinIntegration::OnUnreadCountUpdateTimer(nsITimer *timer, void *osIntegration)
{
nsMessengerWinIntegration *winIntegration = (nsMessengerWinIntegration*)osIntegration;
winIntegration->mUnreadTimerActive = false;
nsresult rv = winIntegration->UpdateUnreadCount();
NS_ASSERTION(NS_SUCCEEDED(rv), "updating unread count failed");
}
nsresult
nsMessengerWinIntegration::RemoveCurrentFromRegistry()
{
// If Windows XP, open the registry and get rid of old account registry entries
// If there is a email prefix, get it and use it to build the registry key.
// Otherwise, just the email address will be the registry key.
nsAutoString currentUnreadMailCountKey;
if (!mEmailPrefix.IsEmpty()) {
currentUnreadMailCountKey.Assign(mEmailPrefix);
currentUnreadMailCountKey.Append(NS_ConvertASCIItoUTF16(mEmail));
}
else
CopyASCIItoUTF16(mEmail, currentUnreadMailCountKey);
WCHAR registryUnreadMailCountKey[_MAX_PATH] = {0};
// Enumerate through registry entries to delete the key matching
// currentUnreadMailCountKey
int index = 0;
while (SUCCEEDED(SHEnumerateUnreadMailAccountsW(HKEY_CURRENT_USER,
index,
registryUnreadMailCountKey,
sizeof(registryUnreadMailCountKey))))
{
if (wcscmp(registryUnreadMailCountKey, currentUnreadMailCountKey.get())==0) {
nsAutoString deleteKey;
deleteKey.Assign(NS_LITERAL_STRING(UNREADMAILNODEKEY).get());
deleteKey.Append(currentUnreadMailCountKey.get());
if (!deleteKey.IsEmpty()) {
// delete this key and berak out of the loop
RegDeleteKey(HKEY_CURRENT_USER,
NS_ConvertUTF16toUTF8(deleteKey).get());
break;
}
else {
index++;
}
}
else {
index++;
}
}
return NS_OK;
}
nsresult
nsMessengerWinIntegration::UpdateRegistryWithCurrent()
{
if (mInboxURI.IsEmpty() || mEmail.IsEmpty())
return NS_OK;
// only update the registry if the count has changed
// and if the unread count is valid
if ((mCurrentUnreadCount < 0) || (mCurrentUnreadCount == mLastUnreadCountWrittenToRegistry))
return NS_OK;
// commandliner has to be built in the form of statement
// which can be open the mailer app to the default user account
// For given profile 'foo', commandliner will be built as
// ""<absolute path to application>" -p foo -mail" where absolute
// path to application is extracted from mAppName
nsAutoString commandLinerForAppLaunch;
commandLinerForAppLaunch.Assign(NS_LITERAL_STRING(DOUBLE_QUOTE));
commandLinerForAppLaunch.Append(mAppName);
commandLinerForAppLaunch.Append(NS_LITERAL_STRING(DOUBLE_QUOTE));
commandLinerForAppLaunch.Append(NS_LITERAL_STRING(PROFILE_COMMANDLINE_ARG));
commandLinerForAppLaunch.Append(NS_LITERAL_STRING(DOUBLE_QUOTE));
commandLinerForAppLaunch.Append(mProfilePath);
commandLinerForAppLaunch.Append(NS_LITERAL_STRING(DOUBLE_QUOTE));
commandLinerForAppLaunch.Append(NS_LITERAL_STRING(MAIL_COMMANDLINE_ARG));
if (!commandLinerForAppLaunch.IsEmpty())
{
nsAutoString pBuffer;
if (!mEmailPrefix.IsEmpty()) {
pBuffer.Assign(mEmailPrefix);
pBuffer.Append(NS_ConvertASCIItoUTF16(mEmail));
}
else
CopyASCIItoUTF16(mEmail, pBuffer);
// Write the info into the registry
HRESULT hr = SHSetUnreadMailCountW(pBuffer.get(),
mCurrentUnreadCount,
commandLinerForAppLaunch.get());
}
// do this last
mLastUnreadCountWrittenToRegistry = mCurrentUnreadCount;
return NS_OK;
}
nsresult
nsMessengerWinIntegration::SetupInbox()
{
nsresult rv;
// get default account
nsCOMPtr <nsIMsgAccountManager> accountManager =
do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr <nsIMsgAccount> account;
rv = accountManager->GetDefaultAccount(getter_AddRefs(account));
NS_ENSURE_SUCCESS(rv, rv);
if (!account) {
// this can happen if we launch mail on a new profile
// we don't have a default account yet
mDefaultAccountMightHaveAnInbox = false;
return NS_OK;
}
// get incoming server
nsCOMPtr <nsIMsgIncomingServer> server;
rv = account->GetIncomingServer(getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv,rv);
if (!server)
return NS_ERROR_FAILURE;
nsCString type;
rv = server->GetType(type);
NS_ENSURE_SUCCESS(rv,rv);
// we only care about imap and pop3
if (type.EqualsLiteral("imap") || type.EqualsLiteral("pop3")) {
// imap and pop3 account should have an Inbox
mDefaultAccountMightHaveAnInbox = true;
mEmailPrefix.Truncate();
// Get user's email address
nsCOMPtr<nsIMsgIdentity> identity;
rv = account->GetDefaultIdentity(getter_AddRefs(identity));
NS_ENSURE_SUCCESS(rv,rv);
if (!identity)
return NS_ERROR_FAILURE;
rv = identity->GetEmail(mEmail);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr<nsIMsgFolder> rootMsgFolder;
rv = server->GetRootMsgFolder(getter_AddRefs(rootMsgFolder));
NS_ENSURE_SUCCESS(rv,rv);
if (!rootMsgFolder)
return NS_ERROR_FAILURE;
nsCOMPtr <nsIMsgFolder> inboxFolder;
rootMsgFolder->GetFolderWithFlags(nsMsgFolderFlags::Inbox,
getter_AddRefs(inboxFolder));
NS_ENSURE_TRUE(inboxFolder, NS_ERROR_FAILURE);
rv = inboxFolder->GetURI(mInboxURI);
NS_ENSURE_SUCCESS(rv,rv);
rv = inboxFolder->GetNumUnread(false, &mCurrentUnreadCount);
NS_ENSURE_SUCCESS(rv,rv);
}
else {
// the default account is valid, but it's not something
// that we expect to have an inbox. (local folders, news accounts)
// set this flag to avoid calling SetupInbox() every time
// the timer goes off.
mDefaultAccountMightHaveAnInbox = false;
}
return NS_OK;
}
nsresult
nsMessengerWinIntegration::UpdateUnreadCount()
{
nsresult rv;
if (mDefaultAccountMightHaveAnInbox && mInboxURI.IsEmpty()) {
rv = SetupInbox();
NS_ENSURE_SUCCESS(rv,rv);
}
return UpdateRegistryWithCurrent();
}
nsresult
nsMessengerWinIntegration::SetupUnreadCountUpdateTimer()
{
mUnreadTimerActive = true;
if (mUnreadCountUpdateTimer)
mUnreadCountUpdateTimer->Cancel();
else
mUnreadCountUpdateTimer = do_CreateInstance("@mozilla.org/timer;1");
mUnreadCountUpdateTimer->InitWithFuncCallback(OnUnreadCountUpdateTimer,
(void *)this, UNREAD_UPDATE_INTERVAL, nsITimer::TYPE_ONE_SHOT);
return NS_OK;
}
|