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 BEntry fileEntry(fFilePath.Path()); 26 if (!fileEntry.Exists()) { 27 // Create default repos 28 BStringList nameList, urlList; 29 int32 count = (sizeof(kDefaultRepos) / sizeof(Repository)); 30 for (int16 index = 0; index < count; index++) { 31 nameList.Add(kDefaultRepos[index].name); 32 urlList.Add(kDefaultRepos[index].url); 33 } 34 SetRepositories(nameList, urlList); 35 } 36 fInitStatus = status; 37 } 38 39 40 BRect 41 RepositoriesSettings::GetFrame() 42 { 43 BMessage settings(_ReadFromFile()); 44 BRect frame; 45 status_t status = settings.FindRect(key_frame, &frame); 46 // Set default off screen so it will center itself 47 if (status != B_OK) 48 frame.Set(-10, -10, 750, 300); 49 return frame; 50 } 51 52 53 void 54 RepositoriesSettings::SetFrame(BRect frame) 55 { 56 BMessage settings(_ReadFromFile()); 57 settings.RemoveData(key_frame); 58 settings.AddRect(key_frame, frame); 59 _SaveToFile(settings); 60 } 61 62 63 status_t 64 RepositoriesSettings::GetRepositories(int32& repoCount, BStringList& nameList, 65 BStringList& urlList) 66 { 67 BMessage settings(_ReadFromFile()); 68 type_code type; 69 int32 count; 70 settings.GetInfo(key_name, &type, &count); 71 72 status_t result = B_OK; 73 int32 index, total = 0; 74 BString foundName, foundUrl; 75 // get each repository and add to lists 76 for (index = 0; index < count; index++) { 77 status_t result1 = settings.FindString(key_name, index, &foundName); 78 status_t result2 = settings.FindString(key_url, index, &foundUrl); 79 if (result1 == B_OK && result2 == B_OK) { 80 nameList.Add(foundName); 81 urlList.Add(foundUrl); 82 total++; 83 } else 84 result = B_ERROR; 85 } 86 repoCount = total; 87 return result; 88 } 89 90 91 void 92 RepositoriesSettings::SetRepositories(BStringList& nameList, BStringList& urlList) 93 { 94 BMessage settings(_ReadFromFile()); 95 settings.RemoveName(key_name); 96 settings.RemoveName(key_url); 97 98 int32 index, count = nameList.CountStrings(); 99 for (index = 0; index < count; index++) { 100 settings.AddString(key_name, nameList.StringAt(index)); 101 settings.AddString(key_url, urlList.StringAt(index)); 102 } 103 _SaveToFile(settings); 104 } 105 106 107 BMessage 108 RepositoriesSettings::_ReadFromFile() 109 { 110 BMessage settings; 111 status_t status = fFile.SetTo(fFilePath.Path(), B_READ_ONLY); 112 if (status == B_OK) 113 status = settings.Unflatten(&fFile); 114 fFile.Unset(); 115 return settings; 116 } 117 118 119 status_t 120 RepositoriesSettings::_SaveToFile(BMessage settings) 121 { 122 status_t status = fFile.SetTo(fFilePath.Path(), 123 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 124 if (status == B_OK) 125 status = settings.Flatten(&fFile); 126 fFile.Unset(); 127 return status; 128 } 129