1 /* 2 * Copyright 2017 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Brian Hill 7 */ 8 9 10 #include "RepositoriesSettings.h" 11 12 #include <FindDirectory.h> 13 #include <StringList.h> 14 15 #include "constants.h" 16 17 const char* settingsFilename = "Repositories_settings"; 18 19 20 RepositoriesSettings::RepositoriesSettings() 21 { 22 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &fFilePath); 23 if (status == B_OK) 24 status = fFilePath.Append(settingsFilename); 25 fInitStatus = status; 26 } 27 28 29 BRect 30 RepositoriesSettings::GetFrame() 31 { 32 BMessage settings(_ReadFromFile()); 33 BRect frame; 34 status_t status = settings.FindRect(key_frame, &frame); 35 // Set default off screen so it will center itself 36 if (status != B_OK) 37 frame.Set(-10, -10, 750, 300); 38 return frame; 39 } 40 41 42 void 43 RepositoriesSettings::SetFrame(BRect frame) 44 { 45 BMessage settings(_ReadFromFile()); 46 settings.RemoveData(key_frame); 47 settings.AddRect(key_frame, frame); 48 _SaveToFile(settings); 49 } 50 51 52 status_t 53 RepositoriesSettings::GetRepositories(int32& repoCount, BStringList& nameList, 54 BStringList& urlList) 55 { 56 BMessage settings(_ReadFromFile()); 57 type_code type; 58 int32 count; 59 settings.GetInfo(key_name, &type, &count); 60 61 status_t result = B_OK; 62 int32 index, total = 0; 63 BString foundName, foundUrl; 64 // get each repository and add to lists 65 for (index = 0; index < count; index++) { 66 status_t result1 = settings.FindString(key_name, index, &foundName); 67 status_t result2 = settings.FindString(key_url, index, &foundUrl); 68 if (result1 == B_OK && result2 == B_OK) { 69 nameList.Add(foundName); 70 urlList.Add(foundUrl); 71 total++; 72 } else 73 result = B_ERROR; 74 } 75 repoCount = total; 76 return result; 77 } 78 79 80 void 81 RepositoriesSettings::SetRepositories(BStringList& nameList, BStringList& urlList) 82 { 83 BMessage settings(_ReadFromFile()); 84 settings.RemoveName(key_name); 85 settings.RemoveName(key_url); 86 87 int32 index, count = nameList.CountStrings(); 88 for (index = 0; index < count; index++) { 89 settings.AddString(key_name, nameList.StringAt(index)); 90 settings.AddString(key_url, urlList.StringAt(index)); 91 } 92 _SaveToFile(settings); 93 } 94 95 96 BMessage 97 RepositoriesSettings::_ReadFromFile() 98 { 99 BMessage settings; 100 status_t status = fFile.SetTo(fFilePath.Path(), B_READ_ONLY); 101 if (status == B_OK) 102 status = settings.Unflatten(&fFile); 103 fFile.Unset(); 104 return settings; 105 } 106 107 108 status_t 109 RepositoriesSettings::_SaveToFile(BMessage settings) 110 { 111 status_t status = fFile.SetTo(fFilePath.Path(), 112 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 113 if (status == B_OK) 114 status = settings.Flatten(&fFile); 115 fFile.Unset(); 116 return status; 117 } 118