I have made the following changes intended for :
  CE:MW:Shared / libcommhistory

Please review and accept or decline.
BOSS has already run some checks on this request.
See the "Messages from BOSS" section below.

https://build.pub.meego.com//request/show/7589

Thank You,
John Brooks

[This message was auto-generated]

---

Request # 7589:

Messages from BOSS:

State: review at 2012-12-28T22:38:30 by bossbot

Reviews:
       accepted by bossbot : Prechecks succeeded.
       new for CE-maintainers : Please replace this text with a review and 
approve/reject the review (not the SR). BOSS will take care of the rest

Changes:
  submit: home:special:branches:CE:MW:Shared / libcommhistory -> CE:MW:Shared / 
libcommhistory
  
changes files:
--------------
--- libcommhistory.changes
+++ libcommhistory.changes
@@ -0,0 +1,4 @@
+* Fri Dec 28 2012 John Brooks <[email protected]> - 1.4.5
+- [declarative] Fix GroupObject ownership
+- [declarative] Add Group.markAsRead() function
+

old:
----
  libcommhistory-1.4.4.tar.bz2

new:
----
  libcommhistory-1.4.5.tar.bz2

spec files:
-----------
--- libcommhistory.spec
+++ libcommhistory.spec
@@ -1,6 +1,6 @@
 Name:       libcommhistory
 Summary:    Communications event history database API
-Version:    1.4.4
+Version:    1.4.5
 Release:    1
 Group:      System/Libraries
 License:    LGPL

other changes:
--------------

++++++ libcommhistory-1.4.4.tar.bz2 -> libcommhistory-1.4.5.tar.bz2
--- declarative/src/groupobject.cpp
+++ declarative/src/groupobject.cpp
@@ -30,6 +30,7 @@
 
 #include "groupobject.h"
 #include "groupmodel.h"
+#include "groupproxymodel.h"
 
 using namespace CommHistory;
 
@@ -38,75 +39,67 @@
 {
 }
 
-GroupObject::GroupObject(const Group &group, QAbstractItemModel *parent)
+GroupObject::GroupObject(const Group &group, GroupProxyModel *parent)
     : QObject(parent), Group(group), model(parent)
 {
-    connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
-            SLOT(modelDataChanged(QModelIndex,QModelIndex)));
-    connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
-            SLOT(modelRowsRemoved(QModelIndex,int,int)));
-}
-
-void GroupObject::modelDataChanged(const QModelIndex &topLeft, const 
QModelIndex &bottomRight)
-{
-    Q_ASSERT(topLeft.parent() == bottomRight.parent());
-
-    for (int r = topLeft.row(); r <= bottomRight.row(); r++) {
-        const Group &g = model->index(r, 0, 
topLeft.parent()).data(GroupModel::GroupRole).value<Group>();
-
-        if (g.id() != id())
-            continue;
-
-        Group old(static_cast<Group&>(*this));
-        Group::operator=(g);
-
-        if (old.localUid() != localUid())
-            emit localUidChanged();
-        if (old.remoteUids() != remoteUids())
-            emit remoteUidsChanged();
-        if (old.chatType() != chatType())
-            emit chatTypeChanged();
-        if (old.chatName() != chatName())
-            emit chatNameChanged();
-        if (old.startTime() != startTime())
-            emit startTimeChanged();
-        if (old.endTime() != endTime())
-            emit endTimeChanged();
-        if (old.totalMessages() != totalMessages())
-            emit totalMessagesChanged();
-        if (old.unreadMessages() != unreadMessages())
-            emit unreadMessagesChanged();
-        if (old.sentMessages() != sentMessages())
-            emit sentMessagesChanged();
-        if (old.lastEventId() != lastEventId())
-            emit lastEventIdChanged();
-        if (old.contacts() != contacts())
-            emit contactsChanged();
-        if (old.lastMessageText() != lastMessageText())
-            emit lastMessageTextChanged();
-        if (old.lastVCardFileName() != lastVCardFileName())
-            emit lastVCardFileNameChanged();
-        if (old.lastVCardLabel() != lastVCardLabel())
-            emit lastVCardLabelChanged();
-        if (old.lastEventStatus() != lastEventStatus())
-            emit lastEventStatusChanged();
-        if (old.lastModified() != lastModified())
-            emit lastModifiedChanged();
+}
 
-        break;
-    }
+GroupObject::~GroupObject()
+{
 }
 
