xref: /haiku/src/kits/mail/MailSettings.cpp (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /* BMailSettings - the mail daemon's settings
2 **
3 ** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4 */
5 
6 
7 #include <Message.h>
8 #include <FindDirectory.h>
9 #include <Directory.h>
10 #include <File.h>
11 #include <Entry.h>
12 #include <Path.h>
13 #include <String.h>
14 #include <Messenger.h>
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 
20 class BMailSettings;
21 
22 namespace MailInternal {
23 	status_t WriteMessageFile(const BMessage& archive, const BPath& path, const char* name);
24 }
25 
26 #include <MailSettings.h>
27 
28 BMailSettings::BMailSettings()
29 {
30 	Reload();
31 }
32 
33 BMailSettings::~BMailSettings()
34 {
35 }
36 
37 status_t BMailSettings::InitCheck() const
38 {
39 	return B_OK;
40 }
41 
42 
43 status_t BMailSettings::Save(bigtime_t /*timeout*/)
44 {
45 	status_t ret;
46 	//
47 	// Find chain-saving directory
48 	//
49 
50 	BPath path;
51 	ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
52 	if (ret != B_OK)
53 	{
54 		fprintf(stderr, "Couldn't find user settings directory: %s\n",
55 			strerror(ret));
56 		return ret;
57 	}
58 
59 	path.Append("Mail");
60 
61 	status_t result = MailInternal::WriteMessageFile(data,path,"new_mail_daemon");
62 	if (result < B_OK)
63 		return result;
64 
65 	BMessenger("application/x-vnd.Be-POST").SendMessage('mrrs');
66 
67 	return B_OK;
68 }
69 
70 status_t BMailSettings::Reload()
71 {
72 	status_t ret;
73 
74 	BPath path;
75 	ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
76 	if (ret != B_OK)
77 	{
78 		fprintf(stderr, "Couldn't find user settings directory: %s\n",
79 			strerror(ret));
80 		return ret;
81 	}
82 
83 	path.Append("Mail/new_mail_daemon");
84 
85 	// open
86 	BFile settings(path.Path(),B_READ_ONLY);
87 	ret = settings.InitCheck();
88 	if (ret != B_OK)
89 	{
90 		fprintf(stderr, "Couldn't open settings file '%s': %s\n",
91 			path.Path(), strerror(ret));
92 		return ret;
93 	}
94 
95 	// read settings
96 	BMessage tmp;
97 	ret = tmp.Unflatten(&settings);
98 	if (ret != B_OK)
99 	{
100 		fprintf(stderr, "Couldn't read settings from '%s': %s\n",
101 			path.Path(), strerror(ret));
102 		return ret;
103 	}
104 
105 	// clobber old settings
106 	data = tmp;
107 	return B_OK;
108 }
109 
110 
111 // Chain methods
112 
113 //
114 // To do
115 //
116 _EXPORT BMailChain* NewMailChain()
117 {
118 	// attempted solution: use time(NULL) and hope it's unique. Is there a better idea?
119 	// note that two chains in two second is quite possible. how to fix this?
120 	// maybe we could | in some bigtime_t as well. hrrm...
121 
122 	// This is to fix a problem with generating the correct id for chains.
123 	// Basically if the chains dir does not exist, the first time you create
124 	// an account both the inbound and outbound chains will be called 0.
125 	create_directory("/boot/home/config/settings/Mail/chains",0777);
126 
127 	BPath path;
128 	find_directory(B_USER_SETTINGS_DIRECTORY, &path);
129 	path.Append("Mail/chains");
130 	BDirectory chain_dir(path.Path());
131 	BDirectory outbound_dir(&chain_dir,"outbound"), inbound_dir(&chain_dir,"inbound");
132 
133 	chain_dir.Lock(); //---------Try to lock the directory
134 
135 	int32 id = -1; //-----When inc'ed, we start with 0----
136 	chain_dir.ReadAttr("last_issued_chain_id",B_INT32_TYPE,0,&id,sizeof(id));
137 
138 	BString string_id;
139 
140 	do {
141 		id++;
142 		string_id = "";
143 		string_id << id;
144 	} while ((outbound_dir.Contains(string_id.String())) || (inbound_dir.Contains(string_id.String())));
145 
146 
147 	chain_dir.WriteAttr("last_issued_chain_id",B_INT32_TYPE,0,&id,sizeof(id));
148 
149 	return new BMailChain(id);
150 }
151 //
152 // Done
153 //
154 
155 _EXPORT BMailChain* GetMailChain(uint32 id)
156 {
157 	return new BMailChain(id);
158 }
159 
160 _EXPORT status_t GetInboundMailChains(BList *list)
161 {
162 	BPath path;
163 	status_t ret = B_OK;
164 
165 	ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
166 	if (ret != B_OK)
167 	{
168 		fprintf(stderr, "Couldn't find user settings directory: %s\n",
169 			strerror(ret));
170 		return ret;
171 	}
172 
173 	path.Append("Mail/chains/inbound");
174 	BDirectory chain_dir(path.Path());
175 	entry_ref ref;
176 
177 	while (chain_dir.GetNextRef(&ref)==B_OK)
178 	{
179 		char *end;
180 		uint32 id = strtoul(ref.name, &end, 10);
181 
182 		if (!end || *end == '\0')
183 			list->AddItem((void*)new BMailChain(id));
184 	}
185 
186 	return ret;
187 }
188 
189 _EXPORT status_t GetOutboundMailChains(BList *list)
190 {
191 	BPath path;
192 	status_t ret = B_OK;
193 
194 	ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
195 	if (ret != B_OK)
196 	{
197 		fprintf(stderr, "Couldn't find user settings directory: %s\n",
198 			strerror(ret));
199 		return ret;
200 	}
201 
202 	path.Append("Mail/chains/outbound");
203 	BDirectory chain_dir(path.Path());
204 	entry_ref ref;
205 
206 	while (chain_dir.GetNextRef(&ref)==B_OK)
207 	{
208 		char *end;
209 		uint32 id = strtoul(ref.name, &end, 10);
210 		if (!end || *end == '\0')
211 			list->AddItem((void*)new BMailChain(id));
212 	}
213 
214 	return ret;
215 }
216 
217 
218 // Global settings
219 int32 BMailSettings::WindowFollowsCorner()
220 {
221 	return data.FindInt32("WindowFollowsCorner");
222 }
223 void BMailSettings::SetWindowFollowsCorner(int32 which_corner)
224 {
225 	if (data.ReplaceInt32("WindowFollowsCorner",which_corner))
226 		data.AddInt32("WindowFollowsCorner",which_corner);
227 }
228 
229 uint32 BMailSettings::ShowStatusWindow()
230 {
231 	return data.FindInt32("ShowStatusWindow");
232 }
233 void BMailSettings::SetShowStatusWindow(uint32 mode)
234 {
235 	if (data.ReplaceInt32("ShowStatusWindow",mode))
236 		data.AddInt32("ShowStatusWindow",mode);
237 }
238 
239 bool BMailSettings::DaemonAutoStarts()
240 {
241 	return data.FindBool("DaemonAutoStarts");
242 }
243 void BMailSettings::SetDaemonAutoStarts(bool does_it)
244 {
245 	if (data.ReplaceBool("DaemonAutoStarts",does_it))
246 		data.AddBool("DaemonAutoStarts",does_it);
247 }
248 
249 BRect BMailSettings::ConfigWindowFrame()
250 {
251 	return data.FindRect("ConfigWindowFrame");
252 }
253 void BMailSettings::SetConfigWindowFrame(BRect frame)
254 {
255 	if (data.ReplaceRect("ConfigWindowFrame",frame))
256 		data.AddRect("ConfigWindowFrame",frame);
257 }
258 
259 BRect BMailSettings::StatusWindowFrame()
260 {
261 	return data.FindRect("StatusWindowFrame");
262 }
263 void BMailSettings::SetStatusWindowFrame(BRect frame)
264 {
265 	if (data.ReplaceRect("StatusWindowFrame",frame))
266 		data.AddRect("StatusWindowFrame",frame);
267 }
268 
269 int32 BMailSettings::StatusWindowWorkspaces()
270 {
271 	return data.FindInt32("StatusWindowWorkSpace");
272 }
273 void BMailSettings::SetStatusWindowWorkspaces(int32 workspace)
274 {
275 	if (data.ReplaceInt32("StatusWindowWorkSpace",workspace))
276 		data.AddInt32("StatusWindowWorkSpace",workspace);
277 
278 	BMessage msg('wsch');
279 	msg.AddInt32("StatusWindowWorkSpace",workspace);
280 	BMessenger("application/x-vnd.Be-POST").SendMessage(&msg);
281 }
282 
283 int32 BMailSettings::StatusWindowLook()
284 {
285 	return data.FindInt32("StatusWindowLook");
286 }
287 void BMailSettings::SetStatusWindowLook(int32 look)
288 {
289 	if (data.ReplaceInt32("StatusWindowLook",look))
290 		data.AddInt32("StatusWindowLook",look);
291 
292 	BMessage msg('lkch');
293 	msg.AddInt32("StatusWindowLook",look);
294 	BMessenger("application/x-vnd.Be-POST").SendMessage(&msg);
295 }
296 
297 bigtime_t BMailSettings::AutoCheckInterval() {
298 	bigtime_t value = B_INFINITE_TIMEOUT;
299 	data.FindInt64("AutoCheckInterval",&value);
300 	return value;
301 }
302 
303 void BMailSettings::SetAutoCheckInterval(bigtime_t interval) {
304 	if (data.ReplaceInt64("AutoCheckInterval",interval))
305 		data.AddInt64("AutoCheckInterval",interval);
306 }
307 
308 bool BMailSettings::CheckOnlyIfPPPUp() {
309 	return data.FindBool("CheckOnlyIfPPPUp");
310 }
311 
312 void BMailSettings::SetCheckOnlyIfPPPUp(bool yes) {
313 	if (data.ReplaceBool("CheckOnlyIfPPPUp",yes))
314 		data.AddBool("CheckOnlyIfPPPUp",yes);
315 }
316 
317 bool BMailSettings::SendOnlyIfPPPUp() {
318 	return data.FindBool("SendOnlyIfPPPUp");
319 }
320 
321 void BMailSettings::SetSendOnlyIfPPPUp(bool yes) {
322 	if (data.ReplaceBool("SendOnlyIfPPPUp",yes))
323 		data.AddBool("SendOnlyIfPPPUp",yes);
324 }
325 
326 uint32 BMailSettings::DefaultOutboundChainID() {
327 	return data.FindInt32("DefaultOutboundChainID");
328 }
329 
330 void BMailSettings::SetDefaultOutboundChainID(uint32 to) {
331 	if (data.ReplaceInt32("DefaultOutboundChainID",to))
332 		data.AddInt32("DefaultOutboundChainID",to);
333 }
334