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
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
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 "List.h"
#include "PackageInfoListener.h"
class BBitmap;
@@ -181,6 +182,9 @@ typedef BReference<PackageCategory> CategoryRef;
typedef List<CategoryRef, false> CategoryList;
typedef List<PackageInfoListenerRef, false, 2> PackageListenerList;
class PackageInfo : public BReferenceable {
public:
PackageInfo();
@@ -225,6 +229,14 @@ public:
const BitmapList& Screenshots() const
{ return fScreenshots; }
bool AddListener(
const PackageInfoListenerRef& listener);
void RemoveListener(
const PackageInfoListenerRef& listener);
private:
void _NotifyListeners(uint32 changes);
private:
BitmapRef fIcon;
BString fTitle;
@@ -236,6 +248,7 @@ private:
CategoryList fCategories;
UserRatingList fUserRatings;
BitmapList fScreenshots;
PackageListenerList fListeners;
};
@@ -7,6 +7,8 @@
#include <stdio.h>
#include "PackageInfo.h"
// #pragma mark - PackageInfoEvent
+9 -2
View File
@@ -6,7 +6,7 @@
#define PACKAGE_INFO_LISTENER_H
#include "PackageInfo.h"
#include <Referenceable.h>
enum {
@@ -17,6 +17,10 @@ enum {
};
class PackageInfo;
typedef BReference<PackageInfo> PackageInfoRef;
class PackageInfoEvent {
public:
PackageInfoEvent();
@@ -41,7 +45,7 @@ private:
};
class PackageInfoListener {
class PackageInfoListener : public BReferenceable {
public:
PackageInfoListener();
virtual ~PackageInfoListener();
@@ -51,4 +55,7 @@ public:
};
typedef BReference<PackageInfoListener> PackageInfoListenerRef;
#endif // PACKAGE_INFO_LISTENER_H