xref: /haiku/src/apps/deskcalc/CalcApplication.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
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 	:
34 	BApplication(kAppSig),
35 	fCalcWindow(NULL)
36 {
37 }
38 
39 
40 CalcApplication::~CalcApplication()
41 {
42 }
43 
44 
45 void
46 CalcApplication::ReadyToRun()
47 {
48 	BMessage settings;
49 	_LoadSettings(settings);
50 
51 	BRect frame(0, 0, kDefaultWindowWidth - 1, kDefaultWindowHeight - 1);
52 	fCalcWindow = new CalcWindow(frame, &settings);
53 
54 	// reveal window
55 	fCalcWindow->Show();
56 }
57 
58 
59 void
60 CalcApplication::AboutRequested()
61 {
62 	// TODO: implement me!
63 	return BApplication::AboutRequested();
64 }
65 
66 
67 bool
68 CalcApplication::QuitRequested()
69 {
70 	// save current user preferences
71 	_SaveSettings();
72 
73 	return true;
74 }
75 
76 
77 // #pragma mark -
78 
79 
80 void
81 CalcApplication::_LoadSettings(BMessage& archive)
82 {
83 	// locate preferences file
84 	BFile prefsFile;
85 	if (_InitSettingsFile(&prefsFile, false) < B_OK) {
86 		printf("no preference file found.\n");
87 		return;
88 	}
89 
90 	// unflatten settings data
91 	if (archive.Unflatten(&prefsFile) < B_OK) {
92 		printf("error unflattening settings.\n");
93 	}
94 }
95 
96 
97 void
98 CalcApplication::_SaveSettings()
99 {
100 	if (!fCalcWindow->Lock())
101 		return;
102 
103 	// archive the current state of the calculator
104 	BMessage archive;
105 	status_t ret = fCalcWindow->SaveSettings(&archive);
106 
107 	fCalcWindow->Unlock();
108 
109 	if (ret < B_OK) {
110 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
111 						"error from window: %s\n", strerror(ret));
112 		return;
113 	}
114 
115 	// flatten entire acrhive and write to settings file
116 	BFile prefsFile;
117 	ret = _InitSettingsFile(&prefsFile, true);
118 	if (ret < B_OK) {
119 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
120 						"error creating file: %s\n", strerror(ret));
121 		return;
122 	}
123 
124 	ret = archive.Flatten(&prefsFile);
125 	if (ret < B_OK) {
126 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
127 						"error flattening to file: %s\n", strerror(ret));
128 		return;
129 	}
130 }
131 
132 
133 status_t
134 CalcApplication::_InitSettingsFile(BFile* file, bool write)
135 {
136 	// find user settings directory
137 	BPath prefsPath;
138 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
139 	if (ret < B_OK)
140 		return ret;
141 
142 	ret = prefsPath.Append(kSettingsFileName);
143 	if (ret < B_OK)
144 		return ret;
145 
146 	if (write) {
147 		ret = file->SetTo(prefsPath.Path(),
148 			B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
149 	} else
150 		ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
151 
152 	return ret;
153 }
154 
155 
156