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 "CalcWindow.h" 26 27 28 #undef B_TRANSLATION_CONTEXT 29 #define B_TRANSLATION_CONTEXT "CalcApplication" 30 31 32 static const char* kSettingsFileName = "DeskCalc_settings"; 33 const char* kAppSig = "application/x-vnd.Haiku-DeskCalc"; 34 35 static const float kDefaultWindowWidth = 220.0; 36 static const float kDefaultWindowHeight = 140.0; 37 38 39 CalcApplication::CalcApplication() 40 : 41 BApplication(kAppSig), 42 fCalcWindow(NULL) 43 { 44 } 45 46 47 CalcApplication::~CalcApplication() 48 { 49 } 50 51 52 void 53 CalcApplication::ReadyToRun() 54 { 55 BMessage settings; 56 _LoadSettings(settings); 57 58 BRect frame(0, 0, kDefaultWindowWidth - 1, kDefaultWindowHeight - 1); 59 fCalcWindow = new CalcWindow(frame, &settings); 60 61 // reveal window 62 fCalcWindow->Show(); 63 } 64 65 66 void 67 CalcApplication::AboutRequested() 68 { 69 // TODO: implement me! 70 return BApplication::AboutRequested(); 71 } 72 73 74 bool 75 CalcApplication::QuitRequested() 76 { 77 // save current user preferences 78 _SaveSettings(); 79 80 return true; 81 } 82 83 84 // #pragma mark - 85 86 87 void 88 CalcApplication::_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 104 void 105 CalcApplication::_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 140 status_t 141 CalcApplication::_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