Update change log and spec for wrt-plugins-tizen_0.4.38
[framework/web/wrt-plugins-tizen.git] / src / Content / ContentManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <vector>
19 #include <string>
20 #include <algorithm>
21 #include <media_content.h>
22 #include <Commons/Regex.h>
23 #include <Commons/StringUtils.h>
24 #include <CommonsJavaScript/Converter.h>
25
26 #include "ContentManager.h"
27 #include "ContentFilterValidator.h"
28 #include "ContentUtility.h"
29 #include <Logger.h>
30
31 using namespace WrtDeviceApis::Commons;
32
33
34 namespace DeviceAPI {
35 namespace Content {
36
37
38 int MediacontentManager::m_instanceCount = 0;
39
40 MediacontentManager::MediacontentManager()
41 {
42         //protect for pararel execution
43     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
44     if (m_instanceCount == 0)
45     {
46         //when it is first instance then connect to media content service.
47         if( MEDIA_CONTENT_ERROR_NONE != media_content_connect())
48         {
49             LoggerD("Media Content Service Connect Fail...");
50             return;
51         }
52     }
53     //increase counter of instances
54     m_instanceCount++;
55 }
56
57 MediacontentManager::~MediacontentManager()
58 {
59     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
60     //decrease counter of instances
61     m_instanceCount--;
62     if (m_instanceCount == 0)
63     {
64         //when it is last instance then clse connection to database
65         if ( MEDIA_CONTENT_ERROR_NONE != media_content_disconnect())
66         {
67             LoggerD("Media Content Service Disconnect Fail...");
68         }
69     }
70 }
71
72 tm* MediacontentManager::toDateTm(time_t date)
73 {
74         tm *tm_date = (struct tm *)calloc(1, sizeof(struct tm));
75
76         localtime_r(&date, tm_date);
77
78         return tm_date;
79 }
80
81 media_content_orientation_e MediacontentManager::convertToOrientation(string orientation)
82 {
83         media_content_orientation_e ret = MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE;
84
85         if ( orientation.compare("NORMAL")==0)
86         {
87                 ret = MEDIA_CONTENT_ORIENTATION_NORMAL;
88         }
89         else if (orientation.compare("FLIP_HORIZONTAL")==0)
90         {
91                 ret = MEDIA_CONTENT_ORIENTATION_HFLIP;
92         }
93         else if (orientation.compare("ROTATE_180")==0)
94         {
95                 ret = MEDIA_CONTENT_ORIENTATION_ROT_180;
96         }
97         else if (orientation.compare("FLIP_VERTICAL")==0)
98         {
99                 ret = MEDIA_CONTENT_ORIENTATION_VFLIP;
100         }
101         else if (orientation.compare("TRANSPOSE")==0)
102         {
103                 ret = MEDIA_CONTENT_ORIENTATION_TRANSPOSE;
104         }
105         else if (orientation.compare("ROTATE_90")==0)
106         {
107                 ret = MEDIA_CONTENT_ORIENTATION_ROT_90;
108         }
109         else if (orientation.compare("TRANSVERSE")==0)
110         {
111                 ret =MEDIA_CONTENT_ORIENTATION_TRANSVERSE;
112         }
113         else if (orientation.compare("ROTATE_270")==0)
114         {
115                 ret =MEDIA_CONTENT_ORIENTATION_ROT_270;
116         }       
117         else
118         {
119                 LoggerD("wrong value.");
120                 ret = MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE;
121         }
122
123         return ret;
124 }
125
126 void MediacontentManager::convertToPlatformFolder(media_folder_h media_folder, MediacontentFolderPtr& newFolder)
127 {
128         char* tmpStr = NULL;
129         time_t date;
130         media_content_storage_e storageType;
131
132         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_folder_id(media_folder, &tmpStr))
133         {
134                 if ( tmpStr )
135                 {
136                         newFolder->setFolderUUID(tmpStr);
137                         free(tmpStr);
138                         tmpStr = NULL;
139                 }
140         }
141
142         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_path(media_folder, &tmpStr))
143         {
144                 if ( tmpStr )
145                 {               
146                         newFolder->setFolderPath(ContentUtility::convertPathToUri(tmpStr));
147                         free(tmpStr);
148                         tmpStr = NULL;
149                 }
150         }
151
152         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_name(media_folder, &tmpStr))
153         {
154                 if ( tmpStr )
155                 {               
156                         newFolder->setFolderName(tmpStr);
157                         free(tmpStr);
158                         tmpStr = NULL;  
159                 }
160         }
161         
162         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_modified_time(media_folder, &date))
163         {
164                 newFolder->setFolderModifiedDate(toDateTm(date));
165         }
166         
167         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_storage_type(media_folder, &storageType))
168         {       
169                 string type;
170                 if(storageType == MEDIA_CONTENT_STORAGE_INTERNAL )
171                 {
172                         type = "INTERNAL";
173                 }
174                 else if( storageType == MEDIA_CONTENT_STORAGE_EXTERNAL)
175                 {
176                         type = "EXTERNAL";
177                 }
178                 else
179                 {
180                         type = "UNKNOWN";
181                 }
182                 newFolder->setFolderStorageType(type);
183         }
184                 
185                 
186 }
187
188 void MediacontentManager::readCommonDataFromMediaInfo(media_info_h info, MediacontentMedia* newMedia)
189 {       
190
191         char* tmpStr = NULL;
192         time_t tmpDate;
193         int tmpInt = 0;
194         unsigned long long tmpLongLong = 0;
195         bool tmpBool= false;
196
197         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_id(info, &tmpStr) )
198         {
199                 if (tmpStr)
200                 {
201                         newMedia->setMediaUUID(tmpStr);
202                         free(tmpStr);
203                         tmpStr = NULL;
204                 }
205         }
206
207         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_mime_type(info, &tmpStr) )
208         {
209                 if (tmpStr)
210                 {
211                         newMedia->setMimeType(tmpStr);
212                         free(tmpStr);
213                         tmpStr = NULL;
214                 }
215         }
216
217         if (MEDIA_CONTENT_ERROR_NONE == media_info_get_display_name(info, &tmpStr) )
218         {
219                 if (tmpStr)
220                 {
221                         newMedia->setDisplayName(tmpStr);
222                         free(tmpStr);
223                         tmpStr = NULL;
224                 }
225         }
226
227         if (MEDIA_CONTENT_ERROR_NONE == media_info_get_title(info, &tmpStr) )
228         {
229                 if (tmpStr)
230                 {
231                     newMedia->setTitle(tmpStr);
232                     free(tmpStr);
233                     tmpStr = NULL;
234                 }
235         }
236
237         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_file_path(info, &tmpStr) )
238         {
239                 if (tmpStr)
240                 {
241                         newMedia->setFilePath(ContentUtility::convertPathToUri(tmpStr));
242                         free(tmpStr);
243                         tmpStr = NULL;
244                 }
245         }
246
247         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_thumbnail_path (info, &tmpStr) )
248         {
249                 if (tmpStr)
250                 {
251                         newMedia->setThumbnailPath(ContentUtility::convertPathToUri(tmpStr));
252                         free(tmpStr);
253                         tmpStr = NULL;
254                 }
255         }
256         
257         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_description(info, &tmpStr) )
258         {
259                 if (tmpStr)
260                 {
261                         newMedia->setDescription(tmpStr);
262                         free(tmpStr);
263                         tmpStr = NULL;
264                 }
265         }
266
267         //newImage->setReleasedDate(toDateTm(sqlite3_column_int(pStmt,7)));
268         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_modified_time (info, &tmpDate) )
269         {
270             if(tmpDate)
271             {
272                 newMedia->setModifiedDate(toDateTm(tmpDate));
273             }
274         }
275
276         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_favorite (info, &tmpBool) )
277         {
278                 newMedia->setFavorite(tmpBool);
279         }
280
281         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_rating  (info, &tmpInt) )
282         {
283                 newMedia->setRating(tmpInt);
284         }
285
286         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_size(info, &tmpLongLong) )
287         {
288                 newMedia->setSize(tmpLongLong);
289         }
290         
291 }
292
293 void MediacontentManager::readImageFromMediaInfo( media_info_h info, MediacontentImage* newImage)
294 {       
295         char* tmpStr = NULL;
296         double tmpDouble;
297         int tmpInt = 0;
298         
299         if ( info) 
300         {
301                 newImage->setMediaType("IMAGE");
302
303                 readCommonDataFromMediaInfo(info, newImage);
304
305                 image_meta_h img;
306                 
307                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_image(info, &img))
308                 {
309                         //created time
310                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_date_taken (img, &tmpStr) )
311                         {       
312                                 if ( tmpStr )
313                                 {
314                                         struct tm *result = (struct tm *)calloc(1, sizeof(struct tm));
315                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
316                                                 LoggerE( "Couldn't convert supplied date.");
317                                         }
318                                 
319                                         newImage->setReleasedDate(result);
320                                         free(tmpStr);
321                                         tmpStr = NULL;
322                                 }
323                         }
324                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_width(img, &tmpInt) )
325                         {
326                                 newImage->setImageWidth(tmpInt);
327                         }
328
329                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_height(img, &tmpInt) )
330                         {
331                                 newImage->setImageHeight(tmpInt);
332                         }
333
334                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble) )
335                         {
336                                 newImage->setImageLatitude(tmpDouble);
337                         }
338                         
339                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble) )
340                         {
341                                 newImage->setImageLongitude(tmpDouble);
342                         }
343                                         
344                         media_content_orientation_e orientation;
345                         
346                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_orientation(img, &orientation) )
347                         {       
348         
349                                 string orientationStr;
350                                 switch(orientation)
351                                 {
352                                         case 0:
353                                         case 1: 
354                                                 orientationStr = "NORMAL";
355                                                 break;
356                                         case 2: 
357                                                 orientationStr = "FLIP_HORIZONTAL";
358                                                 break;
359                                         case 3: 
360                                                 orientationStr = "ROTATE_180";
361                                                 break;
362                                         case 4: 
363                                                 orientationStr = "FLIP_VERTICAL";
364                                                 break;
365                                         case 5: 
366                                                 orientationStr = "TRANSPOSE";
367                                                 break;
368                                         case 6: 
369                                                 orientationStr = "ROTATE_90";
370                                                 break;
371                                         case 7: 
372                                                 orientationStr = "TRANSVERSE";
373                                                 break;
374                                         case 8: 
375                                                 orientationStr = "ROTATE_270";
376                                                 break;
377                                 }       
378                                 
379                                 newImage->setImageOrientation(orientationStr);
380
381                         }
382
383
384                         if ( METADATA_EXTRACTOR_ERROR_NONE != image_meta_destroy(img))
385                         {
386                                 LoggerD(" image_meta_destroy is fail... ");
387                         }
388                                                 
389                 }
390                 else {
391                         LoggerD("fetch Image Info failed..");                   
392                 }
393                         
394         }
395         else
396         {
397                 LoggerD("media info is NULL");
398         }
399 }
400
401 void MediacontentManager::readVideoFromMediaInfo( media_info_h info, MediacontentVideo* newVideo)
402 {       
403         char* tmpStr;
404         int tmpInt = 0;
405         double tmpDouble;
406         
407         if ( info) 
408         {
409                 newVideo->setMediaType("VIDEO");        
410
411                 readCommonDataFromMediaInfo(info, newVideo);    //set common media infomation
412
413                 video_meta_h video;
414                 
415                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_video(info, &video))
416                 {
417
418                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_recorded_date(video, &tmpStr) )
419                         {
420                                 if ( tmpStr )
421                                 {       
422                                         struct tm *result=(struct tm *)calloc(1, sizeof(struct tm));
423                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
424                                                 LoggerE( "Couldn't convert supplied date.");
425                                         }
426                                         newVideo->setReleasedDate(result);
427                                         
428                                         free(tmpStr);
429                                         tmpStr = NULL;
430                                 }
431                         }
432                 
433                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_album(video, &tmpStr) )
434                         {
435                                 if( tmpStr )
436                                 {
437                                         newVideo->setVideoAlbum(tmpStr);
438                                         free(tmpStr);
439                                         tmpStr = NULL;
440                                 }
441                         }
442
443                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_artist(video, &tmpStr) )
444                         {       
445                                 if ( tmpStr)
446                                 {
447                                         std::vector<std::string> artists = String::split(tmpStr, TAG_DELIMETER);
448                                         for( unsigned int i=0; i < artists.size(); i++)
449                                         {
450                                                 string artist = artists.at(i);
451                                                 newVideo->appendVideoArtist(artist);
452                                         }
453                                         
454                                         free(tmpStr);
455                                         tmpStr = NULL;
456                                 }
457                         }
458
459                         
460                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble) )
461                         {
462                                 newVideo->setVideoLongitude(tmpDouble);
463                         }
464
465                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble) )
466                         {
467                                 newVideo->setVideoLatitude(tmpDouble);
468                         }
469
470                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_width(video, &tmpInt) )
471                         {
472                                 newVideo->setVideoWidth(tmpInt);
473                         }
474                         
475                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_height(video, &tmpInt) )
476                         {
477                                 newVideo->setVideoHeight(tmpInt);
478                         }
479
480                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_duration(video, &tmpInt) )
481                         {
482                                 newVideo->setVideoDuration(tmpInt);
483                         }
484
485                         if ( METADATA_EXTRACTOR_ERROR_NONE != video_meta_destroy(video))
486                         {
487                                 LoggerD(" video_meta_destroy is fail... ");
488                         }
489                                                 
490                 }
491                 else
492                 {
493                         LoggerD("fetch Video info fail...");
494                 }
495         }
496         else
497         {
498                 LoggerD("media info is NULL");
499         }
500 }
501
502
503 void MediacontentManager::readMusicFromMediaInfo( media_info_h info, MediacontentAudio* newAudio)
504 {       
505         char* tmpStr;
506         int tmpInt = 0;
507         unsigned long long tmpLongLong = 0;
508                 
509         if ( info) 
510         {
511                 newAudio->setMediaType("AUDIO");        
512                 
513                 readCommonDataFromMediaInfo(info, newAudio);    //set common media infomation
514
515                 audio_meta_h audio;
516                 
517                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(info, &audio))
518                 {       
519                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_recorded_date(audio, &tmpStr) )
520                         {
521                                 if ( tmpStr )
522                                 {       
523                                         struct tm *result = (struct tm *)calloc(1, sizeof(struct tm));
524                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
525                                                 LoggerE( "Couldn't convert supplied date.");
526                                         }
527                                         newAudio->setReleasedDate(result);
528                                         
529                                         free(tmpStr);
530                                         tmpStr = NULL;
531                                 }
532                         }
533
534                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_album(audio, &tmpStr) )
535                         {
536                                 if (tmpStr)
537                                 {
538                                         newAudio->setAudioAlbum(tmpStr);
539                                         free(tmpStr);
540                                         tmpStr = NULL;
541                                 }
542                         }
543
544                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_artist(audio, &tmpStr) )
545                         {       
546                                 if (tmpStr)
547                                 {
548                                         std::vector<std::string> artists = String::split(tmpStr, TAG_DELIMETER);
549                                         for( unsigned int i=0; i < artists.size(); i++)
550                                         {
551                                                 string artist = artists.at(i);
552                                                 newAudio->appendAudioArtist(artist);
553                                         }
554                                         
555                                         free(tmpStr);
556                                         tmpStr = NULL;
557                                 }
558                         }
559                 
560                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_composer(audio, &tmpStr) )
561                         {
562                                 if (tmpStr)
563                                 {       
564                                         std::vector<std::string> composers = String::split(tmpStr, TAG_DELIMETER);
565                                         for( unsigned int i=0; i < composers.size(); i++)
566                                         {
567                                                 string composer = composers.at(i);
568                                                 newAudio->appendAudioComposer(composer);
569                                         }
570                                         
571                                         free(tmpStr);
572                                         tmpStr = NULL;
573                                 }
574                         }
575                         
576                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_duration(audio, &tmpInt) )
577                         {
578                                 newAudio->setAudioDuration(tmpInt);
579                         }
580
581                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_copyright(audio, &tmpStr) )
582                         {
583                                 if (tmpStr)
584                                 {
585                                         newAudio->setAudioCopyright(tmpStr);
586                                         free(tmpStr);
587                                         tmpStr = NULL;
588                                 }
589                         }
590                 
591                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_track_num(audio, &tmpStr) )
592                         {       
593                                 int trackNum = 0;
594                                 if (tmpStr)
595                                 {
596                                         try {
597                                                 istringstream(tmpStr) >> trackNum;
598                                         } catch (...) {
599                                                 LoggerD("Track Number type is wrong. (track number:" << tmpStr << ")");
600                                                 trackNum = 0;
601                                         }
602                                         newAudio->setAudioTrackNum(trackNum);
603                                         free(tmpStr);
604                                         tmpStr = NULL;
605                                 }
606                         }
607
608                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_size(info, &tmpLongLong) )
609                         {       
610                                 newAudio->setAudioSize(tmpLongLong);
611                         }
612
613                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_bit_rate(audio, &tmpInt) )
614                         {       
615                                 newAudio->setAudioBitrate(tmpInt);
616                         }
617
618                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_genre(audio, &tmpStr) )
619                         {       
620                                 if (tmpStr)
621                                 {
622                                         std::vector<std::string> genres = String::split(tmpStr, TAG_DELIMETER);
623
624                                         for( unsigned int i=0; i < genres.size(); i++)
625                                         {
626                                                 string genre = genres.at(i);
627                                                 newAudio->appendAudioGenre(genre);
628                                         }
629                                         
630                                         free(tmpStr);
631                                         tmpStr = NULL;
632                                 }
633                         }
634                                                 
635                         if ( METADATA_EXTRACTOR_ERROR_NONE != audio_meta_destroy(audio))
636                         {
637                                 LoggerD(" audio_meta_destroy is fail... ");
638                         }
639                         
640
641                 }
642                 else
643                 {
644                         LoggerD("fetch Music Info fail...");
645                 }
646                 
647                         
648         }
649         else
650         {
651                 LoggerD("media info is NULL");
652         }
653 }
654
655
656
657 //Callback.
658 bool MediacontentManager::mediaFolderCallback(media_folder_h folder, void *user_data)
659 {
660         if (user_data != NULL){
661                 IEventFindFolder* event = (IEventFindFolder*)user_data;
662                 if ( folder )
663                 {
664                         MediacontentFolderPtr newFolder(new MediacontentFolder());
665                         convertToPlatformFolder(folder, newFolder);
666                         
667                         event->addFolder(newFolder);
668                 }
669         }
670         else
671         {
672                 LoggerD("user data is NULL");
673         }
674         return true;
675
676 }
677
678 bool MediacontentManager::mediaItemCallback(media_info_h info, void* user_data)
679 {
680         if (user_data != NULL){
681                 IEventBrowseFolder* event = (IEventBrowseFolder*)user_data;
682
683                 media_content_type_e type;
684                 
685                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
686                 {
687                         if ( type == MEDIA_CONTENT_TYPE_IMAGE)
688                         {
689                                 MediacontentImage *newImage(new MediacontentImage());
690                                 readImageFromMediaInfo(info, newImage);
691                                 event->addMedia(newImage);
692                         }
693                         else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
694                         {
695                                 MediacontentVideo *newVideo(new MediacontentVideo());
696                                 readVideoFromMediaInfo(info, newVideo);
697                                 event->addMedia(newVideo);
698                         }
699                         else if ( type == MEDIA_CONTENT_TYPE_MUSIC)
700                         {
701                                 MediacontentAudio *newVAudio(new MediacontentAudio());
702                                 readMusicFromMediaInfo(info, newVAudio);
703                                 event->addMedia(newVAudio);
704                         }
705                         else if( type == MEDIA_CONTENT_TYPE_OTHERS)
706                         {
707                                 MediacontentMedia *newMedia(new MediacontentMedia());
708
709                                 newMedia->setMediaType("OTHER");
710                                 readCommonDataFromMediaInfo(info, newMedia);
711                                 event->addMedia(newMedia);
712                         }
713
714                 }
715         }
716         else
717         {
718                 LoggerD("event is NULL");
719         }
720
721         return true;
722 }
723
724 void MediacontentManager::OnRequestReceived(const IEventFindFolderPtr &eFolder)
725 {
726     filter_h filter = NULL;
727     //set filter
728     if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
729     {
730         string condition="(FOLDER_STORAGE_TYPE = 0 OR FOLDER_STORAGE_TYPE = 1)";
731         LoggerI("condition:" + condition);
732         media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT );  //set condition
733         if ( MEDIA_CONTENT_ERROR_NONE != 
734             media_folder_foreach_folder_from_db (filter, mediaFolderCallback, eFolder.Get()))
735         {
736             LoggerE("A platform error occurs in media_folder_foreach_folder_from_db");
737             eFolder->setResult(false);
738         }
739         else{
740             eFolder->setResult(true);
741         }
742         if ( MEDIA_CONTENT_ERROR_NONE != media_filter_destroy(filter))
743         {
744             LoggerE("A platform error occurs in media_filter_destroy");
745         }
746     }
747 }
748
749 void MediacontentManager::OnRequestReceived(const IEventBrowseFolderPtr &eBrowse)
750 {
751     MediaSearchVisitorPtr visitor(new MediaSearchVisitor());
752     visitor->setQueryType(MediaSearchVisitor::QUERY_MEDIA);
753     int ret = MEDIA_CONTENT_ERROR_NONE;
754
755     try
756     {
757         filter_h filter = NULL;
758
759         //set filter
760         if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
761         {
762             string condition = "(MEDIA_STORAGE_TYPE = 0 OR MEDIA_STORAGE_TYPE = 1)";;
763             if (eBrowse->getFilterIsSet())
764             {
765                 FilterPtr jsFilter = eBrowse->getFilter();
766                 FilterValidatorPtr validator = MediaFilterValidatorFactory::getMediaFilterValidator(MediaFilterValidatorFactory::QUERY_MEDIA);
767                 bool success = jsFilter->validate(validator);
768
769                 if(!success)
770                     ThrowMsg(PlatformException, "Invalid attirbutes.");
771
772                 IFilterVisitorPtr IVisitor = DPL::StaticPointerCast<IFilterVisitor>(visitor);
773                 jsFilter->travel(IVisitor);
774                 condition += " AND ";
775                 condition += visitor->getResult();
776             }
777
778             LoggerI("condition:" + condition);
779             media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT );  //set condition
780
781             if(eBrowse->getSortModesIsSet())
782             {
783                 media_content_order_e order;
784                 SortModePtr attr = eBrowse->getSortMode();
785
786                 if ( attr )
787                 {
788                     string attriName = attr->getAttributeName();
789                     attriName = visitor->getPlatformAttr(attriName);
790
791                     if (attriName.compare("") != 0)
792                     {
793                         if (attr->getOrder() == DeviceAPI::Tizen::ASCENDING_SORT_ORDER)
794                         {
795                             order = MEDIA_CONTENT_ORDER_ASC;
796                         }
797                         else
798                         {
799                             order = MEDIA_CONTENT_ORDER_DESC;
800                         }
801
802                         if ( MEDIA_CONTENT_ERROR_NONE !=
803                             media_filter_set_order(filter, order, attriName.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT ))           //set order
804                         {
805                             LoggerE("A platform error occurs in media_filter_set_order");
806                         }
807                     }
808                 }
809
810             }
811
812             if (eBrowse->getLimitIsSet() ||eBrowse->getOffsetIsSet() )
813             {
814                 int count = -1;
815                 int offset = 0;
816
817                 if ( eBrowse->getLimitIsSet() )
818                 {
819                     count =  eBrowse->getLimit();
820                 }
821
822                 if ( eBrowse->getOffsetIsSet() )
823                 {
824                     offset = eBrowse->getOffset();
825                 }
826
827                 if ( MEDIA_CONTENT_ERROR_NONE != media_filter_set_offset(filter, offset, count))
828                 {
829                     LoggerE("A platform error occurs in media_filter_set_offset");
830                 }
831             }
832         }
833         else
834         {
835             LoggerE("A platform error occurs in media_filter_create");
836             eBrowse->setResult(false);
837         }
838
839         if ( eBrowse->getFolderIdIsSet())
840         {
841             string folderID = eBrowse->getFolderID();
842
843             if ( MEDIA_CONTENT_ERROR_NONE !=  
844                 media_folder_foreach_media_from_db (folderID.c_str(), filter, mediaItemCallback, eBrowse.Get()))
845             {
846                 LoggerE("A platform error occurs in media_folder_foreach_media_from_db");
847                 eBrowse->setResult(false);
848             }
849             else
850             {
851                 eBrowse->setResult(true);
852             }
853         }
854         else
855         {
856             if ( MEDIA_CONTENT_ERROR_NONE !=  
857                 media_info_foreach_media_from_db (filter, mediaItemCallback, eBrowse.Get()))
858             {
859                 LoggerE("A platform error occurs in media_info_foreach_media_from_db");
860                 eBrowse->setResult(false);
861             }
862             else
863             {
864                 eBrowse->setResult(true);
865             }
866         }
867
868         //destory Filter
869         if(filter)
870         {
871             if ( MEDIA_CONTENT_ERROR_NONE != media_filter_destroy(filter))
872             {
873                 LoggerE("A platform error occurs in media_filter_destroy");
874             }
875         }
876     }
877     catch(const Exception &ex)
878     {
879         LoggerE("Exception: " << ex.DumpToString());
880         eBrowse->setResult(false);
881         return;
882     }
883 }
884
885 bool MediacontentManager::updateMediaToDB(MediacontentMediaPtr mediaPtr)
886 {
887         string type  = mediaPtr->getMediaType();        //media type.
888         string mediaId = mediaPtr->getMediaUUID();
889
890         media_info_h media = NULL;
891
892         bool ret = true;
893
894         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_from_db(mediaId.c_str(), &media))
895         {
896                 if ( mediaPtr->isChangedRating())
897                 {
898                         if ( MEDIA_CONTENT_ERROR_NONE
899                                 != media_info_set_rating(media, mediaPtr->getRating()))
900                         {
901                                 LoggerD("Error: set rating");
902                         }
903                 }
904                 if ( mediaPtr->isChangedDescription() )
905                 {
906                         if ( MEDIA_CONTENT_ERROR_NONE
907                                 != media_info_set_description (media, mediaPtr->getDescription().c_str()))
908                         {
909                                 LoggerD("Error: set description");
910                         }
911                 }
912                 if( mediaPtr->isChangedDisplayName())
913                 {
914                         if ( MEDIA_CONTENT_ERROR_NONE
915                                 != media_info_set_display_name (media, mediaPtr->getDisplayName().c_str()))
916                         {
917                                 LoggerD("Error: set displayname");
918                         }
919                 }
920
921                 if(type.compare("IMAGE") ==0 )
922                 {
923                         MediacontentImagePtr imagePtr = DPL::DynamicPointerCast<MediacontentImage>(mediaPtr);
924
925                         if(imagePtr)
926                         {
927                                 image_meta_h img=NULL;
928                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_image(media, &img))
929                                 {
930                                         if(imagePtr->isChangedOrientaion())
931                                         {
932                                                 if ( MEDIA_CONTENT_ERROR_NONE !=
933                                                         image_meta_set_orientation (img, convertToOrientation(imagePtr->getImageOrientation())))
934                                                 {
935                                                         LoggerD("Error: set orientation");
936                                                         ret = false;
937                                                 }
938                                         }
939
940                                         if ( MEDIA_CONTENT_ERROR_NONE != 
941                                                 media_info_set_latitude(media, imagePtr->getImageLatitude())
942                                                 )
943                                         {
944                                                 LoggerD("Error: set Latitude");
945                                                 ret = false;
946                                         }
947
948                                         if ( MEDIA_CONTENT_ERROR_NONE != 
949                                                 media_info_set_longitude(media, imagePtr->getImageLongitude())
950                                                 )
951                                         {
952                                                 LoggerD("Error: set Longitude");
953                                                 ret = false;
954                                         }
955
956                                         if ( MEDIA_CONTENT_ERROR_NONE != image_meta_update_to_db (img) )
957                                         {
958                                                 LoggerD("Error: update db");
959                                                 ret = false;
960                                         }
961
962                                         if ( MEDIA_CONTENT_ERROR_NONE != image_meta_destroy(img))
963                                         {
964                                                 LoggerD("Error: destroy media info");
965                                                 ret = false;
966                                         }
967
968                                         img = NULL;
969
970                                 }
971                                 else
972                                 {
973                                         LoggerD("Error: get Image from DB");
974                                         ret = false;
975                                 }
976                         }
977
978                 }
979
980                 if(type.compare("VIDEO") ==0 )
981                 {
982                         MediacontentVideoPtr videoPtr = DPL::DynamicPointerCast<MediacontentVideo>(mediaPtr);
983                         if (videoPtr)
984                         {
985
986                                 video_meta_h video = NULL;
987                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_video(media, &video))
988                                 {
989                                         if ( MEDIA_CONTENT_ERROR_NONE != 
990                                                 media_info_set_latitude(media, videoPtr->getVideoLatitude())
991                                                 )
992                                         {
993                                                 LoggerD("Error: set Latitude");
994                                                 ret = false;
995                                         }
996
997                                         if ( MEDIA_CONTENT_ERROR_NONE != 
998                                                 media_info_set_longitude(media, videoPtr->getVideoLongitude())
999                                                 )
1000                                         {
1001                                                 LoggerD("Error: set Latitude");
1002                                                 ret = false;
1003                                         }
1004
1005
1006                                         if ( MEDIA_CONTENT_ERROR_NONE != video_meta_update_to_db (video) )
1007                                         {
1008                                                 LoggerD("Error: update db");
1009                                                 ret = false;
1010                                         }
1011
1012                                         if ( MEDIA_CONTENT_ERROR_NONE != video_meta_destroy(video))
1013                                         {
1014                                                 LoggerD("Error: destroy video meta");
1015                                                 ret = false;
1016                                         }
1017
1018                                         video = NULL;
1019
1020                                 }
1021                         }
1022
1023                 } //video
1024
1025
1026
1027                 if(type.compare("AUDIO") ==0 )
1028                 {
1029
1030                         MediacontentAudioPtr audioPtr = DPL::DynamicPointerCast<MediacontentAudio>(mediaPtr);
1031                         if (audioPtr && ( audioPtr->isChangedAudioArtist () ||  audioPtr->isChangedAudioAlbum() 
1032                                 || audioPtr->isChangedAudioGenre() ||audioPtr->isChangedAudioComposer()
1033                                 || audioPtr->isChangedAudioTrackNumber()) )
1034                         {
1035
1036                                 audio_meta_h audio=NULL;
1037                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(media, &audio))
1038                                 {
1039                                         if ( MEDIA_CONTENT_ERROR_NONE != audio_meta_update_to_db (audio) )
1040                                         {
1041                                                 LoggerD("Error: update db");
1042                                                 ret = false;
1043                                         }
1044                                         
1045                                         if ( MEDIA_CONTENT_ERROR_NONE != audio_meta_destroy(audio))
1046                                         {
1047                                                 LoggerD("Error: destroy audio meta");
1048                                                 ret = false;
1049                                         }
1050                                         audio = NULL;
1051                                 }
1052
1053                         }
1054
1055                 }
1056                 
1057                 
1058                 //update media info
1059                 if ( MEDIA_CONTENT_ERROR_NONE != media_info_update_to_db(media))
1060                 {
1061                         LoggerD("Error: update to DB");
1062                         ret = false;
1063                 }
1064         }
1065
1066         if( media != NULL)
1067         {
1068                 if ( MEDIA_CONTENT_ERROR_NONE !=media_info_destroy(media))
1069                 {
1070                         LoggerD("Error: destroy media info");
1071                         ret = false;
1072                 }
1073         }
1074         return ret;
1075
1076 }
1077
1078 void MediacontentManager::OnRequestReceived(const IEventUpdateMediaPtr &eMedia)
1079 {
1080         try
1081         {
1082                 MediacontentMediaPtr mediaPtr = eMedia->getMediaItem();
1083
1084                 if(updateMediaToDB(mediaPtr))
1085                 {
1086                         eMedia->setResult(true);
1087                 }
1088                 else
1089                 {
1090                         eMedia->setResult(false);
1091                         ThrowMsg(PlatformException, "DB operation is failed");
1092                 }
1093         }
1094     catch (const Exception &ex)
1095     {
1096         LoggerE("Exception: " << ex.DumpToString());
1097         eMedia->setResult(false);
1098     }
1099 }
1100
1101
1102 void MediacontentManager::OnRequestReceived(const IEventUpdateMediaItemsPtr &eItems)
1103 {
1104         try
1105         {
1106                 MediacontentMediaListPtr mediaListPtr = eItems->getMediaItems();
1107                 if (mediaListPtr->empty()) 
1108             {
1109                     LoggerD("Item vector is empty");
1110               }
1111                 else
1112                 {
1113                          for(unsigned int i=0; i<mediaListPtr->size(); i++)
1114                         {
1115                                 
1116                                 MediacontentMediaPtr mediaPtr = mediaListPtr->at(i);
1117                                 if(updateMediaToDB(mediaPtr))
1118                                 {
1119                                         eItems->setResult(true);
1120                                         LoggerD("update success");
1121                                 }
1122                                 else
1123                                 {
1124                                         eItems->setResult(false);
1125                                         ThrowMsg(PlatformException, "DB operation is failed");
1126                                 }
1127                                 
1128                         }
1129                 }
1130         }
1131         catch (const Exception &ex)
1132         {
1133                  LoggerE("Exception: " << ex.DumpToString());
1134                  eItems->setResult(false);
1135         }       
1136 }
1137
1138 }
1139 }