1/*
2 * Copyright 2020-2021, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#ifndef PACKAGE_ICON_TAR_REPOSITORY_H
6#define PACKAGE_ICON_TAR_REPOSITORY_H
7
8
9#include <DataIO.h>
10#include <HashMap.h>
11#include <HashString.h>
12#include <Locker.h>
13#include <Path.h>
14#include <Referenceable.h>
15
16#include "IconTarPtr.h"
17#include "LRUCache.h"
18#include "PackageIconRepository.h"
19
20typedef BReference<IconTarPtr> IconTarPtrRef;
21
22
23class PackageIconTarRepository : public PackageIconRepository {
24public:
25								PackageIconTarRepository();
26	virtual						~PackageIconTarRepository();
27
28			status_t			Init(BPath& tarPath);
29
30			void				AddIconTarPtr(const BString& pkgName,
31									BitmapSize size, off_t offset);
32	virtual	status_t			GetIcon(const BString& pkgName, BitmapSize size,
33									BitmapRef& bitmap);
34	virtual	bool				HasAnyIcon(const BString& pkgName);
35	virtual	void				Clear();
36
37	static	void				CleanupDefaultIcon();
38
39private:
40			void				_Close();
41
42	const	char*				_ToIconCacheKeySuffix(BitmapSize size);
43	const	HashString			_ToIconCacheKey(const BString& pkgName,
44									BitmapSize size);
45
46			IconTarPtrRef		_GetOrCreateIconTarPtr(const BString& pkgName);
47			IconTarPtrRef		_GetIconTarPtr(const BString& pkgName) const;
48
49			status_t			_CreateIconFromTarOffset(off_t offset,
50									BitmapRef& bitmap);
51
52	static	off_t				_OffsetToBestRepresentation(
53									const IconTarPtrRef iconTarPtrRef,
54									BitmapSize desiredSize,
55									BitmapSize* actualSize);
56
57private:
58			BLocker				fLock;
59			BPositionIO*		fTarIo;
60			LRUCache<HashString, BitmapRef>
61								fIconCache;
62			HashMap<HashString, IconTarPtrRef>
63								fIconTarPtrs;
64
65	static	BitmapRef			sDefaultIcon;
66};
67
68#endif // PACKAGE_ICON_TAR_REPOSITORY_H
69