wrt-plugins-tizen_0.4.23
[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     LoggerD("entered");
43
44         //protect for pararel execution
45         DPL::Mutex::ScopedLock mx(&m_constructorMutex);
46         if (m_instanceCount == 0)
47         {
48                 //when it is first instance then connect to media content service.
49                 if( MEDIA_CONTENT_ERROR_NONE != media_content_connect())
50                 {       
51                         LoggerD("Media Content Service Connect Fail...");
52                         return;
53                 }
54         }
55         //increase counter of instances
56         m_instanceCount++;
57         
58 }
59
60 MediacontentManager::~MediacontentManager()
61 {
62         LoggerD("entered : " << m_instanceCount);
63         DPL::Mutex::ScopedLock mx(&m_constructorMutex);
64         //decrease counter of instances
65         m_instanceCount--;
66         if (m_instanceCount == 0)
67         {
68                 LoggerD("instance Count is zero ");
69                 //when it is last instance then clse connection to database
70                 if ( MEDIA_CONTENT_ERROR_NONE != media_content_disconnect())
71                 {
72                         LoggerD("Media Content Service Disconnect Fail...");
73                 }
74         }
75 }
76
77 /*
78 void MediacontentManager::OnRequestReceived(const IEventGetMediacontentPtr &event)
79 {
80         LoggerD( "OnRequestReceived::entered");
81     //IMediacontentPtr newMediacontent(new Mediacontent());
82     //IMediacontentPtr newMediacontent = MediacontentFactory::getInstance().createMediacontentObject();
83     
84     Try
85     {
86         LoggerD( "get Mediacontent \n");
87 //              event->setMediacontent(m_mediaContent);
88                 event->setResult(true);
89     }
90     Catch(Exception)
91     {
92         LogError("error occuered during obtaining data");
93         event->setResult(false);
94     }
95     event->setCancelAllowed(true);
96 }
97 */
98
99 tm* MediacontentManager::toDateTm(time_t date)
100 {
101         tm *tm_date = (struct tm *)calloc(1, sizeof(struct tm));
102
103         localtime_r(&date, tm_date);
104
105         return tm_date;
106 }
107
108 media_content_orientation_e MediacontentManager::convertToOrientation(string orientation)
109 {
110         media_content_orientation_e ret = MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE;
111
112         if ( orientation.compare("NORMAL")==0)
113         {
114                 ret = MEDIA_CONTENT_ORIENTATION_NORMAL;
115         }
116         else if (orientation.compare("FLIP_HORIZONTAL")==0)
117         {
118                 ret = MEDIA_CONTENT_ORIENTATION_HFLIP;
119         }
120         else if (orientation.compare("ROTATE_180")==0)
121         {
122                 ret = MEDIA_CONTENT_ORIENTATION_ROT_180;
123         }
124         else if (orientation.compare("FLIP_VERTICAL")==0)
125         {
126                 ret = MEDIA_CONTENT_ORIENTATION_VFLIP;
127         }
128         else if (orientation.compare("TRANSPOSE")==0)
129         {
130                 ret = MEDIA_CONTENT_ORIENTATION_TRANSPOSE;
131         }
132         else if (orientation.compare("ROTATE_90")==0)
133         {
134                 ret = MEDIA_CONTENT_ORIENTATION_ROT_90;
135         }
136         else if (orientation.compare("TRANSVERSE")==0)
137         {
138                 ret =MEDIA_CONTENT_ORIENTATION_TRANSVERSE;
139         }
140         else if (orientation.compare("ROTATE_270")==0)
141         {
142                 ret =MEDIA_CONTENT_ORIENTATION_ROT_270;
143         }       
144         else
145         {
146                 LoggerD("wrong value.");
147                 ret = MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE;
148         }
149
150         return ret;
151 }
152
153 void MediacontentManager::convertToPlatformFolder(media_folder_h media_folder, MediacontentFolderPtr& newFolder)
154 {
155         char* tmpStr = NULL;
156         time_t date;
157         media_content_storage_e storageType;
158
159         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_folder_id(media_folder, &tmpStr))
160         {
161                 if ( tmpStr )
162                 {
163                         LoggerD("Folder UUID : " << tmpStr);
164                         newFolder->setFolderUUID(tmpStr);
165                         free(tmpStr);
166                         tmpStr = NULL;
167                 }
168         }
169
170         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_path(media_folder, &tmpStr))
171         {
172                 if ( tmpStr )
173                 {               
174                         LoggerD("Folder path : " << tmpStr);
175                         newFolder->setFolderPath(ContentUtility::convertPathToUri(tmpStr));
176                         free(tmpStr);
177                         tmpStr = NULL;
178                 }
179         }
180
181         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_name(media_folder, &tmpStr))
182         {
183                 if ( tmpStr )
184                 {               
185                         LoggerD("Folder name : " << tmpStr);
186                         newFolder->setFolderName(tmpStr);
187                         free(tmpStr);
188                         tmpStr = NULL;  
189                 }
190         }
191         
192         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_modified_time(media_folder, &date))
193         {
194                 LoggerD("get Modified Time ");
195                 newFolder->setFolderModifiedDate(toDateTm(date));
196         }
197         
198         if (MEDIA_CONTENT_ERROR_NONE == media_folder_get_storage_type(media_folder, &storageType))
199         {       
200                 LoggerD("storage Type=" << storageType);
201                 string type;
202                 if(storageType == MEDIA_CONTENT_STORAGE_INTERNAL )
203                 {
204                         type = "INTERNAL";
205                 }
206                 else if( storageType == MEDIA_CONTENT_STORAGE_EXTERNAL)
207                 {
208                         type = "EXTERNAL";
209                 }
210                 else
211                 {
212                         type = "UNKNOWN";
213                 }
214                 newFolder->setFolderStorageType(type);
215         }
216                 
217                 
218 }
219
220 void MediacontentManager::readCommonDataFromMediaInfo(media_info_h info, MediacontentMedia* newMedia)
221 {       
222
223         char* tmpStr = NULL;
224         time_t tmpDate;
225         int tmpInt = 0;
226         unsigned long long tmpLongLong = 0;
227         bool tmpBool= false;
228
229         LoggerD("enter Common Data type : " << newMedia->getMediaType());
230         
231         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_id(info, &tmpStr) )
232         {
233                 if (tmpStr)
234                 {
235                         newMedia->setMediaUUID(tmpStr);
236                         LoggerD("UUID : " << tmpStr);
237                         free(tmpStr);
238                         tmpStr = NULL;
239                 }
240         }
241
242         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_mime_type(info, &tmpStr) )
243         {
244                 if (tmpStr)
245                 {
246                         newMedia->setMimeType(tmpStr);
247                         LoggerD("MineType : " << tmpStr);
248                         free(tmpStr);
249                         tmpStr = NULL;
250                 }
251         }
252
253         if (MEDIA_CONTENT_ERROR_NONE == media_info_get_display_name(info, &tmpStr) )
254         {
255                 if (tmpStr)
256                 {
257                         newMedia->setDisplayName(tmpStr);
258                         LoggerD("Display Name : " << tmpStr);
259                         free(tmpStr);
260                         tmpStr = NULL;
261                 }
262         }
263
264         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_file_path(info, &tmpStr) )
265         {
266                 if (tmpStr)
267                 {
268                         newMedia->setFilePath(ContentUtility::convertPathToUri(tmpStr));
269                         LoggerD("Path : " << newMedia->getFilePath());
270                         free(tmpStr);
271                         tmpStr = NULL;
272                 }
273         }
274
275         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_thumbnail_path (info, &tmpStr) )
276         {
277                 if (tmpStr)
278                 {
279                         newMedia->setThumbnailPath(ContentUtility::convertPathToUri(tmpStr));
280                         LoggerD("Thumnail Path : " << newMedia->getThumbnailPath());
281                         free(tmpStr);
282                         tmpStr = NULL;
283                 }
284         }
285         
286         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_description(info, &tmpStr) )
287         {
288                 if (tmpStr)
289                 {
290                         newMedia->setDescription(tmpStr);
291                         LoggerD("Description : " << tmpStr);
292                         free(tmpStr);
293                         tmpStr = NULL;
294                 }
295         }
296
297         //newImage->setReleasedDate(toDateTm(sqlite3_column_int(pStmt,7)));
298         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_modified_time (info, &tmpDate) )
299         {
300             if(tmpDate)
301             {
302                 newMedia->setModifiedDate(toDateTm(tmpDate));
303             }
304         }
305
306         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_favorite (info, &tmpBool) )
307         {
308                 newMedia->setFavorite(tmpBool);
309                 LoggerD("favorite " << tmpBool);
310         }
311
312         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_rating  (info, &tmpInt) )
313         {
314                 newMedia->setRating(tmpInt);
315                 LoggerD("rating " << tmpInt);
316         }
317
318         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_size(info, &tmpLongLong) )
319         {
320                 newMedia->setSize(tmpLongLong);
321                 LoggerD("Size : " << tmpLongLong);
322         }
323         
324 }
325
326 void MediacontentManager::readImageFromMediaInfo( media_info_h info, MediacontentImage* newImage)
327 {       
328         char* tmpStr = NULL;
329         double tmpDouble;
330         int tmpInt = 0;
331         
332         if ( info) 
333         {
334                 newImage->setMediaType("IMAGE");
335
336                 readCommonDataFromMediaInfo(info, newImage);
337
338                 image_meta_h img;
339                 
340                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_image(info, &img))
341                 {
342                         //created time
343                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_date_taken (img, &tmpStr) )
344                         {       
345                                 if ( tmpStr )
346                                 {
347                                         struct tm *result = (struct tm *)calloc(1, sizeof(struct tm));
348                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
349                                                 LogError( "Couldn't convert supplied date.");
350                                         }
351                                 
352                                         LoggerD("image_meta_get_date_taken : " <<  tmpStr); //time??
353                                         newImage->setReleasedDate(result);
354                                         free(tmpStr);
355                                         tmpStr = NULL;
356                                 }
357                         }
358                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_title(img, &tmpStr) )
359                         {
360                                 if( tmpStr )
361                                 {
362                                         newImage->setTitle(tmpStr);
363                                         LoggerD(" Image Title: " << tmpStr);
364                                         free(tmpStr);
365                                         tmpStr = NULL;
366                                 }
367                         }
368
369                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_width(img, &tmpInt) )
370                         {
371                                 newImage->setImageWidth(tmpInt);
372                                 LoggerD("Image Width " << tmpInt);
373                         }
374
375                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_height(img, &tmpInt) )
376                         {
377                                 newImage->setImageHeight(tmpInt);
378                                 LoggerD("Image Height " << tmpInt);
379                         }
380
381                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble) )
382                         {
383                                 newImage->setImageLatitude(tmpDouble);
384                                 LoggerD("Image Latitude " << tmpDouble);
385                         }
386                         
387                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble) )
388                         {
389                                 newImage->setImageLongitude(tmpDouble);
390                                 LoggerD("Image Longitude " << tmpDouble);
391                         }
392                                         
393                         media_content_orientation_e orientation;
394                         
395                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_orientation(img, &orientation) )
396                         {       
397         
398                                 string orientationStr;
399                                 switch(orientation)
400                                 {
401                                         case 0:
402                                         case 1: 
403                                                 orientationStr = "NORMAL";
404                                                 break;
405                                         case 2: 
406                                                 orientationStr = "FLIP_HORIZONTAL";
407                                                 break;
408                                         case 3: 
409                                                 orientationStr = "ROTATE_180";
410                                                 break;
411                                         case 4: 
412                                                 orientationStr = "FLIP_VERTICAL";
413                                                 break;
414                                         case 5: 
415                                                 orientationStr = "TRANSPOSE";
416                                                 break;
417                                         case 6: 
418                                                 orientationStr = "ROTATE_90";
419                                                 break;
420                                         case 7: 
421                                                 orientationStr = "TRANSVERSE";
422                                                 break;
423                                         case 8: 
424                                                 orientationStr = "ROTATE_270";
425                                                 break;
426                                 }       
427                                 
428                                 newImage->setImageOrientation(orientationStr);
429                                 LoggerD(" image Orientation. " << orientationStr);
430
431                         }
432
433
434                         if ( METADATA_EXTRACTOR_ERROR_NONE != image_meta_destroy(img))
435                         {
436                                 LoggerD(" image_meta_destroy is fail... ");
437                         }
438                                                 
439                 }
440                 else {
441                         LoggerD("fetch Image Info failed..");                   
442                 }
443                         
444         }
445         else
446         {
447                 LoggerD("media info is NULL");
448         }
449 }
450
451 void MediacontentManager::readVideoFromMediaInfo( media_info_h info, MediacontentVideo* newVideo)
452 {       
453         char* tmpStr;
454         int tmpInt = 0;
455         double tmpDouble;
456         
457         if ( info) 
458         {
459                 newVideo->setMediaType("VIDEO");        
460
461                 readCommonDataFromMediaInfo(info, newVideo);    //set common media infomation
462
463                 video_meta_h video;
464                 
465                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_video(info, &video))
466                 {
467
468                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_recorded_date(video, &tmpStr) )
469                         {
470                                 if ( tmpStr )
471                                 {       
472                                         struct tm *result=(struct tm *)calloc(1, sizeof(struct tm));
473                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
474                                                 LogError( "Couldn't convert supplied date.");
475                                         }
476                                         newVideo->setReleasedDate(result);
477                                         
478                                         LoggerD("audio_meta_get_recorded_date : " << tmpStr);
479                                         free(tmpStr);
480                                         tmpStr = NULL;
481                                 }
482                         }
483                 
484                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_album(video, &tmpStr) )
485                         {
486                                 if( tmpStr )
487                                 {
488                                         newVideo->setVideoAlbum(tmpStr);
489                                         LoggerD(" Video Album. " << tmpStr);
490                                         free(tmpStr);
491                                         tmpStr = NULL;
492                                 }
493                         }
494
495                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_title(video, &tmpStr) )
496                         {
497                                 if( tmpStr )
498                                 {
499                                         newVideo->setTitle(tmpStr);
500                                         LoggerD(" Video Title. " << tmpStr);
501                                         free(tmpStr);
502                                         tmpStr = NULL;
503                                 }
504                         }
505
506                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_artist(video, &tmpStr) )
507                         {       
508                                 if ( tmpStr)
509                                 {
510                                         std::vector<std::string> artists = String::split(tmpStr, TAG_DELIMETER);
511                                         for( unsigned int i=0; i < artists.size(); i++)
512                                         {
513                                                 string artist = artists.at(i);
514                                                 LoggerD("Audio artist: " << artist);
515                                                 newVideo->appendVideoArtist(artist);
516                                         }
517                                         
518                                         free(tmpStr);
519                                         tmpStr = NULL;
520                                 }
521                         }
522
523                         
524                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble) )
525                         {
526                                 newVideo->setVideoLongitude(tmpDouble);
527                         }
528
529                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble) )
530                         {
531                                 newVideo->setVideoLatitude(tmpDouble);
532                         }
533
534                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_width(video, &tmpInt) )
535                         {
536                                 newVideo->setVideoWidth(tmpInt);
537                         }
538                         
539                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_height(video, &tmpInt) )
540                         {
541                                 newVideo->setVideoHeight(tmpInt);
542                         }
543
544                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_duration(video, &tmpInt) )
545                         {
546                                 newVideo->setVideoDuration(tmpInt);
547                         }
548
549                         if ( METADATA_EXTRACTOR_ERROR_NONE != video_meta_destroy(video))
550                         {
551                                 LoggerD(" video_meta_destroy is fail... ");
552                         }
553                                                 
554                 }
555                 else
556                 {
557                         LoggerD("fetch Video info fail...");
558                 }
559         }
560         else
561         {
562                 LoggerD("media info is NULL");
563         }
564 }
565
566
567 void MediacontentManager::readMusicFromMediaInfo( media_info_h info, MediacontentAudio* newAudio)
568 {       
569         char* tmpStr;
570         int tmpInt = 0;
571         unsigned long long tmpLongLong = 0;
572                 
573         if ( info) 
574         {
575                 newAudio->setMediaType("AUDIO");        
576                 
577                 readCommonDataFromMediaInfo(info, newAudio);    //set common media infomation
578
579                 audio_meta_h audio;
580                 
581                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(info, &audio))
582                 {       
583                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_recorded_date(audio, &tmpStr) )
584                         {
585                                 if ( tmpStr )
586                                 {       
587                                         struct tm *result = (struct tm *)calloc(1, sizeof(struct tm));
588                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
589                                                 LogError( "Couldn't convert supplied date.");
590                                         }
591                                         newAudio->setReleasedDate(result);
592                                         
593                                         LoggerD("audio_meta_get_recorded_date : " << tmpStr);
594                                         free(tmpStr);
595                                         tmpStr = NULL;
596                                 }
597                         }
598
599                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_title(audio, &tmpStr) )
600                         {
601                                 if (tmpStr)
602                                 {
603                                         newAudio->setTitle(tmpStr);
604                                         LoggerD("Audio Title : " << tmpStr);
605                                         free(tmpStr);
606                                         tmpStr = NULL;
607                                 }
608                         }
609
610                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_album(audio, &tmpStr) )
611                         {
612                                 if (tmpStr)
613                                 {
614                                         newAudio->setAudioAlbum(tmpStr);
615                                         LoggerD("Audio Album : " << tmpStr);
616                                         free(tmpStr);
617                                         tmpStr = NULL;
618                                 }
619                         }
620
621                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_artist(audio, &tmpStr) )
622                         {       
623                                 if (tmpStr)
624                                 {
625                                         std::vector<std::string> artists = String::split(tmpStr, TAG_DELIMETER);
626                                         for( unsigned int i=0; i < artists.size(); i++)
627                                         {
628                                                 string artist = artists.at(i);
629                                                 LoggerD("Audio artist: " << artist);
630                                                 newAudio->appendAudioArtist(artist);
631                                         }
632                                         
633                                         free(tmpStr);
634                                         tmpStr = NULL;
635                                 }
636                         }
637                 
638                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_composer(audio, &tmpStr) )
639                         {
640                                 if (tmpStr)
641                                 {       
642                                         std::vector<std::string> composers = String::split(tmpStr, TAG_DELIMETER);
643                                         for( unsigned int i=0; i < composers.size(); i++)
644                                         {
645                                                 string composer = composers.at(i);
646                                                 LoggerD("Audio Composer : " << composer);
647                                                 newAudio->appendAudioComposer(composer);
648                                         }
649                                         
650                                         free(tmpStr);
651                                         tmpStr = NULL;
652                                 }
653                         }
654                         
655                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_duration(audio, &tmpInt) )
656                         {
657                                 newAudio->setAudioDuration(tmpInt);
658                                 LoggerD("Audio Duration : " << tmpInt);
659                         }
660
661                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_copyright(audio, &tmpStr) )
662                         {
663                                 if (tmpStr)
664                                 {
665                                         newAudio->setAudioCopyright(tmpStr);
666                                         LoggerD("Audio CopyRight: " << tmpStr);
667                                         free(tmpStr);
668                                         tmpStr = NULL;
669                                 }
670                         }
671                 
672                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_track_num(audio, &tmpStr) )
673                         {       
674                                 int trackNum = 0;
675                                 if (tmpStr)
676                                 {
677                                         LoggerD("trackNum: " << tmpStr);
678                                         try {
679                                                 istringstream(tmpStr) >> trackNum;
680                                         } catch (...) {
681                                                 LoggerD("Track Number type is wrong. (track number:" << tmpStr << ")");
682                                                 trackNum = 0;
683                                         }
684                                         newAudio->setAudioTrackNum(trackNum);
685                                         free(tmpStr);
686                                         tmpStr = NULL;
687                                 }
688                         }
689
690                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_size(info, &tmpLongLong) )
691                         {       
692                                 newAudio->setAudioSize(tmpLongLong);
693                                 LoggerD("Audio Size: " << tmpLongLong);
694                         }
695
696                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_bit_rate(audio, &tmpInt) )
697                         {       
698                                 newAudio->setAudioBitrate(tmpInt);
699                                 LoggerD("Audio Bitrate: " << tmpInt);
700                         }
701
702                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_genre(audio, &tmpStr) )
703                         {       
704                                 if (tmpStr)
705                                 {
706                                         std::vector<std::string> genres = String::split(tmpStr, TAG_DELIMETER);
707
708                                         for( unsigned int i=0; i < genres.size(); i++)
709                                         {
710                                                 string genre = genres.at(i);
711                                                 LoggerD("Audio Genre: " << genre);
712                                                 newAudio->appendAudioGenre(genre);
713                                         }
714                                         
715                                         free(tmpStr);
716                                         tmpStr = NULL;
717                                 }
718                         }
719                                                 
720                         if ( METADATA_EXTRACTOR_ERROR_NONE != audio_meta_destroy(audio))
721                         {
722                                 LoggerD(" audio_meta_destroy is fail... ");
723                         }
724                         
725
726                 }
727                 else
728                 {
729                         LoggerD("fetch Music Info fail...");
730                 }
731                 
732                         
733         }
734         else
735         {
736                 LoggerD("media info is NULL");
737         }
738 }
739
740
741
742 //Callback.
743 bool MediacontentManager::mediaFolderCallback(media_folder_h folder, void *user_data)
744 {       
745         LoggerD("enter");
746         if (user_data != NULL){
747                 IEventFindFolder* event = (IEventFindFolder*)user_data;
748                 LoggerD("user data is valide");
749                 if ( folder )
750                 {
751                         LoggerD("folder is valide");
752                         MediacontentFolderPtr newFolder(new MediacontentFolder());
753                         convertToPlatformFolder(folder, newFolder);
754                         LoggerD("folder type" << newFolder->getFolderStorageType());
755                         
756                         event->addFolder(newFolder);
757                 }
758         }
759         else
760         {
761                 LoggerD("user data is NULL");
762         }
763         
764         return true;
765
766 }
767
768 bool MediacontentManager::mediaItemCallback(media_info_h info, void* user_data)
769 {
770         if (user_data != NULL){
771                 IEventBrowseFolder* event = (IEventBrowseFolder*)user_data;
772
773                 media_content_type_e type;
774                 
775                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
776                 {
777                         if ( type == MEDIA_CONTENT_TYPE_IMAGE)
778                         {
779                                 MediacontentImage *newImage(new MediacontentImage());
780                                 readImageFromMediaInfo(info, newImage);
781                                 event->addMedia(newImage);
782                         }
783                         else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
784                         {
785                                 MediacontentVideo *newVideo(new MediacontentVideo());
786                                 readVideoFromMediaInfo(info, newVideo);
787                                 event->addMedia(newVideo);
788                         }
789                         else if ( type == MEDIA_CONTENT_TYPE_MUSIC)
790                         {
791                                 MediacontentAudio *newVAudio(new MediacontentAudio());
792                                 readMusicFromMediaInfo(info, newVAudio);
793                                 event->addMedia(newVAudio);
794                         }
795                         else if( type == MEDIA_CONTENT_TYPE_OTHERS)
796                         {
797                                 MediacontentMedia *newMedia(new MediacontentMedia());
798
799                                 newMedia->setMediaType("OTHER");
800                                 readCommonDataFromMediaInfo(info, newMedia);
801                                 event->addMedia(newMedia);
802                         }
803
804                 }
805         }
806         else 
807         {
808                 LoggerD("event is NULL");       
809         }
810
811         return true;
812 }
813
814 void MediacontentManager::OnRequestReceived(const IEventFindFolderPtr &eFolder)
815 {
816         LoggerD("entered");
817                 
818         if ( MEDIA_CONTENT_ERROR_NONE != 
819                 media_folder_foreach_folder_from_db (NULL, mediaFolderCallback, eFolder.Get()))
820         {       
821                 LogError("error ( media_folder_foreach_folder_from_db ) : ");
822                 eFolder->setResult(false);
823         }
824         else
825         {
826                 eFolder->setResult(true);
827         }
828         LoggerD("end");
829 }
830
831 void MediacontentManager::OnRequestReceived(const IEventBrowseFolderPtr &eBrowse)
832 {
833         LoggerD("OnRequestReceived::IEventBrowseFolderPtr entered");
834
835         MediaSearchVisitorPtr visitor(new MediaSearchVisitor());
836         visitor->setQueryType(MediaSearchVisitor::QUERY_MEDIA);
837         int ret = MEDIA_CONTENT_ERROR_NONE;
838
839         try{
840                 filter_h filter = NULL;
841
842                 if(eBrowse->getFilterIsSet() || eBrowse->getFolderIdIsSet() || eBrowse->getSortModesIsSet()
843                         || eBrowse->getLimitIsSet() ||eBrowse->getOffsetIsSet() )
844                 {
845                         //set filter
846                         if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
847                         {
848                                 if (eBrowse->getFilterIsSet())
849                                 {
850                                         string condition;
851                                         FilterPtr jsFilter = eBrowse->getFilter();
852
853                                         FilterValidatorPtr validator = MediaFilterValidatorFactory::getMediaFilterValidator(MediaFilterValidatorFactory::QUERY_MEDIA);
854                                         bool success = jsFilter->validate(validator);
855
856                                         if(!success)
857                                                 ThrowMsg(PlatformException, "Invalid attirbutes.");
858                                         
859                                         IFilterVisitorPtr IVisitor = DPL::StaticPointerCast<IFilterVisitor>(visitor);
860
861                                         jsFilter->travel(IVisitor);
862                                         condition = visitor->getResult();
863
864                                         LoggerD("execute condition [" << condition << "]");
865                                         media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT );  //set condition
866                                 }
867
868                                 if(eBrowse->getSortModesIsSet()) 
869                                 {
870                                         media_content_order_e order;
871
872                                         SortModePtr attr = eBrowse->getSortMode();
873
874                                         if ( attr )
875                                         {
876                                                 string attriName = attr->getAttributeName();
877                                                 attriName = visitor->getPlatformAttr(attriName);
878                                                 LoggerD("Attribute Name [" << attriName << "]");
879
880                                                 if (attriName.compare("") != 0) 
881                                                 {       
882                                                         if (attr->getOrder() == DeviceAPI::Tizen::ASCENDING_SORT_ORDER) 
883                                                         {
884                                                                 order = MEDIA_CONTENT_ORDER_ASC;
885                                                         } 
886                                                         else 
887                                                         {
888                                                                 order = MEDIA_CONTENT_ORDER_DESC;
889                                                         }
890
891                                                         if ( MEDIA_CONTENT_ERROR_NONE !=
892                                                                 media_filter_set_order(filter, order, attriName.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT ))               //set order
893                                                         {
894                                                                 LoggerD("media_filter_set_order fail...");      
895                                                         }
896                                                 }
897                                         }
898                                         
899                                 }                               
900
901                                 if (eBrowse->getLimitIsSet() ||eBrowse->getOffsetIsSet() ) 
902                                 {       
903                                         LoggerD("limit or offset");
904                                         
905                                         int count = -1;
906                                         int offset = 0;
907                                         
908                                         if ( eBrowse->getLimitIsSet() )
909                                         {
910                                                 count =  eBrowse->getLimit();
911                                                 LoggerD("limit is " << count);
912                                         }
913                                         
914                                         if ( eBrowse->getOffsetIsSet() )
915                                         {
916                                                 offset = eBrowse->getOffset();
917                                                 LoggerD("offset is " << offset);
918                                         }
919                                         
920                                         if ( MEDIA_CONTENT_ERROR_NONE != media_filter_set_offset(filter, offset, count))
921                                         {
922                                                 LoggerD("set limit or offset fail...");
923                                         }
924                                 }
925                                 
926                         }
927                         else
928                         {
929                                 
930                                 LogError("error ( media filter create ) : ");
931                                 eBrowse->setResult(false);
932                         }
933                 }
934
935                 if ( eBrowse->getFolderIdIsSet())
936                 {       
937                         string folderID = eBrowse->getFolderID();
938                         LoggerD("FolderID :" << folderID);
939                         
940                         if ( MEDIA_CONTENT_ERROR_NONE !=  
941                                 media_folder_foreach_media_from_db (folderID.c_str(), filter, mediaItemCallback, eBrowse.Get()))
942                         {       
943                                 LogError("error ( media_folder_foreach_folder_from_db ) : " << ret);
944                                 eBrowse->setResult(false);
945                         }
946                         else
947                         {       
948                                 eBrowse->setResult(true);
949                         }
950                 }
951                 else
952                 {
953                         if ( MEDIA_CONTENT_ERROR_NONE !=  
954                                 media_info_foreach_media_from_db (filter, mediaItemCallback, eBrowse.Get()))
955                         {
956                                 LogError("error ( media_folder_foreach_folder_from_db ) : " << ret);
957                                 eBrowse->setResult(false);
958                         }
959                         else
960                         {
961                                 eBrowse->setResult(true);
962                         }
963                 }
964
965                 //destory Filter
966                 if(filter)
967                 {
968                         if ( MEDIA_CONTENT_ERROR_NONE != media_filter_destroy(filter))
969                         {
970                                 LogError("media_filter_create Error: " << ret);
971                         }
972                 }
973
974         }
975         catch(const Exception &ex){
976                         LogError("Exception: " << ex.DumpToString());
977                         eBrowse->setResult(false);
978                         return;
979         }
980
981
982
983 }
984
985 bool MediacontentManager::updateMediaToDB(MediacontentMediaPtr mediaPtr)
986 {
987         string type  = mediaPtr->getMediaType();        //media type.
988         string mediaId = mediaPtr->getMediaUUID();
989
990         LoggerD("MediaId : " << mediaId);
991         media_info_h media = NULL;
992
993         bool ret = true;
994
995         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_from_db(mediaId.c_str(), &media))
996         {
997                 if ( mediaPtr->isChangedRating())
998                 {
999                         if ( MEDIA_CONTENT_ERROR_NONE
1000                                 != media_info_set_rating(media, mediaPtr->getRating()))
1001                         {
1002                                 LoggerD("Error: set rating");
1003                         }
1004                 }
1005                 if ( mediaPtr->isChangedDescription() )
1006                 {
1007                         if ( MEDIA_CONTENT_ERROR_NONE
1008                                 != media_info_set_description (media, mediaPtr->getDescription().c_str()))
1009                         {
1010                                 LoggerD("Error: set description");
1011                         }
1012                 }
1013                 if( mediaPtr->isChangedDisplayName())
1014                 {
1015                         if ( MEDIA_CONTENT_ERROR_NONE
1016                                 != media_info_set_display_name (media, mediaPtr->getDisplayName().c_str()))
1017                         {
1018                                 LoggerD("Error: set displayname");
1019                         }
1020                 }
1021
1022                 if(type.compare("IMAGE") ==0 )
1023                 {
1024                         MediacontentImagePtr imagePtr = DPL::DynamicPointerCast<MediacontentImage>(mediaPtr);
1025
1026                         if(imagePtr)
1027                         {
1028                                 image_meta_h img=NULL;
1029                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_image(media, &img))
1030                                 {
1031                                         if(imagePtr->isChangedOrientaion())
1032                                         {
1033                                                 if ( MEDIA_CONTENT_ERROR_NONE !=
1034                                                         image_meta_set_orientation (img, convertToOrientation(imagePtr->getImageOrientation())))
1035                                                 {
1036                                                         LoggerD("Error: set orientation");
1037                                                         ret = false;
1038                                                 }
1039                                         }
1040
1041                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1042                                                 media_info_set_latitude(media, imagePtr->getImageLatitude())
1043                                                 )
1044                                         {
1045                                                 LoggerD("Error: set Latitude");
1046                                                 ret = false;
1047                                         }
1048
1049                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1050                                                 media_info_set_longitude(media, imagePtr->getImageLongitude())
1051                                                 )
1052                                         {
1053                                                 LoggerD("Error: set Longitude");
1054                                                 ret = false;
1055                                         }
1056
1057                                         if ( MEDIA_CONTENT_ERROR_NONE != image_meta_update_to_db (img) )
1058                                         {
1059                                                 LoggerD("Error: update db");
1060                                                 ret = false;
1061                                         }
1062
1063                                         if ( MEDIA_CONTENT_ERROR_NONE != image_meta_destroy(img))
1064                                         {
1065                                                 LoggerD("Error: destroy media info");
1066                                                 ret = false;
1067                                         }
1068
1069                                         img = NULL;
1070
1071                                 }
1072                                 else
1073                                 {
1074                                         LoggerD("Error: get Image from DB");
1075                                         ret = false;
1076                                 }
1077                         }
1078
1079                 }
1080
1081                 if(type.compare("VIDEO") ==0 )
1082                 {
1083                         MediacontentVideoPtr videoPtr = DPL::DynamicPointerCast<MediacontentVideo>(mediaPtr);
1084                         if (videoPtr)
1085                         {
1086
1087                                 video_meta_h video = NULL;
1088                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_video(media, &video))
1089                                 {
1090 #if 0 //NOT support
1091                                         if (  videoPtr->isChangedPlayCount() )
1092                                         {
1093                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1094                                                         video_meta_set_played_count (video, videoPtr->getVideoPlayCount()))
1095                                                 {
1096                                                         LoggerD("Error: set play count"); 
1097                                                         ret = false;
1098
1099                                                 }
1100                                         }
1101
1102                                         if (videoPtr->isChangedPlayedTime())
1103                                         {
1104                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1105                                                         video_meta_set_played_position (video, videoPtr->getVideoPlayedTime()))
1106                                                 {
1107                                                         LoggerD("Error: set played time"); 
1108                                                         ret = false;
1109                                                 }
1110                                         }
1111
1112                                         if (videoPtr->isChangedAlbum())
1113                                         {
1114                                                 LoggerD("Not support - Album API"); 
1115                                                 ret = false;
1116                                         }
1117
1118                                         if (videoPtr->isChangedArtists())
1119                                         {
1120                                                 LoggerD("Not support - Artist API"); 
1121                                                 ret = false;
1122                                         }
1123 #endif
1124                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1125                                                 media_info_set_latitude(media, videoPtr->getVideoLatitude())
1126                                                 )
1127                                         {
1128                                                 LoggerD("Error: set Latitude");
1129                                                 ret = false;
1130                                         }
1131
1132                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1133                                                 media_info_set_longitude(media, videoPtr->getVideoLongitude())
1134                                                 )
1135                                         {
1136                                                 LoggerD("Error: set Latitude");
1137                                                 ret = false;
1138                                         }
1139
1140
1141                                         if ( MEDIA_CONTENT_ERROR_NONE != video_meta_update_to_db (video) )
1142                                         {
1143                                                 LoggerD("Error: update db");
1144                                                 ret = false;
1145                                         }
1146
1147                                         if ( MEDIA_CONTENT_ERROR_NONE != video_meta_destroy(video))
1148                                         {
1149                                                 LoggerD("Error: destroy video meta");
1150                                                 ret = false;
1151                                         }
1152
1153                                         video = NULL;
1154
1155                                 }
1156                         }
1157
1158                 } //video
1159
1160
1161
1162                 if(type.compare("AUDIO") ==0 )
1163                 {
1164
1165                         MediacontentAudioPtr audioPtr = DPL::DynamicPointerCast<MediacontentAudio>(mediaPtr);
1166                         if (audioPtr && ( audioPtr->isChangedAudioArtist () ||  audioPtr->isChangedAudioAlbum() 
1167                                 || audioPtr->isChangedAudioGenre() ||audioPtr->isChangedAudioComposer()
1168                                 || audioPtr->isChangedAudioTrackNumber()) )
1169                         {
1170
1171                                 audio_meta_h audio=NULL;
1172                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(media, &audio))
1173                                 {
1174 #if 0 //NOT support
1175                                         if (  audioPtr->isChangedPlayCount() )
1176                                         {
1177                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1178                                                         audio_meta_set_played_count (audio, audioPtr->getAudioPlayCount()))
1179                                                 {
1180                                                         LoggerD("Error: set play count"); 
1181                                                         ret = false;
1182                                                 }
1183                                         }
1184
1185                                         if (audioPtr->isChangedPlayedTime())
1186                                         {
1187                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1188                                                         audio_meta_set_played_position (audio, audioPtr->getAudioPlayedTime()))
1189                                                 {
1190                                                         LoggerD("Error: set played time"); 
1191                                                         ret = false;
1192                                                 }
1193                                         }
1194
1195                                         if (audioPtr->isChangedAudioArtist())
1196                                         {
1197                                                 LoggerD("Not Support API"); 
1198                                                 ret = false;
1199                                         }
1200
1201                                         if (audioPtr->isChangedAudioAlbum())
1202                                         {
1203                                                 LoggerD("Not Support API"); 
1204                                                 ret = false;
1205                                         }
1206
1207                                         if (audioPtr->isChangedAudioGenre())
1208                                         {
1209                                                 LoggerD("Not Support API"); 
1210                                                 ret = false;
1211                                         }
1212
1213                                         if (audioPtr->isChangedAudioComposer())
1214                                         {
1215                                                 LoggerD("Not Support API"); 
1216                                                 ret = false;
1217                                         }
1218
1219                                         if (audioPtr->isChangedAudioTrackNumber())
1220                                         {
1221                                                 LoggerD("Not Support API"); 
1222                                                 ret = false;
1223                                         }
1224 #endif
1225                                         if ( MEDIA_CONTENT_ERROR_NONE != audio_meta_update_to_db (audio) )
1226                                         {
1227                                                 LoggerD("Error: update db");
1228                                                 ret = false;
1229                                         }
1230                                         
1231                                         if ( MEDIA_CONTENT_ERROR_NONE != audio_meta_destroy(audio))
1232                                         {
1233                                                 LoggerD("Error: destroy audio meta");
1234                                                 ret = false;
1235                                         }
1236                                         audio = NULL;
1237                                 }
1238
1239                         }
1240
1241                 }
1242                 
1243                 
1244                 //update media info
1245                 if ( MEDIA_CONTENT_ERROR_NONE != media_info_update_to_db(media))
1246                 {
1247                         LoggerD("Error: update to DB");
1248                         ret = false;
1249                 }
1250         }
1251
1252         if( media != NULL)
1253         {
1254                 if ( MEDIA_CONTENT_ERROR_NONE !=media_info_destroy(media))
1255                 {
1256                         LoggerD("Error: destroy media info");
1257                         ret = false;
1258                 }
1259         }
1260         return ret;
1261
1262 }
1263
1264 void MediacontentManager::OnRequestReceived(const IEventUpdateMediaPtr &eMedia)
1265 {
1266     LoggerD("OnRequestReceived::IEventUpdateMediaPtr entered");
1267         try
1268         {
1269                 MediacontentMediaPtr mediaPtr = eMedia->getMediaItem();
1270
1271                 if(updateMediaToDB(mediaPtr))
1272                 {
1273                         eMedia->setResult(true);
1274                         LoggerD("update success");
1275                 }
1276                 else
1277                 {
1278                         eMedia->setResult(false);
1279                         ThrowMsg(PlatformException, "DB operation is failed");
1280                 }
1281         }
1282     catch (const Exception &ex)
1283     {
1284         LogError("Exception: " << ex.DumpToString());
1285         eMedia->setResult(false);
1286     }
1287 }
1288
1289
1290 void MediacontentManager::OnRequestReceived(const IEventUpdateMediaItemsPtr &eItems)
1291 {
1292         LoggerD("OnRequestReceived::IEventUpdateMediaItemsPtr entered");
1293
1294         try
1295         {
1296                 MediacontentMediaListPtr mediaListPtr = eItems->getMediaItems();
1297                 if (mediaListPtr->empty()) 
1298                 { 
1299                     LoggerD("Item vector is empty");
1300                 }
1301                 else
1302                 {
1303                          for(unsigned int i=0; i<mediaListPtr->size(); i++)
1304                         {
1305                                 
1306                                 MediacontentMediaPtr mediaPtr = mediaListPtr->at(i);
1307                                 if(updateMediaToDB(mediaPtr))
1308                                 {
1309                                         eItems->setResult(true);
1310                                         LoggerD("update success");
1311                                 }
1312                                 else
1313                                 {
1314                                         eItems->setResult(false);
1315                                         ThrowMsg(PlatformException, "DB operation is failed");
1316                                 }
1317                                 
1318                         }
1319                 }
1320         }
1321         catch (const Exception &ex)
1322         {
1323                  LogError("Exception: " << ex.DumpToString());
1324                  eItems->setResult(false);
1325         }
1326         //eItems->setCancelAllowed(false);
1327         
1328 }
1329
1330 }
1331 }