[content] Fix compat TC fails
[platform/framework/native/content.git] / src / FCnt_ContentSearchImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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  * @file                FCnt_ContentSearchImpl.cpp
18  * @brief               This is the implementation file for the %_ContentSearchImpl class.
19  *
20  * This file contains implementation of the %_ContentSearchImpl class.
21  */
22
23 #include <new>
24 #include <time.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseInteger.h>
27 #include <FBaseLongLong.h>
28 #include <FBaseFloat.h>
29 #include <FBaseColIList.h>
30 #include <FBaseUtilStringTokenizer.h>
31 #include <FCntContentSearch.h>
32 #include <FCntContentSearchResult.h>
33 #include <FSysEnvironment.h>
34 #include <FApp_AppInfo.h>
35 #include <FBase_StringConverter.h>
36 #include <FBase_LocalizedNumParser.h>
37 #include <FIo_FileImpl.h>
38 #include "FCnt_ContentUtility.h"
39 #include "FCnt_ContentSearchImpl.h"
40 #include "FCnt_ContentInfoImpl.h"
41 #include "FCnt_ImageContentInfoImpl.h"
42 #include "FCnt_AudioContentInfoImpl.h"
43 #include "FCnt_VideoContentInfoImpl.h"
44 #include "FCnt_OtherContentInfoImpl.h"
45 #include "FCnt_ContentInfoHelper.h"
46
47 using namespace Tizen::App;
48 using namespace Tizen::Base;
49 using namespace Tizen::Base::Collection;
50 using namespace Tizen::Base::Utility;
51 using namespace Tizen::Io;
52 using namespace Tizen::System;
53
54 namespace Tizen { namespace Content
55 {
56
57 static const int QUERY_LENGTH = 4096;
58
59 // Declaration for Callback function registered to each media info details
60 bool MediaItemCb(media_info_h media, void* pUserdata);
61 // Declaration for Callback function registered to each column details
62 bool GroupItemCb(const char* pGroupName, void* pUserdata);
63 // Declaration for Callback function registered to each album details
64 bool AlbumItemCb(media_album_h album, void* pUserdata);
65
66 // Default constructor
67 _ContentSearchImpl::_ContentSearchImpl(void)
68         : __contentType(CONTENT_TYPE_UNKNOWN)
69         , __pFinalOutList(NULL)
70         , __inputExpr(L"")
71         , __inputColumnName(L"")
72         , __inputSortOrder(SORT_ORDER_NONE)
73         , __pFilterHandle(NULL)
74 {
75         SysLog(NID_CNT, "Enter\n");
76 }
77
78
79 // Default destructor (disconnects the DB connection)
80 _ContentSearchImpl::~_ContentSearchImpl(void)
81 {
82         SysLog(NID_CNT, "Enter\n");
83
84         int ret = MEDIA_CONTENT_ERROR_NONE;
85         result r = E_SUCCESS;
86
87         ret = media_content_disconnect();
88         r = MapCoreErrorToNativeResult(ret);
89         SysTryLog(NID_CNT, r == E_SUCCESS, "[%s] Propagated in ~_ContentSearchImpl", GetErrorMessage(r));
90 }
91
92  _ContentSearchImpl*
93  _ContentSearchImpl::GetInstance(ContentSearch& contentSearch)
94 {
95         return contentSearch.__pImpl;
96 }
97
98 const _ContentSearchImpl*
99 _ContentSearchImpl::GetInstance(const ContentSearch& contentSearch)
100 {
101         return contentSearch.__pImpl;
102 }
103
104 //make a connection to DB
105 result
106 _ContentSearchImpl::Construct(ContentType type)
107 {
108         SysLog(NID_CNT, "Enter\n");
109
110         result r = E_SUCCESS;
111         int ret = MEDIA_CONTENT_ERROR_NONE;
112
113         ret = media_content_connect();
114         SysTryReturnResult(NID_CNT, r == E_SUCCESS , E_SYSTEM, "Failed to perform media_content_connect operation.");
115
116         __contentType = type;
117
118         return r;
119 }
120
121 // Creates a filter for the input query string along with content type
122 // Initializes filter handle
123 // Image - MEDIA_TYPE=0
124 // Vide0 - MEDIA_TYPE=1
125 // Audio - MEDIA_TYPE=2 or MEDIA_TYPE=3
126 // Others - MEDIA_TYPE=4
127 // All - none
128 // Appends MEDIA_TYPE with "AND" (if the input expression is not empty and content type is not ALL) and  input expression
129 // Sets the condition and order.
130 // If any argument is not proper E_INVALID_ARG is returned
131 result
132 _ContentSearchImpl::CreateQueryFilter(bool isAndAppendReq) const
133 {
134         int ret  = MEDIA_CONTENT_ERROR_NONE;
135         result r = E_SUCCESS;
136
137         std::unique_ptr<char[]> pInputCond;
138         std::unique_ptr<char[]> pSortCol;
139
140         String inputCondition;
141         String slpColumn;
142
143         filter_h tempFilter = NULL;
144
145         ret = media_filter_create(&tempFilter);
146         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_create operation.");
147
148         std::unique_ptr<filter_s, SearchFilterHandleDeleter> pFilterHandle(tempFilter);
149         SysTryReturnResult(NID_CNT, pFilterHandle != null, E_OUT_OF_MEMORY, "pFilterHandle is null.");
150
151         switch (__contentType)
152         {
153                 // Image-0,video-1,sound-2,music-3,other-4
154         case CONTENT_TYPE_OTHER:
155                 inputCondition = "MEDIA_TYPE=4 ";
156                 break;
157         case CONTENT_TYPE_IMAGE:
158                 inputCondition = "MEDIA_TYPE=0 ";
159                 break;
160         case CONTENT_TYPE_AUDIO:
161                 inputCondition = "(MEDIA_TYPE=2 or MEDIA_TYPE=3) ";
162                 break;
163         case CONTENT_TYPE_VIDEO:
164                 inputCondition = "MEDIA_TYPE=1 ";
165                 break;
166         case CONTENT_TYPE_ALL:
167                 //If content type is CONTENT_TYPE_ALL, then MEDIA_TYPE is empty
168                 break;
169         default:
170                 break;
171         }
172
173         if (!__inputExpr.IsEmpty())
174         {
175                 if (isAndAppendReq && (!inputCondition.IsEmpty())) //For CONTENT_TYPE_ALL inputCondition is empty
176                 {
177                         r = inputCondition.Append("AND ");
178                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform append operation.");
179                 }
180
181                 r = inputCondition.Append(__inputExpr);
182                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform append operation.");
183         }
184
185         if (!inputCondition.IsEmpty())
186         {
187                 //CopyToCharArrayN: utility function, converts a osp string to char*
188                 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
189                 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "pInputCond is NULL.");
190
191                 SysLog(NID_CNT, "pInputCond = %s", pInputCond.get());
192
193                 ret = media_filter_set_condition(pFilterHandle.get(), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
194                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_set_condition operation.");
195         }
196
197         if (!__inputColumnName.IsEmpty()) // SortColumn is optional in case of SearchN
198         {
199                 //__inputColumnName (osp column name) is replaced with slpColumn (slp column name).
200                 r = GetSlpColumnName(slpColumn);
201                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform GetSlpColumnName operation.");
202
203                 //CopyToCharArrayN: utility function, converts a osp string to char*
204
205                 pSortCol = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(slpColumn));
206                 SysTryReturnResult(NID_CNT, pSortCol, E_OUT_OF_MEMORY, "pSortCol is NULL.");
207
208                 if (__inputSortOrder == SORT_ORDER_ASCENDING)
209                 {
210                         ret = media_filter_set_order(pFilterHandle.get(), MEDIA_CONTENT_ORDER_ASC, pSortCol.get(), MEDIA_CONTENT_COLLATE_NOCASE);
211                         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_set_order operation.");
212                 }
213                 else if (__inputSortOrder == SORT_ORDER_DESCENDING)
214                 {
215                         ret = media_filter_set_order(pFilterHandle.get(), MEDIA_CONTENT_ORDER_DESC, pSortCol.get(), MEDIA_CONTENT_COLLATE_NOCASE);
216                         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "Failed to perform media_filter_set_order operation.");
217                 }
218         }
219
220         __pFilterHandle.reset(pFilterHandle.release());
221
222         return r;
223 }
224
225 // Osp column names are mapped with slp column names
226 // CONTENT_TYPE_OTHER and CONTENT_TYPE_IMAGE (0 - 13 ) are valid columns
227 // CONTENT_TYPE_VIDEO  (0 - 16 ) are valid columns
228 // CONTENT_TYPE_ALL and  CONTENT_TYPE_VIDEO (0 - 18 ) are valid columns
229 // if the given osp column is out of the specified range of the type, E_INVALID_ARG is retuned.
230 result
231 _ContentSearchImpl::GetSlpColumnName(String& inputCol) const
232 {
233         String          ospColumnName(L"");
234         String          slpColumnName(L"");
235         String          columnName(__inputColumnName);
236         result          r = E_SUCCESS;
237         int             maxCols = 0;
238         switch (__contentType)
239         {
240         case CONTENT_TYPE_OTHER:
241                 //fall through
242         case CONTENT_TYPE_IMAGE:
243                 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
244                 break;
245         case CONTENT_TYPE_VIDEO:
246                 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
247                 break;
248         case CONTENT_TYPE_AUDIO:
249                 //fall through
250         case CONTENT_TYPE_ALL:
251                 maxCols = MAX_QUERY_COLUMNS;
252                 break;
253         default:
254                 break;
255         }
256         for (int colIndex = 0; colIndex < maxCols; colIndex++)
257         {
258                 ospColumnName.Clear();
259                 slpColumnName.Clear();
260
261                 ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
262                 slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
263
264                 ospColumnName.ToUpper();
265                 columnName.ToUpper();
266                 if (columnName == ospColumnName)
267                 {
268                         inputCol = slpColumnName;
269                         return r;
270                 }
271         }
272         return E_INVALID_ARG;
273 }
274
275 //If the input expression contains any osp column name, it will be replaced with slp column name.
276 // only CONTENT_TYPE_AUDIO and CONTENT_TYPE_ALL allowed to use all given columns
277 // so, check for invalid column for CONTENT_TYPE_OTHER,CONTENT_TYPE_IMAGE and CONTENT_TYPE_VIDEO.
278 // all the osp columns in the expression should be replaced with slp column names.
279 // ' is used as a delimeter in parsing user query. so, \\' is saved as a special@apostrophe string.
280 // \\' is replaced with '', which considers ' as normal character.
281
282 result
283 _ContentSearchImpl::ReplaceOspColumnNameWithSlp(void) const
284 {
285         String          ospColumnName(L"");
286         String          slpColumnName(L"");
287         String          splApostrophe(L"special@apostrophe");
288         result          r = E_SUCCESS;
289         int             maxCols = 0;
290
291         int indexOf = 0;
292
293         String strToBeReplaced(L"\\'");
294         int strLen = strToBeReplaced.GetLength();
295         int startIndex = 0;
296
297         while(E_SUCCESS == __inputExpr.IndexOf(strToBeReplaced,startIndex,indexOf))
298         {
299                 int lenAfterSubStr = indexOf + strLen;
300                 while ((lenAfterSubStr < __inputExpr.GetLength()) && (__inputExpr[lenAfterSubStr] == ' '))
301                 {
302                         lenAfterSubStr++;
303                 }
304                 if ((lenAfterSubStr < __inputExpr.GetLength()) && ((__inputExpr[lenAfterSubStr] == '\'') ||
305                                 ((!__inputExpr.StartsWith(L"AND ", lenAfterSubStr)) && (!__inputExpr.StartsWith(L"OR ", lenAfterSubStr)))))
306                 {
307                         r = __inputExpr.Remove(indexOf,strLen);
308                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "Failed to perform Remove operation.");
309                         r = __inputExpr.Insert(splApostrophe,indexOf);
310                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "Failed to perform Insert operation.");
311                         startIndex = indexOf + splApostrophe.GetLength();
312                 }
313                 else
314                 {
315                         startIndex = lenAfterSubStr;
316                 }
317         }
318
319         SysLog(NID_CNT, "__inputExpr after splApostrophe append = %ls", __inputExpr.GetPointer());
320
321         switch (__contentType)
322         {
323         case CONTENT_TYPE_OTHER:
324                 //fall through
325         case CONTENT_TYPE_IMAGE:
326                 r = CheckInvalidColumnInQuery();
327                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_INVALID_ARG, "Invalid Column In QueryString.");
328                 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
329                 break;
330         case CONTENT_TYPE_VIDEO:
331                 r = CheckInvalidColumnInQuery();
332                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_INVALID_ARG, "Invalid Column In QueryString.");
333                 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
334                 break;
335         case CONTENT_TYPE_AUDIO:
336                 //fall through
337         case CONTENT_TYPE_ALL:
338                 maxCols = MAX_QUERY_COLUMNS;
339                 break;
340         default:
341                 break;
342         }
343         for (int colIndex = 0; colIndex < maxCols; colIndex++)
344         {
345                 ospColumnName.Clear();
346                 slpColumnName.Clear();
347
348                 ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
349                 slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
350
351                 ReplaceString(ospColumnName,slpColumnName);
352         }
353
354         // Append ESCAPE '\' for LIKE query
355         r = AppendEscapeKeywordForLikeQuery();
356         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "AppendEscapeKeywordForLikeQuery failed.");
357
358         r = ReplaceDateTimeStringWithInt();
359         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_INVALID_ARG, "ReplaceDateTimeStringWithInt failed.");
360
361         // replace splApostrophe string with actual
362         r = __inputExpr.Replace(splApostrophe, "''");
363         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string Replace failed.");
364
365         return r;
366 }
367
368 //This function is to appened ESCAPE keyword for _ and % special characters in LIKE query.
369
370 result
371 _ContentSearchImpl::AppendEscapeKeywordForLikeQuery(void) const
372 {
373         String  delim = L"'"; //Delimeters  is  ' .
374         result r = E_SUCCESS;
375         bool isCol = true;
376         bool isAppendEscape = false;
377         String token;
378         String tokenUpper;
379         String inputExpr = __inputExpr;
380
381         // Create a StringTokenizer instance
382         StringTokenizer  strTok(inputExpr, delim);
383
384         inputExpr.Clear();
385
386         while (strTok.HasMoreTokens())
387         {
388                 r = strTok.GetNextToken(token);
389                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "GetNextToken failed.");
390                 if (isCol) //column name
391                 {
392                         isCol = false;
393                         tokenUpper = token;
394                         tokenUpper.ToUpper();
395                         if (tokenUpper.Contains(" LIKE"))
396                         {
397                                 isAppendEscape = true;
398                         }
399                         else
400                         {
401                                 isAppendEscape = false;
402                         }
403                         r = inputExpr.Append(token);
404                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
405                 }
406                 else // value of the column
407                 {
408                         isCol = true;
409                         r = inputExpr.Append("'");
410                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
411                         r = inputExpr.Append(token);
412                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
413                         r = inputExpr.Append("'");
414                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
415
416                         if (isAppendEscape)
417                         {
418                                 if (token.Contains("\\_") || token.Contains("\\%"))
419                                 {
420                                         r = inputExpr.Append("ESCAPE'\\'  ");
421                                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
422                                 }
423                         }
424                 }
425         }
426
427         __inputExpr.Clear();
428         r = __inputExpr.Insert(inputExpr, 0);
429         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string insert failed.");
430
431         return r;
432 }
433
434 //This function is to replace DateTime value(osp datetime type is string) which is string with int.(SLP dateTime column type is int)
435
436 result
437 _ContentSearchImpl::ReplaceDateTimeStringWithInt(void) const
438 {
439         String  delim = L"'"; //Delimeters  is  ' .
440         result r = E_SUCCESS;
441         bool isCol = true;
442         bool isConvertReq = false;
443         bool isBetweenExistsInDateTimeQuery = false;
444         String token;
445         String tokenUpper;
446         String inputExpr = __inputExpr;
447
448         if(!inputExpr.Contains("MEDIA_ADDED_TIME"))
449         {
450                 return r;
451         }
452
453         // Create a StringTokenizer instance
454         StringTokenizer  strTok(inputExpr, delim);
455
456         inputExpr.Clear();
457
458         while (strTok.HasMoreTokens())
459         {
460                 r = strTok.GetNextToken(token);
461                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "GetNextToken failed.");
462                 if (isCol) //column name
463                 {
464                         isCol = false;
465                         tokenUpper = token;
466                         tokenUpper.ToUpper();
467                         if(isBetweenExistsInDateTimeQuery)
468                         {
469                                 isBetweenExistsInDateTimeQuery = false;
470                                 isConvertReq = true;
471                         }
472                         else
473                         {
474                                 if (tokenUpper.Contains("MEDIA_ADDED_TIME"))
475                                 {
476                                         if (tokenUpper.Contains("BETWEEN"))
477                                         {
478                                                 isBetweenExistsInDateTimeQuery = true;
479                                         }
480                                         isConvertReq = true;
481                                 }
482                                 else
483                                 {
484                                         isConvertReq = false;
485                                 }
486                         }
487
488                         r = inputExpr.Append(token);
489                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
490                 }
491                 else // value of the column
492                 {
493                         isCol = true;
494                         r = inputExpr.Append("'");
495                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
496
497                         if (isConvertReq)
498                         {
499                                 Tizen::Base::DateTime dt;
500                                 r = Tizen::Base::DateTime::Parse(token, dt);
501                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to parse DateTime.");
502
503                                 int year = dt.GetYear();
504                                 int month = dt.GetMonth();
505                                 int day = dt.GetDay();
506                                 int hour = dt.GetHour();
507                                 int minute = dt.GetMinute();
508                                 int second = dt.GetSecond();
509
510                                 time_t rawTime;
511                                 struct tm* timeInfo;
512
513                                 time(&rawTime);
514                                 timeInfo = localtime(&rawTime);
515                                 timeInfo->tm_year = year - 1900;
516                                 timeInfo->tm_mon = month - 1;
517                                 timeInfo->tm_mday = day;
518                                 timeInfo->tm_hour = hour;
519                                 timeInfo->tm_min = minute;
520                                 timeInfo->tm_sec = second;
521
522                                 time_t seconds = mktime(timeInfo);
523                                 SysTryReturnResult(NID_CNT, seconds != -1, E_INVALID_ARG, "Failed to convert DateTime to broken-down time.");
524
525                                 long long ticksInSeconds = (long long)seconds;
526
527                                 r = inputExpr.Append(ticksInSeconds);
528                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
529                         }
530                         else
531                         {
532                                 r = inputExpr.Append(token);
533                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
534                         }
535
536                         r = inputExpr.Append("'");
537                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string append failed.");
538                 }
539         }
540
541         __inputExpr.Clear();
542         r = __inputExpr.Insert(inputExpr, 0);
543         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "string insert failed.");
544
545         return r;
546 }
547
548 // CONTENT_TYPE_OTHER and CONTENT_TYPE_IMAGE (0 - 13 ) are valid columns
549 // CONTENT_TYPE_VIDEO  (0 - 16 ) are valid columns
550 // CONTENT_TYPE_ALL and  CONTENT_TYPE_VIDEO (0 - 18 ) are valid columns
551 // This functions checks for invalid column in the given input string (only allowed columns should be used by content type other wise
552 // E_INVALID_ARG is returned)
553 // StringTokenizer is used to parse input expression, by using delimater "'", to differentiate column and value.
554 // As values may also contain column names
555 // Ex: 1. where author = syam_author_01 2. where album = jalsa_album_name_01.
556
557 result
558 _ContentSearchImpl::CheckInvalidColumnInQuery(void) const
559 {
560         result r = E_SUCCESS;
561         int tokenNo = 0;
562         String  delim = L"'"; //Delimeters  is  ' .
563          bool isColReplaceReq = true;
564         String token;
565         String inputExpr = __inputExpr;
566
567         // Create a StringTokenizer instance
568         StringTokenizer  strTok(inputExpr, delim);
569
570         inputExpr.Clear();
571
572         while (strTok.HasMoreTokens())
573         {
574                 r = strTok.GetNextToken(token);
575                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "strTok GetNextToken failed.");
576
577                 SysLog(NID_CNT, "In CheckInvalidColumnInQuery token[%d] = %ls", ++tokenNo, token.GetPointer());
578
579                 if (isColReplaceReq)
580                 {
581                         isColReplaceReq = false;
582                         r = inputExpr.Append(token);
583                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
584                         token.ToUpper();
585                         switch (__contentType)
586                         {
587                         case CONTENT_TYPE_OTHER:
588                                 //fall through
589                         case CONTENT_TYPE_IMAGE:
590                                 if ((token.Contains("TITLE")) || (token.Contains("ARTIST")) || (token.Contains("GENRE")))
591                                 {
592                                         SysLog(NID_CNT, "Title or Artist or Genre are not valid cloumns for this content type");
593                                         return E_INVALID_ARG;
594                                 }
595                                 //fall through
596                         case CONTENT_TYPE_VIDEO:
597                                 if ((token.Contains("COMPOSER")) || (token.Contains("ALBUM")))
598                                 {
599                                         SysLog(NID_CNT, "Composer or Album  are not valid cloumns for this content type");
600                                         return E_INVALID_ARG;
601                                 }
602                                 break;
603                         default:
604                                 break;
605                         }
606                 }
607                 else
608                 {
609                         isColReplaceReq = true;
610                         r = inputExpr.Append("'");
611                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
612                         r = inputExpr.Append(token);
613                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
614                         r = inputExpr.Append("'");
615                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
616                 }
617         }
618         return r;
619 }
620
621 // It replaces native column name with core column name.
622 // StringTokenizer is used to parse input expression, by using delimiter "'", to differentiate column and value.
623 // As values may also contain column names
624 // Only column names are replaced but not values(values may contain column names)
625 // Ex: 1. where author = syam_author_01 2. where album = jalsa_album_name_01.
626
627 result
628 _ContentSearchImpl::ReplaceString(String ospColumnName, String slpColumnName) const
629 {
630         String  delim = L"'"; //Delimiters  is  ' .
631         result r = E_SUCCESS;
632          bool isColReplaceReq = true;
633         String token;
634         String inputExpr = __inputExpr;
635
636         // Create a StringTokenizer instance
637         StringTokenizer  strTok(inputExpr, delim);
638
639         inputExpr.Clear();
640
641         while (strTok.HasMoreTokens())
642         {
643                 r = strTok.GetNextToken(token);         // Osp, StringTokenizer, Sample, code
644                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string GetNextToken() failed.");
645                 if (isColReplaceReq) //column name
646                 {
647                         isColReplaceReq = false;
648                         r = token.Replace(ospColumnName, slpColumnName);
649                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string replace() failed.");
650                         r = inputExpr.Append(token);
651                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
652                 }
653                 else // value of the column
654                 {
655                         isColReplaceReq = true;
656                         r = inputExpr.Append("'");
657                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
658                         r = inputExpr.Append(token);
659                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
660                         r = inputExpr.Append("'");
661                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string append failed.");
662                 }
663         }
664         __inputExpr.Clear();
665         r = __inputExpr.Insert(inputExpr, 0);
666         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "string insert() failed.");
667
668         return r;
669 }
670
671 // Returns list of search result for the given expression and content type.
672 Collection::IList*
673 _ContentSearchImpl::SearchN(int pageNo, int countPerPage, int& totalPageCount, int& totalCount, const String& whereExpr, const String& sortColumn, SortOrder sortOrder) const
674 {
675         SysLog(NID_CNT, "pageNo = %d, countPerPage = %d, whereExpr = %ls", pageNo, countPerPage, whereExpr.GetPointer());
676
677         ClearLastResult();
678
679         result r        = E_SUCCESS;
680         int ret         = MEDIA_CONTENT_ERROR_NONE;
681         int offset      = 0;
682
683         __inputColumnName.Clear();
684         __inputExpr.Clear();
685         __inputColumnName = sortColumn;
686         __inputSortOrder = sortOrder;
687         totalPageCount = 0;
688         totalCount = 0;
689
690         if (!whereExpr.IsEmpty()) // copy whereExpr if not empty to class variable __inputExpr
691         {
692                 r = __inputExpr.Append(whereExpr);
693                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] __inputExpr.Append Failed as whereExpr is not empty.", GetErrorMessage(r));
694
695                 r = ReplaceOspColumnNameWithSlp();
696                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] ReplaceOspColumnNameWithSlp for InputExpr Failed.", GetErrorMessage(r));
697
698                 SysLog(NID_CNT, "After replace __inputExpr = %ls", __inputExpr.GetPointer());
699         }
700         if ((__inputColumnName.IsEmpty()) && ((__inputSortOrder == SORT_ORDER_ASCENDING) || (__inputSortOrder == SORT_ORDER_DESCENDING)))
701         {
702                 SysLog(NID_CNT, "sort column name is empty and sort oder is not none. so,E_INVALID_ARG occured.");
703                 SetLastResult(E_INVALID_ARG);
704                 return null;
705         }
706         else
707         {
708                 std::unique_ptr<ArrayList, AllElementsDeleter> pFinalOutList(new (std::nothrow) ArrayList());
709                 SysTryReturn(NID_CNT, pFinalOutList.get() != null, NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
710
711                 r = pFinalOutList->Construct();
712                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, r, "[%s] Failed to construct ArrayList.", GetErrorMessage(r));
713
714                 __pFinalOutList = std::move(pFinalOutList);
715
716                 r = CreateQueryFilter(true);
717                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, r, "[%s] Failed to perform CreateQueryFilter operation.", GetErrorMessage(r));
718
719                 ret = media_info_get_media_count_from_db(__pFilterHandle.get(), &totalCount);
720                 r = MapCoreErrorToNativeResult(ret);
721                 SysTryReturn(NID_CNT, r == E_SUCCESS , NULL, r, "[%s] Failed to perform media_info_get_media_count_from_db operation.", GetErrorMessage(r));
722
723                 SysLog(NID_CNT, "totalCount is [%d] for media_info_get_media_count_from_db", totalCount);
724
725                 if (totalCount > 0)
726                 {
727                         if ((totalCount % countPerPage) == 0)
728                         {
729                                 totalPageCount = totalCount / countPerPage;
730                         }
731                         else
732                         {
733                                 totalPageCount = (totalCount / countPerPage) + 1;
734                         }
735
736                         SysTryReturn(NID_CNT, ((pageNo >= 1) && (pageNo <= totalPageCount)) , NULL, E_INVALID_ARG, "[E_INVALID_ARG] (pageNo < 1) || (pageNo > totalPageCount).");
737
738                         offset = (pageNo * countPerPage) - countPerPage;
739
740                         SysLog(NID_CNT, " SearchN   totalCount [%d] totalPageCount[%d] __countPerPage[%d] __pageNo[%d] offset[%d]",
741                                         totalCount, totalPageCount, countPerPage, pageNo, offset);
742
743                         ret = media_filter_set_offset(__pFilterHandle.get(), offset, countPerPage);
744                         r = MapCoreErrorToNativeResult(ret);
745                         SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r,
746                         "[%s] Failed to perform media_filter_set_offset operation.", GetErrorMessage(r));
747
748                         r = ExecuteAndFillFinalOutList();
749                         SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, E_SYSTEM, "[E_SYSTEM] ExecuteAndFillFinalOutList Failed.");
750                 }
751                 else if (pageNo > 1)
752                 {
753                         SysLogException(NID_CNT, E_INVALID_ARG, "[%s] (pageNo > 1) and  (totalcount = 0).", GetErrorMessage(E_INVALID_ARG));
754
755                         return null;
756                 }
757         }
758         SetLastResult(r);
759         return __pFinalOutList.release();
760 }
761
762 // Fills final result list
763 result
764 _ContentSearchImpl::ExecuteAndFillFinalOutList(void) const
765 {
766         SysLog(NID_CNT, "Enter\n");
767
768         int ret                                         = MEDIA_CONTENT_ERROR_NONE;
769         result r                                        = E_SUCCESS;
770
771         std::unique_ptr<GList, SearchGListDeleter> pItemList;
772         GList* pTemp = NULL;
773         std::unique_ptr<media_info_s, SearchMediaHandleDeleter> pMediaHandle;
774
775         std::unique_ptr<ImageContentInfo> pImageContentInfo;
776         std::unique_ptr<AudioContentInfo> pAudioContentInfo;
777         std::unique_ptr<VideoContentInfo> pVideoContentInfo;
778         std::unique_ptr<OtherContentInfo> pOtherContentInfo;
779
780         _ImageContentInfoImpl* pImageContentInfoImpl = null;
781         _AudioContentInfoImpl* pAudioContentInfoImpl = null;
782         _VideoContentInfoImpl* pVideoContentInfoImpl = null;
783         _OtherContentInfoImpl* pOtherContentInfoImpl = null;
784
785         std::unique_ptr<ContentSearchResult> pContentSearchResult;
786
787         pTemp = pItemList.get();
788
789         ret = media_info_foreach_media_from_db(__pFilterHandle.get(), MediaItemCb, &pTemp);
790         r = MapCoreErrorToNativeResult(ret);
791         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_info_foreach_media_from_db operation.");
792
793         SysTryReturnResult(NID_CNT, pTemp != NULL, r, "media_info_foreach_media_from_db pTemp is null.");
794
795         media_content_type_e mediaType = MEDIA_CONTENT_TYPE_OTHERS;
796
797         for (int idx = 0; idx < (int)g_list_length(pTemp); idx++)
798         {
799                 pMediaHandle.reset(static_cast<media_info_h>(g_list_nth_data(pTemp, idx)));
800
801                 ret = media_info_get_media_type(pMediaHandle.get(), &mediaType);
802                 r = MapCoreErrorToNativeResult(ret);
803                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_info_get_media_type.");
804
805                 std::unique_ptr<char, UtilCharDeleter> pMediaPath;
806                 char* pTempPath = null;
807
808                 ret = media_info_get_file_path(pMediaHandle.get(), &pTempPath);
809                 r = MapCoreErrorToNativeResult(ret);
810                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_info_get_file_path operation.");
811
812                 pMediaPath.reset(pTempPath);
813                 String contentPath(pMediaPath.get());
814                 String changedPath(contentPath);
815
816                 // If the api version is 2.0, the content path has to be changed.
817                 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
818                 {
819                         r = _FileImpl::ConvertPhysicalToVirtualPath(contentPath, changedPath);
820                         SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG,
821                                         "[%s] Failed to convert physical path to virtual path.", GetErrorMessage(E_INVALID_ARG));
822                 }
823
824                 switch (mediaType)
825                 {
826                 case MEDIA_CONTENT_TYPE_OTHERS:
827                         pOtherContentInfo = std::unique_ptr<OtherContentInfo>(new (std::nothrow) OtherContentInfo);
828                         SysTryReturnResult(NID_CNT, pOtherContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pOtherContentInfo.");
829
830                         r = pOtherContentInfo->Construct(&changedPath);
831                         r = ConvertErrorToResult(r);
832                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to OtherContentInfo.");
833
834                         pOtherContentInfoImpl = null;
835                         pOtherContentInfoImpl = _OtherContentInfoImpl::GetInstance(*(pOtherContentInfo.get()));
836                         SysTryReturnResult(NID_CNT, pOtherContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pOtherContentInfoImpl.");
837
838                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pOtherContentInfoImpl);
839                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
840
841                         _ContentInfoHelper::SetContentInfoImpl(pOtherContentInfo.get(), pOtherContentInfoImpl);
842
843                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
844                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
845
846                         pContentSearchResult->SetContentType(CONTENT_TYPE_OTHER);
847                         pContentSearchResult->SetContentInfo(pOtherContentInfo.release());
848
849                         // Shallow copy, adds just the pointer: not the element
850                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
851                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
852
853                         break;
854                 case MEDIA_CONTENT_TYPE_IMAGE:
855                         pImageContentInfo = std::unique_ptr<ImageContentInfo>(new (std::nothrow) ImageContentInfo);
856                         SysTryReturnResult(NID_CNT, pImageContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pImageContentInfo.");
857
858                         r = pImageContentInfo->Construct(&changedPath);
859                         r = ConvertErrorToResult(r);
860                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to ImageContentInfo.");
861
862                         pImageContentInfoImpl = null;
863                         pImageContentInfoImpl = _ImageContentInfoImpl::GetInstance(*(pImageContentInfo.get()));
864                         SysTryReturnResult(NID_CNT, pImageContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pImageContentInfoImpl.");
865
866                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pImageContentInfoImpl);
867                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
868
869                         r = _ContentUtility::FillImageContentData(pMediaHandle.get(), pImageContentInfoImpl);
870                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform GetDataFromImageTable operation.");
871
872                         _ContentInfoHelper::SetContentInfoImpl(pImageContentInfo.get(), pImageContentInfoImpl);
873
874                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
875                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
876
877                         pContentSearchResult->SetContentType(CONTENT_TYPE_IMAGE);
878                         pContentSearchResult->SetContentInfo(pImageContentInfo.release());
879
880                         // Shallow copy, adds just the pointer: not the element
881                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
882                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
883
884                         break;
885                 case MEDIA_CONTENT_TYPE_MUSIC:
886                         //fall through
887                 case MEDIA_CONTENT_TYPE_SOUND:
888                         pAudioContentInfo = std::unique_ptr<AudioContentInfo>(new (std::nothrow) AudioContentInfo);
889                         SysTryReturnResult(NID_CNT, pAudioContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pAudioContentInfo.");
890
891                         r = pAudioContentInfo->Construct(&changedPath);
892                         r = ConvertErrorToResult(r);
893                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to AudioContentInfo.");
894
895                         pAudioContentInfoImpl = null;
896                         pAudioContentInfoImpl = _AudioContentInfoImpl::GetInstance(*(pAudioContentInfo.get()));
897                         SysTryReturnResult(NID_CNT, pAudioContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pAudioContentInfoImpl.");
898
899                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pAudioContentInfoImpl);
900                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
901
902                         r = _ContentUtility::FillAudioContentData(pMediaHandle.get(), pAudioContentInfoImpl);
903                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillAudioContentData operation.");
904
905                         _ContentInfoHelper::SetContentInfoImpl(pAudioContentInfo.get(), pAudioContentInfoImpl);
906
907                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
908                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
909
910                         pContentSearchResult->SetContentType(CONTENT_TYPE_AUDIO);
911                         pContentSearchResult->SetContentInfo(pAudioContentInfo.release());
912
913                         // Shallow copy, adds just the pointer: not the element
914                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
915                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
916
917                         break;
918                 case MEDIA_CONTENT_TYPE_VIDEO:
919                         pVideoContentInfo = std::unique_ptr<VideoContentInfo>(new (std::nothrow) VideoContentInfo);
920                         SysTryReturnResult(NID_CNT, pVideoContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pVideoContentInfo.");
921
922                         r = pVideoContentInfo->Construct(&changedPath);
923                         r = ConvertErrorToResult(r);
924                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Construct operation to VideoContentInfo.");
925
926                         pVideoContentInfoImpl = null;
927                         pVideoContentInfoImpl = _VideoContentInfoImpl::GetInstance(*(pVideoContentInfo.get()));
928                         SysTryReturnResult(NID_CNT, pVideoContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to assign operation to pVideoContentInfoImpl.");
929
930                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pVideoContentInfoImpl);
931                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
932
933                         r = _ContentUtility::FillVideoContentData(pMediaHandle.get(), pVideoContentInfoImpl);
934                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillVideoContentData operation.");
935
936                         _ContentInfoHelper::SetContentInfoImpl(pVideoContentInfo.get(), pVideoContentInfoImpl);
937
938                         pContentSearchResult = std::unique_ptr<ContentSearchResult>(new (std::nothrow) ContentSearchResult);
939                         SysTryReturnResult(NID_CNT, pContentSearchResult.get() != null, E_OUT_OF_MEMORY, "Failed to create pContentSearchResult.");
940
941                         pContentSearchResult->SetContentType(CONTENT_TYPE_VIDEO);
942                         pContentSearchResult->SetContentInfo(pVideoContentInfo.release());
943
944                         // Shallow copy, adds just the pointer: not the element
945                         r = __pFinalOutList->Add(*(pContentSearchResult.release()));
946                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation for ArrayList.");
947
948                         break;
949                 default:
950                         break;
951                 }
952         }
953         return r;
954 }
955
956 // returns  given column value list in the requested order
957 Collection::IList*
958 _ContentSearchImpl::GetValueListN(const String& sortColumn, SortOrder sortOrder)
959 {
960         SysLog(NID_CNT, "inputColumn = %ls", sortColumn.GetPointer());
961         ClearLastResult();
962
963         result r        = E_SUCCESS;
964         int colIndex    = 0;
965         int maxCols     = 0;
966
967         __inputColumnName.Clear();
968         __inputExpr.Clear();
969
970         __inputColumnName = sortColumn;
971         __inputSortOrder  = sortOrder;
972
973         String          ospColumnName(L"");
974         String          slpColumnName(L"");
975
976         String          columnName(__inputColumnName);
977
978         if ((__inputColumnName.IsEmpty()) && ((__inputSortOrder == SORT_ORDER_ASCENDING) || (__inputSortOrder == SORT_ORDER_DESCENDING)))
979         {
980                 SysLog(NID_CNT, "sort column name is empty and sort oder is not none. so,E_INVALID_ARG occured.");
981                 SetLastResult(E_INVALID_ARG);
982                 return null;
983         }
984         else
985         {
986                 std::unique_ptr<ArrayList, AllElementsDeleter> pFinalOutList(new (std::nothrow) ArrayList());
987                 SysTryReturn(NID_CNT, pFinalOutList.get() != null, NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
988
989                 r = pFinalOutList->Construct();
990                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
991
992                 __pFinalOutList = std::move(pFinalOutList);
993
994                 switch (__contentType)
995                 {
996                 case CONTENT_TYPE_OTHER:
997                         //fall through
998                 case CONTENT_TYPE_IMAGE:
999                         maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
1000                         break;
1001                 case CONTENT_TYPE_VIDEO:
1002                         maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
1003                         break;
1004                 case CONTENT_TYPE_AUDIO:
1005                         //fall through
1006                 case CONTENT_TYPE_ALL:
1007                         maxCols = MAX_QUERY_COLUMNS;
1008                         break;
1009                 default:
1010                         break;
1011                 }
1012
1013                 for (colIndex = 0; colIndex < maxCols; colIndex++)
1014                 {
1015                         ospColumnName.Clear();
1016                         slpColumnName.Clear();
1017
1018                         ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
1019                         slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
1020
1021                         ospColumnName.ToUpper();
1022                         columnName.ToUpper();
1023                         if (columnName == ospColumnName)
1024                         {
1025                                 r = FillColumnsList(colIndex);
1026                                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] FillColumnsList Failed.", GetErrorMessage(r));
1027                                 break;
1028                         }
1029                 }
1030                 if (colIndex == maxCols)
1031                 {
1032                         SysLogException(NID_CNT, E_INVALID_ARG, "[%s] The column is invalid.", GetErrorMessage(E_INVALID_ARG));
1033
1034                         return null;
1035                 }
1036         }
1037         SetLastResult(r);
1038         return __pFinalOutList.release();
1039 }
1040
1041 // returns  given column value list in the requested order
1042 Collection::IList*
1043 _ContentSearchImpl::GetValueListN(int pageNo, int countPerPage, int& totalPageCount, int& totalCount, const String& sortColumn, SortOrder sortOrder) const
1044 {
1045         SysLog(NID_CNT, "pageNo = %d, countPerPage = %d, inputColumn = %ls", pageNo, countPerPage, sortColumn.GetPointer());
1046
1047         ClearLastResult();
1048
1049         result r        = E_SUCCESS;
1050         int colIndex    = 0;
1051         int maxCols     = 0;
1052
1053         __inputColumnName.Clear();
1054         __inputExpr.Clear();
1055
1056         __inputColumnName = sortColumn;
1057         __inputSortOrder  = sortOrder;
1058
1059         String          ospColumnName(L"");
1060         String          slpColumnName(L"");
1061
1062         String          columnName(__inputColumnName);
1063
1064         if ((__inputColumnName.IsEmpty()) && ((__inputSortOrder == SORT_ORDER_ASCENDING) || (__inputSortOrder == SORT_ORDER_DESCENDING)))
1065         {
1066                 SysLog(NID_CNT, "sort column name is empty and sort oder is not none. so,E_INVALID_ARG occured.");
1067                 SetLastResult(E_INVALID_ARG);
1068                 return null;
1069         }
1070         else
1071         {
1072                 std::unique_ptr<ArrayList, AllElementsDeleter> pFinalOutList(new (std::nothrow) ArrayList());
1073                 SysTryReturn(NID_CNT, pFinalOutList.get() != null, NULL, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to construct ArrayList.");
1074
1075                 r = pFinalOutList->Construct();
1076                 SysTryReturn(NID_CNT, !IsFailed(r), NULL, r, "[%s] Failed to construct ArrayList.", GetErrorMessage(r));
1077
1078                 __pFinalOutList = std::move(pFinalOutList);
1079
1080                 switch (__contentType)
1081                 {
1082                 case CONTENT_TYPE_OTHER:
1083                         //fall through
1084                 case CONTENT_TYPE_IMAGE:
1085                         maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
1086                         break;
1087                 case CONTENT_TYPE_VIDEO:
1088                         maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
1089                         break;
1090                 case CONTENT_TYPE_AUDIO:
1091                         //fall through
1092                 case CONTENT_TYPE_ALL:
1093                         maxCols = MAX_QUERY_COLUMNS;
1094                         break;
1095                 default:
1096                         break;
1097                 }
1098
1099                 for (colIndex = 0; colIndex < maxCols; colIndex++)
1100                 {
1101                         ospColumnName.Clear();
1102                         slpColumnName.Clear();
1103
1104                         ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
1105                         slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
1106
1107                         ospColumnName.ToUpper();
1108                         columnName.ToUpper();
1109                         if (columnName == ospColumnName)
1110                         {
1111                                 r = FillColumnsList(pageNo, countPerPage, totalPageCount, totalCount, colIndex);
1112                                 SysTryReturn(NID_CNT, r == E_SUCCESS, NULL, r, "[%s] FillColumnsList Failed.", GetErrorMessage(r));
1113                                 break;
1114                         }
1115                 }
1116                 if (colIndex == maxCols)
1117                 {
1118                         SysLogException(NID_CNT, E_INVALID_ARG, "[%s] The column is invalid.", GetErrorMessage(E_INVALID_ARG));
1119
1120                         return null;
1121                 }
1122         }
1123         SetLastResult(r);
1124         return __pFinalOutList.release();
1125 }
1126
1127 // prepares query expression to be used in group api. colIndex is mapped to actual group value.
1128 media_group_e
1129 _ContentSearchImpl::GetIndexAndCreateQueryExp(int colIndex) const
1130 {
1131         media_group_e groupIndex = MEDIA_CONTENT_GROUP_DISPLAY_NAME;;
1132         switch (colIndex)
1133         {
1134         case 0://"ContentType", "MEDIA_TYPE"
1135                 groupIndex = MEDIA_CONTENT_GROUP_TYPE;
1136                 __inputExpr = "MEDIA_TYPE IS NOT NULL";
1137                 break;
1138         case 1://"ContentFileName", "MEDIA_DISPLAY_NAME"
1139                 groupIndex = MEDIA_CONTENT_GROUP_DISPLAY_NAME;
1140                 __inputExpr = "MEDIA_DISPLAY_NAME IS NOT NULL";
1141                 break;
1142         case 2://"ContentName", "MEDIA_CONTENT_NAME"
1143                 groupIndex = MEDIA_CONTENT_GROUP_CONTENT_NAME;
1144                 __inputExpr = "MEDIA_CONTENT_NAME IS NOT NULL";
1145                 break;
1146         case 3://"Category", "MEDIA_CATEGORY"
1147                 groupIndex = MEDIA_CONTENT_GROUP_CATEGORY;
1148                 __inputExpr = "MEDIA_CATEGORY IS NOT NULL";
1149                 break;
1150         case 4://"Author", "MEDIA_AUTHOR"
1151                 groupIndex = MEDIA_CONTENT_GROUP_AUTHOR;
1152                 __inputExpr = "MEDIA_AUTHOR IS NOT NULL";
1153                 break;
1154         case 5://"keyword", "MEDIA_KEYWORD"
1155                 groupIndex = MEDIA_CONTENT_GROUP_KEYWORD;
1156                 __inputExpr = "MEDIA_KEYWORD IS NOT NULL";
1157                 break;
1158         case 6://"Provider", "MEDIA_PROVIDER"
1159                 groupIndex = MEDIA_CONTENT_GROUP_PROVIDER;
1160                 __inputExpr = "MEDIA_PROVIDER IS NOT NULL";
1161                 break;
1162         case 7://"Rating", "MEDIA_AGE_RATING"
1163                 groupIndex = MEDIA_CONTENT_GROUP_AGE_RATING;
1164                 __inputExpr = "MEDIA_AGE_RATING IS NOT NULL";
1165                 break;
1166         case 8://"LocationTag", "MEDIA_LOCATION_TAG"
1167                 groupIndex = MEDIA_CONTENT_GROUP_LOCATION_TAG;
1168                 __inputExpr = "MEDIA_LOCATION_TAG IS NOT NULL";
1169                 break;
1170         case 9://"ContentSize", "MEDIA_SIZE"
1171                 groupIndex = MEDIA_CONTENT_GROUP_SIZE;
1172                 __inputExpr = "MEDIA_SIZE IS NOT NULL";
1173                 break;
1174         case 10://"DateTime", "MEDIA_ADDED_TIME"
1175                 groupIndex = MEDIA_CONTENT_GROUP_ADDED_TIME;
1176                 __inputExpr = "MEDIA_ADDED_TIME IS NOT NULL";
1177                 break;
1178         case 11://"Latitude", "MEDIA_LATITUDE"
1179                 groupIndex = MEDIA_CONTENT_GROUP_LATITUDE;
1180                 __inputExpr = "MEDIA_LATITUDE IS NOT NULL";
1181                 break;
1182         case 12://"Longitude", "MEDIA_LONGITUDE"
1183                 groupIndex = MEDIA_CONTENT_GROUP_LONGITUDE;
1184                 __inputExpr = "MEDIA_LONGITUDE IS NOT NULL";
1185                 break;
1186         case 13://"Altitude", "MEDIA_ALTITUDE"
1187                 groupIndex = MEDIA_CONTENT_GROUP_ALTITUDE;
1188                 __inputExpr = "MEDIA_ALTITUDE IS NOT NULL";
1189                 break;
1190         case 14://"Title", "MEDIA_TITLE"
1191                 groupIndex = MEDIA_CONTENT_GROUP_TITLE;
1192                 __inputExpr = "MEDIA_TITLE IS NOT NULL";
1193                 break;
1194         case 15://"Artist", "MEDIA_ARTIST"
1195                 groupIndex = MEDIA_CONTENT_GROUP_ARTIST;
1196                 __inputExpr = "MEDIA_ARTIST IS NOT NULL";
1197                 break;
1198         case 16://"Genre", "MEDIA_GENRE"
1199                 groupIndex = MEDIA_CONTENT_GROUP_GENRE;
1200                 __inputExpr = "MEDIA_GENRE IS NOT NULL";
1201                 break;
1202         case 17://"Year", "MEDIA_YEAR"
1203                 groupIndex = MEDIA_CONTENT_GROUP_YEAR;
1204                 __inputExpr = "MEDIA_YEAR IS NOT NULL";
1205                 break;
1206         case 18://"Composer", "MEDIA_COMPOSER"
1207                 groupIndex = MEDIA_CONTENT_GROUP_COMPOSER;
1208                 __inputExpr = "MEDIA_COMPOSER IS NOT NULL";
1209                 break;
1210         case 19://"Album", "MEDIA_ALBUM"
1211                 __inputExpr = "MEDIA_ALBUM IS NOT NULL";
1212                 break;
1213         default:
1214                 break;
1215         }
1216         return groupIndex;
1217 }
1218
1219 // Fills  given column value list and destroys filter handle
1220 result
1221 _ContentSearchImpl::FillColumnsList(int colIndex) const
1222 {
1223         result r        = E_SUCCESS;
1224         int ret         = MEDIA_CONTENT_ERROR_NONE;
1225         int totalCount  = 0;
1226         media_group_e groupIndex = GetIndexAndCreateQueryExp(colIndex);
1227
1228         r = CreateQueryFilter(true);
1229         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "CreateQueryFilter Failed.");
1230
1231         if (colIndex == ALBUM_COLUMN_NUM)
1232         {
1233                 ret = media_album_get_album_count_from_db(__pFilterHandle.get(), &totalCount);
1234         }
1235         else
1236         {
1237                 ret = media_group_get_group_count_from_db(__pFilterHandle.get(), groupIndex, &totalCount);
1238         }
1239         r = MapCoreErrorToNativeResult(ret);
1240         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_album/group count_from_db operation.");
1241
1242         SysLog(NID_CNT, "totalCount = %d for media_album/group count_from_db", totalCount);
1243
1244         if (totalCount > 0)
1245         {
1246                 if (colIndex == ALBUM_COLUMN_NUM)
1247                 {
1248                         r = ExecuteAndFillAlbumValues();
1249                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillAlbumValues Failed.");
1250                 }
1251                 else
1252                 {
1253                         r = ExecuteAndFillGetValueListN(groupIndex, colIndex);
1254                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillGetValueListN Failed.");
1255                 }
1256         }
1257         return r;
1258 }
1259
1260 // prepares input expression to be sent for create filter and fills  given column value list
1261 result
1262 _ContentSearchImpl::FillColumnsList(int pageNo, int countPerPage, int& totalPageCount, int& totalCount, int colIndex) const
1263 {
1264         result r        = E_SUCCESS;
1265         int ret         = MEDIA_CONTENT_ERROR_NONE;
1266         int offset      = 0;
1267         totalPageCount = 0;
1268         totalCount = 0;
1269         media_group_e groupIndex = GetIndexAndCreateQueryExp(colIndex);
1270
1271         r = CreateQueryFilter(true);
1272         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "CreateQueryFilter Failed.");
1273
1274         if (colIndex == ALBUM_COLUMN_NUM)
1275         {
1276                 ret = media_album_get_album_count_from_db(__pFilterHandle.get(), &totalCount);
1277         }
1278         else
1279         {
1280                 ret = media_group_get_group_count_from_db(__pFilterHandle.get(), groupIndex, &totalCount);
1281         }
1282         r = MapCoreErrorToNativeResult(ret);
1283         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_album/group count_from_db operation.");
1284
1285         SysLog(NID_CNT, "totalCount = %d for media_album/group count_from_db", totalCount);
1286
1287         if (totalCount > 0)
1288         {
1289                 if ((totalCount % countPerPage) == 0)
1290                 {
1291                         totalPageCount = totalCount / countPerPage;
1292                 }
1293                 else
1294                 {
1295                         totalPageCount = (totalCount / countPerPage) + 1;
1296                 }
1297
1298                 SysTryReturnResult(NID_CNT, ((pageNo >= 1) && (pageNo <= totalPageCount)) , r, "(pageNo < 1) || (pageNo > totalPageCount).");
1299
1300                 offset = (pageNo * countPerPage) - countPerPage;
1301
1302                 SysLog(NID_CNT, "GetValueListN totalCount [%d] totalPageCount[%d] __countPerPage[%d] __pageNo[%d] offset[%d]",
1303                                 totalCount, totalPageCount, countPerPage, pageNo, offset);
1304
1305                 ret = media_filter_set_offset(__pFilterHandle.get(),offset,countPerPage);
1306                 r = MapCoreErrorToNativeResult(ret);
1307                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "failed to perform media_filter_set_offset operation.");
1308
1309                 if (colIndex == ALBUM_COLUMN_NUM)
1310                 {
1311                         r = ExecuteAndFillAlbumValues();
1312                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillAlbumValues Failed.");
1313                 }
1314                 else
1315                 {
1316                         r = ExecuteAndFillGetValueListN(groupIndex, colIndex);
1317                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "ExecuteAndFillGetValueListN Failed.");
1318                 }
1319
1320         }
1321         else if (pageNo > 1)
1322         {
1323                 SysLogException(NID_CNT, r = E_INVALID_ARG, "[%s] (pageNo > 1) and  (totalcount = 0).", GetErrorMessage(E_INVALID_ARG));
1324         }
1325
1326         return r;
1327 }
1328
1329 //  fills  given column value list for GetValuelistN api
1330 result
1331 _ContentSearchImpl::ExecuteAndFillGetValueListN(media_group_e groupIndex, int colIndex) const
1332 {
1333         SysLog(NID_CNT, "Enter\n");
1334
1335         int ret                     = MEDIA_CONTENT_ERROR_NONE;
1336         result r                    = E_SUCCESS;
1337         result res                  = E_SUCCESS;
1338         std::unique_ptr<GList, SearchGListDeleter> pItemList;
1339         GList* pTemp = NULL;
1340
1341         std::unique_ptr<Object> pValue;
1342
1343         DateTime dateTime;
1344
1345         long long contentSize = 0;
1346         long long addedTime     = 0;
1347
1348         double dVal     = 0;
1349         char *pColumnVal = null;
1350         int contentType = 0;
1351
1352         pTemp = pItemList.get();
1353
1354         ret = media_group_foreach_group_from_db(__pFilterHandle.get(), groupIndex, GroupItemCb, &pTemp);
1355         r = MapCoreErrorToNativeResult(ret);
1356         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_group_foreach_group_from_db operation.");
1357
1358         SysTryReturnResult(NID_CNT, pTemp != NULL, r, "media_info_foreach_media_from_db pTemp is null.");
1359
1360         for (int idx = 0; idx < (int)g_list_length(pTemp); idx++)
1361         {
1362                 SysLog(NID_CNT, "idx = %d and (int)g_list_length(pItemList) = %d", idx, (int)g_list_length(pTemp));
1363
1364                 pColumnVal = (char *)g_list_nth_data(pTemp, idx);
1365
1366                 String strColVal(pColumnVal);
1367
1368                 _ContentUtility::DoSafeFree(pColumnVal);
1369
1370                 SysLog(NID_CNT, "pColumnVal = %ls", strColVal.GetPointer());
1371
1372                 switch (colIndex)
1373                 {
1374                 case 0://"ContentType", "MEDIA_TYPE"
1375                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1376                         {
1377                                 res = Integer::Parse(strColVal, contentType);
1378                                 SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] Integer parse failed.", GetErrorMessage(res));
1379                         }
1380                         switch ((media_content_type_e)contentType)
1381                         {
1382                         case MEDIA_CONTENT_TYPE_OTHERS:
1383                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_OTHER));
1384                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_OTHER");
1385                                 break;
1386                         case MEDIA_CONTENT_TYPE_IMAGE:
1387                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_IMAGE));
1388                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_IMAGE");
1389                                 break;
1390                         case MEDIA_CONTENT_TYPE_SOUND:
1391                                 //fall through
1392                         case MEDIA_CONTENT_TYPE_MUSIC:
1393                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_AUDIO));
1394                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_AUDIO");
1395                                 break;
1396                         case MEDIA_CONTENT_TYPE_VIDEO:
1397                                 pValue.reset(new (std::nothrow) String(CONTENT_TYPE_VIDEO));
1398                                 SysLog(NID_CNT, "mediaType = CONTENT_TYPE_VIDEO");
1399                                 break;
1400                         default:
1401                                 break;
1402                         }
1403                         break;
1404                 case 1://"ContentFileName", "MEDIA_DISPLAY_NAME"
1405                 //fall through
1406                 case 2://"ContentName", "MEDIA_CONTENT_NAME"
1407                 //fall through
1408                 case 3://"Category", "MEDIA_CATEGORY"
1409                 //fall through
1410                 case 4://"Author", "MEDIA_AUTHOR"
1411                 //fall through
1412                 case 5://"keyword", "MEDIA_KEYWORD"
1413                 //fall through
1414                 case 6://"Provider", "MEDIA_PROVIDER"
1415                 //fall through
1416                 case 7://"Rating", "MEDIA_AGE_RATING"
1417                 //fall through
1418                 case 8://"LocationTag", "MEDIA_LOCATION_TAG"
1419                 //fall through
1420                 case 14://"Title", "MEDIA_TITLE"
1421                 //fall through
1422                 case 15://"Artist", "MEDIA_ARTIST"
1423                 //fall through
1424                 case 16://"Genre", "MEDIA_GENRE"
1425                 //fall through
1426                 case 17://"Year", "MEDIA_YEAR"
1427                 //fall through
1428                 case 18://"Composer", "MEDIA_COMPOSER"
1429
1430                         pValue.reset(new (std::nothrow) String(strColVal));
1431                         break;
1432
1433                 case 9://"ContentSize", "MEDIA_SIZE" 
1434                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1435                         {
1436                                 res = LongLong::Parse(strColVal, contentSize);
1437                                 SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] LongLong parse failed.", GetErrorMessage(res));
1438                         }
1439
1440                         pValue.reset(new (std::nothrow) LongLong(contentSize));
1441                         break;
1442                 case 10://"DateTime", "MEDIA_ADDED_TIME" 
1443                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1444                         {
1445                                 res = LongLong::Parse(strColVal, addedTime);
1446                                 SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] LongLong parse failed.", GetErrorMessage(res));
1447                         }
1448
1449                         res = dateTime.SetValue(1970, 1, 1);
1450                         SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] dateTime.SetValue failed.", GetErrorMessage(res));
1451
1452                         res = dateTime.AddSeconds(addedTime);
1453                         SysTryLog(NID_CNT, res == E_SUCCESS, "[%s] dateTime.AddSeconds failed.", GetErrorMessage(res));
1454
1455                         SysLog(NID_CNT, "DateTime : %ls", dateTime.ToString().GetPointer());
1456
1457                         pValue.reset(new (std::nothrow) DateTime(dateTime));
1458
1459                         break;
1460                 case 11://"Latitude", "MEDIA_LATITUDE"
1461                         //fall through
1462                 case 12://"Longitude", "MEDIA_LONGITUDE"
1463                         //fall through
1464                 case 13://"Altitude", "MEDIA_ALTITUDE"
1465                         if ((strColVal.CompareTo(L"Unknown") != 0) && (!strColVal.IsEmpty()))
1466                         {
1467                                 dVal = _LocalizedNumParser::ToDouble(strColVal, "C");
1468                                 r = GetLastResult();
1469                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform ToDouble operation.");
1470                         }
1471                         pValue.reset(new (std::nothrow) Double(dVal));
1472                         break;
1473
1474                 case 19://"Album", "MEDIA_ALBUM"
1475                         break;
1476                 default:
1477                         break;
1478                 }
1479                 if (pValue.get() != NULL)
1480                 {
1481                         r = __pFinalOutList->Add(*(pValue.release()));
1482                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform arraylist Add operation.");
1483                 }
1484         }
1485
1486         return r;
1487 }
1488
1489 result
1490 _ContentSearchImpl::ExecuteAndFillAlbumValues(void) const
1491 {
1492         int ret = MEDIA_CONTENT_ERROR_NONE;
1493         result r = E_SUCCESS;
1494         std::unique_ptr<GList, SearchGListDeleter> pItemList;
1495         GList* pTemp = NULL;
1496         std::unique_ptr<media_album_s, AlbumHandleDeleter> pAlbumHandle;
1497         std::unique_ptr<media_album_s, AlbumHandleDeleter> pTempAlbumHandle;
1498
1499         char* pName = NULL;
1500         std::unique_ptr<char, CharDeleter> pAlbumName;
1501         std::unique_ptr< String > pValue;
1502         int lastIndex = 0;
1503
1504         pTemp = pItemList.get();
1505
1506         ret = media_album_foreach_album_from_db(__pFilterHandle.get(), AlbumItemCb, &pTemp);
1507         r = MapCoreErrorToNativeResult(ret);
1508         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_album_foreach_album_from_db operation.");
1509         SysTryReturnResult(NID_CNT, pTemp != NULL, r, "media_info_foreach_media_from_db pTemp is null.");
1510
1511         for (int idx = 0; idx < (int)g_list_length(pTemp); idx++)
1512         {
1513                 pAlbumHandle.reset(static_cast<media_album_h>(g_list_nth_data(pTemp, idx)));
1514                 ret = media_album_get_name(pAlbumHandle.get(), &pName);
1515                 r = MapCoreErrorToNativeResult(ret);
1516                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_album_get_name.");
1517                 
1518                 if (pName != NULL)
1519                 {
1520                         SysLog(NID_CNT, "pColumnVal = %s", pName);
1521
1522                         pAlbumName.reset(pName);
1523                         pValue.reset(new (std::nothrow) String(pAlbumName.get()));
1524                         SysTryReturnResult(NID_CNT, pValue != NULL, E_OUT_OF_MEMORY, "media_info_foreach_media_from_db pTemp is null.");
1525
1526                         if (idx == 0)
1527                         {
1528                                 r = __pFinalOutList->Add(pValue.get());
1529                                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform arraylist Add operation.");
1530                                 pValue.release();
1531                         }
1532                         else
1533                         {
1534                                 String* pTempNameList(static_cast< String* >(__pFinalOutList->GetAt(lastIndex)));
1535
1536                                 if (pValue->CompareTo(*pTempNameList) != 0)
1537                                 {
1538                                         r = __pFinalOutList->Add(pValue.get());
1539                                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform arraylist Add operation.");
1540
1541                                         pValue.release();
1542                                         lastIndex++;
1543                                 }
1544                         }
1545                 }
1546         }
1547
1548         return r;
1549 }
1550
1551 result
1552 _ContentSearchImpl::MapCoreErrorToNativeResult(int reason) const
1553 {
1554         result r = E_SUCCESS;
1555
1556         switch (reason)
1557         {
1558         case MEDIA_CONTENT_ERROR_NONE:
1559                 r = E_SUCCESS;
1560                 break;
1561
1562         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
1563                 r = E_INVALID_ARG;
1564                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
1565                 break;
1566
1567         case MEDIA_CONTENT_ERROR_DB_FAILED:
1568                 r = E_SYSTEM;
1569                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
1570                 break;
1571
1572         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
1573                 r = E_OUT_OF_MEMORY;
1574                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
1575                 break;
1576
1577         default:
1578                 SysLog(NID_CNT, "default");
1579                 r = E_SYSTEM;
1580                 break;
1581         }
1582         return r;
1583 }
1584
1585 result
1586 _ContentSearchImpl::ConvertErrorToResult(result res) const
1587 {
1588         result r = E_SUCCESS;
1589
1590         switch (res)
1591         {
1592         case E_FILE_NOT_FOUND:
1593                 // Fall through
1594         case E_IO:
1595                 r = E_SYSTEM;
1596                 break;
1597
1598         default:
1599                 r = res;
1600                 break;
1601         }
1602         return r;
1603 }
1604
1605 // Callback function registered to each media info details
1606 // all items are appended to the list
1607 bool
1608 MediaItemCb(media_info_h media, void* pUserdata)
1609 {
1610         int ret  = MEDIA_CONTENT_ERROR_NONE;
1611         media_info_h new_media = NULL;
1612         ret = media_info_clone(&new_media, media);
1613         SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Failed to perform media_info_clone");
1614
1615         GList** pList = (GList**)pUserdata;
1616         *pList = g_list_append(*pList, new_media);
1617
1618         return true;
1619 }
1620
1621 // Callback function registered for column values
1622 // all items are appended to the list
1623 bool
1624 GroupItemCb(const char* pGroupName, void* pUserdata)
1625 {
1626         char* pGrpName = strdup(pGroupName);
1627         GList** pList = (GList**)pUserdata;
1628         *pList = g_list_append(*pList, pGrpName);
1629
1630         return true;
1631 }
1632
1633 // Callback function registered to each media info details
1634 // all items are appended to the list
1635 bool
1636 AlbumItemCb(media_album_h album, void* pUserdata)
1637 {
1638         int ret  = MEDIA_CONTENT_ERROR_NONE;
1639         media_album_h new_album = NULL;
1640         ret = media_album_clone(&new_album, album);
1641         SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Failed to perform media_album_clone");
1642
1643         GList** pList = (GList**)pUserdata;
1644         *pList = g_list_append(*pList, new_album);
1645
1646         return true;
1647 }
1648
1649 }}