xref: /haiku/src/apps/deskcalc/CalcApplication.cpp (revision 03187b607b2b5eec7ee059f1ead09bdba14991fb)
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 	BMessage settings;
48 	_LoadSettings(settings);
49 
50 	BRect frame(0, 0, kDefaultWindowWidth - 1, kDefaultWindowHeight - 1);
51 	fCalcWindow = new CalcWindow(frame, &settings);
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 void
80 CalcApplication::_LoadSettings(BMessage &archive)
81 {
82 	// locate preferences file
83 	BFile prefsFile;
84 	if (_InitSettingsFile(&prefsFile, false) < B_OK) {
85 		printf("no preference file found.\n");
86 		return;
87 	}
88 
89 	// unflatten settings data
90 	if (archive.Unflatten(&prefsFile) < B_OK) {
91 		printf("error unflattening settings.\n");
92 	}
93 }
94 
95 
96 void
97 CalcApplication::_SaveSettings()
98 {
99 	if (!fCalcWindow->Lock())
100 		return;
101 
102 	// archive the current state of the calculator
103 	BMessage archive;
104 	status_t ret = fCalcWindow->SaveSettings(&archive);
105 
106 	fCalcWindow->Unlock();
107 
108 	if (ret < B_OK) {
109 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
110 						"error from window: %s\n", strerror(ret));
111 		return;
112 	}
113 
114 	// flatten entire acrhive and write to settings file
115 	BFile prefsFile;
116 	ret = _InitSettingsFile(&prefsFile, true);
117 	if (ret < B_OK) {
118 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
119 						"error creating file: %s\n", strerror(ret));
120 		return;
121 	}
122 
123 	ret = archive.Flatten(&prefsFile);
124 	if (ret < B_OK) {
125 		fprintf(stderr, "CalcApplication::_SaveSettings() - "
126 						"error flattening to file: %s\n", strerror(ret));
127 		return;
128 	}
129 }
130 
131 status_t
132 CalcApplication::_InitSettingsFile(BFile* file, bool write)
133 {
134 	// find user settings directory
135 	BPath prefsPath;
136 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
137 	if (ret < B_OK)
138 		return ret;
139 
140 	ret = prefsPath.Append(kSettingsFileName);
141 	if (ret < B_OK)
142 		return ret;
143 
144 	if (write)
145 		ret = file->SetTo(prefsPath.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
146 	else
147 		ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
148 
149 	return ret;
150 }
151 
152 
153