xref: /haiku/src/apps/icon-o-matic/generic/support/support_settings.cpp (revision b55a57da7173b9af0432bd3e148d03f06161d036)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "support_settings.h"
10 
11 #include <stdio.h>
12 
13 #include <Directory.h>
14 #include <File.h>
15 #include <FindDirectory.h>
16 #include <Message.h>
17 #include <Path.h>
18 
19 // load_settings
20 status_t
21 load_settings(BMessage* message, const char* fileName, const char* folder)
22 {
23 	status_t ret = B_BAD_VALUE;
24 	if (message) {
25 		BPath path;
26 		if ((ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) == B_OK) {
27 			// passing folder is optional
28 			if (folder)
29 				ret = path.Append(folder);
30 			if (ret == B_OK && (ret = path.Append(fileName)) == B_OK) {
31 				BFile file(path.Path(), B_READ_ONLY);
32 				if ((ret = file.InitCheck()) == B_OK) {
33 					ret = message->Unflatten(&file);
34 					file.Unset();
35 				}
36 			}
37 		}
38 	}
39 	return ret;
40 }
41 
42 // save_settings
43 status_t
44 save_settings(BMessage* message, const char* fileName, const char* folder)
45 {
46 	status_t ret = B_BAD_VALUE;
47 	if (message) {
48 		BPath path;
49 		if ((ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) == B_OK) {
50 			// passing folder is optional
51 			if (folder && (ret = path.Append(folder)) == B_OK)
52 				ret = create_directory(path.Path(), 0777);
53 			if (ret == B_OK && (ret = path.Append(fileName)) == B_OK) {
54 				BFile file(path.Path(),
55 						   B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
56 				if ((ret = file.InitCheck()) == B_OK) {
57 					ret = message->Flatten(&file);
58 					file.Unset();
59 				}
60 			}
61 		}
62 	}
63 	return ret;
64 }
65 
66