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