2 * Copyright 2004-2015, Haiku Inc. All rights reserved.
3 * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved.
5 * Distributed under the terms of the MIT License.
9 //! The mail daemon's settings
12 #include <MailSettings.h>
18 #include <Directory.h>
21 #include <FindDirectory.h>
22 #include <MailDaemon.h>
24 #include <Messenger.h>
29 #include <MailPrivate.h>
32 // #pragma mark - BMailSettings
35 BMailSettings::BMailSettings()
41 BMailSettings::~BMailSettings()
47 BMailSettings::InitCheck() const
57 status_t status
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
59 fprintf(stderr
, "Couldn't find user settings directory: %s\n",
66 status
= BPrivate::WriteMessageFile(fData
, path
, "new_mail_daemon");
70 BMessenger(B_MAIL_DAEMON_SIGNATURE
).SendMessage(
71 BPrivate::kMsgSettingsUpdated
);
77 BMailSettings::Reload()
79 // Try directories from most specific to least
80 directory_which which
[] = {
81 B_USER_SETTINGS_DIRECTORY
,
82 B_SYSTEM_SETTINGS_DIRECTORY
};
83 status_t status
= B_ENTRY_NOT_FOUND
;
85 for (size_t i
= 0; i
< sizeof(which
) / sizeof(which
[0]); i
++) {
87 status
= find_directory(which
[i
], &path
);
91 path
.Append("Mail/new_mail_daemon");
93 status
= file
.SetTo(path
.Path(), B_READ_ONLY
);
99 status
= settings
.Unflatten(&file
);
100 if (status
!= B_OK
) {
101 fprintf(stderr
, "Couldn't read settings from '%s': %s\n",
102 path
.Path(), strerror(status
));
106 // clobber old settings
115 // # pragma mark - Global settings
119 BMailSettings::WindowFollowsCorner()
121 return fData
.FindInt32("WindowFollowsCorner");
126 BMailSettings::SetWindowFollowsCorner(int32 whichCorner
)
128 if (fData
.ReplaceInt32("WindowFollowsCorner", whichCorner
) != B_OK
)
129 fData
.AddInt32("WindowFollowsCorner", whichCorner
);
134 BMailSettings::ShowStatusWindow()
136 int32 showStatusWindow
;
137 if (fData
.FindInt32("ShowStatusWindow", &showStatusWindow
) != B_OK
) {
138 // show during send and receive
142 return showStatusWindow
;
147 BMailSettings::SetShowStatusWindow(uint32 mode
)
149 if (fData
.ReplaceInt32("ShowStatusWindow", mode
) != B_OK
)
150 fData
.AddInt32("ShowStatusWindow", mode
);
155 BMailSettings::DaemonAutoStarts()
157 return fData
.FindBool("DaemonAutoStarts");
162 BMailSettings::SetDaemonAutoStarts(bool startIt
)
164 if (fData
.ReplaceBool("DaemonAutoStarts", startIt
) != B_OK
)
165 fData
.AddBool("DaemonAutoStarts", startIt
);
170 BMailSettings::ConfigWindowFrame()
172 return fData
.FindRect("ConfigWindowFrame");
177 BMailSettings::SetConfigWindowFrame(BRect frame
)
179 if (fData
.ReplaceRect("ConfigWindowFrame", frame
) != B_OK
)
180 fData
.AddRect("ConfigWindowFrame", frame
);
185 BMailSettings::StatusWindowFrame()
188 if (fData
.FindRect("StatusWindowFrame", &frame
) != B_OK
)
189 return BRect(100, 100, 200, 120);
196 BMailSettings::SetStatusWindowFrame(BRect frame
)
198 if (fData
.ReplaceRect("StatusWindowFrame", frame
) != B_OK
)
199 fData
.AddRect("StatusWindowFrame", frame
);
204 BMailSettings::StatusWindowWorkspaces()
207 if (fData
.FindInt32("StatusWindowWorkSpace", (int32
*)&workspaces
) != B_OK
)
208 return B_ALL_WORKSPACES
;
215 BMailSettings::SetStatusWindowWorkspaces(int32 workspace
)
217 if (fData
.ReplaceInt32("StatusWindowWorkSpace", workspace
) != B_OK
)
218 fData
.AddInt32("StatusWindowWorkSpace", workspace
);
220 BMessage
msg('wsch');
221 msg
.AddInt32("StatusWindowWorkSpace",workspace
);
222 BMessenger(B_MAIL_DAEMON_SIGNATURE
).SendMessage(&msg
);
227 BMailSettings::StatusWindowLook()
229 return fData
.FindInt32("StatusWindowLook");
234 BMailSettings::SetStatusWindowLook(int32 look
)
236 if (fData
.ReplaceInt32("StatusWindowLook", look
) != B_OK
)
237 fData
.AddInt32("StatusWindowLook", look
);
239 BMessage
msg('lkch');
240 msg
.AddInt32("StatusWindowLook", look
);
241 BMessenger(B_MAIL_DAEMON_SIGNATURE
).SendMessage(&msg
);
246 BMailSettings::AutoCheckInterval()
249 if (fData
.FindInt64("AutoCheckInterval", &value
) != B_OK
) {
251 return 5 * 60 * 1000 * 1000;
258 BMailSettings::SetAutoCheckInterval(bigtime_t interval
)
260 if (fData
.ReplaceInt64("AutoCheckInterval", interval
) != B_OK
)
261 fData
.AddInt64("AutoCheckInterval", interval
);
266 BMailSettings::CheckOnlyIfPPPUp()
268 return fData
.FindBool("CheckOnlyIfPPPUp");
273 BMailSettings::SetCheckOnlyIfPPPUp(bool yes
)
275 if (fData
.ReplaceBool("CheckOnlyIfPPPUp", yes
))
276 fData
.AddBool("CheckOnlyIfPPPUp", yes
);
281 BMailSettings::SendOnlyIfPPPUp()
283 return fData
.FindBool("SendOnlyIfPPPUp");
288 BMailSettings::SetSendOnlyIfPPPUp(bool yes
)
290 if (fData
.ReplaceBool("SendOnlyIfPPPUp", yes
))
291 fData
.AddBool("SendOnlyIfPPPUp", yes
);
296 BMailSettings::DefaultOutboundAccount()
298 return fData
.FindInt32("DefaultOutboundAccount");
303 BMailSettings::SetDefaultOutboundAccount(int32 to
)
305 if (fData
.ReplaceInt32("DefaultOutboundAccount", to
) != B_OK
)
306 fData
.AddInt32("DefaultOutboundAccount", to
);
313 BMailAccounts::BMailAccounts()
316 status_t status
= AccountsPath(path
);
320 BDirectory
dir(path
.Path());
321 if (dir
.InitCheck() != B_OK
)
324 std::vector
<time_t> creationTimeList
;
326 while (dir
.GetNextEntry(&entry
) != B_ENTRY_NOT_FOUND
) {
329 if (node
.GetCreationTime(&creationTime
) != B_OK
)
332 BMailAccountSettings
* account
= new BMailAccountSettings(entry
);
333 if (account
->InitCheck() != B_OK
) {
338 // sort by creation time
339 int insertIndex
= -1;
340 for (unsigned int i
= 0; i
< creationTimeList
.size(); i
++) {
341 if (creationTimeList
[i
] > creationTime
) {
346 if (insertIndex
< 0) {
347 fAccounts
.AddItem(account
);
348 creationTimeList
.push_back(creationTime
);
350 fAccounts
.AddItem(account
, insertIndex
);
351 creationTimeList
.insert(creationTimeList
.begin() + insertIndex
,
359 BMailAccounts::AccountsPath(BPath
& path
)
361 status_t status
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
364 return path
.Append("Mail/accounts");
368 BMailAccounts::~BMailAccounts()
370 for (int i
= 0; i
< fAccounts
.CountItems(); i
++)
371 delete fAccounts
.ItemAt(i
);
376 BMailAccounts::CountAccounts()
378 return fAccounts
.CountItems();
382 BMailAccountSettings
*
383 BMailAccounts::AccountAt(int32 index
)
385 return fAccounts
.ItemAt(index
);
389 BMailAccountSettings
*
390 BMailAccounts::AccountByID(int32 id
)
392 for (int i
= 0; i
< fAccounts
.CountItems(); i
++) {
393 BMailAccountSettings
* account
= fAccounts
.ItemAt(i
);
394 if (account
->AccountID() == id
)
401 BMailAccountSettings
*
402 BMailAccounts::AccountByName(const char* name
)
404 for (int i
= 0; i
< fAccounts
.CountItems(); i
++) {
405 BMailAccountSettings
* account
= fAccounts
.ItemAt(i
);
406 if (strcmp(account
->Name(), name
) == 0)
416 BMailAddOnSettings::BMailAddOnSettings()
421 BMailAddOnSettings::~BMailAddOnSettings()
427 BMailAddOnSettings::Load(const BMessage
& message
)
429 const char* pathString
= NULL
;
430 if (message
.FindString("add-on path", &pathString
) != B_OK
)
433 BPath
path(pathString
);
434 if (!path
.IsAbsolute()) {
435 directory_which which
[] = {
436 B_USER_ADDONS_DIRECTORY
,
437 B_SYSTEM_ADDONS_DIRECTORY
440 for (size_t i
= 0; i
< sizeof(which
) / sizeof(which
[0]); i
++) {
441 status_t status
= find_directory(which
[i
], &path
);
445 path
.Append("mail_daemon");
446 path
.Append(pathString
);
448 if (BEntry(path
.Path()).Exists())
453 status_t status
= get_ref_for_path(path
.Path(), &fRef
);
458 message
.FindMessage("settings", &settings
);
463 fOriginalSettings
= *this;
470 BMailAddOnSettings::Save(BMessage
& message
)
473 status_t status
= message
.AddString("add-on path", _RelativizePath(path
));
475 status
= message
.AddMessage("settings", this);
479 fOriginalSettings
= *this;
486 BMailAddOnSettings::SetAddOnRef(const entry_ref
& ref
)
493 BMailAddOnSettings::AddOnRef() const
500 BMailAddOnSettings::HasBeenModified() const
502 return fRef
!= fOriginalRef
503 || !fOriginalSettings
.HasSameData(*this, true, true);
507 /*! Cuts off the ".../add-ons/mail_daemon" part of the provided \a path
508 in case it exists. Otherwise, the complete path will be returned.
511 BMailAddOnSettings::_RelativizePath(const BPath
& path
) const
513 const char* string
= path
.Path();
514 const char* parentDirectory
= "/mail_daemon/";
515 const char* at
= strstr(string
, parentDirectory
);
519 return at
+ strlen(parentDirectory
);
526 BMailProtocolSettings::BMailProtocolSettings()
528 fFiltersSettings(5, true)
533 BMailProtocolSettings::~BMailProtocolSettings()
539 BMailProtocolSettings::Load(const BMessage
& message
)
541 status_t status
= BMailAddOnSettings::Load(message
);
547 message
.GetInfo("filters", &typeFound
, &countFound
);
548 if (typeFound
!= B_MESSAGE_TYPE
)
551 for (int i
= 0; i
< countFound
; i
++) {
552 int32 index
= AddFilterSettings();
556 BMailAddOnSettings
* filterSettings
= fFiltersSettings
.ItemAt(index
);
558 BMessage filterMessage
;
559 message
.FindMessage("filters", i
, &filterMessage
);
560 if (filterSettings
->Load(filterMessage
) != B_OK
)
561 RemoveFilterSettings(index
);
568 BMailProtocolSettings::Save(BMessage
& message
)
570 status_t status
= BMailAddOnSettings::Save(message
);
574 for (int i
= 0; i
< CountFilterSettings(); i
++) {
576 BMailAddOnSettings
* filterSettings
= fFiltersSettings
.ItemAt(i
);
577 filterSettings
->Save(filter
);
578 message
.AddMessage("filters", &filter
);
585 BMailProtocolSettings::CountFilterSettings() const
587 return fFiltersSettings
.CountItems();
592 BMailProtocolSettings::AddFilterSettings(const entry_ref
* ref
)
594 BMailAddOnSettings
* filterSettings
= new BMailAddOnSettings();
596 filterSettings
->SetAddOnRef(*ref
);
598 if (fFiltersSettings
.AddItem(filterSettings
))
599 return fFiltersSettings
.CountItems() - 1;
601 delete filterSettings
;
607 BMailProtocolSettings::RemoveFilterSettings(int32 index
)
609 fFiltersSettings
.RemoveItemAt(index
);
614 BMailProtocolSettings::MoveFilterSettings(int32 from
, int32 to
)
616 if (from
< 0 || from
>= (int32
)CountFilterSettings() || to
< 0
617 || to
>= (int32
)CountFilterSettings())
622 BMailAddOnSettings
* settings
= fFiltersSettings
.RemoveItemAt(from
);
623 fFiltersSettings
.AddItem(settings
, to
);
629 BMailProtocolSettings::FilterSettingsAt(int32 index
) const
631 return fFiltersSettings
.ItemAt(index
);
636 BMailProtocolSettings::HasBeenModified() const
638 if (BMailAddOnSettings::HasBeenModified())
640 for (int32 i
= 0; i
< CountFilterSettings(); i
++) {
641 if (FilterSettingsAt(i
)->HasBeenModified())
651 BMailAccountSettings::BMailAccountSettings()
654 fInboundEnabled(true),
655 fOutboundEnabled(true),
658 fAccountID
= real_time_clock();
662 BMailAccountSettings::BMailAccountSettings(BEntry account
)
664 fAccountFile(account
),
671 BMailAccountSettings::~BMailAccountSettings()
678 BMailAccountSettings::SetAccountID(int32 id
)
686 BMailAccountSettings::AccountID() const
693 BMailAccountSettings::SetName(const char* name
)
701 BMailAccountSettings::Name() const
708 BMailAccountSettings::SetRealName(const char* realName
)
711 fRealName
= realName
;
716 BMailAccountSettings::RealName() const
723 BMailAccountSettings::SetReturnAddress(const char* returnAddress
)
726 fReturnAdress
= returnAddress
;
731 BMailAccountSettings::ReturnAddress() const
733 return fReturnAdress
;
738 BMailAccountSettings::SetInboundAddOn(const char* name
)
741 status_t status
= find_directory(B_BEOS_ADDONS_DIRECTORY
, &path
);
744 path
.Append("mail_daemon");
745 path
.Append("inbound_protocols");
748 get_ref_for_path(path
.Path(), &ref
);
749 fInboundSettings
.SetAddOnRef(ref
);
756 BMailAccountSettings::SetOutboundAddOn(const char* name
)
759 status_t status
= find_directory(B_BEOS_ADDONS_DIRECTORY
, &path
);
762 path
.Append("mail_daemon");
763 path
.Append("outbound_protocols");
766 get_ref_for_path(path
.Path(), &ref
);
767 fOutboundSettings
.SetAddOnRef(ref
);
774 BMailAccountSettings::InboundAddOnRef() const
776 return fInboundSettings
.AddOnRef();
781 BMailAccountSettings::OutboundAddOnRef() const
783 return fOutboundSettings
.AddOnRef();
787 BMailProtocolSettings
&
788 BMailAccountSettings::InboundSettings()
790 return fInboundSettings
;
794 const BMailProtocolSettings
&
795 BMailAccountSettings::InboundSettings() const
797 return fInboundSettings
;
801 BMailProtocolSettings
&
802 BMailAccountSettings::OutboundSettings()
804 return fOutboundSettings
;
808 const BMailProtocolSettings
&
809 BMailAccountSettings::OutboundSettings() const
811 return fOutboundSettings
;
816 BMailAccountSettings::HasInbound()
818 return BEntry(&fInboundSettings
.AddOnRef()).Exists();
823 BMailAccountSettings::HasOutbound()
825 return BEntry(&fOutboundSettings
.AddOnRef()).Exists();
830 BMailAccountSettings::SetInboundEnabled(bool enabled
)
832 fInboundEnabled
= enabled
;
838 BMailAccountSettings::IsInboundEnabled() const
840 return fInboundEnabled
;
845 BMailAccountSettings::SetOutboundEnabled(bool enabled
)
847 fOutboundEnabled
= enabled
;
853 BMailAccountSettings::IsOutboundEnabled() const
855 return fOutboundEnabled
;
860 BMailAccountSettings::Reload()
862 BFile
file(&fAccountFile
, B_READ_ONLY
);
863 status_t status
= file
.InitCheck();
867 settings
.Unflatten(&file
);
870 if (settings
.FindInt32("id", &id
) == B_OK
)
872 settings
.FindString("name", &fAccountName
);
873 settings
.FindString("real_name", &fRealName
);
874 settings
.FindString("return_address", &fReturnAdress
);
876 BMessage inboundSettings
;
877 settings
.FindMessage("inbound", &inboundSettings
);
878 fInboundSettings
.Load(inboundSettings
);
879 BMessage outboundSettings
;
880 settings
.FindMessage("outbound", &outboundSettings
);
881 fOutboundSettings
.Load(outboundSettings
);
883 if (settings
.FindBool("inbound_enabled", &fInboundEnabled
) != B_OK
)
884 fInboundEnabled
= true;
885 if (settings
.FindBool("outbound_enabled", &fOutboundEnabled
) != B_OK
)
886 fOutboundEnabled
= true;
894 BMailAccountSettings::Save()
899 settings
.AddInt32("id", fAccountID
);
900 settings
.AddString("name", fAccountName
);
901 settings
.AddString("real_name", fRealName
);
902 settings
.AddString("return_address", fReturnAdress
);
904 BMessage inboundSettings
;
905 fInboundSettings
.Save(inboundSettings
);
906 settings
.AddMessage("inbound", &inboundSettings
);
907 BMessage outboundSettings
;
908 fOutboundSettings
.Save(outboundSettings
);
909 settings
.AddMessage("outbound", &outboundSettings
);
911 settings
.AddBool("inbound_enabled", fInboundEnabled
);
912 settings
.AddBool("outbound_enabled", fOutboundEnabled
);
914 status_t status
= _CreateAccountFilePath();
918 BFile
file(&fAccountFile
, B_READ_WRITE
| B_CREATE_FILE
| B_ERASE_FILE
);
919 status
= file
.InitCheck();
922 return settings
.Flatten(&file
);
927 BMailAccountSettings::Delete()
929 return fAccountFile
.Remove();
934 BMailAccountSettings::HasBeenModified() const
937 || fInboundSettings
.HasBeenModified()
938 || fOutboundSettings
.HasBeenModified();
943 BMailAccountSettings::AccountFile() const
950 BMailAccountSettings::_CreateAccountFilePath()
953 status_t status
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
956 path
.Append("Mail/accounts");
957 create_directory(path
.Path(), 777);
959 if (fAccountFile
.InitCheck() == B_OK
)
962 BString fileName
= fAccountName
;
964 fileName
<< fAccountID
;
965 for (int i
= 0; ; i
++) {
966 BString testFileName
= fileName
;
971 BPath
testPath(path
);
972 testPath
.Append(testFileName
);
973 BEntry
testEntry(testPath
.Path());
974 if (!testEntry
.Exists()) {
975 fileName
= testFileName
;
980 path
.Append(fileName
);
981 return fAccountFile
.SetTo(path
.Path());