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