-void GroupObject::modelRowsRemoved(const QModelIndex &parent, int start, int 
end)
+bool GroupObject::markAsRead()
 {
-    for (int r = start; r <= end; r++) {
-        QModelIndex index = model->index(r, GroupModel::GroupId, parent);
-        if (index.data().toInt() != id())
-            continue;
-
-        qDebug() << Q_FUNC_INFO << "group removed";
-        emit groupRemoved();
-        break;
+    if (!model || !model->groupModel()) {
+        qWarning() << "org.nemomobile.commhistory: GroupObject has no model";
+        return false;
     }
+
+    return model->groupModel()->markAsReadGroup(id());
+}
+
+void GroupObject::updateGroup(const Group &g)
+{
+    Group old(static_cast<Group&>(*this));
+    Group::operator=(g);
+
+    if (old.localUid() != localUid())
+        emit localUidChanged();
+    if (old.remoteUids() != remoteUids())
+        emit remoteUidsChanged();
+    if (old.chatType() != chatType())
+        emit chatTypeChanged();
+    if (old.chatName() != chatName())
+        emit chatNameChanged();
+    if (old.startTime() != startTime())
+        emit startTimeChanged();
+    if (old.endTime() != endTime())
+        emit endTimeChanged();
+    if (old.totalMessages() != totalMessages())
+        emit totalMessagesChanged();
+    if (old.unreadMessages() != unreadMessages())
+        emit unreadMessagesChanged();
+    if (old.sentMessages() != sentMessages())
+        emit sentMessagesChanged();
+    if (old.lastEventId() != lastEventId())
+        emit lastEventIdChanged();
+    if (old.contacts() != contacts())
+        emit contactsChanged();
+    if (old.lastMessageText() != lastMessageText())
+        emit lastMessageTextChanged();
+    if (old.lastVCardFileName() != lastVCardFileName())
+        emit lastVCardFileNameChanged();
+    if (old.lastVCardLabel() != lastVCardLabel())
+        emit lastVCardLabelChanged();
+    if (old.lastEventStatus() != lastEventStatus())
+        emit lastEventStatusChanged();
+    if (old.lastModified() != lastModified())
+        emit lastModifiedChanged();
+}
+
+void GroupObject::removeGroup()
+{
+    qDebug() << Q_FUNC_INFO << "group removed";
+    emit groupRemoved();
 }
 
--- declarative/src/groupobject.h
+++ declarative/src/groupobject.h
@@ -34,7 +34,7 @@
 #include <QObject>
 #include "group.h"
 
-class QAbstractItemModel;
+class GroupProxyModel;
 class QModelIndex;
 
 class GroupObject : public QObject, CommHistory::Group
@@ -44,7 +44,8 @@
 
 public:
     GroupObject(QObject *parent = 0);
-    GroupObject(const CommHistory::Group &group, QAbstractItemModel *parent = 
0);
+    GroupObject(const CommHistory::Group &group, GroupProxyModel *parent = 0);
+    virtual ~GroupObject();
 
     Q_PROPERTY(bool isValid READ isValid CONSTANT);
     Q_PROPERTY(int id READ id CONSTANT);
@@ -90,6 +91,11 @@
     using Group::contactId;
     using Group::contactName;
 
+    Q_INVOKABLE bool markAsRead();
+
+    void updateGroup(const CommHistory::Group &group);
+    void removeGroup();
+
 signals:
     void localUidChanged();
     void remoteUidsChanged();
@@ -111,12 +117,8 @@
 
     void groupRemoved();
 
-private slots:
-    void modelDataChanged(const QModelIndex &topLeft, const QModelIndex 
&bottomRight);
-    void modelRowsRemoved(const QModelIndex &parent, int start, int end);
-
 private:
-    QAbstractItemModel *model;
+    GroupProxyModel *model;
 };
 
 #endif
--- declarative/src/groupproxymodel.cpp
+++ declarative/src/groupproxymodel.cpp
@@ -40,50 +40,103 @@
 {
 }
 
