2 * Copyright 2011-2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Oliver Tappe <zooey@hirschkaefer.de>
7 * Ingo Weinhold <ingo_weinhold@gmx.de>
11 #include <package/RepositoryCache.h>
17 #include <Directory.h>
19 #include <FindDirectory.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
),
49 fPackageInfoContentHandler(fPackageInfo
, errorOutput
)
53 virtual status_t
HandlePackage(const char* packageName
)
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();
72 result
= fPackages
.AddInfo(fPackageInfo
);
79 virtual status_t
HandleRepositoryInfo(const BRepositoryInfo
& repositoryInfo
)
81 fRepositoryInfo
= repositoryInfo
;
86 virtual void HandleErrorOccurred()
91 BRepositoryInfo
& fRepositoryInfo
;
92 BPackageInfo fPackageInfo
;
93 BPackageInfoSet
& fPackages
;
94 BPackageInfoContentHandler fPackageInfoContentHandler
;
98 // #pragma mark - BRepositoryCache
101 BRepositoryCache::BRepositoryCache()
103 fIsUserSpecific(false),
109 BRepositoryCache::~BRepositoryCache()
115 BRepositoryCache::Entry() const
121 const BRepositoryInfo
&
122 BRepositoryCache::Info() const
129 BRepositoryCache::IsUserSpecific() const
131 return fIsUserSpecific
;
136 BRepositoryCache::SetIsUserSpecific(bool isUserSpecific
)
138 fIsUserSpecific
= isUserSpecific
;
143 BRepositoryCache::SetTo(const BEntry
& entry
)
146 fPackages
.MakeEmpty();
149 // get cache file path
152 BPath repositoryCachePath
;
154 if ((result
= entry
.GetPath(&repositoryCachePath
)) != B_OK
)
157 // read repository cache
158 BStandardErrorOutput errorOutput
;
159 BRepositoryReader
repositoryReader(&errorOutput
);
160 if ((result
= repositoryReader
.Init(repositoryCachePath
.Path())) != B_OK
)
163 RepositoryContentHandler
handler(fInfo
, fPackages
, &errorOutput
);
164 if ((result
= repositoryReader
.ParseContent(&handler
)) != B_OK
)
167 BPath userSettingsPath
;
168 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &userSettingsPath
) == B_OK
) {
169 BDirectory
userSettingsDir(userSettingsPath
.Path());
170 fIsUserSpecific
= userSettingsDir
.Contains(&entry
);
172 fIsUserSpecific
= false;
179 BRepositoryCache::CountPackages() const
181 return fPackages
.CountInfos();
185 BRepositoryCache::Iterator
186 BRepositoryCache::GetIterator() const
188 return fPackages
.GetIterator();
192 } // namespace BPackageKit