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