27ca5b7e6e237f447e139e72129a257706bac722
[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;
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                 newMedia->setModifiedDate(toDateTm(tmpDate));
304         }
305
306         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_favorite (info, &tmpBool) )
307         {
308                 newMedia->setFavorite(tmpBool);
309                 LogDebug("favorite " << tmpBool);
310         }
311
312         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_rating  (info, &tmpInt) )
313         {
314                 newMedia->setRating(tmpInt);
315                 LogDebug("rating " << tmpInt);
316         }
317
318         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_size(info, &tmpLongLong) )
319         {
320                 newMedia->setSize(tmpLongLong);
321                 LogDebug("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;
348                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", &result) == NULL) {
349                                                 LogError( "Couldn't convert supplied date.");
350                                         }
351                                 
352                                         LogDebug("image_meta_get_date_taken : " <<  tmpStr); //time??
353                                         newImage->setReleasedDate(result);
354                                         free(tmpStr);
355                                         tmpStr = NULL;
356                                 }
357                         }
358
359                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_width(img, &tmpInt) )
360                         {
361                                 newImage->setImageWidth(tmpInt);
362                                 LogDebug("Image Width " << tmpInt);
363                         }
364
365                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_height(img, &tmpInt) )
366                         {
367                                 newImage->setImageHeight(tmpInt);
368                                 LogDebug("Image Height " << tmpInt);
369                         }
370
371                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble) )
372                         {
373                                 newImage->setImageLatitude(tmpDouble);
374                                 LogDebug("Image Latitude " << tmpDouble);
375                         }
376                         
377                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble) )
378                         {
379                                 newImage->setImageLongitude(tmpDouble);
380                                 LogDebug("Image Longitude " << tmpDouble);
381                         }
382                                         
383                         media_content_orientation_e orientation;
384                         
385                         if ( MEDIA_CONTENT_ERROR_NONE == image_meta_get_orientation(img, &orientation) )
386                         {       
387         
388                                 string orientationStr;
389                                 switch(orientation)
390                                 {
391                                         case 0:
392                                         case 1: 
393                                                 orientationStr = "NORMAL";
394                                                 break;
395                                         case 2: 
396                                                 orientationStr = "FLIP_HORIZONTAL";
397                                                 break;
398                                         case 3: 
399                                                 orientationStr = "ROTATE_180";
400                                                 break;
401                                         case 4: 
402                                                 orientationStr = "FLIP_VERTICAL";
403                                                 break;
404                                         case 5: 
405                                                 orientationStr = "TRANSPOSE";
406                                                 break;
407                                         case 6: 
408                                                 orientationStr = "ROTATE_90";
409                                                 break;
410                                         case 7: 
411                                                 orientationStr = "TRANSVERSE";
412                                                 break;
413                                         case 8: 
414                                                 orientationStr = "ROTATE_270";
415                                                 break;
416                                 }       
417                                 
418                                 newImage->setImageOrientation(orientationStr);
419                                 LogDebug(" image Orientation. " << orientationStr);
420
421                         }
422
423
424                         if ( METADATA_EXTRACTOR_ERROR_NONE != image_meta_destroy(img))
425                         {
426                                 LogDebug(" image_meta_destroy is fail... ");
427                         }
428                                                 
429                 }
430                 else {
431                         LogDebug("fetch Image Info failed..");                  
432                 }
433                         
434         }
435         else
436         {
437                 LogDebug("media info is NULL");
438         }
439 }
440
441 void MediacontentManager::readVideoFromMediaInfo( media_info_h info, MediacontentVideo* newVideo)
442 {       
443         char* tmpStr;
444         int tmpInt = 0;
445         double tmpDouble;
446         
447         if ( info) 
448         {
449                 newVideo->setMediaType("VIDEO");        
450
451                 readCommonDataFromMediaInfo(info, newVideo);    //set common media infomation
452
453                 video_meta_h video;
454                 
455                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_video(info, &video))
456                 {
457
458                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_recorded_date(video, &tmpStr) )
459                         {
460                                 if ( tmpStr )
461                                 {       
462                                         struct tm result;
463                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", &result) == NULL) {
464                                                 LogError( "Couldn't convert supplied date.");
465                                         }
466                                         newVideo->setReleasedDate(result);
467                                         
468                                         LogDebug("audio_meta_get_recorded_date : " << tmpStr);
469                                         free(tmpStr);
470                                         tmpStr = NULL;
471                                 }
472                         }
473                 
474                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_album(video, &tmpStr) )
475                         {
476                                 if( tmpStr )
477                                 {
478                                         newVideo->setVideoAlbum(tmpStr);
479                                         LogDebug(" Video Album. " << tmpStr);
480                                         free(tmpStr);
481                                         tmpStr = NULL;
482                                 }
483                         }
484
485                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_title(video, &tmpStr) )
486                         {
487                                 if( tmpStr )
488                                 {
489                                         newVideo->setTitle(tmpStr);
490                                         LogDebug(" Video Title. " << tmpStr);
491                                         free(tmpStr);
492                                         tmpStr = NULL;
493                                 }
494                         }
495
496                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_artist(video, &tmpStr) )
497                         {       
498                                 if ( tmpStr)
499                                 {
500                                         std::vector<std::string> artists = String::split(tmpStr, TAG_DELIMETER);
501                                         for( unsigned int i=0; i < artists.size(); i++)
502                                         {
503                                                 string artist = artists.at(i);
504                                                 LogDebug("Audio artist: " << artist);
505                                                 newVideo->appendVideoArtist(artist);
506                                         }
507                                         
508                                         free(tmpStr);
509                                         tmpStr = NULL;
510                                 }
511                         }
512
513                         
514                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble) )
515                         {
516                                 newVideo->setVideoLongitude(tmpDouble);
517                         }
518
519                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble) )
520                         {
521                                 newVideo->setVideoLatitude(tmpDouble);
522                         }
523
524                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_width(video, &tmpInt) )
525                         {
526                                 newVideo->setVideoWidth(tmpInt);
527                         }
528                         
529                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_height(video, &tmpInt) )
530                         {
531                                 newVideo->setVideoHeight(tmpInt);
532                         }
533
534                         if ( MEDIA_CONTENT_ERROR_NONE == video_meta_get_duration(video, &tmpInt) )
535                         {
536                                 newVideo->setVideoDuration(tmpInt);
537                         }
538
539                         if ( METADATA_EXTRACTOR_ERROR_NONE != video_meta_destroy(video))
540                         {
541                                 LogDebug(" video_meta_destroy is fail... ");
542                         }
543                                                 
544                 }
545                 else
546                 {
547                         LogDebug("fetch Video info fail...");
548                 }
549         }
550         else
551         {
552                 LogDebug("media info is NULL");
553         }
554 }
555
556
557 void MediacontentManager::readMusicFromMediaInfo( media_info_h info, MediacontentAudio* newAudio)
558 {       
559         char* tmpStr;
560         int tmpInt = 0;
561         unsigned long long tmpLongLong = 0;
562                 
563         if ( info) 
564         {
565                 newAudio->setMediaType("AUDIO");        
566                 
567                 readCommonDataFromMediaInfo(info, newAudio);    //set common media infomation
568
569                 audio_meta_h audio;
570                 
571                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(info, &audio))
572                 {       
573                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_recorded_date(audio, &tmpStr) )
574                         {
575                                 if ( tmpStr )
576                                 {       
577                                         struct tm result;
578                                         if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", &result) == NULL) {
579                                                 LogError( "Couldn't convert supplied date.");
580                                         }
581                                         newAudio->setReleasedDate(result);
582                                         
583                                         LogDebug("audio_meta_get_recorded_date : " << tmpStr);
584                                         free(tmpStr);
585                                         tmpStr = NULL;
586                                 }
587                         }
588
589                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_title(audio, &tmpStr) )
590                         {
591                                 if (tmpStr)
592                                 {
593                                         newAudio->setTitle(tmpStr);
594                                         LogDebug("Audio Title : " << tmpStr);
595                                         free(tmpStr);
596                                         tmpStr = NULL;
597                                 }
598                         }
599
600                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_album(audio, &tmpStr) )
601                         {
602                                 if (tmpStr)
603                                 {
604                                         newAudio->setAudioAlbum(tmpStr);
605                                         LogDebug("Audio Album : " << tmpStr);
606                                         free(tmpStr);
607                                         tmpStr = NULL;
608                                 }
609                         }
610
611                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_artist(audio, &tmpStr) )
612                         {       
613                                 if (tmpStr)
614                                 {
615                                         std::vector<std::string> artists = String::split(tmpStr, TAG_DELIMETER);
616                                         for( unsigned int i=0; i < artists.size(); i++)
617                                         {
618                                                 string artist = artists.at(i);
619                                                 LogDebug("Audio artist: " << artist);
620                                                 newAudio->appendAudioArtist(artist);
621                                         }
622                                         
623                                         free(tmpStr);
624                                         tmpStr = NULL;
625                                 }
626                         }
627                 
628                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_composer(audio, &tmpStr) )
629                         {
630                                 if (tmpStr)
631                                 {       
632                                         std::vector<std::string> composers = String::split(tmpStr, TAG_DELIMETER);
633                                         for( unsigned int i=0; i < composers.size(); i++)
634                                         {
635                                                 string composer = composers.at(i);
636                                                 LogDebug("Audio Composer : " << composer);
637                                                 newAudio->appendAudioComposer(composer);
638                                         }
639                                         
640                                         free(tmpStr);
641                                         tmpStr = NULL;
642                                 }
643                         }
644                         
645                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_duration(audio, &tmpInt) )
646                         {
647                                 newAudio->setAudioDuration(tmpInt);
648                                 LogDebug("Audio Duration : " << tmpInt);
649                         }
650
651                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_copyright(audio, &tmpStr) )
652                         {
653                                 if (tmpStr)
654                                 {
655                                         newAudio->setAudioCopyright(tmpStr);
656                                         LogDebug("Audio CopyRight: " << tmpStr);
657                                         free(tmpStr);
658                                         tmpStr = NULL;
659                                 }
660                         }
661                 
662                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_track_num(audio, &tmpStr) )
663                         {       
664                                 int trackNum = 0;
665                                 if (tmpStr)
666                                 {
667                                         LogDebug("trackNum: " << tmpStr);
668                                         try {
669                                                 istringstream(tmpStr) >> trackNum;
670                                         } catch (...) {
671                                                 LogDebug("Track Number type is wrong. (track number:" << tmpStr << ")");
672                                                 trackNum = 0;
673                                         }
674                         
675                                         newAudio->setAudioTrackNum(trackNum);
676                                         free(tmpStr);
677                                         tmpStr = NULL;
678                                 }
679                         }
680
681                         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_size(info, &tmpLongLong) )
682                         {       
683                                 LogDebug("size");
684                                 newAudio->setAudioSize(tmpLongLong);
685                                 LogDebug("Audio Size: " << tmpLongLong);
686                         }
687
688                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_bit_rate(audio, &tmpInt) )
689                         {       
690                                 LogDebug("bitrate");
691                                 newAudio->setAudioBitrate(tmpInt);
692                                 LogDebug("Audio Bitrate: " << tmpInt);
693                         }
694
695                         if ( MEDIA_CONTENT_ERROR_NONE == audio_meta_get_genre(audio, &tmpStr) )
696                         {       
697                                 LogDebug("gener");
698                                 if (tmpStr)
699                                 {
700                                         std::vector<std::string> genres = String::split(tmpStr, TAG_DELIMETER);
701
702                                         for( unsigned int i=0; i < genres.size(); i++)
703                                         {
704                                                 string genre = genres.at(i);
705                                                 LogDebug("Audio Genre: " << genre);
706                                                 newAudio->appendAudioGenre(genre);
707                                         }
708                                         
709                                         free(tmpStr);
710                                         tmpStr = NULL;
711                                 }
712                         }
713                                                 
714                         if ( METADATA_EXTRACTOR_ERROR_NONE != audio_meta_destroy(audio))
715                         {
716                                 LogDebug(" audio_meta_destroy is fail... ");
717                         }
718                         
719
720                 }
721                 else
722                 {
723                         LogDebug("fetch Music Info fail...");
724                 }
725                 
726                         
727         }
728         else
729         {
730                 LogDebug("media info is NULL");
731         }
732 }
733
734
735
736 //Callback.
737 bool MediacontentManager::mediaFolderCallback(media_folder_h folder, void *user_data)
738 {       
739         LogDebug("enter");
740         if (user_data != NULL){
741                 IEventFindFolder* event = (IEventFindFolder*)user_data;
742                 LogDebug("user data is valide");
743                 if ( folder )
744                 {
745                         LogDebug("folder is valide");
746                         MediacontentFolderPtr newFolder(new MediacontentFolder());
747                         convertToPlatformFolder(folder, newFolder);
748                         LogDebug("folder type" << newFolder->getFolderStorageType());
749                         
750                         event->addFolder(newFolder);
751                 }
752         }
753         else
754         {
755                 LogDebug("user data is NULL");
756         }
757         
758         return true;
759
760 }
761
762 bool MediacontentManager::mediaItemCallback(media_info_h info, void* user_data)
763 {
764         if (user_data != NULL){
765                 IEventBrowseFolder* event = (IEventBrowseFolder*)user_data;
766
767                 media_content_type_e type;
768                 
769                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
770                 {
771                         if ( type == MEDIA_CONTENT_TYPE_IMAGE)
772                         {
773                                 MediacontentImage *newImage(new MediacontentImage());
774                                 readImageFromMediaInfo(info, newImage);
775                                 event->addMedia(newImage);
776                         }
777                         else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
778                         {
779                                 MediacontentVideo *newVideo(new MediacontentVideo());
780                                 readVideoFromMediaInfo(info, newVideo);
781                                 event->addMedia(newVideo);
782                         }
783                         else if ( type == MEDIA_CONTENT_TYPE_MUSIC)
784                         {
785                                 MediacontentAudio *newVAudio(new MediacontentAudio());
786                                 readMusicFromMediaInfo(info, newVAudio);
787                                 event->addMedia(newVAudio);
788                         }
789                         else if( type == MEDIA_CONTENT_TYPE_OTHERS)
790                         {
791                                 MediacontentMedia *newMedia(new MediacontentMedia());
792
793                                 newMedia->setMediaType("OTHER");
794                                 readCommonDataFromMediaInfo(info, newMedia);
795                                 event->addMedia(newMedia);
796                         }
797
798                 }
799         }
800         else 
801         {
802                 LogDebug("event is NULL");      
803         }
804
805         return true;
806 }
807
808 void MediacontentManager::OnRequestReceived(const IEventFindFolderPtr &eFolder)
809 {
810         LogDebug("entered");
811                 
812         if ( MEDIA_CONTENT_ERROR_NONE != 
813                 media_folder_foreach_folder_from_db (NULL, mediaFolderCallback, eFolder.Get()))
814         {       
815                 LogError("error ( media_folder_foreach_folder_from_db ) : ");
816                 eFolder->setResult(false);
817         }
818         else
819         {
820                 eFolder->setResult(true);
821         }
822
823
824         LogDebug("end");
825 }
826
827 void MediacontentManager::OnRequestReceived(const IEventBrowseFolderPtr &eBrowse)
828 {
829         LogDebug("OnRequestReceived::IEventBrowseFolderPtr entered");
830
831         MediaSearchVisitorPtr visitor(new MediaSearchVisitor());
832         visitor->setQueryType(MediaSearchVisitor::QUERY_MEDIA);
833         int ret = MEDIA_CONTENT_ERROR_NONE;
834
835         try{
836                 filter_h filter = NULL;
837
838                 if(eBrowse->getFilterIsSet() || eBrowse->getFolderIdIsSet() || eBrowse->getSortModesIsSet()
839                         || eBrowse->getLimitIsSet() ||eBrowse->getOffsetIsSet() )
840                 {
841                         //set filter
842                         if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
843                         {
844                                 if (eBrowse->getFilterIsSet())
845                                 {
846                                         string condition;
847                                         FilterPtr jsFilter = eBrowse->getFilter();
848
849                                         FilterValidatorPtr validator = MediaFilterValidatorFactory::getMediaFilterValidator(MediaFilterValidatorFactory::QUERY_MEDIA);
850                                         bool success = jsFilter->validate(validator);
851
852                                         if(!success)
853                                                 ThrowMsg(PlatformException, "Invalid attirbutes.");
854                                         
855                                         IFilterVisitorPtr IVisitor = DPL::StaticPointerCast<IFilterVisitor>(visitor);
856
857                                         jsFilter->travel(IVisitor);
858                                         condition = visitor->getResult();
859
860                                         LogDebug("execute condition [" << condition << "]");
861                                         media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT );  //set condition
862                                 }
863
864                                 if(eBrowse->getSortModesIsSet()) 
865                                 {
866                                         media_content_order_e order;
867
868                                         SortModePtr attr = eBrowse->getSortMode();
869
870                                         if ( attr )
871                                         {
872                                                 string attriName = attr->getAttributeName();
873                                                 attriName = visitor->getPlatformAttr(attriName);
874                                                 LogDebug("Attribute Name [" << attriName << "]");
875
876                                                 if (attriName.compare("") != 0) 
877                                                 {       
878                                                         if (attr->getOrder() == DeviceAPI::Tizen::ASCENDING_SORT_ORDER) 
879                                                         {
880                                                                 order = MEDIA_CONTENT_ORDER_ASC;
881                                                         } 
882                                                         else 
883                                                         {
884                                                                 order = MEDIA_CONTENT_ORDER_DESC;
885                                                         }
886
887                                                         if ( MEDIA_CONTENT_ERROR_NONE !=
888                                                                 media_filter_set_order(filter, order, attriName.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT ))               //set order
889                                                         {
890                                                                 LogDebug("media_filter_set_order fail...");     
891                                                         }
892                                                 }
893                                         }
894                                         
895                                 }                               
896
897                                 if (eBrowse->getLimitIsSet() ||eBrowse->getOffsetIsSet() ) 
898                                 {       
899                                         LogDebug("limit or offset");
900                                         
901                                         int count = -1;
902                                         int offset = 0;
903                                         
904                                         if ( eBrowse->getLimitIsSet() )
905                                         {
906                                                 count =  eBrowse->getLimit();
907                                                 LogDebug("limit is " << count);
908                                         }
909                                         
910                                         if ( eBrowse->getOffsetIsSet() )
911                                         {
912                                                 offset = eBrowse->getOffset();
913                                                 LogDebug("offset is " << offset);
914                                         }
915                                         
916                                         if ( MEDIA_CONTENT_ERROR_NONE != media_filter_set_offset(filter, offset, count))
917                                         {
918                                                 LogDebug("set limit or offset fail...");
919                                         }
920                                 }
921                                 
922                         }
923                         else
924                         {
925                                 
926                                 LogError("error ( media filter create ) : ");
927                                 eBrowse->setResult(false);
928                         }
929                 }
930
931                 if ( eBrowse->getFolderIdIsSet())
932                 {       
933                         string folderID = eBrowse->getFolderID();
934                         LogDebug("FolderID :" << folderID);
935                         
936                         if ( MEDIA_CONTENT_ERROR_NONE !=  
937                                 media_folder_foreach_media_from_db (folderID.c_str(), filter, mediaItemCallback, eBrowse.Get()))
938                         {       
939                                 LogError("error ( media_folder_foreach_folder_from_db ) : " << ret);
940                                 eBrowse->setResult(false);
941                         }
942                         else
943                         {       
944                                 eBrowse->setResult(true);
945                         }
946                 }
947                 else
948                 {
949                         if ( MEDIA_CONTENT_ERROR_NONE !=  
950                                 media_info_foreach_media_from_db (filter, mediaItemCallback, eBrowse.Get()))
951                         {
952                                 LogError("error ( media_folder_foreach_folder_from_db ) : " << ret);
953                                 eBrowse->setResult(false);
954                         }
955                         else
956                         {
957                                 eBrowse->setResult(true);
958                         }
959                 }
960
961                 //destory Filter
962                 if(filter)
963                 {
964                         if ( MEDIA_CONTENT_ERROR_NONE != media_filter_destroy(filter))
965                         {
966                                 LogError("media_filter_create Error: " << ret);
967                         }
968                 }
969
970         }
971         catch(const Exception &ex){
972                         LogError("Exception: " << ex.DumpToString());
973                         eBrowse->setResult(false);
974                         return;
975         }
976
977
978
979 }
980
981 bool MediacontentManager::updateMediaToDB(MediacontentMediaPtr mediaPtr)
982 {
983         string type  = mediaPtr->getMediaType();        //media type.
984         string mediaId = mediaPtr->getMediaUUID();
985
986         LogDebug("MediaId : " << mediaId);
987         media_info_h media = NULL;
988
989         bool ret = true;
990
991         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_from_db(mediaId.c_str(), &media))
992         {
993                 if ( mediaPtr->isChangedRating())
994                 {
995                         if ( MEDIA_CONTENT_ERROR_NONE
996                                 != media_info_set_rating(media, mediaPtr->getRating()))
997                         {
998                                 LogDebug("Error: set rating");
999                         }
1000                 }
1001                 if ( mediaPtr->isChangedDescription() )
1002                 {
1003                         if ( MEDIA_CONTENT_ERROR_NONE
1004                                 != media_info_set_description (media, mediaPtr->getDescription().c_str()))
1005                         {
1006                                 LogDebug("Error: set description");
1007                         }
1008                 }
1009                 if( mediaPtr->isChangedDisplayName())
1010                 {
1011                         if ( MEDIA_CONTENT_ERROR_NONE
1012                                 != media_info_set_display_name (media, mediaPtr->getDisplayName().c_str()))
1013                         {
1014                                 LogDebug("Error: set displayname");
1015                         }
1016                 }
1017
1018                 if(type.compare("IMAGE") ==0 )
1019                 {
1020                         MediacontentImagePtr imagePtr = DPL::DynamicPointerCast<MediacontentImage>(mediaPtr);
1021
1022                         if(imagePtr)
1023                         {
1024                                 image_meta_h img=NULL;
1025                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_image(media, &img))
1026                                 {
1027                                         if(imagePtr->isChangedOrientaion())
1028                                         {
1029                                                 if ( MEDIA_CONTENT_ERROR_NONE !=
1030                                                         image_meta_set_orientation (img, convertToOrientation(imagePtr->getImageOrientation())))
1031                                                 {
1032                                                         LogDebug("Error: set orientation");
1033                                                         ret = false;
1034                                                 }
1035                                         }
1036
1037                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1038                                                 media_info_set_latitude(media, imagePtr->getImageLatitude())
1039                                                 )
1040                                         {
1041                                                 LogDebug("Error: set Latitude");
1042                                                 ret = false;
1043                                         }
1044
1045                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1046                                                 media_info_set_longitude(media, imagePtr->getImageLongitude())
1047                                                 )
1048                                         {
1049                                                 LogDebug("Error: set Longitude");
1050                                                 ret = false;
1051                                         }
1052
1053                                         if ( MEDIA_CONTENT_ERROR_NONE != image_meta_update_to_db (img) )
1054                                         {
1055                                                 LogDebug("Error: update db");
1056                                                 ret = false;
1057                                         }
1058
1059                                         if ( MEDIA_CONTENT_ERROR_NONE != image_meta_destroy(img))
1060                                         {
1061                                                 LogDebug("Error: destroy media info");
1062                                                 ret = false;
1063                                         }
1064
1065                                         img = NULL;
1066
1067                                 }
1068                                 else
1069                                 {
1070                                         LogDebug("Error: get Image from DB");
1071                                         ret = false;
1072                                 }
1073                         }
1074
1075                 }
1076
1077                 if(type.compare("VIDEO") ==0 )
1078                 {
1079                         MediacontentVideoPtr videoPtr = DPL::DynamicPointerCast<MediacontentVideo>(mediaPtr);
1080                         if (videoPtr)
1081                         {
1082
1083                                 video_meta_h video = NULL;
1084                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_video(media, &video))
1085                                 {
1086 #if 0 //NOT support
1087                                         if (  videoPtr->isChangedPlayCount() )
1088                                         {
1089                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1090                                                         video_meta_set_played_count (video, videoPtr->getVideoPlayCount()))
1091                                                 {
1092                                                         LogDebug("Error: set play count"); 
1093                                                         ret = false;
1094
1095                                                 }
1096                                         }
1097
1098                                         if (videoPtr->isChangedPlayedTime())
1099                                         {
1100                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1101                                                         video_meta_set_played_position (video, videoPtr->getVideoPlayedTime()))
1102                                                 {
1103                                                         LogDebug("Error: set played time"); 
1104                                                         ret = false;
1105                                                 }
1106                                         }
1107
1108                                         if (videoPtr->isChangedAlbum())
1109                                         {
1110                                                 LogDebug("Not support - Album API"); 
1111                                                 ret = false;
1112                                         }
1113
1114                                         if (videoPtr->isChangedArtists())
1115                                         {
1116                                                 LogDebug("Not support - Artist API"); 
1117                                                 ret = false;
1118                                         }
1119 #endif
1120                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1121                                                 media_info_set_latitude(media, videoPtr->getVideoLatitude())
1122                                                 )
1123                                         {
1124                                                 LogDebug("Error: set Latitude");
1125                                                 ret = false;
1126                                         }
1127
1128                                         if ( MEDIA_CONTENT_ERROR_NONE != 
1129                                                 media_info_set_longitude(media, videoPtr->getVideoLongitude())
1130                                                 )
1131                                         {
1132                                                 LogDebug("Error: set Latitude");
1133                                                 ret = false;
1134                                         }
1135
1136
1137                                         if ( MEDIA_CONTENT_ERROR_NONE != video_meta_update_to_db (video) )
1138                                         {
1139                                                 LogDebug("Error: update db");
1140                                                 ret = false;
1141                                         }
1142
1143                                         if ( MEDIA_CONTENT_ERROR_NONE != video_meta_destroy(video))
1144                                         {
1145                                                 LogDebug("Error: destroy video meta");
1146                                                 ret = false;
1147                                         }
1148
1149                                         video = NULL;
1150
1151                                 }
1152                         }
1153
1154                 } //video
1155
1156
1157
1158                 if(type.compare("AUDIO") ==0 )
1159                 {
1160
1161                         MediacontentAudioPtr audioPtr = DPL::DynamicPointerCast<MediacontentAudio>(mediaPtr);
1162                         if (audioPtr && ( audioPtr->isChangedAudioArtist () ||  audioPtr->isChangedAudioAlbum() 
1163                                 || audioPtr->isChangedAudioGenre() ||audioPtr->isChangedAudioComposer()
1164                                 || audioPtr->isChangedAudioTrackNumber()) )
1165                         {
1166
1167                                 audio_meta_h audio=NULL;
1168                                 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(media, &audio))
1169                                 {
1170 #if 0 //NOT support
1171                                         if (  audioPtr->isChangedPlayCount() )
1172                                         {
1173                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1174                                                         audio_meta_set_played_count (audio, audioPtr->getAudioPlayCount()))
1175                                                 {
1176                                                         LogDebug("Error: set play count"); 
1177                                                         ret = false;
1178                                                 }
1179                                         }
1180
1181                                         if (audioPtr->isChangedPlayedTime())
1182                                         {
1183                                                 if ( MEDIA_CONTENT_ERROR_NONE != 
1184                                                         audio_meta_set_played_position (audio, audioPtr->getAudioPlayedTime()))
1185                                                 {
1186                                                         LogDebug("Error: set played time"); 
1187                                                         ret = false;
1188                                                 }
1189                                         }
1190
1191                                         if (audioPtr->isChangedAudioArtist())
1192                                         {
1193                                                 LogDebug("Not Support API"); 
1194                                                 ret = false;
1195                                         }
1196
1197                                         if (audioPtr->isChangedAudioAlbum())
1198                                         {
1199                                                 LogDebug("Not Support API"); 
1200                                                 ret = false;
1201                                         }
1202
1203                                         if (audioPtr->isChangedAudioGenre())
1204                                         {
1205                                                 LogDebug("Not Support API"); 
1206                                                 ret = false;
1207                                         }
1208
1209                                         if (audioPtr->isChangedAudioComposer())
1210                                         {
1211                                                 LogDebug("Not Support API"); 
1212                                                 ret = false;
1213                                         }
1214
1215                                         if (audioPtr->isChangedAudioTrackNumber())
1216                                         {
1217                                                 LogDebug("Not Support API"); 
1218                                                 ret = false;
1219                                         }
1220 #endif
1221                                         if ( MEDIA_CONTENT_ERROR_NONE != audio_meta_update_to_db (audio) )
1222                                         {
1223                                                 LogDebug("Error: update db");
1224                                                 ret = false;
1225                                         }
1226                                         
1227                                         if ( MEDIA_CONTENT_ERROR_NONE != audio_meta_destroy(audio))
1228                                         {
1229                                                 LogDebug("Error: destroy audio meta");
1230                                                 ret = false;
1231                                         }
1232                                         audio = NULL;
1233                                 }
1234
1235                         }
1236
1237                 }
1238                 
1239                 
1240                 //update media info
1241                 if ( MEDIA_CONTENT_ERROR_NONE != media_info_update_to_db(media))
1242                 {
1243                         LogDebug("Error: update to DB");
1244                         ret = false;
1245                 }
1246         }
1247
1248         if( media != NULL)
1249         {
1250                 if ( MEDIA_CONTENT_ERROR_NONE !=media_info_destroy(media))
1251                 {
1252                         LogDebug("Error: destroy media info");
1253                         ret = false;
1254                 }
1255         }
1256         return ret;
1257
1258 }
1259
1260 void MediacontentManager::OnRequestReceived(const IEventUpdateMediaPtr &eMedia)
1261 {
1262     LogDebug("OnRequestReceived::IEventUpdateMediaPtr entered");
1263         try
1264         {
1265                 MediacontentMediaPtr mediaPtr = eMedia->getMediaItem();
1266
1267                 if(updateMediaToDB(mediaPtr))
1268                 {
1269                         eMedia->setResult(true);
1270                         LogDebug("update success");
1271                 }
1272                 else
1273                 {
1274                         eMedia->setResult(false);
1275                         ThrowMsg(PlatformException, "DB operation is failed");
1276                 }
1277         }
1278     catch (const Exception &ex)
1279     {
1280         LogError("Exception: " << ex.DumpToString());
1281         eMedia->setResult(false);
1282     }
1283 }
1284
1285
1286 void MediacontentManager::OnRequestReceived(const IEventUpdateMediaItemsPtr &eItems)
1287 {
1288         LogDebug("OnRequestReceived::IEventUpdateMediaItemsPtr entered");
1289
1290         try
1291         {
1292                 MediacontentMediaListPtr mediaListPtr = eItems->getMediaItems();
1293                 if (mediaListPtr->empty()) 
1294                 { 
1295                     LogDebug("Item vector is empty");
1296                 }
1297                 else
1298                 {
1299                          for(unsigned int i=0; i<mediaListPtr->size(); i++)
1300                         {
1301                                 
1302                                 MediacontentMediaPtr mediaPtr = mediaListPtr->at(i);
1303                                 if(updateMediaToDB(mediaPtr))
1304                                 {
1305                                         eItems->setResult(true);
1306                                         LogDebug("update success");
1307                                 }
1308                                 else
1309                                 {
1310                                         eItems->setResult(false);
1311                                         ThrowMsg(PlatformException, "DB operation is failed");
1312                                 }
1313                                 
1314                         }
1315                 }
1316         }
1317         catch (const Exception &ex)
1318         {
1319                  LogError("Exception: " << ex.DumpToString());
1320                  eItems->setResult(false);
1321         }
1322         //eItems->setCancelAllowed(false);
1323         
1324 }
1325
1326 }
1327 }