[haiku-commits] haiku: hrev46065 - src/apps/haiku-depot

  • From: superstippi@xxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 15 Sep 2013 15:47:48 +0200 (CEST)

hrev46065 adds 1 changeset to branch 'master'
old head: a07d888425a1f4949c4b0f2ca022fe93dd120a6a
new head: 83f9d3c431b1e0b236690acc62d1eb9e875342a5
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=83f9d3c+%5Ea07d888

----------------------------------------------------------------------------

83f9d3c: HaikuDepot: Added some listener support to PackageInfo

                                      [ Stephan Aßmus <superstippi@xxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev46065
Commit:      83f9d3c431b1e0b236690acc62d1eb9e875342a5
URL:         http://cgit.haiku-os.org/haiku/commit/?id=83f9d3c
Author:      Stephan Aßmus <superstippi@xxxxxx>
Date:        Sun Sep 15 15:46:01 2013 UTC

----------------------------------------------------------------------------

4 files changed, 74 insertions(+), 4 deletions(-)
src/apps/haiku-depot/PackageInfo.cpp         | 52 +++++++++++++++++++++++-
src/apps/haiku-depot/PackageInfo.h           | 13 ++++++
src/apps/haiku-depot/PackageInfoListener.cpp |  2 +
src/apps/haiku-depot/PackageInfoListener.h   | 11 ++++-

----------------------------------------------------------------------------

diff --git a/src/apps/haiku-depot/PackageInfo.cpp 
b/src/apps/haiku-depot/PackageInfo.cpp
index 6b78f98..2823c77 100644
--- a/src/apps/haiku-depot/PackageInfo.cpp
+++ b/src/apps/haiku-depot/PackageInfo.cpp
@@ -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);
+       }
 }
 
 
diff --git a/src/apps/haiku-depot/PackageInfo.h 
b/src/apps/haiku-depot/PackageInfo.h
index d2bc66a..f4f74fa 100644
--- a/src/apps/haiku-depot/PackageInfo.h
+++ b/src/apps/haiku-depot/PackageInfo.h
@@ -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;
 };
 
 
diff --git a/src/apps/haiku-depot/PackageInfoListener.cpp 
b/src/apps/haiku-depot/PackageInfoListener.cpp
index 36a1277..7329c2b 100644
--- a/src/apps/haiku-depot/PackageInfoListener.cpp
+++ b/src/apps/haiku-depot/PackageInfoListener.cpp
@@ -7,6 +7,8 @@
 
 #include <stdio.h>
 
+#include "PackageInfo.h"
+
 
 // #pragma mark - PackageInfoEvent
 
diff --git a/src/apps/haiku-depot/PackageInfoListener.h 
b/src/apps/haiku-depot/PackageInfoListener.h
index 1dcde5c..323733a 100644
--- a/src/apps/haiku-depot/PackageInfoListener.h
+++ b/src/apps/haiku-depot/PackageInfoListener.h
@@ -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


Other related posts:

  • » [haiku-commits] haiku: hrev46065 - src/apps/haiku-depot - superstippi