2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file MpSearchPresentationModel.cpp
19 * @brief This is the implementation file for SearchPresentationModel class.
22 #include "MpSearchPresentationModel.h"
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;
31 static const wchar_t* QUERY_PREFIX = L" like '%";
32 static const wchar_t* QUERY_SUFFIX = L"%'";
34 SearchPresentationModel* SearchPresentationModel::pSearchPresentationModelInstance = null;
36 SearchPresentationModel::SearchPresentationModel(void)
43 SearchPresentationModel::~SearchPresentationModel(void)
50 SearchPresentationModel::Construct(void)
58 SearchPresentationModel*
59 SearchPresentationModel::GetInstance(void)
62 if (pSearchPresentationModelInstance == null)
64 pSearchPresentationModelInstance = new (std::nothrow) SearchPresentationModel();
65 TryReturn(pSearchPresentationModelInstance != null, null, "SearchPresentationModel instance is not created.");
67 result r = pSearchPresentationModelInstance->Construct();
68 TryCatch(r == E_SUCCESS, , null, "SearchPresentationModel instance is not constrcuted.");
71 return pSearchPresentationModelInstance;
74 delete pSearchPresentationModelInstance;
79 SearchPresentationModel::GetAlbumIndex(void)
83 if (__pSearchList == null)
87 __pSearchList->IndexOf(STRING_SORT_ALBUM, index);
94 SearchPresentationModel::GetArtistIndex(void)
98 if (__pSearchList == null)
102 __pSearchList->IndexOf(STRING_SORT_ARTIST, index);
109 SearchPresentationModel::GetSongIndex(void)
111 AppLogDebug("ENTER");
113 if (__pSearchList == null)
117 __pSearchList->IndexOf(STRING_SORT_TITLE, index);
124 SearchPresentationModel::GetTotalContentCount(void)
126 AppLogDebug("ENTER");
127 if (__pSearchList == null)
133 return __pSearchList->GetCount();
137 SearchPresentationModel::RemoveSearchResult(void)
139 AppLogDebug("ENTER");
140 if (__pSearchList != null)
142 __pSearchList->RemoveAll(true);
148 SearchPresentationModel::InitializeContentList(const Tizen::Base::String& keyword)
150 AppLogDebug("ENTER");
151 if (__pSearchList != null)
153 __pSearchList->RemoveAll(true);
154 delete __pSearchList;
155 __pSearchList = null;
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);
162 if (pArtistSearch != null && pArtistSearch->GetCount() != 0)
164 RemoveDuplicateEntries(*pArtistSearch);
165 pArtistSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_ARTIST)), 0);
166 __pSearchList = pArtistSearch;
168 if (pAlbumSearch != null && pAlbumSearch->GetCount() != 0)
170 RemoveDuplicateEntries(*pAlbumSearch, false);
171 pAlbumSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_ALBUM)), 0);
172 __pSearchList->AddItems(*pAlbumSearch);
175 if (pSongSearch != null && pSongSearch->GetCount() != 0)
177 pSongSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_TITLE)), 0);
178 __pSearchList->AddItems(*pSongSearch);
181 else if (pAlbumSearch != null && pAlbumSearch->GetCount() != 0)
183 RemoveDuplicateEntries(*pAlbumSearch, false);
184 pAlbumSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_ALBUM)), 0);
185 __pSearchList = pAlbumSearch;
187 if (pSongSearch != null && pSongSearch->GetCount() != 0)
189 pSongSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_TITLE)), 0);
190 __pSearchList->AddItems(*pSongSearch);
193 else if (pSongSearch != null && pSongSearch->GetCount() != 0)
195 pSongSearch->InsertAt(*(new (std::nothrow) String(STRING_SORT_TITLE)), 0);
196 __pSearchList = pSongSearch;
200 __pSearchList = null;
206 SearchPresentationModel::Release(void)
208 AppLogDebug("ENTER");
209 if (pSearchPresentationModelInstance != null)
211 delete pSearchPresentationModelInstance;
212 pSearchPresentationModelInstance = null;
218 SearchPresentationModel::GetContentInfoN(int index)
220 AppLogDebug("ENTER");
221 if (__pSearchList == null || index < INIT_VALUE)
223 AppLogDebug("EXIT(index(%d) MUST be greater than or equal to 0 OR __pSearchList is null)", index);
227 ContentInformation* pContentInfoStruct = null;
228 ContentSearchResult* pResult = static_cast<ContentSearchResult*>(__pSearchList->GetAt(index));
229 if (pResult->GetContentType() == CONTENT_TYPE_AUDIO)
231 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pResult->GetContentInfo());
232 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
236 return pContentInfoStruct;
240 SearchPresentationModel::MakeQuery(const Tizen::Base::String& WhereExpr, const Tizen::Base::String& keyword)
242 AppLogDebug("ENTER");
243 if (WhereExpr == null || keyword == null)
248 String strMakeQuery = WhereExpr;
249 strMakeQuery.Append(QUERY_PREFIX);
250 strMakeQuery.Append(CommonUtil::SpecialReplaceChange(keyword));
251 strMakeQuery.Append(QUERY_SUFFIX);
258 SearchPresentationModel::RemoveDuplicateEntries(Tizen::Base::Collection::IList& contentList, const bool isArtist)
260 if (contentList.GetCount())
262 ArrayList* pList = new (std::nothrow) ArrayList();
265 for (int count = 0; count < contentList.GetCount(); count++)
267 ContentSearchResult* pSearchResult = static_cast<ContentSearchResult*>(contentList.GetAt(count));
268 bool foundContent = false;
270 for (int copiedCount = 0; copiedCount < pList->GetCount(); copiedCount++)
272 ContentSearchResult* pTempSearchResult = static_cast<ContentSearchResult*>(pList->GetAt(copiedCount));
273 if (pTempSearchResult)
276 if (isArtist && (static_cast<AudioContentInfo*>(pTempSearchResult->GetContentInfo())->GetArtist().CompareTo(static_cast<AudioContentInfo*>(pSearchResult->GetContentInfo())->GetArtist()) == 0))
281 else if (!isArtist && (static_cast<AudioContentInfo*>(pTempSearchResult->GetContentInfo())->GetAlbumName().CompareTo(static_cast<AudioContentInfo*>(pSearchResult->GetContentInfo())->GetAlbumName()) == 0))
290 pList->Add(pSearchResult);
294 delete pSearchResult;
298 contentList.RemoveAll(false);
299 contentList.AddItems(*pList);