xref: /haiku/src/apps/mail/Settings.cpp (revision a5bf12376daeded4049521eb17a6cc41192250d9)
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_TRANSLATE_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(true),
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 	fContentFont.SetSize(12.0);
83 	fSignature = B_TRANSLATE("None");
84 
85 	LoadSettings();
86 
87 	fContentFont.SetSpacing(B_BITMAP_SPACING);
88 
89 	_CheckForSpamFilterExistence();
90 }
91 
92 
93 Settings::~Settings()
94 {
95 }
96 
97 
98 void
99 Settings::SetPrintSettings(const BMessage* printSettings)
100 {
101 	BAutolock lock(be_app);
102 
103 	if (printSettings == fPrintSettings)
104 		return;
105 
106 	delete fPrintSettings;
107 	if (printSettings)
108 		fPrintSettings = new BMessage(*printSettings);
109 	else
110 		fPrintSettings = NULL;
111 }
112 
113 
114 bool
115 Settings::HasPrintSettings()
116 {
117 	BAutolock lock(be_app);
118 	return fPrintSettings != NULL;
119 }
120 
121 
122 BMessage
123 Settings::PrintSettings()
124 {
125 	BAutolock lock(be_app);
126 	return BMessage(*fPrintSettings);
127 }
128 
129 
130 void
131 Settings::ClearPrintSettings()
132 {
133 	delete fPrintSettings;
134 	fPrintSettings = NULL;
135 }
136 
137 
138 void
139 Settings::SetWindowFrame(BRect frame)
140 {
141 	BAutolock lock(be_app);
142 	fMailWindowFrame = frame;
143 }
144 
145 
146 BRect
147 Settings::WindowFrame()
148 {
149 	BAutolock lock(be_app);
150 	return fMailWindowFrame;
151 }
152 
153 
154 status_t
155 Settings::_GetSettingsPath(BPath &path)
156 {
157 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
158 	if (status != B_OK)
159 		return status;
160 
161 	path.Append("Mail");
162 	return create_directory(path.Path(), 0755);
163 }
164 
165 
166 status_t
167 Settings::SaveSettings()
168 {
169 	BMailSettings chainSettings;
170 
171 	if (fDefaultChain != ~0UL) {
172 		chainSettings.SetDefaultOutboundChainID(fDefaultChain);
173 		chainSettings.Save();
174 	}
175 
176 	BPath path;
177 	status_t status = _GetSettingsPath(path);
178 	if (status != B_OK)
179 		return status;
180 
181 	path.Append("Mail Settings~");
182 
183 	BFile file;
184 	status = file.SetTo(path.Path(),
185 		B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
186 
187 	if (status != B_OK)
188 		return status;
189 
190 	BMessage settings('BeMl');
191 	settings.AddRect("MailWindowSize", fMailWindowFrame);
192 
193 	font_family fontFamily;
194 	font_style fontStyle;
195 	fContentFont.GetFamilyAndStyle(&fontFamily, &fontStyle);
196 
197 	settings.AddString("FontFamily", fontFamily);
198 	settings.AddString("FontStyle", fontStyle);
199 	settings.AddFloat("FontSize", fContentFont.Size());
200 
201 	settings.AddBool("WordWrapMode", fWrapMode);
202 	settings.AddBool("AutoMarkRead", fAutoMarkRead);
203 	settings.AddString("SignatureText", fSignature);
204 	settings.AddInt32("CharacterSet", fMailCharacterSet);
205 	settings.AddInt8("ShowButtonBar", fShowButtonBar);
206 	settings.AddInt32("UseAccountFrom", fUseAccountFrom);
207 	settings.AddBool("ColoredQuotes", fColoredQuotes);
208 	settings.AddString("ReplyPreamble", fReplyPreamble);
209 	settings.AddBool("AttachAttributes", fAttachAttributes);
210 	settings.AddBool("WarnAboutUnencodableCharacters",
211 		fWarnAboutUnencodableCharacters);
212 	settings.AddBool("StartWithSpellCheck", fStartWithSpellCheckOn);
213 
214 	BEntry entry;
215 	status = entry.SetTo(path.Path());
216 	if (status != B_OK)
217 		return status;
218 
219 	status = settings.Flatten(&file);
220 	if (status == B_OK) {
221 		// replace original settings file
222 		status = entry.Rename("Mail Settings", true);
223 	} else
224 		entry.Remove();
225 
226 	return status;
227 }
228 
229 
230 status_t
231 Settings::LoadSettings()
232 {
233 	BMailSettings chainSettings;
234 	fDefaultChain = chainSettings.DefaultOutboundChainID();
235 
236 	BPath path;
237 	status_t status = _GetSettingsPath(path);
238 	if (status != B_OK)
239 		return status;
240 
241 	path.Append("Mail Settings");
242 
243 	BFile file;
244 	status = file.SetTo(path.Path(), B_READ_ONLY);
245 	if (status != B_OK) {
246 		_GetSettingsPath(path);
247 		path.Append("BeMail Settings");
248 		status = file.SetTo(path.Path(), B_READ_ONLY);
249 		if (status != B_OK)
250 			return status;
251 	}
252 
253 	BMessage settings;
254 	status = settings.Unflatten(&file);
255 	if (status < B_OK || settings.what != 'BeMl')
256 		return status;
257 
258 	BRect rect;
259 	if (settings.FindRect("MailWindowSize", &rect) == B_OK)
260 		fMailWindowFrame = rect;
261 
262 	int32 int32Value;
263 
264 	const char *fontFamily;
265 	if (settings.FindString("FontFamily", &fontFamily) == B_OK) {
266 		const char *fontStyle;
267 		if (settings.FindString("FontStyle", &fontStyle) == B_OK) {
268 			float size;
269 			if (settings.FindFloat("FontSize", &size) == B_OK) {
270 				if (size >= 7)
271 					fContentFont.SetSize(size);
272 
273 				if (fontFamily[0] && fontStyle[0]) {
274 					fContentFont.SetFamilyAndStyle(
275 						fontFamily[0] ? fontFamily : NULL,
276 						fontStyle[0] ? fontStyle : NULL);
277 				}
278 			}
279 		}
280 	}
281 
282 	bool boolValue;
283 	if (settings.FindBool("WordWrapMode", &boolValue) == B_OK)
284 		fWrapMode = boolValue;
285 
286 	if (settings.FindBool("AutoMarkRead", &boolValue) == B_OK)
287 		fAutoMarkRead = boolValue;
288 
289 	BString string;
290 	if (settings.FindString("SignatureText", &string) == B_OK) {
291 		fSignature = string;
292 	}
293 
294 	if (settings.FindInt32("CharacterSet", &int32Value) == B_OK)
295 		fMailCharacterSet = int32Value;
296 	if (fMailCharacterSet != B_MAIL_UTF8_CONVERSION
297 		&& fMailCharacterSet != B_MAIL_US_ASCII_CONVERSION
298 		&& BCharacterSetRoster::GetCharacterSetByConversionID(fMailCharacterSet)
299 			== NULL) {
300 		fMailCharacterSet = B_MS_WINDOWS_CONVERSION;
301 	}
302 
303 	int8 int8Value;
304 	if (settings.FindInt8("ShowButtonBar", &int8Value) == B_OK)
305 		fShowButtonBar = int8Value;
306 
307 	if (settings.FindInt32("UseAccountFrom", &int32Value) == B_OK)
308 		fUseAccountFrom = int32Value;
309 	if (fUseAccountFrom < ACCOUNT_USE_DEFAULT
310 		|| fUseAccountFrom > ACCOUNT_FROM_MAIL)
311 		fUseAccountFrom = ACCOUNT_USE_DEFAULT;
312 
313 	if (settings.FindBool("ColoredQuotes", &boolValue) == B_OK)
314 		fColoredQuotes = boolValue;
315 
316 	if (settings.FindString("ReplyPreamble", &string) == B_OK) {
317 		fReplyPreamble = string;
318 	}
319 
320 	if (settings.FindBool("AttachAttributes", &boolValue) == B_OK)
321 		fAttachAttributes = boolValue;
322 
323 	if (settings.FindBool("WarnAboutUnencodableCharacters", &boolValue) == B_OK)
324 		fWarnAboutUnencodableCharacters = boolValue;
325 
326 	if (settings.FindBool("StartWithSpellCheck", &boolValue) == B_OK)
327 		fStartWithSpellCheckOn = boolValue;
328 
329 	return B_OK;
330 }
331 
332 
333 bool
334 Settings::AutoMarkRead()
335 {
336 	BAutolock lock(be_app);
337 	return fAutoMarkRead;
338 }
339 
340 
341 BString
342 Settings::Signature()
343 {
344 	BAutolock lock(be_app);
345 	return BString(fSignature);
346 }
347 
348 
349 BString
350 Settings::ReplyPreamble()
351 {
352 	BAutolock lock(be_app);
353 	return BString(fReplyPreamble);
354 }
355 
356 
357 bool
358 Settings::WrapMode()
359 {
360 	BAutolock lock(be_app);
361 	return fWrapMode;
362 }
363 
364 
365 bool
366 Settings::AttachAttributes()
367 {
368 	BAutolock lock(be_app);
369 	return fAttachAttributes;
370 }
371 
372 
373 bool
374 Settings::ColoredQuotes()
375 {
376 	BAutolock lock(be_app);
377 	return fColoredQuotes;
378 }
379 
380 
381 uint8
382 Settings::ShowButtonBar()
383 {
384 	BAutolock lock(be_app);
385 	return fShowButtonBar;
386 }
387 
388 
389 bool
390 Settings::WarnAboutUnencodableCharacters()
391 {
392 	BAutolock lock(be_app);
393 	return fWarnAboutUnencodableCharacters;
394 }
395 
396 
397 bool
398 Settings::StartWithSpellCheckOn()
399 {
400 	BAutolock lock(be_app);
401 	return fStartWithSpellCheckOn;
402 }
403 
404 
405 void
406 Settings::SetDefaultChain(uint32 chain)
407 {
408 	BAutolock lock(be_app);
409 	fDefaultChain = chain;
410 }
411 
412 
413 uint32
414 Settings::DefaultChain()
415 {
416 	BAutolock lock(be_app);
417 	return fDefaultChain;
418 }
419 
420 
421 int32
422 Settings::UseAccountFrom()
423 {
424 	BAutolock lock(be_app);
425 	return fUseAccountFrom;
426 }
427 
428 
429 uint32
430 Settings::MailCharacterSet()
431 {
432 	BAutolock lock(be_app);
433 	return fMailCharacterSet;
434 }
435 
436 
437 BFont
438 Settings::ContentFont()
439 {
440 	BAutolock lock(be_app);
441 	return fContentFont;
442 }
443 
444 
445 void
446 Settings::_CheckForSpamFilterExistence()
447 {
448 	int32 addonNameIndex;
449 	const char* addonNamePntr;
450 	BDirectory inChainDir;
451 	BPath path;
452 	BEntry settingsEntry;
453 	BFile settingsFile;
454 	BMessage settingsMessage;
455 
456 	fShowSpamGUI = false;
457 
458 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
459 		return;
460 	path.Append("Mail/chains/inbound");
461 	if (inChainDir.SetTo(path.Path()) != B_OK)
462 		return;
463 
464 	while (inChainDir.GetNextEntry (&settingsEntry, true) == B_OK) {
465 		if (!settingsEntry.IsFile())
466 			continue;
467 		if (settingsFile.SetTo (&settingsEntry, B_READ_ONLY) != B_OK)
468 			continue;
469 		if (settingsMessage.Unflatten (&settingsFile) != B_OK)
470 			continue;
471 		for (addonNameIndex = 0;
472 				B_OK == settingsMessage.FindString("filter_addons",
473 					addonNameIndex, &addonNamePntr);
474 				addonNameIndex++) {
475 			if (strstr(addonNamePntr, "Spam Filter") != NULL) {
476 				fShowSpamGUI = true;
477 				return;
478 			}
479 		}
480 	}
481 }
482 
483