xref: /haiku/src/servers/mail/DefaultNotifier.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2011-2012, Haiku, Inc. All rights reserved.
3  * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include "DefaultNotifier.h"
10 
11 #include <Catalog.h>
12 #include <IconUtils.h>
13 #include <MailDaemon.h>
14 #include <Roster.h>
15 
16 #include <MailPrivate.h>
17 
18 
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "Notifier"
21 
22 
23 DefaultNotifier::DefaultNotifier(const char* accountName, bool inbound,
24 	ErrorLogWindow* errorWindow, uint32 showMode)
25 	:
26 	fAccountName(accountName),
27 	fIsInbound(inbound),
28 	fErrorWindow(errorWindow),
29 	fNotification(B_PROGRESS_NOTIFICATION),
30 	fShowMode(showMode),
31 	fTotalItems(0),
32 	fItemsDone(0),
33 	fTotalSize(0),
34 	fSizeDone(0)
35 {
36 	BString desc(fIsInbound ? B_TRANSLATE("Fetching mail for %name")
37 		: B_TRANSLATE("Sending mail for %name"));
38 	desc.ReplaceFirst("%name", fAccountName);
39 
40 	BString identifier;
41 	identifier << accountName << inbound;
42 		// Two windows for each acocunt : one for sending and the other for
43 		// receiving mails
44 	fNotification.SetMessageID(identifier);
45 	fNotification.SetGroup(B_TRANSLATE("Mail status"));
46 	fNotification.SetTitle(desc);
47 
48 	app_info info;
49 	be_roster->GetAppInfo(B_MAIL_DAEMON_SIGNATURE, &info);
50 	BBitmap icon(BRect(0, 0, 32, 32), B_RGBA32);
51 	BNode node(&info.ref);
52 	BIconUtils::GetVectorIcon(&node, "BEOS:ICON", &icon);
53 	fNotification.SetIcon(&icon);
54 }
55 
56 
57 DefaultNotifier::~DefaultNotifier()
58 {
59 }
60 
61 
62 BMailNotifier*
63 DefaultNotifier::Clone()
64 {
65 	return new DefaultNotifier(fAccountName, fIsInbound, fErrorWindow,
66 		fShowMode);
67 }
68 
69 
70 void
71 DefaultNotifier::ShowError(const char* error)
72 {
73 	fErrorWindow->AddError(B_WARNING_ALERT, error, fAccountName);
74 }
75 
76 
77 void
78 DefaultNotifier::ShowMessage(const char* message)
79 {
80 	fErrorWindow->AddError(B_INFO_ALERT, message, fAccountName);
81 }
82 
83 
84 void
85 DefaultNotifier::SetTotalItems(uint32 items)
86 {
87 	fTotalItems = items;
88 	BString progress;
89 	progress << fItemsDone << "/" << fTotalItems;
90 	fNotification.SetContent(progress);
91 }
92 
93 
94 void
95 DefaultNotifier::SetTotalItemsSize(uint64 size)
96 {
97 	fTotalSize = size;
98 	fNotification.SetProgress(fSizeDone / (float)fTotalSize);
99 }
100 
101 
102 void
103 DefaultNotifier::ReportProgress(uint32 messages, uint64 bytes,
104 	const char* message)
105 {
106 	fSizeDone += bytes;
107 	fItemsDone += messages;
108 
109 	if (fTotalSize > 0)
110 		fNotification.SetProgress(fSizeDone / (float)fTotalSize);
111 	else if (fTotalItems > 0) {
112 		// No size information available
113 		// Report progress in terms of message count instead
114 		fNotification.SetProgress(fItemsDone / (float)fTotalItems);
115 	} else {
116 		// No message count information either
117 		// TODO: we should use a B_INFORMATION_NOTIFICATION here, but it is not
118 		// possible to change the BNotification type after creating it...
119 		fNotification.SetProgress(0);
120 	}
121 
122 	BString progress;
123 	progress << message << "\t";
124 
125 	if (fTotalItems > 0)
126 		progress << fItemsDone << "/" << fTotalItems;
127 
128 	fNotification.SetContent(progress);
129 
130 	int timeout = 0; // Default timeout
131 	if (fItemsDone >= fTotalItems && fTotalItems != 0)
132 		timeout = 1; // We're done, make the window go away faster
133 
134 	_NotifyIfAllowed(timeout);
135 }
136 
137 
138 void
139 DefaultNotifier::ResetProgress(const char* message)
140 {
141 	fSizeDone = 0;
142 	fItemsDone = 0;
143 	fNotification.SetProgress(0);
144 	if (message != NULL)
145 		fNotification.SetTitle(message);
146 	_NotifyIfAllowed(1); // go away faster
147 }
148 
149 
150 void
151 DefaultNotifier::_NotifyIfAllowed(int timeout)
152 {
153 	int32 flag;
154 	if (fIsInbound)
155 		flag = B_MAIL_SHOW_STATUS_WINDOW_WHEN_ACTIVE;
156 	else
157 		flag = B_MAIL_SHOW_STATUS_WINDOW_WHEN_SENDING;
158 
159 	if ((fShowMode & flag) != 0)
160 		fNotification.Send(timeout);
161 }
162