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