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