Initialize Tizen 2.3
[apps/osp/Contacts.git] / src / CtGroupPresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        CtGroupPresentationModel.cpp
19  * @brief       This is the implementation file for the GroupPresentationModel class.
20  */
21
22 #include "CtGroupPresentationModel.h"
23 #include "CtIContactEventListener.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27 using namespace Tizen::Social;
28
29 GroupPresentationModel::GroupPresentationModel(void)
30  : __pContactChangeListenerList(null)
31  , __pAddressbook(null)
32  , __pCategory(null)
33 {
34 }
35
36 GroupPresentationModel::~GroupPresentationModel(void)
37 {
38         if (__pContactChangeListenerList != null)
39         {
40                 __pContactChangeListenerList->RemoveAll(false);
41                 delete __pContactChangeListenerList;
42         }
43
44         delete __pAddressbook;
45         delete __pCategory;
46 }
47
48 result
49 GroupPresentationModel::Construct(Tizen::Social::RecordId categoryId)
50 {
51         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
52         __pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
53         if (__pAddressbook == null)
54         {
55                 return E_FAILURE;
56         }
57
58         if (categoryId != INVALID_RECORD_ID)
59         {
60                 __pCategory = __pAddressbook->GetCategoryN(categoryId);
61                 if (__pCategory == null)
62                 {
63                         return E_FAILURE;
64                 }
65         }
66         else
67         {
68                 __pCategory = new (std::nothrow) Category();
69                 if (__pCategory == null)
70                 {
71                         return E_FAILURE;
72                 }
73         }
74
75         __pContactChangeListenerList = new (std::nothrow) ArrayList();
76         __pContactChangeListenerList->Construct();
77
78         __pAddressbook->SetAddressbookChangeEventListener(this);
79
80         return E_SUCCESS;
81 }
82
83 bool
84 GroupPresentationModel::IsDuplicatedCategory(void)
85 {
86         bool isDuplicated = false;
87         Category* pCategory = null;
88
89         IList* pList = __pAddressbook->GetAllCategoriesN();
90         if (pList == null)
91         {
92                 return false;
93         }
94
95         IEnumerator* pEnum = pList->GetEnumeratorN();
96         while (pEnum->MoveNext() == E_SUCCESS)
97         {
98                 pCategory = static_cast<Category*>(pEnum->GetCurrent());
99                 TryCatch(pCategory != null, , "Unable to cast object to category");
100
101                 if (pCategory->IsDefault())
102                 {
103                         continue;
104                 }
105
106                 if (pCategory->GetRecordId() != __pCategory->GetRecordId() &&
107                         pCategory->GetName().Equals(__pCategory->GetName()) == true)
108                 {
109                         isDuplicated = true;
110                 }
111         }
112
113 CATCH:
114         pList->RemoveAll(true);
115         delete pList;
116         delete pEnum;
117
118         return isDuplicated;
119 }
120
121 bool
122 GroupPresentationModel::IsDefaultCategory(void)
123 {
124         return __pCategory->IsDefault();
125 }
126
127 result
128 GroupPresentationModel::SetCategoryName(const Tizen::Base::String& groupName)
129 {
130         return __pCategory->SetName(groupName);
131 }
132
133 result
134 GroupPresentationModel::SetRingtone(const Tizen::Base::String& ringtonePath)
135 {
136         return __pCategory->SetRingtonePath(ringtonePath);
137 }
138
139 result
140 GroupPresentationModel::AddCategory(void)
141 {
142         return __pAddressbook->AddCategory(*__pCategory);
143 }
144
145 result
146 GroupPresentationModel::UpdateCategory(void)
147 {
148         bool isSame = false;
149         Category* pCategory = __pAddressbook->GetCategoryN(GetCategoryId());
150
151         if (pCategory && pCategory->Equals(*__pCategory))
152         {
153                 isSame = true;
154         }
155
156         delete pCategory;
157
158         // If the category is not changed, OnCategoriesChanged will not be called. But UpdateCategory() returns E_SCCUESS.
159         result r = __pAddressbook->UpdateCategory(*__pCategory);
160         return isSame ? E_FAILURE : r;
161 }
162
163 result
164 GroupPresentationModel::SetThumbnailPath(const Tizen::Base::String& filePath)
165 {
166         return __pCategory->SetThumbnail(filePath);
167 }
168
169 Tizen::Base::String
170 GroupPresentationModel::GetThumbnailPath(void)
171 {
172         return __pCategory->GetThumbnailPath();
173 }
174
175 Tizen::Base::String
176 GroupPresentationModel::GetCategoryName(void)
177 {
178         return __pCategory->GetName();
179 }
180
181 Tizen::Base::String
182 GroupPresentationModel::GetRingtone(void)
183 {
184         return __pCategory->GetRingtonePath();
185 }
186
187 int
188 GroupPresentationModel::GetCategoryCount(void)
189 {
190         return __pAddressbook->GetCategoryCount();
191 }
192
193 Tizen::Social::RecordId
194 GroupPresentationModel::GetCategoryId(void)
195 {
196         return __pCategory->GetRecordId();
197 }
198
199 void
200 GroupPresentationModel::AddContactChangeListener(const IContactEventListener& listener)
201 {
202         __pContactChangeListenerList->Add(listener);
203 }
204
205 void
206 GroupPresentationModel::RemoveContactChangeListener(const IContactEventListener& listener)
207 {
208         __pContactChangeListenerList->Remove(listener, false);
209 }
210
211 void
212 GroupPresentationModel::OnContactsChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
213 {
214 }
215
216 void
217 GroupPresentationModel::OnCategoriesChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
218 {
219         IContactEventListener* pInterface = null;
220         IEnumerator* pEnum = __pContactChangeListenerList->GetEnumeratorN();
221         while (pEnum->MoveNext() == E_SUCCESS)
222         {
223                 pInterface = static_cast<IContactEventListener *>(pEnum->GetCurrent());
224                 if (pInterface == null)
225                 {
226                         delete pEnum;
227                         return;
228                 }
229                 pInterface->OnCategoriesChanged();
230         }
231
232         delete pEnum;
233 }