source: SMSSender/src/business/BCGroupManager.cpp @ 71:415b45c71663

3.0
Last change on this file since 71:415b45c71663 was 71:415b45c71663, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • Reorganized all the libraries. libinterfaces and libgateways are removed now and libdatatypes contains now all these files.
File size: 2.5 KB
Line 
1/*
2 * BCGroupManager.cpp
3 *
4 *  Created on: Jun 20, 2009
5 *      Author: saemy
6 */
7
8#include "BCGroupManager.h"
9
10#include "../persistence/PersistenceFactory.h"
11
12BCGroupManager* BCGroupManager::instance_=0;
13BCGroupManager* BCGroupManager::instance(){
14    return instance_ ? instance_ : (instance_ = new BCGroupManager);
15}
16
17BCGroupManager::BCGroupManager(){
18    readGroupsFromStorage();
19}
20
21void BCGroupManager::addGroupToList(IGroup* group) {
22    if ((group == NULL) || groupList_.contains(group->id()))
23        return;
24
25    groupList_.insert(group->id(), group);
26
27    connect(group->eventMapper(), SIGNAL(idChanged(int, int)),
28            this, SLOT(groupIdChanged(int, int)));
29    connect(group->eventMapper(), SIGNAL(dataChanged()),
30            this, SLOT(groupDataChanged()));
31
32    emit groupAdded(group);
33}
34
35void BCGroupManager::removeGroupFromList(IGroup* group) {
36    if ((group == NULL) || !groupList_.contains(group->id()))
37        return;
38
39    disconnect(group->eventMapper(), SIGNAL(idChanged(int, int)),
40               this, SLOT(groupIdChanged(int, int)));
41    disconnect(group->eventMapper(), SIGNAL(dataChanged()),
42               this, SLOT(groupDataChanged()));
43
44    groupList_.remove(group->id());
45    emit groupRemoved(group);
46}
47
48void BCGroupManager::readGroupsFromStorage() {
49    QSet<IGroup*> groupList = PersistenceFactory::instance()->getGroupManager()->getGroupList();
50
51    foreach(IGroup* group, groupList_.values().toSet().subtract(groupList)) {
52        removeGroupFromList(group);
53    }
54
55    foreach(IGroup* group, groupList.subtract(groupList_.values().toSet())) {
56        addGroupToList(group);
57    }
58}
59
60
61IGroup* BCGroupManager::getGroup(int groupId){
62    return groupList_.value(groupId);
63}
64
65QSet<IGroup*> BCGroupManager::getGroupList(){
66    return groupList_.values().toSet();
67}
68
69void BCGroupManager::saveGroup(IGroup* group){
70    PersistenceFactory::instance()->getGroupManager()->saveGroup(group);
71    addGroupToList(group);
72}
73
74void BCGroupManager::removeGroup(int groupId){
75    IGroup* group = getGroup(groupId);
76    if (group == NULL)
77        return;
78
79    PersistenceFactory::instance()->getGroupManager()->removeGroup(group);
80    removeGroupFromList(group);
81}
82
83
84void BCGroupManager::groupIdChanged(int oldId, int newId) {
85    IGroup* group = groupList_.take(oldId);
86    if (group != NULL)
87        groupList_.insert(newId, group);
88}
89void BCGroupManager::groupDataChanged() {
90    IGroup* group= static_cast<SGroupEventMapper*>(sender())->group();
91    Q_ASSERT(group);
92    emit groupUpdated(group);
93}
Note: See TracBrowser for help on using the repository browser.