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