xref: /haiku/src/apps/mail/Settings.cpp (revision 9642f7705b27e5c270c15fa526d14e1848c2c27d)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or
30 registered trademarks of Be Incorporated in the United States and other
31 countries. Other brand product names are registered trademarks or trademarks
32 of their respective holders. All rights reserved.
33 */
34 
35 
36 #include "Settings.h"
37 
38 #include <Application.h>
39 #include <Autolock.h>
40 #include <Catalog.h>
41 #include <CharacterSet.h>
42 #include <CharacterSetRoster.h>
43 #include <Directory.h>
44 #include <Entry.h>
45 #include <File.h>
46 #include <FindDirectory.h>
47 #include <Font.h>
48 #include <Locale.h>
49 #include <MailSettings.h>
50 #include <Message.h>
51 #include <String.h>
52 #include <UTF8.h>
53 
54 #include <mail_encoding.h>
55 
56 
57 #define B_TRANSLATION_CONTEXT "Settings"
58 
59 
60 using namespace BPrivate ;
61 
62 
63 Settings::Settings()
64 	:
65 	fMailWindowFrame(BRect(0, 0, 200, 400)),
66 	fPrintSettings(NULL),
67 	fAutoMarkRead(true),
68 	fSignature(),
69 	fReplyPreamble(),
70 	fWrapMode(true),
71 	fAttachAttributes(true),
72 	fColoredQuotes(true),
73 	fShowButtonBar(kShowToolBar),
74 	fWarnAboutUnencodableCharacters(true),
75 	fStartWithSpellCheckOn(false),
76 	fShowSpamGUI(true),
77 	fDefaultChain(0),
78 	fUseAccountFrom(0),
79 	fMailCharacterSet(B_MS_WINDOWS_CONVERSION),
80 	fContentFont(be_fixed_font)
81 {
82 	fSignature = B_TRANSLATE("None");
83 
84 	LoadSettings();
85 
86 	fContentFont.SetSpacing(B_BITMAP_SPACING);
87 
88 	_CheckForSpamFilterExistence();
89 }
90 
91 
92 Settings::~Settings()
93 {
94 }
95 
96 
97 void
98 Settings::SetPrintSettings(const BMessage* printSettings)
99 {
100 	BAutolock lock(be_app);
101 
102 	if (printSettings == fPrintSettings)
103 		return;
104 
105 	delete fPrintSettings;
106 	if (printSettings)
107 		fPrintSettings = new BMessage(*printSettings);
108 	else
109 		fPrintSettings = NULL;
110 }
111 
112 
113 bool
114 Settings::HasPrintSettings()
115 {
116 	BAutolock lock(be_app);
117 	return fPrintSettings != NULL;
118 }
119 
120 
121 BMessage
122 Settings::PrintSettings()
123 {
124 	BAutolock lock(be_app);
125 	return BMessage(*fPrintSettings);
126 }
127 
128 
129 void
130 Settings::ClearPrintSettings()
131 {
132 	delete fPrintSettings;
133 	fPrintSettings = NULL;
134 }
135 
136 
137 void
138 Settings::SetWindowFrame(BRect frame)
139 {
140 	BAutolock lock(be_app);
141 	fMailWindowFrame = frame;
142 }
143 
144 
145 BRect
146 Settings::WindowFrame()
147 {
148 	BAutolock lock(be_app);
149 	return fMailWindowFrame;
150 }
151 
152 
153 status_t
154 Settings::_GetSettingsPath(BPath &path)
155 {
156 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
157 	if (status != B_OK)
158 		return status;
159 
160 	path.Append("Mail");
161 	return create_directory(path.Path(), 0755);
162 }
163 
164 
165 status_t
166 Settings::SaveSettings()
167 {
168 	BMailSettings chainSettings;
169 
170 	if (fDefaultChain != ~0UL) {
171 		chainSettings.SetDefaultOutboundChainID(fDefaultChain);
172 		chainSettings.Save();
173 	}
174 
175 	BPath path;
176 	status_t status = _GetSettingsPath(path);
177 	if (status != B_OK)
178 		return status;
179 
180 	path.Append("Mail Settings~");
181 
182 	BFile file;
183 	status = file.SetTo(path.Path(),
184 		B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
185 
186 	if (status != B_OK)
187 		return status;
188 
189 	BMessage settings('BeMl');
190 	settings.AddRect("MailWindowSize", fMailWindowFrame);
191 
192 	font_family fontFamily;
193 	font_style fontStyle;
194 	fContentFont.GetFamilyAndStyle(&fontFamily, &fontStyle);
195 
196 	settings.AddString("FontFamily", fontFamily);
197 	settings.AddString("FontStyle", fontStyle);
198 	settings.AddFloat("FontSize", fContentFont.Size());
199 
200 	settings.AddBool("WordWrapMode", fWrapMode);
201 	settings.AddBool("AutoMarkRead", fAutoMarkRead);
202 	settings.AddString("SignatureText", fSignature);
203 	settings.AddInt32("CharacterSet", fMailCharacterSet);
204 	settings.AddInt8("ShowButtonBar", fShowButtonBar);
205 	settings.AddInt32("UseAccountFrom", fUseAccountFrom);
206 	settings.AddBool("ColoredQuotes", fColoredQuotes);
207 	settings.AddString("ReplyPreamble", fReplyPreamble);
208 	settings.AddBool("AttachAttributes", fAttachAttributes);
209 	settings.AddBool("WarnAboutUnencodableCharacters",
210 		fWarnAboutUnencodableCharacters);
211 	settings.AddBool("StartWithSpellCheck", fStartWithSpellCheckOn);
212 
213 	BEntry entry;
214 	status = entry.SetTo(path.Path());
215 	if (status != B_OK)
216 		return status;
217 
218 	status = settings.Flatten(&file);
219 	if (status == B_OK) {
220 		// replace original settings file
221 		status = entry.Rename("Mail Settings", true);
222 	} else
223 		entry.Remove();
224 
225 	return status;
226 }
227 
228 
229 status_t
230 Settings::LoadSettings()
231 {
232 	BMailSettings chainSettings;
233 	fDefaultChain = chainSettings.DefaultOutboundChainID();
234 
235 	BPath path;
236 	status_t status = _GetSettingsPath(path);
237 	if (status != B_OK)
238 		return status;
239 
240 	path.Append("Mail Settings");
241 
242 	BFile file;
243 	status = file.SetTo(path.Path(), B_READ_ONLY);
244 	if (status != B_OK) {
245 		_GetSettingsPath(path);
246 		path.Append("BeMail Settings");
247 		status = file.SetTo(path.Path(), B_READ_ONLY);
248 		if (status != B_OK)
249 			return status;
250 	}
251 
252 	BMessage settings;
253 	status = settings.Unflatten(&file);
254 	if (status < B_OK || settings.what != 'BeMl')
255 		return status;
256 
257 	BRect rect;
258 	if (settings.FindRect("MailWindowSize", &rect) == B_OK)
259 		fMailWindowFrame = rect;
260 
261 	int32 int32Value;
262 
263 	const char *fontFamily;
264 	if (settings.FindString("FontFamily", &fontFamily) == B_OK) {
265 		const char *fontStyle;
266 		if (settings.FindString("FontStyle", &fontStyle) == B_OK) {
267 			float size;
268 			if (settings.FindFloat("FontSize", &size) == B_OK) {
269 				if (size >= 7)
270 					fContentFont.SetSize(size);
271 
272 				if (fontFamily[0] && fontStyle[0]) {
273 					fContentFont.SetFamilyAndStyle(
274 						fontFamily[0] ? fontFamily : NULL,
275 						fontStyle[0] ? fontStyle : NULL);
276 				}
277 			}
278 		}
279 	}
280 
281 	fWrapMode = settings.GetBool("WordWrapMode", fWrapMode);
282 	fAutoMarkRead = settings.GetBool("AutoMarkRead", fAutoMarkRead);
283 
284 	BString string;
285 	if (settings.FindString("SignatureText", &string) == B_OK)
286 		fSignature = string;
287 
288 	if (settings.FindInt32("CharacterSet", &int32Value) == B_OK)
289 		fMailCharacterSet = int32Value;
290 	if (fMailCharacterSet != B_MAIL_UTF8_CONVERSION
291 		&& fMailCharacterSet != B_MAIL_US_ASCII_CONVERSION
292 		&& BCharacterSetRoster::GetCharacterSetByConversionID(fMailCharacterSet)
293 			== NULL) {
294 		fMailCharacterSet = B_MS_WINDOWS_CONVERSION;
295 	}
296 
297 	fShowButtonBar = settings.GetInt8("ShowButtonBar", fShowButtonBar);
298 
299 	if (settings.FindInt32("UseAccountFrom", &int32Value) == B_OK)
300 		fUseAccountFrom = int32Value;
301 	if (fUseAccountFrom < ACCOUNT_USE_DEFAULT
302 		|| fUseAccountFrom > ACCOUNT_FROM_MAIL)
303 		fUseAccountFrom = ACCOUNT_USE_DEFAULT;
304 
305 	fColoredQuotes = settings.GetBool("ColoredQuotes", fColoredQuotes);
306 
307 	if (settings.FindString("ReplyPreamble", &string) == B_OK)
308 		fReplyPreamble = string;
309 
310 	fAttachAttributes = settings.GetBool("AttachAttributes", fAttachAttributes);
311 	fWarnAboutUnencodableCharacters = settings.GetBool(
312 		"WarnAboutUnencodableCharacters", fWarnAboutUnencodableCharacters);
313 	fStartWithSpellCheckOn = settings.GetBool("StartWithSpellCheck",
314 		fStartWithSpellCheckOn);
315 
316 	return B_OK;
317 }
318 
319 
320 bool
321 Settings::AutoMarkRead()
322 {
323 	BAutolock lock(be_app);
324 	return fAutoMarkRead;
325 }
326 
327 
328 BString
329 Settings::Signature()
330 {
331 	BAutolock lock(be_app);
332 	return BString(fSignature);
333 }
334 
335 
336 BString
337 Settings::ReplyPreamble()
338 {
339 	BAutolock lock(be_app);
340 	return BString(fReplyPreamble);
341 }
342 
343 
344 bool
345 Settings::WrapMode()
346 {
347 	BAutolock lock(be_app);
348 	return fWrapMode;
349 }
350 
351 
352 bool
353 Settings::AttachAttributes()
354 {
355 	BAutolock lock(be_app);
356 	return fAttachAttributes;
357 }
358 
359 
360 bool
361 Settings::ColoredQuotes()
362 {
363 	BAutolock lock(be_app);
364 	return fColoredQuotes;
365 }
366 
367 
368 uint8
369 Settings::ShowButtonBar()
370 {
371 	BAutolock lock(be_app);
372 	return fShowButtonBar;
373 }
374 
375 
376 bool
377 Settings::WarnAboutUnencodableCharacters()
378 {
379 	BAutolock lock(be_app);
380 	return fWarnAboutUnencodableCharacters;
381 }
382 
383 
384 bool
385 Settings::StartWithSpellCheckOn()
386 {
387 	BAutolock lock(be_app);
388 	return fStartWithSpellCheckOn;
389 }
390 
391 
392 void
393 Settings::SetDefaultChain(uint32 chain)
394 {
395 	BAutolock lock(be_app);
396 	fDefaultChain = chain;
397 }
398 
399 
400 uint32
401 Settings::DefaultChain()
402 {
403 	BAutolock lock(be_app);
404 	return fDefaultChain;
405 }
406 
407 
408 int32
409 Settings::UseAccountFrom()
410 {
411 	BAutolock lock(be_app);
412 	return fUseAccountFrom;
413 }
414 
415 
416 uint32
417 Settings::MailCharacterSet()
418 {
419 	BAutolock lock(be_app);
420 	return fMailCharacterSet;
421 }
422 
423 
424 BFont
425 Settings::ContentFont()
426 {
427 	BAutolock lock(be_app);
428 	return fContentFont;
429 }
430 
431 
432 void
433 Settings::_CheckForSpamFilterExistence()
434 {
435 	int32 addonNameIndex;
436 	const char* addonNamePntr;
437 	BDirectory inChainDir;
438 	BPath path;
439 	BEntry settingsEntry;
440 	BFile settingsFile;
441 	BMessage settingsMessage;
442 
443 	fShowSpamGUI = false;
444 
445 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
446 		return;
447 	path.Append("Mail/chains/inbound");
448 	if (inChainDir.SetTo(path.Path()) != B_OK)
449 		return;
450 
451 	while (inChainDir.GetNextEntry (&settingsEntry, true) == B_OK) {
452 		if (!settingsEntry.IsFile())
453 			continue;
454 		if (settingsFile.SetTo (&settingsEntry, B_READ_ONLY) != B_OK)
455 			continue;
456 		if (settingsMessage.Unflatten (&settingsFile) != B_OK)
457 			continue;
458 		for (addonNameIndex = 0;
459 				B_OK == settingsMessage.FindString("filter_addons",
460 					addonNameIndex, &addonNamePntr);
461 				addonNameIndex++) {
462 			if (strstr(addonNamePntr, "Spam Filter") != NULL) {
463 				fShowSpamGUI = true;
464 				return;
465 			}
466 		}
467 	}
468 }
469 
470