libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / apps / mail / Settings.cpp
blob2c90230348223aaffa90edd6c59e433bae51da65
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
36 #include "Settings.h"
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>
54 #include <mail_encoding.h>
57 #define B_TRANSLATION_CONTEXT "Settings"
60 using namespace BPrivate ;
63 Settings::Settings()
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)
82 fSignature = B_TRANSLATE("None");
84 LoadSettings();
86 fContentFont.SetSpacing(B_BITMAP_SPACING);
88 _CheckForSpamFilterExistence();
92 Settings::~Settings()
97 void
98 Settings::SetPrintSettings(const BMessage* printSettings)
100 BAutolock lock(be_app);
102 if (printSettings == fPrintSettings)
103 return;
105 delete fPrintSettings;
106 if (printSettings)
107 fPrintSettings = new BMessage(*printSettings);
108 else
109 fPrintSettings = NULL;
113 bool
114 Settings::HasPrintSettings()
116 BAutolock lock(be_app);
117 return fPrintSettings != NULL;
121 BMessage
122 Settings::PrintSettings()
124 BAutolock lock(be_app);
125 return BMessage(*fPrintSettings);
129 void
130 Settings::ClearPrintSettings()
132 delete fPrintSettings;
133 fPrintSettings = NULL;
137 void
138 Settings::SetWindowFrame(BRect frame)
140 BAutolock lock(be_app);
141 fMailWindowFrame = frame;
145 BRect
146 Settings::WindowFrame()
148 BAutolock lock(be_app);
149 return fMailWindowFrame;
153 status_t
154 Settings::_GetSettingsPath(BPath &path)
156 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
157 if (status != B_OK)
158 return status;
160 path.Append("Mail");
161 return create_directory(path.Path(), 0755);
165 status_t
166 Settings::SaveSettings()
168 BMailSettings chainSettings;
170 if (fDefaultChain != ~0UL) {
171 chainSettings.SetDefaultOutboundChainID(fDefaultChain);
172 chainSettings.Save();
175 BPath path;
176 status_t status = _GetSettingsPath(path);
177 if (status != B_OK)
178 return status;
180 path.Append("Mail Settings~");
182 BFile file;
183 status = file.SetTo(path.Path(),
184 B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
186 if (status != B_OK)
187 return status;
189 BMessage settings('BeMl');
190 settings.AddRect("MailWindowSize", fMailWindowFrame);
192 font_family fontFamily;
193 font_style fontStyle;
194 fContentFont.GetFamilyAndStyle(&fontFamily, &fontStyle);
196 settings.AddString("FontFamily", fontFamily);
197 settings.AddString("FontStyle", fontStyle);
198 settings.AddFloat("FontSize", fContentFont.Size());
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);
213 BEntry entry;
214 status = entry.SetTo(path.Path());
215 if (status != B_OK)
216 return status;
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();
225 return status;
229 status_t
230 Settings::LoadSettings()
232 BMailSettings chainSettings;
233 fDefaultChain = chainSettings.DefaultOutboundChainID();
235 BPath path;
236 status_t status = _GetSettingsPath(path);
237 if (status != B_OK)
238 return status;
240 path.Append("Mail Settings");
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;
252 BMessage settings;
253 status = settings.Unflatten(&file);
254 if (status < B_OK || settings.what != 'BeMl')
255 return status;
257 BRect rect;
258 if (settings.FindRect("MailWindowSize", &rect) == B_OK)
259 fMailWindowFrame = rect;
261 int32 int32Value;
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);
272 if (fontFamily[0] && fontStyle[0]) {
273 fContentFont.SetFamilyAndStyle(
274 fontFamily[0] ? fontFamily : NULL,
275 fontStyle[0] ? fontStyle : NULL);
281 fWrapMode = settings.GetBool("WordWrapMode", fWrapMode);
282 fAutoMarkRead = settings.GetBool("AutoMarkRead", fAutoMarkRead);
284 BString string;
285 if (settings.FindString("SignatureText", &string) == B_OK)
286 fSignature = string;
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;
297 fShowButtonBar = settings.GetInt8("ShowButtonBar", fShowButtonBar);
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;
305 fColoredQuotes = settings.GetBool("ColoredQuotes", fColoredQuotes);
307 if (settings.FindString("ReplyPreamble", &string) == B_OK)
308 fReplyPreamble = string;
310 fAttachAttributes = settings.GetBool("AttachAttributes", fAttachAttributes);
311 fWarnAboutUnencodableCharacters = settings.GetBool(
312 "WarnAboutUnencodableCharacters", fWarnAboutUnencodableCharacters);
313 fStartWithSpellCheckOn = settings.GetBool("StartWithSpellCheck",
314 fStartWithSpellCheckOn);
316 return B_OK;
320 bool
321 Settings::AutoMarkRead()
323 BAutolock lock(be_app);
324 return fAutoMarkRead;
328 BString
329 Settings::Signature()
331 BAutolock lock(be_app);
332 return BString(fSignature);
336 BString
337 Settings::ReplyPreamble()
339 BAutolock lock(be_app);
340 return BString(fReplyPreamble);
344 bool
345 Settings::WrapMode()
347 BAutolock lock(be_app);
348 return fWrapMode;
352 bool
353 Settings::AttachAttributes()
355 BAutolock lock(be_app);
356 return fAttachAttributes;
360 bool
361 Settings::ColoredQuotes()
363 BAutolock lock(be_app);
364 return fColoredQuotes;
368 uint8
369 Settings::ShowButtonBar()
371 BAutolock lock(be_app);
372 return fShowButtonBar;
376 bool
377 Settings::WarnAboutUnencodableCharacters()
379 BAutolock lock(be_app);
380 return fWarnAboutUnencodableCharacters;
384 bool
385 Settings::StartWithSpellCheckOn()
387 BAutolock lock(be_app);
388 return fStartWithSpellCheckOn;
392 void
393 Settings::SetDefaultChain(uint32 chain)
395 BAutolock lock(be_app);
396 fDefaultChain = chain;
400 uint32
401 Settings::DefaultChain()
403 BAutolock lock(be_app);
404 return fDefaultChain;
408 int32
409 Settings::UseAccountFrom()
411 BAutolock lock(be_app);
412 return fUseAccountFrom;
416 uint32
417 Settings::MailCharacterSet()
419 BAutolock lock(be_app);
420 return fMailCharacterSet;
424 BFont
425 Settings::ContentFont()
427 BAutolock lock(be_app);
428 return fContentFont;
432 void
433 Settings::_CheckForSpamFilterExistence()
435 int32 addonNameIndex;
436 const char* addonNamePntr;
437 BDirectory inChainDir;
438 BPath path;
439 BEntry settingsEntry;
440 BFile settingsFile;
441 BMessage settingsMessage;
443 fShowSpamGUI = false;
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;
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;