BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / RepositoryCache.cpp
blobc172eb5a9bdb4a3919ab06db3540cca0d2c37a8a
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 * Ingo Weinhold <ingo_weinhold@gmx.de>
8 */
11 #include <package/RepositoryCache.h>
13 #include <stdio.h>
15 #include <new>
17 #include <Directory.h>
18 #include <File.h>
19 #include <FindDirectory.h>
20 #include <Path.h>
22 #include <package/hpkg/ErrorOutput.h>
23 #include <package/hpkg/PackageInfoAttributeValue.h>
24 #include <package/hpkg/RepositoryContentHandler.h>
25 #include <package/hpkg/RepositoryReader.h>
26 #include <package/hpkg/StandardErrorOutput.h>
27 #include <package/PackageInfo.h>
28 #include <package/RepositoryInfo.h>
30 #include <package/PackageInfoContentHandler.h>
33 namespace BPackageKit {
36 using namespace BHPKG;
39 // #pragma mark - RepositoryContentHandler
42 struct BRepositoryCache::RepositoryContentHandler : BRepositoryContentHandler {
43 RepositoryContentHandler(BRepositoryInfo& repositoryInfo,
44 BPackageInfoSet& packages, BErrorOutput* errorOutput)
46 fRepositoryInfo(repositoryInfo),
47 fPackageInfo(),
48 fPackages(packages),
49 fPackageInfoContentHandler(fPackageInfo, errorOutput)
53 virtual status_t HandlePackage(const char* packageName)
55 fPackageInfo.Clear();
56 return B_OK;
60 virtual status_t HandlePackageAttribute(
61 const BPackageInfoAttributeValue& value)
63 return fPackageInfoContentHandler.HandlePackageAttribute(value);
66 virtual status_t HandlePackageDone(const char* packageName)
68 status_t result = fPackageInfo.InitCheck();
69 if (result != B_OK)
70 return result;
72 result = fPackages.AddInfo(fPackageInfo);
73 if (result != B_OK)
74 return result;
76 return B_OK;
79 virtual status_t HandleRepositoryInfo(const BRepositoryInfo& repositoryInfo)
81 fRepositoryInfo = repositoryInfo;
83 return B_OK;
86 virtual void HandleErrorOccurred()
90 private:
91 BRepositoryInfo& fRepositoryInfo;
92 BPackageInfo fPackageInfo;
93 BPackageInfoSet& fPackages;
94 BPackageInfoContentHandler fPackageInfoContentHandler;
98 // #pragma mark - BRepositoryCache
101 BRepositoryCache::BRepositoryCache()
103 fIsUserSpecific(false),
104 fPackages()
109 BRepositoryCache::~BRepositoryCache()
114 const BEntry&
115 BRepositoryCache::Entry() const
117 return fEntry;
121 const BRepositoryInfo&
122 BRepositoryCache::Info() const
124 return fInfo;
128 bool
129 BRepositoryCache::IsUserSpecific() const
131 return fIsUserSpecific;
135 void
136 BRepositoryCache::SetIsUserSpecific(bool isUserSpecific)
138 fIsUserSpecific = isUserSpecific;
142 status_t
143 BRepositoryCache::SetTo(const BEntry& entry)
145 // unset
146 fPackages.MakeEmpty();
147 fEntry.Unset();
149 // get cache file path
150 fEntry = entry;
152 BPath repositoryCachePath;
153 status_t result;
154 if ((result = entry.GetPath(&repositoryCachePath)) != B_OK)
155 return result;
157 // read repository cache
158 BStandardErrorOutput errorOutput;
159 BRepositoryReader repositoryReader(&errorOutput);
160 if ((result = repositoryReader.Init(repositoryCachePath.Path())) != B_OK)
161 return result;
163 RepositoryContentHandler handler(fInfo, fPackages, &errorOutput);
164 if ((result = repositoryReader.ParseContent(&handler)) != B_OK)
165 return result;
167 BPath userSettingsPath;
168 if (find_directory(B_USER_SETTINGS_DIRECTORY, &userSettingsPath) == B_OK) {
169 BDirectory userSettingsDir(userSettingsPath.Path());
170 fIsUserSpecific = userSettingsDir.Contains(&entry);
171 } else
172 fIsUserSpecific = false;
174 return B_OK;
178 uint32
179 BRepositoryCache::CountPackages() const
181 return fPackages.CountInfos();
185 BRepositoryCache::Iterator
186 BRepositoryCache::GetIterator() const
188 return fPackages.GetIterator();
192 } // namespace BPackageKit