BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / deskcalc / CalcApplication.cpp
blob9fb468292c8bef057b133ba846690f7446a7bd71
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.
6 * Authors:
7 * Stephan Aßmus, superstippi@gmx.de
8 * John Scipione, jscipione@gmail.com
9 * Timothy Wayper, timmy@wunderbear.com
13 #include "CalcApplication.h"
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
19 #include <Catalog.h>
20 #include <Directory.h>
21 #include <File.h>
22 #include <FindDirectory.h>
23 #include <Path.h>
25 #include "CalcView.h"
26 #include "CalcWindow.h"
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "CalcApplication"
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";
37 static const float kDefaultWindowWidth = 220.0;
38 static const float kDefaultWindowHeight = 140.0;
41 CalcApplication::CalcApplication()
43 BApplication(kSignature),
44 fCalcWindow(NULL)
49 CalcApplication::~CalcApplication()
54 void
55 CalcApplication::ReadyToRun()
57 BMessage settings;
58 _LoadSettings(settings);
60 float scaling = be_plain_font->Size() / 12.0f;
61 BRect frame(0, 0, (kDefaultWindowWidth * scaling) - 1,
62 (kDefaultWindowHeight * scaling) - 1);
63 fCalcWindow = new CalcWindow(frame, &settings);
65 // reveal window
66 fCalcWindow->Show();
70 void
71 CalcApplication::AboutRequested()
73 fCalcWindow->View()->MessageReceived(new BMessage(B_ABOUT_REQUESTED));
77 bool
78 CalcApplication::QuitRequested()
80 // save current user preferences
81 _SaveSettings();
83 return true;
87 // #pragma mark -
90 void
91 CalcApplication::_LoadSettings(BMessage& archive)
93 // locate preferences file
94 BFile prefsFile;
95 if (_InitSettingsFile(&prefsFile, false) < B_OK) {
96 printf("no preference file found.\n");
97 return;
100 // unflatten settings data
101 if (archive.Unflatten(&prefsFile) < B_OK) {
102 printf("error unflattening settings.\n");
107 void
108 CalcApplication::_SaveSettings()
110 if (!fCalcWindow->Lock())
111 return;
113 // archive the current state of the calculator
114 BMessage archive;
115 status_t ret = fCalcWindow->SaveSettings(&archive);
117 fCalcWindow->Unlock();
119 if (ret < B_OK) {
120 fprintf(stderr, "CalcApplication::_SaveSettings() - error from window: "
121 "%s\n", strerror(ret));
122 return;
125 // flatten entire acrhive and write to settings file
126 BFile prefsFile;
127 ret = _InitSettingsFile(&prefsFile, true);
128 if (ret < B_OK) {
129 fprintf(stderr, "CalcApplication::_SaveSettings() - "
130 "error creating file: %s\n", strerror(ret));
131 return;
134 ret = archive.Flatten(&prefsFile);
135 if (ret < B_OK) {
136 fprintf(stderr, "CalcApplication::_SaveSettings() - error flattening "
137 "to file: %s\n", strerror(ret));
138 return;
143 status_t
144 CalcApplication::_InitSettingsFile(BFile* file, bool write)
146 // find user settings directory
147 BPath prefsPath;
148 status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
149 if (ret < B_OK)
150 return ret;
152 ret = prefsPath.Append(kSettingsFileName);
153 if (ret < B_OK)
154 return ret;
156 if (write) {
157 ret = file->SetTo(prefsPath.Path(),
158 B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
159 } else
160 ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
162 return ret;