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 <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 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); 64 65 // reveal window 66 fCalcWindow->Show(); 67 } 68 69 70 void 71 CalcApplication::AboutRequested() 72 { 73 fCalcWindow->View()->MessageReceived(new BMessage(B_ABOUT_REQUESTED)); 74 } 75 76 77 bool 78 CalcApplication::QuitRequested() 79 { 80 // save current user preferences 81 _SaveSettings(); 82 83 return true; 84 } 85 86 87 // #pragma mark - 88 89 90 void 91 CalcApplication::_LoadSettings(BMessage& archive) 92 { 93 // locate preferences file 94 BFile prefsFile; 95 if (_InitSettingsFile(&prefsFile, false) < B_OK) { 96 printf("no preference file found.\n"); 97 return; 98 } 99 100 // unflatten settings data 101 if (archive.Unflatten(&prefsFile) < B_OK) { 102 printf("error unflattening settings.\n"); 103 } 104 } 105 106 107 void 108 CalcApplication::_SaveSettings() 109 { 110 if (!fCalcWindow->Lock()) 111 return; 112 113 // archive the current state of the calculator 114 BMessage archive; 115 status_t ret = fCalcWindow->SaveSettings(&archive); 116 117 fCalcWindow->Unlock(); 118 119 if (ret < B_OK) { 120 fprintf(stderr, "CalcApplication::_SaveSettings() - error from window: " 121 "%s\n", strerror(ret)); 122 return; 123 } 124 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; 132 } 133 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; 139 } 140 } 141 142 143 status_t 144 CalcApplication::_InitSettingsFile(BFile* file, bool write) 145 { 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; 151 152 ret = prefsPath.Append(kSettingsFileName); 153 if (ret < B_OK) 154 return ret; 155 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); 161 162 return ret; 163 } 164