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