-void GroupProxyModel::setSourceModel(QAbstractItemModel *model)
+void GroupProxyModel::setSourceModel(QAbstractItemModel *m)
 {
-    if (model == sourceModel())
+    if (m == sourceModel())
         return;
 
-    GroupModel *g = qobject_cast<GroupModel*>(model);
+    GroupModel *g = qobject_cast<GroupModel*>(m);
     if (model && !g) {
         qWarning() << Q_FUNC_INFO << "Model must be a CommHistory::GroupModel";
-        model = 0;
+        m = 0;
     }
 
-    groupModel = g;
+    if (model)
+        disconnect(model, 0, this, 0);
+
+    model = g;
     QIdentityProxyModel::setSourceModel(model);
+
+    connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
+            SLOT(sourceDataChanged(QModelIndex,QModelIndex)));
+    connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
+            SLOT(sourceRowsRemoved(QModelIndex,int,int)));
+
     emit sourceModelChanged();
 }
 
 GroupObject *GroupProxyModel::group(int row)
 {
-    if (!groupModel) {
+    if (!model) {
         qWarning() << Q_FUNC_INFO << "No group model instance";
         return 0;
     }
 
-    Group g = groupModel->group(mapToSource(index(row, 0)));
+    Group g = model->group(mapToSource(index(row, 0)));
     if (!g.isValid())
         return 0;
 
-    // Should we keep a list and return the same instances? 
-    return new GroupObject(g, this);
+    GroupObject *re = groupObjects.value(g.id());
+    if (!re) {
+        re = new GroupObject(g, this);
+        groupObjects.insert(g.id(), re);
+    }
+
+    return re;
 }
 
 GroupObject *GroupProxyModel::groupById(int id)
 {
-    if (!groupModel) {
+    if (!model) {
         qWarning() << Q_FUNC_INFO << "No group model instance";
         return 0;
     }
 
-    for (int r = 0; r < groupModel->rowCount(); r++) {
-        Group g = groupModel->group(groupModel->index(r, 0));
-        if (g.isValid() && g.id() == id)
-            return new GroupObject(g, this);
+    GroupObject *re = groupObjects.value(id);
+    if (re)
+        return re;
+
+    for (int r = 0; r < model->rowCount(); r++) {
+        Group g = model->group(model->index(r, 0));
+        if (!g.isValid() || g.id() != id)
+            continue;
+
+        re = new GroupObject(g, this);
+        groupObjects.insert(g.id(), re);
+        break;
     }
 
-    return 0;
+    return re;
+}
+
+void GroupProxyModel::sourceDataChanged(const QModelIndex &topLeft, const 
QModelIndex &bottomRight)
+{
+    Q_ASSERT(topLeft.parent() == bottomRight.parent());
+
+    for (int r = topLeft.row(); r <= bottomRight.row(); r++) {
+        const Group &g = model->group(model->index(r, 0));
+
+        GroupObject *obj = groupObjects.value(g.id());
+        if (!obj)
+            continue;
+
+        obj->updateGroup(g);
+    }
+}
+
+void GroupProxyModel::sourceRowsRemoved(const QModelIndex &parent, int start, 
int end)
+{
+    Q_UNUSED(parent);
+
+    for (int r = start; r <= end; r++) {
+        const Group &g = model->group(model->index(r, 0));
+        GroupObject *obj = groupObjects.value(g.id());
+        if (!obj)
+            continue;
+
+        obj->removeGroup();
+        obj->deleteLater();
+        groupObjects.remove(g.id());
+    }
 }
 
--- declarative/src/groupproxymodel.h
+++ declarative/src/groupproxymodel.h
@@ -32,6 +32,7 @@
 #define COMMHISTORY_DECLARATIVE_GROUPPROXYMODEL_H
 
 #include <QIdentityProxyModel>
+#include <QHash>
 
 class GroupObject;
 
@@ -48,19 +49,26 @@
 
     Q_PROPERTY(QObject* sourceModel READ sourceModel WRITE setSourceModel 
NOTIFY sourceModelChanged)
     virtual void setSourceModel(QAbstractItemModel *sourceModel);
-    void setSourceModel(QObject *model)
+    void setSourceModel(QObject *m)
     {
-        setSourceModel(qobject_cast<QAbstractItemModel*>(model));
+        setSourceModel(qobject_cast<QAbstractItemModel*>(m));
     }
 
+    CommHistory::GroupModel *groupModel() const { return model; }
+
     Q_INVOKABLE GroupObject *group(int row);
     Q_INVOKABLE GroupObject *groupById(int id);
 
 signals:
     void sourceModelChanged();
 
+private slots:
+    void sourceDataChanged(const QModelIndex &topLeft, const QModelIndex 
&bottomRight);
+    void sourceRowsRemoved(const QModelIndex &parent, int start, int end);
+
 private:
-    CommHistory::GroupModel *groupModel;
+    CommHistory::GroupModel *model;
+    QHash<int,GroupObject*> groupObjects;
 };
 
 #endif



Reply via email to