1 /* 2 * Copyright 2020, 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 20 typedef BReference<IconTarPtr> IconTarPtrRef; 21 22 23 class PackageIconTarRepository : public PackageIconRepository { 24 public: 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 36 static void CleanupDefaultIcon(); 37 38 private: 39 void _Close(); 40 41 const char* _ToIconCacheKeySuffix(BitmapSize size); 42 const HashString _ToIconCacheKey(const BString& pkgName, 43 BitmapSize size); 44 45 IconTarPtrRef _GetOrCreateIconTarPtr(const BString& pkgName); 46 IconTarPtrRef _GetIconTarPtr(const BString& pkgName) const; 47 48 status_t _CreateIconFromTarOffset(off_t offset, 49 BitmapRef& bitmap); 50 51 static off_t _OffsetToBestRepresentation( 52 const IconTarPtrRef iconTarPtrRef, 53 BitmapSize desiredSize, 54 BitmapSize* actualSize); 55 56 private: 57 BLocker fLock; 58 BPositionIO* fTarIo; 59 LRUCache<HashString, BitmapRef> 60 fIconCache; 61 HashMap<HashString, IconTarPtrRef> 62 fIconTarPtrs; 63 64 static BitmapRef sDefaultIcon; 65 }; 66 67 #endif // PACKAGE_ICON_TAR_REPOSITORY_H 68