2 * Copyright 2011-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Oliver Tappe <zooey@hirschkaefer.de>
10 #include <package/PackageRoster.h>
15 #include <Directory.h>
17 #include <Messenger.h>
20 #include <StringList.h>
22 #include <package/InstallationLocationInfo.h>
23 #include <package/PackageInfo.h>
24 #include <package/PackageInfoContentHandler.h>
25 #include <package/PackageInfoSet.h>
26 #include <package/RepositoryCache.h>
27 #include <package/RepositoryConfig.h>
29 #include <package/hpkg/PackageReader.h>
32 #if defined(__HAIKU__) && !defined(HAIKU_HOST_PLATFORM_HAIKU)
33 # include <package/DaemonClient.h>
34 # include <RegistrarDefs.h>
35 # include <RosterPrivate.h>
39 namespace BPackageKit
{
42 using namespace BHPKG
;
45 BPackageRoster::BPackageRoster()
50 BPackageRoster::~BPackageRoster()
56 BPackageRoster::GetCommonRepositoryConfigPath(BPath
* path
, bool create
) const
58 return _GetRepositoryPath(path
, create
, B_SYSTEM_SETTINGS_DIRECTORY
);
63 BPackageRoster::GetUserRepositoryConfigPath(BPath
* path
, bool create
) const
65 return _GetRepositoryPath(path
, create
, B_USER_SETTINGS_DIRECTORY
);
70 BPackageRoster::GetCommonRepositoryCachePath(BPath
* path
, bool create
) const
72 return _GetRepositoryPath(path
, create
, B_SYSTEM_CACHE_DIRECTORY
);
77 BPackageRoster::GetUserRepositoryCachePath(BPath
* path
, bool create
) const
79 return _GetRepositoryPath(path
, create
, B_USER_CACHE_DIRECTORY
);
84 BPackageRoster::VisitCommonRepositoryConfigs(BRepositoryConfigVisitor
& visitor
)
86 BPath commonRepositoryConfigPath
;
88 = GetCommonRepositoryConfigPath(&commonRepositoryConfigPath
);
92 return _VisitRepositoryConfigs(commonRepositoryConfigPath
, visitor
);
97 BPackageRoster::VisitUserRepositoryConfigs(BRepositoryConfigVisitor
& visitor
)
99 BPath userRepositoryConfigPath
;
100 status_t result
= GetUserRepositoryConfigPath(&userRepositoryConfigPath
);
104 return _VisitRepositoryConfigs(userRepositoryConfigPath
, visitor
);
109 BPackageRoster::GetRepositoryNames(BStringList
& names
)
111 struct RepositoryNameCollector
: public BRepositoryConfigVisitor
{
112 RepositoryNameCollector(BStringList
& _names
)
116 status_t
operator()(const BEntry
& entry
)
118 char name
[B_FILE_NAME_LENGTH
];
119 status_t result
= entry
.GetName(name
);
122 int32 count
= names
.CountStrings();
123 for (int i
= 0; i
< count
; ++i
) {
124 if (names
.StringAt(i
).Compare(name
) == 0)
132 RepositoryNameCollector
repositoryNameCollector(names
);
133 status_t result
= VisitUserRepositoryConfigs(repositoryNameCollector
);
137 return VisitCommonRepositoryConfigs(repositoryNameCollector
);
142 BPackageRoster::GetRepositoryCache(const BString
& name
,
143 BRepositoryCache
* repositoryCache
)
145 if (repositoryCache
== NULL
)
148 // user path has higher precedence than common path
150 status_t result
= GetUserRepositoryCachePath(&path
);
153 path
.Append(name
.String());
155 BEntry
repoCacheEntry(path
.Path());
156 if (repoCacheEntry
.Exists())
157 return repositoryCache
->SetTo(repoCacheEntry
);
159 if ((result
= GetCommonRepositoryCachePath(&path
, true)) != B_OK
)
161 path
.Append(name
.String());
163 repoCacheEntry
.SetTo(path
.Path());
164 return repositoryCache
->SetTo(repoCacheEntry
);
169 BPackageRoster::GetRepositoryConfig(const BString
& name
,
170 BRepositoryConfig
* repositoryConfig
)
172 if (repositoryConfig
== NULL
)
175 // user path has higher precedence than common path
177 status_t result
= GetUserRepositoryConfigPath(&path
);
180 path
.Append(name
.String());
182 BEntry
repoConfigEntry(path
.Path());
183 if (repoConfigEntry
.Exists())
184 return repositoryConfig
->SetTo(repoConfigEntry
);
186 if ((result
= GetCommonRepositoryConfigPath(&path
, true)) != B_OK
)
188 path
.Append(name
.String());
190 repoConfigEntry
.SetTo(path
.Path());
191 return repositoryConfig
->SetTo(repoConfigEntry
);
196 BPackageRoster::GetInstallationLocationInfo(
197 BPackageInstallationLocation location
, BInstallationLocationInfo
& _info
)
199 // This method makes sense only on an installed Haiku, but not for the build
201 #if defined(__HAIKU__) && !defined(HAIKU_HOST_PLATFORM_HAIKU)
202 return BPackageKit::BPrivate::BDaemonClient().GetInstallationLocationInfo(
205 return B_NOT_SUPPORTED
;
211 BPackageRoster::GetActivePackages(BPackageInstallationLocation location
,
212 BPackageInfoSet
& packageInfos
)
214 // This method makes sense only on an installed Haiku, but not for the build
216 #if defined(__HAIKU__) && !defined(HAIKU_HOST_PLATFORM_HAIKU)
217 BInstallationLocationInfo info
;
218 status_t error
= GetInstallationLocationInfo(location
, info
);
222 packageInfos
= info
.LatestActivePackageInfos();
225 return B_NOT_SUPPORTED
;
231 BPackageRoster::StartWatching(const BMessenger
& target
, uint32 eventMask
)
233 // This method makes sense only on an installed Haiku, but not for the build
235 #if defined(__HAIKU__) && !defined(HAIKU_HOST_PLATFORM_HAIKU)
236 // compose the registrar request
237 BMessage
request(::BPrivate::B_REG_PACKAGE_START_WATCHING
);
239 if ((error
= request
.AddMessenger("target", target
)) != B_OK
240 || (error
= request
.AddUInt32("events", eventMask
)) != B_OK
) {
246 error
= BRoster::Private().SendTo(&request
, &reply
, false);
251 if (reply
.what
!= ::BPrivate::B_REG_SUCCESS
) {
253 if (reply
.FindInt32("error", &result
) != B_OK
)
255 return (status_t
)error
;
260 return B_NOT_SUPPORTED
;
266 BPackageRoster::StopWatching(const BMessenger
& target
)
268 // This method makes sense only on an installed Haiku, but not for the build
270 #if defined(__HAIKU__) && !defined(HAIKU_HOST_PLATFORM_HAIKU)
271 // compose the registrar request
272 BMessage
request(::BPrivate::B_REG_PACKAGE_STOP_WATCHING
);
273 status_t error
= request
.AddMessenger("target", target
);
279 error
= BRoster::Private().SendTo(&request
, &reply
, false);
284 if (reply
.what
!= ::BPrivate::B_REG_SUCCESS
) {
286 if (reply
.FindInt32("error", &result
) != B_OK
)
288 return (status_t
)error
;
293 return B_NOT_SUPPORTED
;
299 BPackageRoster::_GetRepositoryPath(BPath
* path
, bool create
,
300 directory_which whichDir
) const
305 status_t result
= find_directory(whichDir
, path
);
308 if ((result
= path
->Append("package-repositories")) != B_OK
)
312 BEntry
entry(path
->Path(), true);
313 if (!entry
.Exists()) {
314 if (mkdir(path
->Path(), 0755) != 0)
324 BPackageRoster::_VisitRepositoryConfigs(const BPath
& path
,
325 BRepositoryConfigVisitor
& visitor
)
327 BDirectory
directory(path
.Path());
328 status_t result
= directory
.InitCheck();
329 if (result
== B_ENTRY_NOT_FOUND
)
335 while (directory
.GetNextEntry(&entry
, true) == B_OK
) {
336 if ((result
= visitor(entry
)) != B_OK
)
344 } // namespace BPackageKit