Applied latest source code
[apps/native/preloaded/MusicPlayer.git] / src / MpSearchPresentationModel.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                MpSearchPresentationModel.cpp
19  * @brief               This is the implementation file for SearchPresentationModel class.
20  */
21
22 #include "MpSearchPresentationModel.h"
23
24 using namespace Tizen::Base;
25 using namespace Tizen::Base::Collection;
26 using namespace Tizen::Content;
27 using namespace Tizen::Graphics;
28 using namespace Tizen::Social;
29 using namespace Tizen::Ui::Controls;
30
31 static const wchar_t* QUERY_PREFIX = L" like '%";
32 static const wchar_t* QUERY_SUFFIX = L"%'";
33
34 SearchPresentationModel* SearchPresentationModel::pSearchPresentationModelInstance = null;
35
36 SearchPresentationModel::SearchPresentationModel(void)
37         : __pSearchList(null)
38 {
39         AppLogDebug("ENTER");
40         AppLogDebug("EXIT");
41 }
42
43 SearchPresentationModel::~SearchPresentationModel(void)
44 {
45         AppLogDebug("ENTER");
46         AppLogDebug("EXIT");
47 }
48
49 result
50 SearchPresentationModel::Construct(void)
51 {
52         AppLogDebug("ENTER");
53         result r = E_SUCCESS;
54         AppLogDebug("EXIT");
55         return r;
56 }
57
58 SearchPresentationModel*
59 SearchPresentationModel::GetInstance(void)
60 {
61         AppLogDebug("ENTER");
62         if (pSearchPresentationModelInstance == null)
63         {
64                 pSearchPresentationModelInstance = new (std::nothrow) SearchPresentationModel();
65                 TryReturn(pSearchPresentationModelInstance != null, null, "SearchPresentationModel instance is not created.");
66
67                 result r = pSearchPresentationModelInstance->Construct();
68                 TryCatch(r == E_SUCCESS, , null, "SearchPresentationModel instance is not constrcuted.");
69         }
70         AppLogDebug("EXIT");
71         return pSearchPresentationModelInstance;
72
73 CATCH:
74         delete pSearchPresentationModelInstance;
75         return null;
76 }
77
78 int
79 SearchPresentationModel::GetAlbumIndex(void)
80 {
81         AppLogDebug("ENTER");
82         int index = -1;
83         if (__pSearchList == null)
84         {
85                 return index;
86         }
87         __pSearchList->IndexOf(STRING_SORT_ALBUM, index);
88
89         AppLogDebug("EXIT");
90         return index;
91 }
92
93 int
94 SearchPresentationModel::GetArtistIndex(void)
95 {
96         AppLogDebug("ENTER");
97         int index = -1;
98         if (__pSearchList == null)
99         {
100                 return index;
101         }
102         __pSearchList->IndexOf(STRING_SORT_ARTIST, index);
103
104         AppLogDebug("EXIT");
105         return index;
106 }
107
108 int
109 SearchPresentationModel::GetSongIndex(void)
110 {
111         AppLogDebug("ENTER");
112         int index = -1;
113         if (__pSearchList == null)
114         {
115                 return index;
116         }
117         __pSearchList->IndexOf(STRING_SORT_TITLE, index);
118
119         AppLogDebug("EXIT");
120         return index;
121 }
122
123 int
124 SearchPresentationModel::GetTotalContentCount(void)
125 {
126         AppLogDebug("ENTER");
127         if (__pSearchList == null)
128         {
129                 return INIT_VALUE;
130         }
131
132         AppLogDebug("EXIT");
133         return __pSearchList->GetCount();
134 }
135
136 void
137 SearchPresentationModel::RemoveSearchResult(void)
138 {
139         AppLogDebug("ENTER");
140         if (__pSearchList != null)
141         {
142                 __pSearchList->RemoveAll(true);
143         }
144         AppLogDebug("EXIT");
145 }
146
147 void
148 SearchPresentationModel::InitializeContentList(const Tizen::Base::String& keyword)
149 {
150         AppLogDebug("ENTER");
151         if (__pSearchList != null)
152         {
153                 __pSearchList->RemoveAll(true);
154                 delete __pSearchList;
155                 __pSearchList = null;
156         }
157
158         IList* pArtistSearch = CommonUtil::SearchContentList(MakeQuery(STRING_SORT_ARTIST, keyword), STRING_SORT_ARTIST);
159         IList* pAlbumSearch = CommonUtil::SearchContentList(MakeQuery(STRING_SORT_ALBUM, keyword), STRING_SORT_ALBUM);
160         IList* pSongSearch = CommonUtil::SearchContentList(MakeQuery(STRING_SORT_TITLE, keyword), STRING_SORT_TITLE);
161
162         if (pArtistSearch != null && pArtistSearch->GetCount() != 0)
163         {
164                 RemoveDuplicateEntries(*pArtistSearch);
165                 pArtistSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_ARTIST)), 0);
166                 __pSearchList = pArtistSearch;
167
168                 if (pAlbumSearch != null && pAlbumSearch->GetCount() != 0)
169                 {
170                         RemoveDuplicateEntries(*pAlbumSearch, false);
171                         pAlbumSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_ALBUM)), 0);
172                         __pSearchList->AddItems(*pAlbumSearch);
173                 }
174
175                 if (pSongSearch != null && pSongSearch->GetCount() != 0)
176                 {
177                         pSongSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_TITLE)), 0);
178                         __pSearchList->AddItems(*pSongSearch);
179                 }
180         }
181         else if (pAlbumSearch != null && pAlbumSearch->GetCount() != 0)
182         {
183                 RemoveDuplicateEntries(*pAlbumSearch, false);
184                 pAlbumSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_ALBUM)), 0);
185                 __pSearchList = pAlbumSearch;
186
187                 if (pSongSearch != null && pSongSearch->GetCount() != 0)
188                 {
189                         pSongSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_TITLE)), 0);
190                         __pSearchList->AddItems(*pSongSearch);
191                 }
192         }
193         else if (pSongSearch != null && pSongSearch->GetCount() != 0)
194         {
195                 pSongSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_TITLE)), 0);
196                 __pSearchList = pSongSearch;
197         }
198         else
199         {
200                 __pSearchList = null;
201         }
202         AppLogDebug("EXIT");
203 }
204
205 void
206 SearchPresentationModel::Release(void)
207 {
208         AppLogDebug("ENTER");
209         if (pSearchPresentationModelInstance != null)
210         {
211                 delete pSearchPresentationModelInstance;
212                 pSearchPresentationModelInstance = null;
213         }
214         AppLogDebug("EXIT");
215 }
216
217 ContentInformation*
218 SearchPresentationModel::GetContentInfoN(int index)
219 {
220         AppLogDebug("ENTER");
221         if (__pSearchList == null || index < INIT_VALUE)
222         {
223                 AppLogDebug("EXIT(index(%d) MUST be greater than or equal to 0 OR __pSearchList is null)", index);
224                 return null;
225         }
226
227         ContentInformation* pContentInfoStruct = null;
228         ContentSearchResult* pResult = static_cast<ContentSearchResult*>(__pSearchList->GetAt(index));
229         if (pResult->GetContentType() == CONTENT_TYPE_AUDIO)
230         {
231                 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pResult->GetContentInfo());
232                 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
233         }
234
235         AppLogDebug("EXIT");
236         return pContentInfoStruct;
237 }
238
239 Tizen::Base::String
240 SearchPresentationModel::MakeQuery(const Tizen::Base::String& WhereExpr, const Tizen::Base::String& keyword)
241 {
242         AppLogDebug("ENTER");
243         if (WhereExpr == null || keyword == null)
244         {
245                 return String();
246         }
247
248         String strMakeQuery = WhereExpr;
249         strMakeQuery.Append(QUERY_PREFIX);
250         strMakeQuery.Append(CommonUtil::SpecialReplaceChange(keyword));
251         strMakeQuery.Append(QUERY_SUFFIX);
252
253         AppLogDebug("EXIT");
254         return strMakeQuery;
255 }
256
257 void
258 SearchPresentationModel::RemoveDuplicateEntries(Tizen::Base::Collection::IList& contentList, const bool isArtist)
259 {
260         if (contentList.GetCount())
261         {
262                 ArrayList* pList = new (std::nothrow) ArrayList();
263                 pList->Construct();
264
265                 for (int count = 0; count < contentList.GetCount(); count++)
266                 {
267                         ContentSearchResult* pSearchResult = static_cast<ContentSearchResult*>(contentList.GetAt(count));
268                         bool foundContent = false;
269
270                         for (int copiedCount = 0; copiedCount < pList->GetCount(); copiedCount++)
271                         {
272                                 ContentSearchResult* pTempSearchResult = static_cast<ContentSearchResult*>(pList->GetAt(copiedCount));
273                                 if (pTempSearchResult)
274                                 {
275
276                                         if (isArtist && (static_cast<AudioContentInfo*>(pTempSearchResult->GetContentInfo())->GetArtist().CompareTo(static_cast<AudioContentInfo*>(pSearchResult->GetContentInfo())->GetArtist()) == 0))
277                                         {
278                                                 foundContent = true;
279                                                 break;
280                                         }
281                                         else if (!isArtist && (static_cast<AudioContentInfo*>(pTempSearchResult->GetContentInfo())->GetAlbumName().CompareTo(static_cast<AudioContentInfo*>(pSearchResult->GetContentInfo())->GetAlbumName()) == 0))
282                                         {
283                                                 foundContent = true;
284                                                 break;
285                                         }
286                                 }
287                         }
288                         if (!foundContent)
289                         {
290                                 pList->Add(pSearchResult);
291                         }
292                         else
293                         {
294                                 delete pSearchResult;
295                         }
296                 }
297
298                 contentList.RemoveAll(false);
299                 contentList.AddItems(*pList);
300                 delete pList;
301         }
302 }