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