xref: /haiku/src/apps/drivesetup/DriveSetup.cpp (revision 7d24c6470f0b2ceb3ea4973ae1e8311068271810)
1 /*
2  * Copyright 2002-2009 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Erik Jaesler <ejakowatz@users.sourceforge.net>
7  *		Ithamar R. Adema <ithamar@unet.nl>
8  *		Stephan Aßmus <superstippi@gmx.de>
9  */
10 
11 #include "DriveSetup.h"
12 #include "MainWindow.h"
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <File.h>
18 #include <FindDirectory.h>
19 #include <Locale.h>
20 #include <Path.h>
21 
22 
23 DriveSetup::DriveSetup()
24 	: BApplication("application/x-vnd.Haiku-DriveSetup"),
25 	fWindow(NULL),
26 	fSettings(0UL)
27 {
28 }
29 
30 
31 DriveSetup::~DriveSetup()
32 {
33 }
34 
35 
36 void
37 DriveSetup::ReadyToRun()
38 {
39 	be_locale->GetAppCatalog(&fCatalog);
40 
41 	fWindow = new MainWindow(BRect(50, 50, 600, 450));
42 	if (_RestoreSettings() != B_OK)
43 		fWindow->ApplyDefaultSettings();
44 	fWindow->Show();
45 }
46 
47 
48 bool
49 DriveSetup::QuitRequested()
50 {
51 	_StoreSettings();
52 
53 	if (fWindow->Lock()) {
54 		fWindow->Quit();
55 		fWindow = NULL;
56 	}
57 
58 	return true;
59 }
60 
61 
62 // #pragma mark -
63 
64 
65 status_t
66 DriveSetup::_StoreSettings()
67 {
68 	status_t ret = B_ERROR;
69 	if (fWindow->Lock()) {
70 		ret = fWindow->StoreSettings(&fSettings);
71 		fWindow->Unlock();
72 	}
73 
74 	if (ret < B_OK) {
75 		fprintf(stderr, "failed to store settings: %s\n", strerror(ret));
76 		return ret;
77 	}
78 
79 	BFile file;
80 	ret = _GetSettingsFile(file, true);
81 	if (ret < B_OK)
82 		return ret;
83 
84 	ret = fSettings.Flatten(&file);
85 	if (ret < B_OK) {
86 		fprintf(stderr, "failed to flatten settings: %s\n", strerror(ret));
87 		return ret;
88 	}
89 
90 	return B_OK;
91 }
92 
93 
94 status_t
95 DriveSetup::_RestoreSettings()
96 {
97 	BFile file;
98 	status_t ret = _GetSettingsFile(file, false);
99 	if (ret < B_OK)
100 		return ret;
101 
102 	ret = fSettings.Unflatten(&file);
103 	if (ret < B_OK) {
104 		fprintf(stderr, "failed to unflatten settings: %s\n", strerror(ret));
105 		return ret;
106 	}
107 
108 	ret = fWindow->RestoreSettings(&fSettings);
109 	if (ret < B_OK) {
110 		fprintf(stderr, "failed to restore settings: %s\n", strerror(ret));
111 		return ret;
112 	}
113 
114 	return B_OK;
115 }
116 
117 
118 status_t
119 DriveSetup::_GetSettingsFile(BFile& file, bool forWriting) const
120 {
121 	BPath path;
122 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
123 	if (ret != B_OK) {
124 		fprintf(stderr, "failed to get user settings folder: %s\n",
125 			strerror(ret));
126 		return ret;
127 	}
128 
129 	ret = path.Append("DriveSetup");
130 	if (ret != B_OK) {
131 		fprintf(stderr, "failed to construct path: %s\n", strerror(ret));
132 		return ret;
133 	}
134 
135 	uint32 writeFlags = B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY;
136 	uint32 readFlags = B_READ_ONLY;
137 
138 	ret = file.SetTo(path.Path(), forWriting ? writeFlags : readFlags);
139 	if (ret != B_OK) {
140 		fprintf(stderr, "failed to init file: %s\n", strerror(ret));
141 		return ret;
142 	}
143 
144 	return B_OK;
145 }
146 
147 
148 // #pragma mark -
149 
150 
151 int
152 main(int, char**)
153 {
154 	DriveSetup app;
155 	app.Run();
156 	return 0;
157 }
158