xref: /haiku/src/apps/deskcalc/CalcApplication.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2006 Haiku, Inc. All Rights Reserved.
3  * Copyright 1997, 1998 R3 Software Ltd. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  *
6  * Authors:
7  *		Timothy Wayper <timmy@wunderbear.com>
8  *		Stephan Aßmus <superstippi@gmx.de>
9  */
10 
11 #include "CalcApplication.h"
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <Directory.h>
18 #include <File.h>
19 #include <FindDirectory.h>
20 #include <Path.h>
21 
22 #include "CalcWindow.h"
23 
24 
25 static const char* kSettingsFileName	= "DeskCalc_settings";
26 extern const char* kAppSig				= "application/x-vnd.Haiku-DeskCalc";
27 
28 static const float kDefaultWindowWidth	= 220.0;
29 static const float kDefaultWindowHeight	= 140.0;
30 
31 
32 CalcApplication::CalcApplication()
33 	: BApplication(kAppSig),
34 	  fCalcWindow(NULL)
35 {
36 }
37 
38 
39 CalcApplication::~CalcApplication()
40 {
41 }
42 
43 
44 void
45 CalcApplication::ReadyToRun()
46 {
47 	BRect frame(0, 0, kDefaultWindowWidth - 1, kDefaultWindowHeight - 1);
48 	fCalcWindow = new CalcWindow(frame);
49 
50 	if (!_LoadSettings())
51 		fCalcWindow->SetFrame(frame, true);
52 
53 	// reveal window
54 	fCalcWindow->Show();
55 }
56 
57 
58 void
59 CalcApplication::AboutRequested()
60 {
61 	// TODO: implement me!
62 	return BApplication::AboutRequested();
63 }
64 
65 
66 bool
67 CalcApplication::QuitRequested()
68 {
69 	// save current user preferences
70 	_SaveSettings();
71 
72 	return true;
73 }
74 
75 
76 // #pragma mark -
77 
78 
79 bool
80 CalcApplication::_LoadSettings()
81 {
82 	// locate preferences file
83 	BFile prefsFile;
84 	if (_InitSettingsFile(&prefsFile, false) < B_OK) {
85 		printf("no preference file found.\n");
86 		return false;
87 	}
88 
89 	// unflatten settings data
90 	BMessage archive;
91 	if (archive.Unflatten(&prefsFile) < B_OK) {
92 		printf("error unflattening settings.\n");
93 		return false;
94 	}
95 
96 	// apply settings
97 	return fCalcWindow->LoadSettings(&archive);
98 }
99 
100 
101 void
102 CalcApplication::_SaveSettings()
103 {
104 	if (!fCalcWindow->Lock())
105 		return;
106 
107 	// archive the current state of the calculator
108 	BMessage archive;
109 	status_t ret = fCalcWindow->SaveSettings(&archive);
110 
111 	fCalcWindow->Unlock();
112 
113 	if (ret < B_OK) {
114 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
115 						"error from window: %s\n", strerror(ret));
116 		return;
117 	}
118 
119 	// flatten entire acrhive and write to settings file
120 	BFile prefsFile;
121 	ret = _InitSettingsFile(&prefsFile, true);
122 	if (ret < B_OK) {
123 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
124 						"error creating file: %s\n", strerror(ret));
125 		return;
126 	}
127 
128 	ret = archive.Flatten(&prefsFile);
129 	if (ret < B_OK) {
130 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
131 						"error flattening to file: %s\n", strerror(ret));
132 		return;
133 	}
134 }
135 
136 status_t
137 CalcApplication::_InitSettingsFile(BFile* file, bool write)
138 {
139 	// find user settings directory
140 	BPath prefsPath;
141 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
142 	if (ret < B_OK)
143 		return ret;
144 
145 	ret = prefsPath.Append(kSettingsFileName);
146 	if (ret < B_OK)
147 		return ret;
148 
149 	if (write)
150 		ret = file->SetTo(prefsPath.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
151 	else
152 		ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
153 
154 	return ret;
155 }
156 
157 
158