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