BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / RepositoryConfig.cpp
bloba5eeb494478be14657fc1efa91487ffca6cd1620
1 /*
2 * Copyright 2011-2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Oliver Tappe <zooey@hirschkaefer.de>
7 */
10 #include <package/RepositoryConfig.h>
12 #include <stdlib.h>
14 #include <new>
16 #include <Directory.h>
17 #include <driver_settings.h>
18 #include <File.h>
19 #include <FindDirectory.h>
20 #include <Path.h>
22 #include <DriverSettings.h>
25 namespace BPackageKit {
28 BRepositoryConfig::BRepositoryConfig()
30 fInitStatus(B_NO_INIT),
31 fPriority(kUnsetPriority),
32 fIsUserSpecific(false)
37 BRepositoryConfig::BRepositoryConfig(const BEntry& entry)
39 SetTo(entry);
43 BRepositoryConfig::BRepositoryConfig(const BString& name,
44 const BString& baseURL, uint8 priority)
46 fInitStatus(B_OK),
47 fName(name),
48 fBaseURL(baseURL),
49 fPriority(priority),
50 fIsUserSpecific(false)
55 BRepositoryConfig::~BRepositoryConfig()
60 status_t
61 BRepositoryConfig::Store(const BEntry& entry) const
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;
68 BString configString;
69 configString
70 << "url=" << fBaseURL << "\n"
71 << "priority=" << fPriority << "\n";
73 int32 size = configString.Length();
74 if ((result = file.Write(configString.String(), size)) < size)
75 return (result >= 0) ? B_ERROR : result;
77 return B_OK;
81 status_t
82 BRepositoryConfig::InitCheck() const
84 return fInitStatus;
88 status_t
89 BRepositoryConfig::SetTo(const BEntry& entry)
91 fEntry = entry;
92 fInitStatus = B_NO_INIT;
94 entry_ref ref;
95 status_t result = entry.GetRef(&ref);
96 if (result != B_OK)
97 return result;
99 BDriverSettings driverSettings;
100 result = driverSettings.Load(ref);
101 if (result != B_OK)
102 return result;
104 const char* url = driverSettings.GetParameterValue("url");
105 const char* priorityString = driverSettings.GetParameterValue("priority");
107 if (url == NULL || *url == '\0')
108 return B_BAD_DATA;
110 fName = entry.Name();
111 fBaseURL = url;
112 fPriority = priorityString == NULL
113 ? kUnsetPriority : atoi(priorityString);
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;
122 fInitStatus = B_OK;
124 return B_OK;
128 const BString&
129 BRepositoryConfig::Name() const
131 return fName;
135 const BString&
136 BRepositoryConfig::BaseURL() const
138 return fBaseURL;
142 uint8
143 BRepositoryConfig::Priority() const
145 return fPriority;
149 bool
150 BRepositoryConfig::IsUserSpecific() const
152 return fIsUserSpecific;
156 const BEntry&
157 BRepositoryConfig::Entry() const
159 return fEntry;
163 BString
164 BRepositoryConfig::PackagesURL() const
166 if (fBaseURL.IsEmpty())
167 return BString();
168 return BString().SetToFormat("%s/packages", fBaseURL.String());
172 void
173 BRepositoryConfig::SetName(const BString& name)
175 fName = name;
179 void
180 BRepositoryConfig::SetBaseURL(const BString& baseURL)
182 fBaseURL = baseURL;
186 void
187 BRepositoryConfig::SetPriority(uint8 priority)
189 fPriority = priority;
193 void
194 BRepositoryConfig::SetIsUserSpecific(bool isUserSpecific)
196 fIsUserSpecific = isUserSpecific;
200 } // namespace BPackageKit