HaikuDepot: Added some listener support to PackageInfo

This commit is contained in:
Stephan Aßmus
2013-09-17 14:40:40 +02:00
committed by Rene Gollent
parent 697c0d7c63
commit 92dbf1869f
4 changed files with 74 additions and 4 deletions
+50 -2
View File
@@ -535,7 +535,12 @@ PackageInfo::AddCategory(const CategoryRef& category)
bool bool
PackageInfo::AddUserRating(const UserRating& rating) PackageInfo::AddUserRating(const UserRating& rating)
{ {
return fUserRatings.Add(rating); if (!fUserRatings.Add(rating))
return false;
_NotifyListeners(PKG_CHANGED_RATINGS);
return true;
} }
@@ -584,7 +589,50 @@ PackageInfo::CalculateRatingSummary() const
bool bool
PackageInfo::AddScreenshot(const BitmapRef& screenshot) PackageInfo::AddScreenshot(const BitmapRef& screenshot)
{ {
return fScreenshots.Add(screenshot); if (!fScreenshots.Add(screenshot))
return false;
_NotifyListeners(PKG_CHANGED_SCREENSHOTS);
return true;
}
bool
PackageInfo::AddListener(const PackageInfoListenerRef& listener)
{
return fListeners.Add(listener);
}
void
PackageInfo::RemoveListener(const PackageInfoListenerRef& listener)
{
fListeners.Remove(listener);
}
void
PackageInfo::_NotifyListeners(uint32 changes)
{
int count = fListeners.CountItems();
if (count == 0)
return;
// Clone list to avoid listeners detaching themselves in notifications
// to screw up the list while iterating it.
PackageListenerList listeners(fListeners);
// Check if it worked:
if (listeners.CountItems () != count)
return;
PackageInfoEvent event(PackageInfoRef(this), changes);
for (int i = 0; i < count; i++) {
const PackageInfoListenerRef& listener = listeners.ItemAtFast(i);
if (listener.Get() != NULL)
listener->PackageChanged(event);
}
} }
+13
View File
@@ -10,6 +10,7 @@
#include <String.h> #include <String.h>
#include "List.h" #include "List.h"
#include "PackageInfoListener.h"
class BBitmap; class BBitmap;
@@ -181,6 +182,9 @@ typedef BReference<PackageCategory> CategoryRef;
typedef List<CategoryRef, false> CategoryList; typedef List<CategoryRef, false> CategoryList;
typedef List<PackageInfoListenerRef, false, 2> PackageListenerList;
class PackageInfo : public BReferenceable { class PackageInfo : public BReferenceable {
public: public:
PackageInfo(); PackageInfo();
@@ -225,6 +229,14 @@ public:
const BitmapList& Screenshots() const const BitmapList& Screenshots() const
{ return fScreenshots; } { return fScreenshots; }
bool AddListener(
const PackageInfoListenerRef& listener);
void RemoveListener(
const PackageInfoListenerRef& listener);
private:
void _NotifyListeners(uint32 changes);
private: private:
BitmapRef fIcon; BitmapRef fIcon;
BString fTitle; BString fTitle;
@@ -236,6 +248,7 @@ private:
CategoryList fCategories; CategoryList fCategories;
UserRatingList fUserRatings; UserRatingList fUserRatings;
BitmapList fScreenshots; BitmapList fScreenshots;
PackageListenerList fListeners;
}; };
@@ -7,6 +7,8 @@
#include <stdio.h> #include <stdio.h>
#include "PackageInfo.h"
// #pragma mark - PackageInfoEvent // #pragma mark - PackageInfoEvent
+9 -2
View File
@@ -6,7 +6,7 @@
#define PACKAGE_INFO_LISTENER_H #define PACKAGE_INFO_LISTENER_H
#include "PackageInfo.h" #include <Referenceable.h>
enum { enum {
@@ -17,6 +17,10 @@ enum {
}; };
class PackageInfo;
typedef BReference<PackageInfo> PackageInfoRef;
class PackageInfoEvent { class PackageInfoEvent {
public: public:
PackageInfoEvent(); PackageInfoEvent();
@@ -41,7 +45,7 @@ private:
}; };
class PackageInfoListener { class PackageInfoListener : public BReferenceable {
public: public:
PackageInfoListener(); PackageInfoListener();
virtual ~PackageInfoListener(); virtual ~PackageInfoListener();
@@ -51,4 +55,7 @@ public:
}; };
typedef BReference<PackageInfoListener> PackageInfoListenerRef;
#endif // PACKAGE_INFO_LISTENER_H #endif // PACKAGE_INFO_LISTENER_H