xref: /haiku/src/apps/drivesetup/DriveSetup.cpp (revision c9ad965c81b08802fed0827fd1dd16f45297928a)
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 <Path.h>
20 
21 
22 DriveSetup::DriveSetup()
23 	: BApplication("application/x-vnd.Haiku-DriveSetup"),
24 	fWindow(NULL),
25 	fSettings(0UL)
26 {
27 }
28 
29 
30 DriveSetup::~DriveSetup()
31 {
32 }
33 
34 
35 void
36 DriveSetup::ReadyToRun()
37 {
38 	fWindow = new MainWindow(BRect(50, 50, 600, 450));
39 	if (_RestoreSettings() != B_OK)
40 		fWindow->ApplyDefaultSettings();
41 	fWindow->Show();
42 }
43 
44 
45 bool
46 DriveSetup::QuitRequested()
47 {
48 	_StoreSettings();
49 
50 	if (fWindow->Lock()) {
51 		fWindow->Quit();
52 		fWindow = NULL;
53 	}
54 
55 	return true;
56 }
57 
58 
59 // #pragma mark -
60 
61 
62 status_t
63 DriveSetup::_StoreSettings()
64 {
65 	status_t ret = B_ERROR;
66 	if (fWindow->Lock()) {
67 		ret = fWindow->StoreSettings(&fSettings);
68 		fWindow->Unlock();
69 	}
70 
71 	if (ret < B_OK) {
72 		fprintf(stderr, "failed to store settings: %s\n", strerror(ret));
73 		return ret;
74 	}
75 
76 	BFile file;
77 	ret = _GetSettingsFile(file, true);
78 	if (ret < B_OK)
79 		return ret;
80 
81 	ret = fSettings.Flatten(&file);
82 	if (ret < B_OK) {
83 		fprintf(stderr, "failed to flatten settings: %s\n", strerror(ret));
84 		return ret;
85 	}
86 
87 	return B_OK;
88 }
89 
90 
91 status_t
92 DriveSetup::_RestoreSettings()
93 {
94 	BFile file;
95 	status_t ret = _GetSettingsFile(file, false);
96 	if (ret < B_OK)
97 		return ret;
98 
99 	ret = fSettings.Unflatten(&file);
100 	if (ret < B_OK) {
101 		fprintf(stderr, "failed to unflatten settings: %s\n", strerror(ret));
102 		return ret;
103 	}
104 
105 	ret = fWindow->RestoreSettings(&fSettings);
106 	if (ret < B_OK) {
107 		fprintf(stderr, "failed to restore settings: %s\n", strerror(ret));
108 		return ret;
109 	}
110 
111 	return B_OK;
112 }
113 
114 
115 status_t
116 DriveSetup::_GetSettingsFile(BFile& file, bool forWriting) const
117 {
118 	BPath path;
119 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
120 	if (ret != B_OK) {
121 		fprintf(stderr, "failed to get user settings folder: %s\n",
122 			strerror(ret));
123 		return ret;
124 	}
125 
126 	ret = path.Append("DriveSetup");
127 	if (ret != B_OK) {
128 		fprintf(stderr, "failed to construct path: %s\n", strerror(ret));
129 		return ret;
130 	}
131 
132 	uint32 writeFlags = B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY;
133 	uint32 readFlags = B_READ_ONLY;
134 
135 	ret = file.SetTo(path.Path(), forWriting ? writeFlags : readFlags);
136 	if (ret != B_OK) {
137 		fprintf(stderr, "failed to init file: %s\n", strerror(ret));
138 		return ret;
139 	}
140 
141 	return B_OK;
142 }
143 
144 
145 // #pragma mark -
146 
147 
148 int
149 main(int, char**)
150 {
151 	DriveSetup app;
152 	app.Run();
153 	return 0;
154 }
155