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