ImageCache.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2009-2011, Andrea Anzani. All rights reserved.
  3. * Copyright 2021, Jaidyn Levesque. All rights reserved.
  4. * Distributed under the terms of the MIT License.
  5. *
  6. * Authors:
  7. * Andrea Anzani, andrea.anzani@gmail.com
  8. */
  9. #include "ImageCache.h"
  10. #include <AppDefs.h>
  11. #include <Bitmap.h>
  12. #include <Debug.h>
  13. #include <Resources.h>
  14. #include <TranslationUtils.h>
  15. #include <libinterface/BitmapUtils.h>
  16. #include "AppResources.h"
  17. #include "Utils.h"
  18. ImageCache* ImageCache::fInstance = NULL;
  19. ImageCache::ImageCache()
  20. {
  21. _LoadResource(kPersonIcon, "kPersonIcon");
  22. _LoadResource(kOnePersonIcon, "kOnePersonIcon");
  23. _LoadResource(kTwoPeopleIcon, "kTwoPeopleIcon");
  24. _LoadResource(kThreePeopleIcon, "kThreePeopleIcon");
  25. _LoadResource(kFourPeopleIcon, "kFourPeopleIcon");
  26. _LoadResource(kMorePeopleIcon, "kMorePeopleIcon");
  27. _LoadResource(kAwayReplicant, "kAwayReplicant");
  28. _LoadResource(kBusyReplicant, "kBusyReplicant");
  29. _LoadResource(kOfflineReplicant, "kOfflineReplicant");
  30. _LoadResource(kOnlineReplicant, "kOnlineReplicant");
  31. _LoadResource(kAsteriskIcon, "kAsteriskIcon");
  32. }
  33. ImageCache::~ImageCache()
  34. {
  35. while (fBitmaps.CountItems()) {
  36. BBitmap* bit = fBitmaps.ValueFor(0);
  37. delete bit;
  38. }
  39. }
  40. ImageCache*
  41. ImageCache::Get()
  42. {
  43. if (fInstance == NULL)
  44. fInstance = new ImageCache();
  45. return fInstance;
  46. }
  47. BBitmap*
  48. ImageCache::GetImage(const char* keyName)
  49. {
  50. // Loads the bitmap if found
  51. bool found;
  52. BBitmap* bitmap = fBitmaps.ValueFor(BString(keyName), &found);
  53. if (found == true)
  54. return bitmap;
  55. return NULL;
  56. }
  57. void
  58. ImageCache::AddImage(BString name, BBitmap* which)
  59. {
  60. fBitmaps.AddItem(name, which);
  61. }
  62. void
  63. ImageCache::DeleteImage(BString name)
  64. {
  65. BBitmap* bitmap = fBitmaps.ValueFor(name);
  66. if (bitmap) {
  67. fBitmaps.RemoveItemFor(name);
  68. delete bitmap;
  69. }
  70. }
  71. void
  72. ImageCache::Release()
  73. {
  74. if (fInstance != NULL) {
  75. delete fInstance;
  76. fInstance = NULL;
  77. }
  78. }
  79. void
  80. ImageCache::_LoadResource(int identifier, const char* key)
  81. {
  82. BResources res = ChatResources();
  83. if (res.InitCheck() != B_OK)
  84. return;
  85. BBitmap* bitmap = IconFromResources(&res, identifier, B_LARGE_ICON);
  86. if (bitmap != NULL && bitmap->IsValid() == true)
  87. fBitmaps.AddItem(BString(key), bitmap);
  88. }