1 /* 2 * Copyright 2011-2013, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Oliver Tappe <zooey@hirschkaefer.de> 7 */ 8 9 10 #include <package/RepositoryConfig.h> 11 12 #include <stdlib.h> 13 14 #include <new> 15 16 #include <Directory.h> 17 #include <driver_settings.h> 18 #include <File.h> 19 #include <FindDirectory.h> 20 #include <Path.h> 21 22 #include <DriverSettings.h> 23 24 25 namespace BPackageKit { 26 27 28 BRepositoryConfig::BRepositoryConfig() 29 : 30 fInitStatus(B_NO_INIT), 31 fPriority(kUnsetPriority), 32 fIsUserSpecific(false) 33 { 34 } 35 36 37 BRepositoryConfig::BRepositoryConfig(const BEntry& entry) 38 { 39 SetTo(entry); 40 } 41 42 43 BRepositoryConfig::BRepositoryConfig(const BString& name, 44 const BString& baseURL, uint8 priority) 45 : 46 fInitStatus(B_OK), 47 fName(name), 48 fBaseURL(baseURL), 49 fPriority(priority), 50 fIsUserSpecific(false) 51 { 52 } 53 54 55 BRepositoryConfig::~BRepositoryConfig() 56 { 57 } 58 59 60 status_t 61 BRepositoryConfig::Store(const BEntry& entry) const 62 { 63 BFile file(&entry, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 64 status_t result = file.InitCheck(); 65 if (result != B_OK) 66 return result; 67 68 BString configString; 69 configString 70 << "url=" << fBaseURL << "\n" 71 << "priority=" << fPriority << "\n"; 72 73 int32 size = configString.Length(); 74 if ((result = file.Write(configString.String(), size)) < size) 75 return (result >= 0) ? B_ERROR : result; 76 77 return B_OK; 78 } 79 80 81 status_t 82 BRepositoryConfig::InitCheck() const 83 { 84 return fInitStatus; 85 } 86 87 88 status_t 89 BRepositoryConfig::SetTo(const BEntry& entry) 90 { 91 fEntry = entry; 92 fInitStatus = B_NO_INIT; 93 94 entry_ref ref; 95 status_t result = entry.GetRef(&ref); 96 if (result != B_OK) 97 return result; 98 99 BDriverSettings driverSettings; 100 result = driverSettings.Load(ref); 101 if (result != B_OK) 102 return result; 103 104 const char* url = driverSettings.GetParameterValue("url"); 105 const char* priorityString = driverSettings.GetParameterValue("priority"); 106 107 if (url == NULL || *url == '\0') 108 return B_BAD_DATA; 109 110 fName = entry.Name(); 111 fBaseURL = url; 112 fPriority = priorityString == NULL 113 ? kUnsetPriority : atoi(priorityString); 114 115 BPath userSettingsPath; 116 if (find_directory(B_USER_SETTINGS_DIRECTORY, &userSettingsPath) == B_OK) { 117 BDirectory userSettingsDir(userSettingsPath.Path()); 118 fIsUserSpecific = userSettingsDir.Contains(&entry); 119 } else 120 fIsUserSpecific = false; 121 122 fInitStatus = B_OK; 123 124 return B_OK; 125 } 126 127 128 const BString& 129 BRepositoryConfig::Name() const 130 { 131 return fName; 132 } 133 134 135 const BString& 136 BRepositoryConfig::BaseURL() const 137 { 138 return fBaseURL; 139 } 140 141 142 uint8 143 BRepositoryConfig::Priority() const 144 { 145 return fPriority; 146 } 147 148 149 bool 150 BRepositoryConfig::IsUserSpecific() const 151 { 152 return fIsUserSpecific; 153 } 154 155 156 const BEntry& 157 BRepositoryConfig::Entry() const 158 { 159 return fEntry; 160 } 161 162 163 BString 164 BRepositoryConfig::PackagesURL() const 165 { 166 if (fBaseURL.IsEmpty()) 167 return BString(); 168 return BString().SetToFormat("%s/packages", fBaseURL.String()); 169 } 170 171 172 void 173 BRepositoryConfig::SetName(const BString& name) 174 { 175 fName = name; 176 } 177 178 179 void 180 BRepositoryConfig::SetBaseURL(const BString& baseURL) 181 { 182 fBaseURL = baseURL; 183 } 184 185 186 void 187 BRepositoryConfig::SetPriority(uint8 priority) 188 { 189 fPriority = priority; 190 } 191 192 193 void 194 BRepositoryConfig::SetIsUserSpecific(bool isUserSpecific) 195 { 196 fIsUserSpecific = isUserSpecific; 197 } 198 199 200 } // namespace BPackageKit 201