1 /* 2 * Copyright 2002-2007 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT license. 4 * 5 * Authors: 6 * Erik Jaesler <ejakowatz@users.sourceforge.net> 7 * Ithamar R. Adema <ithamar@unet.nl> 8 * Stephan Aßmus <superstippi@gmx.de> 9 */ 10 #include "DriveSetup.h" 11 #include "MainWindow.h" 12 13 #include <stdio.h> 14 #include <string.h> 15 16 #include <File.h> 17 #include <FindDirectory.h> 18 #include <Path.h> 19 20 21 int 22 main(int, char**) 23 { 24 DriveSetup app; 25 app.Run(); 26 return 0; 27 } 28 29 30 // #pragma mark - 31 32 33 DriveSetup::DriveSetup() 34 : BApplication("application/x-vnd.Haiku-DriveSetup") 35 , fWindow(NULL) 36 , fSettings(0UL) 37 { 38 } 39 40 41 DriveSetup::~DriveSetup() 42 { 43 } 44 45 46 void 47 DriveSetup::ReadyToRun() 48 { 49 fWindow = new MainWindow(BRect(50, 50, 600, 450)); 50 _RestoreSettings(); 51 fWindow->Show(); 52 } 53 54 55 bool 56 DriveSetup::QuitRequested() 57 { 58 _StoreSettings(); 59 60 if (fWindow->Lock()) { 61 fWindow->Quit(); 62 fWindow = NULL; 63 } 64 65 return true; 66 } 67 68 69 // #pragma mark - 70 71 72 status_t 73 DriveSetup::_StoreSettings() 74 { 75 status_t ret = B_ERROR; 76 if (fWindow->Lock()) { 77 ret = fWindow->StoreSettings(&fSettings); 78 fWindow->Unlock(); 79 } 80 81 if (ret < B_OK) { 82 fprintf(stderr, "failed to store settings: %s\n", strerror(ret)); 83 return ret; 84 } 85 86 BFile file; 87 ret = _GetSettingsFile(file, true); 88 if (ret < B_OK) 89 return ret; 90 91 ret = fSettings.Flatten(&file); 92 if (ret < B_OK) { 93 fprintf(stderr, "failed to flatten settings: %s\n", strerror(ret)); 94 return ret; 95 } 96 97 return B_OK; 98 } 99 100 101 status_t 102 DriveSetup::_RestoreSettings() 103 { 104 BFile file; 105 status_t ret = _GetSettingsFile(file, false); 106 if (ret < B_OK) 107 return ret; 108 109 ret = fSettings.Unflatten(&file); 110 if (ret < B_OK) { 111 fprintf(stderr, "failed to unflatten settings: %s\n", strerror(ret)); 112 return ret; 113 } 114 115 ret = fWindow->RestoreSettings(&fSettings); 116 if (ret < B_OK) { 117 fprintf(stderr, "failed to restore settings: %s\n", strerror(ret)); 118 return ret; 119 } 120 121 return B_OK; 122 } 123 124 125 status_t 126 DriveSetup::_GetSettingsFile(BFile& file, bool forWriting) const 127 { 128 BPath path; 129 status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 130 if (ret != B_OK) { 131 fprintf(stderr, "failed to get user settings folder: %s\n", 132 strerror(ret)); 133 return ret; 134 } 135 136 ret = path.Append("DriveSetup"); 137 if (ret != B_OK) { 138 fprintf(stderr, "failed to construct path: %s\n", strerror(ret)); 139 return ret; 140 } 141 142 uint32 writeFlags = B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY; 143 uint32 readFlags = B_READ_ONLY; 144 145 ret = file.SetTo(path.Path(), forWriting ? writeFlags : readFlags); 146 if (ret != B_OK) { 147 fprintf(stderr, "failed to init file: %s\n", strerror(ret)); 148 return ret; 149 } 150 151 return B_OK; 152 } 153 154 155