Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / LocalContent / LocalContent / project / src / ContentSearchSampleImpl.cpp
1 //\r
2 // Tizen C++ SDK\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\r
5 // Licensed under the Flora License, Version 1.0 (the License);\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //      http://www.tizenopensource.org/license\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an AS IS BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 //\r
17 \r
18 #include "ContentSearchSampleImpl.h"\r
19 \r
20 using namespace Osp::Base;\r
21 using namespace Osp::Base::Collection;\r
22 using namespace Osp::App;\r
23 using namespace Osp::Content;\r
24 \r
25 static const int MAX_CONTENTSEARCH_COUNTPERPAGE = 100;\r
26 static const int STRING_CAPACITY = 1024;\r
27 \r
28 ContentSearchSampleImpl::ContentSearchSampleImpl(void)\r
29         : __contentType(CONTENT_TYPE_ALL)\r
30         , __pSearchResultList(null)\r
31 {\r
32 }\r
33 \r
34 ContentSearchSampleImpl::~ContentSearchSampleImpl(void)\r
35 {\r
36         initResultList();\r
37 }\r
38 \r
39 void\r
40 ContentSearchSampleImpl::initResultList(void)\r
41 {\r
42         if (__pSearchResultList != null)\r
43         {\r
44                 __pSearchResultList->RemoveAll(true);\r
45                 delete __pSearchResultList;\r
46                 __pSearchResultList = null;\r
47         }\r
48 }\r
49 \r
50 String\r
51 ContentSearchSampleImpl::makeQuery(const String& queryString)\r
52 {\r
53         String strQuery;\r
54         String strTemp;\r
55 \r
56         strTemp = queryString;\r
57         // set capacity for query string\r
58         strQuery.SetCapacity(STRING_CAPACITY);\r
59         strTemp.SetCapacity(STRING_CAPACITY);\r
60 \r
61         // get query string from EditBox\r
62         if (!strTemp.IsEmpty())\r
63         {\r
64                 strQuery.Append(L"ContentFileName");\r
65                 strQuery.Append(L" LIKE '%");\r
66 \r
67                 // convert special character for like query\r
68                 strTemp.Replace(L"\\", L"\\\\");\r
69                 strTemp.Replace(L"_", L"\\_");\r
70                 strTemp.Replace(L"%", L"\\%");\r
71                 strTemp.Replace(L"'", L"\\'");\r
72 \r
73                 strQuery.Append(strTemp);\r
74                 strQuery.Append(L"%'");\r
75         }\r
76 \r
77         return strQuery;\r
78 }\r
79 \r
80 result\r
81 ContentSearchSampleImpl::SearchContent(ContentType contentType, const String& query)\r
82 {\r
83         result r = E_SUCCESS;\r
84         ContentSearch contentSearch;\r
85         String sortColumn = L"";\r
86         String strQuery;\r
87         int totalPage = 0;\r
88         int totalCount = 0;\r
89         int page = 1;\r
90 \r
91         // Clear the previous result\r
92         initResultList();\r
93 \r
94         __contentType = contentType;\r
95         __queryString = query;\r
96 \r
97         r = contentSearch.Construct(__contentType);\r
98         TryReturn(E_SUCCESS == r, r, "Construct() failed by %s.", GetErrorMessage(r));\r
99 \r
100         strQuery = makeQuery(query);\r
101 \r
102         // execute\r
103         __pSearchResultList = contentSearch.SearchN(page, MAX_CONTENTSEARCH_COUNTPERPAGE, totalPage, totalCount, strQuery, sortColumn, SORT_ORDER_NONE);\r
104         r = GetLastResult();\r
105         TryReturn(__pSearchResultList != null, r, "ContentSearch.SearchN() failed by %s.", GetErrorMessage(r));\r
106 }\r
107 \r
108 result\r
109 ContentSearchSampleImpl::RefreshSearchContent(void)\r
110 {\r
111         return SearchContent(__contentType, __queryString);\r
112 }\r
113 \r
114 int\r
115 ContentSearchSampleImpl::GetSearchResultCount() const\r
116 {\r
117         int count = 0;\r
118 \r
119         if (__pSearchResultList != null)\r
120         {\r
121                 count = __pSearchResultList->GetCount();\r
122         }\r
123 \r
124         return count;\r
125 }\r
126 \r
127 String\r
128 ContentSearchSampleImpl::GetContentFileName(int index) const\r
129 {\r
130         String contentFullPath;\r
131         String contentFileName;\r
132         String delimeter = L"/";\r
133         int filenamePos = 0;\r
134         ContentSearchResult* pInfo = null;\r
135         result r = E_SUCCESS;\r
136 \r
137         contentFileName.Clear();\r
138 \r
139         TryReturn(__pSearchResultList != null, contentFileName, "No search result");\r
140 \r
141         pInfo = (ContentSearchResult*)__pSearchResultList->GetAt(index);\r
142         contentFullPath = ((ContentInfo*)pInfo->GetContentInfo())->GetContentPath();\r
143         if (contentFullPath.IsEmpty() == false)\r
144         {\r
145                 // eliminate a directory name from the ContentPath\r
146                 r = contentFullPath.LastIndexOf(delimeter, contentFullPath.GetLength() - 1, filenamePos);\r
147                 if (r == E_SUCCESS && filenamePos > 0)\r
148                 {\r
149                         r = contentFullPath.SubString(filenamePos + 1, contentFileName);\r
150                         if (r != E_SUCCESS)\r
151                         {\r
152                                 contentFileName = contentFullPath;\r
153                         }\r
154                 }\r
155                 else\r
156                 {\r
157                         contentFileName = contentFullPath;\r
158                 }\r
159         }\r
160 \r
161     return contentFileName;\r
162 }\r
163 \r
164 ContentSearchResult*\r
165 ContentSearchSampleImpl::GetSearchResultItem(int index) const\r
166 {\r
167         ContentSearchResult* pSearchItem = null;\r
168 \r
169         if (__pSearchResultList)\r
170         {\r
171                 pSearchItem = (ContentSearchResult*)__pSearchResultList->GetAt(index);\r
172         }\r
173 \r
174         return pSearchItem;\r
175 }\r