upload tizen1.0 source
[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 "MediaSearchVisitor.h"
24
25 #include <mm_file.h>
26 #include <mm_error.h>
27 #include <time.h>
28
29 using namespace TizenApis::Api::Mediacontent;
30 using namespace WrtDeviceApis::Commons;
31
32 #define MEDIA_DEBUG 0
33
34 const string SELECT_FOLDER_QUERY = 
35         "select folder_uuid, path, title, storage_type,modified_date from (select folder_uuid, path, folder_name as title, storage_type,modified_date from visual_folder where valid=1 union select folder_uuid, path, folder_name as title, storage_type,modified_date from audio_folder) where 1  ";
36
37 const string VIEW_MEDIA_QUERY_WITH_FOLDER =
38         "select visual_uuid as media_uuid, folder_uuid, content_type, display_name, path, thumbnail_path,created_date, released_date, modified_date, description,rating, album, artist, last_played_time, played_count,longitude, latitude, width, height, orientation, genre, author, duration,size from \
39         (select visual_uuid,folder_uuid, content_type, display_name, path, thumbnail_path,null as created_date, released_date, modified_date, description,rating, album, artist, last_played_time, null as played_count,longitude, latitude, width, height, orientation,null as genre, null as author, duration,size from visual_media join \
40                 (select * from \
41                         (select visual_uuid as meta_uuid, longitude, latitude, description, width, height, datetaken as released_date, orientation,null as album, null as artist, null as last_played_time,null as duration from image_meta \
42                         union all \
43                         select visual_uuid as meta_uuid, longitude, latitude, description, width, height, datetaken as released_date, null as orientation, album, artist, last_played_time,duration from video_meta)) \
44         on visual_uuid=meta_uuid \
45         union all \
46                 select audio_uuid, folder_uuid as folder_uuid, content_type, title as display_name, path, thumbnail_path, added_time, year, modified_date, description, rating, album, artist, last_played_time, played_count, null as longitude, null as latitude, null as width, null as height, null as orientation, genre, author, duration, size from audio_media) where 1 ";
47
48 const string SELECT_MEDIA_ID_WITH_FOLDER = 
49         "select item_id from item_view where folder_uuid=";
50 const string SELECT_EXTRA_AUDIO = 
51         "select copyright,bitrate,track_num,size from audio_media where audio_uuid=";
52
53
54 namespace TizenApis {
55 namespace Platform {
56 namespace Mediacontent{
57
58 int Mediacontent::db_connnect_count = 0;
59 sqlite3* Mediacontent::hDBCt = NULL;
60
61 Mediacontent::Mediacontent()
62 {
63     LogDebug("entered" + Mediacontent::db_connnect_count);
64
65         if(Mediacontent::db_connnect_count == 0)
66         {
67                 if( SQLITE_OK != db_util_open(MEDIA_DB_PATH, &Mediacontent::hDBCt, 0))
68                 {
69                         return;
70                 }
71         }
72         Mediacontent::db_connnect_count++;              
73 }
74
75 Mediacontent::~Mediacontent()
76 {
77     LogDebug("entered");
78         int ret;
79         Mediacontent::db_connnect_count--;
80         if(Mediacontent::db_connnect_count < 1)
81         {
82                 ret = db_util_close(Mediacontent::hDBCt);
83                 if(ret != SQLITE_OK)
84                 {
85                         ret = -1;
86                         LogDebug("DB disconnection is failed");
87                 }
88                 else
89                 {
90                         Mediacontent::hDBCt = NULL;
91                         LogDebug("DB connection is ok");
92                 }
93                 Mediacontent::db_connnect_count = 0;
94         }
95  
96 }
97
98 tm Mediacontent::toDateTm(time_t date)
99 {
100         tm tm_date;
101
102         localtime_r(&date, &tm_date);
103
104         return tm_date;
105 }       
106
107
108 void Mediacontent::convertToPlatformFolder(folder_s &media_folder, MediacontentFolderPtr& newFolder)
109 {
110         if(media_folder.folder_uuid != NULL)
111         {
112                 newFolder->setFolderUUID(media_folder.folder_uuid);
113         }
114         if(media_folder.folder_path != NULL)
115         {
116                 newFolder->setFolderName(media_folder.folder_name);
117         }
118         if(media_folder.folder_name != NULL)
119         {
120                 newFolder->setFolderPath(media_folder.folder_path);
121         }
122
123         newFolder->setFolderModifiedDate(toDateTm(media_folder.modified_date));
124
125         string storageType;
126         
127         if(media_folder.storage_type == 0)
128         {
129                 storageType = "INTERNAL";
130         }
131         else if( media_folder.storage_type == 1)
132         {
133                 storageType = "EXTERNAL";
134         }
135         else
136         {
137                 storageType = "UNKNOWN";
138         }
139         newFolder->setFolderStorageType(storageType);
140 }
141
142
143
144 string Mediacontent::makeQuerySortMode(SortModeArrayPtr attr)
145 {
146         string query("");
147         string attriName;
148         int cnt = 0;
149         SortModeArray::iterator it = attr->begin();
150         
151         for (;it!=attr->end(); ++it) 
152         {
153                 attriName = (*it)->getAttributeName();
154                 //convert to attribue's name
155                 MediaSearchVisitor visitor;
156                 attriName = visitor.getPlatformAttr(attriName);
157                 if (attriName.compare("") != 0) 
158                 { 
159                         if (cnt == 0) 
160                         {
161                                 query.append(" ORDER BY ");
162                         } else 
163                         {
164                                 query.append(", ");
165                         }
166                         query.append(attriName);
167
168                         if ((*it)->getOrder() == Api::Tizen::ASCENDING_SORT_ORDER) 
169                         {
170                                 query.append(" ASC");
171                         } 
172                         else 
173                         {
174                                 query.append(" DESC");
175                         }
176                         cnt++;
177                 }
178         }
179
180         return query;
181 }
182
183
184
185 void Mediacontent::OnRequestReceived(const IEventFindFolderPtr &eFolder)
186 {
187     LogDebug("entered");
188
189         string projection;
190         string sortMode;
191         string limitOffset;
192         string condition;
193         string query(SELECT_FOLDER_QUERY);
194
195         //set condition
196         MediaSearchVisitorPtr visitor(new MediaSearchVisitor());
197
198         try{
199                 if(eFolder->getFilterIsSet())
200                 {
201                         FilterPtr filter = eFolder->getFilter();
202
203                         IFilterVisitorPtr IVisitor = DPL::StaticPointerCast<IFilterVisitor>(visitor);
204
205                         visitor->setQueryType(MediaSearchVisitor::QUERY_FOLDER);
206                         filter->travel(IVisitor);
207                         condition = visitor->getResult();
208
209                         query.append(" and ");
210                         query.append(condition);
211                 }
212         }
213         catch(const Exception &ex){
214         LogError("Exception: " << ex.DumpToString());
215         eFolder->setResult(false);
216         return;
217         }
218
219         if(eFolder->getSortModesIsSet()) 
220         {
221                 sortMode = makeQuerySortMode(eFolder->getSortModes());  
222
223                 query.append(sortMode);
224         }
225         
226         //set limit/offset
227         if (eFolder->getLimitIsSet()) 
228         {
229                 limitOffset.append(" LIMIT ");
230                 std::stringstream limitStream;
231                 limitStream << eFolder->getLimit();
232                 limitOffset.append(limitStream.str());
233                 if(eFolder->getOffsetIsSet()) 
234                 {
235                         limitOffset.append(" OFFSET ");
236                         std::stringstream offsetStream;
237                         offsetStream << eFolder->getOffset();
238                         limitOffset.append(offsetStream.str());
239                 }
240
241                 query.append(limitOffset);
242         }
243
244         LogDebug("execute condition [" << condition << "]");
245         LogDebug("execute projection [" << projection << "]");
246         LogDebug("execute sortMode [" << sortMode << "]");
247         LogDebug("execute limitOffset [" << limitOffset << "]");        
248
249         try
250         {
251                 sqlite3_stmt* pStmt = NULL;
252                 folder_s media_folder;
253                 sqlite3_prepare_v2(Mediacontent::hDBCt, (char *)(query.c_str()), strlen((char *)(query.c_str())), &pStmt, NULL);
254                 while( sqlite3_step(pStmt) == SQLITE_ROW)
255                 {
256                         media_folder.folder_uuid = (char *)sqlite3_column_text(pStmt, 0);
257                         media_folder.folder_path = (char *)sqlite3_column_text(pStmt, 1);
258                         media_folder.folder_name = (char *)sqlite3_column_text(pStmt, 2);
259                         media_folder.storage_type = sqlite3_column_int(pStmt, 3);
260                         media_folder.modified_date = sqlite3_column_int(pStmt, 4);      
261
262                         MediacontentFolderPtr newFolder(new MediacontentFolder());
263                         convertToPlatformFolder(media_folder, newFolder);
264
265                         eFolder->addFolder(newFolder);
266                 }
267                 sqlite3_finalize(pStmt);
268                 eFolder->setResult(true);
269
270         }
271         catch (const Exception &ex)
272         {
273                 LogError("Exception: " << ex.DumpToString());
274                 eFolder->setResult(false);
275         }
276         LogDebug("dykim:end");
277 }
278
279
280 void Mediacontent::OnRequestReceived(const IEventFindMediaPtr &eMedia)
281 {
282     LogDebug("OnRequestReceived::IEventFindMediaPtr entered");
283
284         string projection;
285         string sortMode;
286         string limitOffset;
287         string condition;
288         
289         string query(VIEW_MEDIA_QUERY_WITH_FOLDER);
290
291         MediaSearchVisitorPtr visitor(new MediaSearchVisitor());
292
293         try
294         {
295                 if(eMedia->getFilterIsSet())
296                 {
297                         FilterPtr filter = eMedia->getFilter();
298
299                         IFilterVisitorPtr IVisitor = DPL::StaticPointerCast<IFilterVisitor>(visitor);
300
301                         visitor->setQueryType(MediaSearchVisitor::QUERY_MEDIA);
302                         filter->travel(IVisitor);
303                         condition = visitor->getResult();
304                         query.append(" and ");
305                         query.append(condition);                
306                 }
307         }
308         catch(const Exception &ex){
309         LogError("Exception: " << ex.DumpToString());
310         eMedia->setResult(false);
311         return;
312         }               
313
314         if(eMedia->getSortModesIsSet()) 
315         {
316                 sortMode = makeQuerySortMode(eMedia->getSortModes());   
317
318                 query.append(sortMode);
319         }
320         if (eMedia->getLimitIsSet()) 
321         {
322                 limitOffset.append(" LIMIT ");
323                 std::stringstream limitStream;
324                 limitStream << eMedia->getLimit();
325                 limitOffset.append(limitStream.str());
326                 if(eMedia->getOffsetIsSet()) 
327                 {
328                         limitOffset.append(" OFFSET ");
329                         std::stringstream offsetStream;
330                         offsetStream << eMedia->getOffset();
331                         limitOffset.append(offsetStream.str());
332                 }
333
334                 query.append(limitOffset);
335         }
336         
337         LogDebug("execute condition [" << condition << "]");
338         LogDebug("execute projection [" << projection << "]");
339         LogDebug("execute sortMode [" << sortMode << "]");
340         LogDebug("execute limitOffset [" << limitOffset << "]");        
341
342         int ret;
343         
344     try
345         {
346                 sqlite3_stmt* pStmt = NULL;
347                 int content_type;
348                 ret = sqlite3_prepare_v2(Mediacontent::hDBCt, (char *)(query.c_str()), query.length(), &pStmt, NULL);
349
350                 while( sqlite3_step(pStmt) == SQLITE_ROW)
351                 {
352
353                         content_type = sqlite3_column_int(pStmt, 2);    
354
355                         if(content_type == MEDIA_TYPE_IMAGE)
356                         {
357
358                                 MediacontentImage *newImage(new MediacontentImage());
359                                 readImageFromDB(pStmt,newImage);
360                                 eMedia->addMedia(newImage);
361                         }
362                         else if(content_type == MEDIA_TYPE_VIDEO)
363                         {
364                                 MediacontentVideo *newVideo(new MediacontentVideo());
365                                 readVideoFromDB(pStmt,newVideo);
366                                 eMedia->addMedia(newVideo);
367                         }
368                         else if(content_type == MEDIA_TYPE_AUDIO)
369                         {
370                                 MediacontentAudio *newVAudio(new MediacontentAudio());
371                                 readAudioFromDB(pStmt,newVAudio);
372                                 eMedia->addMedia(newVAudio);
373
374                         }
375
376                 }
377                 sqlite3_finalize(pStmt);
378                 eMedia->setResult(true);
379
380         }
381     catch (const Exception &ex)
382     {
383         LogError("Exception: " << ex.DumpToString());
384         eMedia->setResult(false);
385     }
386 }
387
388 void Mediacontent::OnRequestReceived(const IEventBrowseFolderPtr &eBrowse)
389 {
390     LogDebug("OnRequestReceived::IEventFindMediaPtr entered");
391
392         string projection;
393         string sortMode;
394         string limitOffset;
395         string condition;
396         
397         string query(VIEW_MEDIA_QUERY_WITH_FOLDER);
398
399         if(eBrowse->getFolderIdIsSet())
400         {
401                 std::stringstream folderStream;
402                 folderStream <<" and folder_uuid='" << eBrowse->getFolderID() <<"' ";
403                 query.append(folderStream.str());
404         }               
405
406         MediaSearchVisitorPtr visitor(new MediaSearchVisitor());
407
408         try{
409                 if(eBrowse->getFilterIsSet())
410                 {
411                         FilterPtr filter = eBrowse->getFilter();
412
413                         // FIXME validator have to be placed at JS binding.
414                         FilterValidatorPtr validator = MediaFilterValidatorFactory::getMediaFilterValidator(MediaFilterValidatorFactory::QUERY_MEDIA);
415                         bool success = filter->validate(validator);
416
417                         if(!success)
418                                 ThrowMsg(PlatformException, "Invalid attirbutes.");
419
420                         IFilterVisitorPtr IVisitor = DPL::StaticPointerCast<IFilterVisitor>(visitor);
421
422                         visitor->setQueryType(MediaSearchVisitor::QUERY_MEDIA);
423                         filter->travel(IVisitor);
424                         condition = visitor->getResult();
425                         query.append(" and ");
426                         query.append(condition);                
427                 }
428         }
429         catch(const Exception &ex){
430         LogError("Exception: " << ex.DumpToString());
431         eBrowse->setResult(false);
432         return;
433         }
434
435         if(eBrowse->getSortModesIsSet()) 
436         {
437                 sortMode = makeQuerySortMode(eBrowse->getSortModes());  
438
439                 query.append(sortMode);
440         }
441         if (eBrowse->getLimitIsSet()) 
442         {
443                 limitOffset.append(" LIMIT ");
444                 std::stringstream limitStream;
445                 limitStream << eBrowse->getLimit();
446                 limitOffset.append(limitStream.str());
447                 if(eBrowse->getOffsetIsSet()) 
448                 {
449                         limitOffset.append(" OFFSET ");
450                         std::stringstream offsetStream;
451                         offsetStream << eBrowse->getOffset();
452                         limitOffset.append(offsetStream.str());
453                 }
454
455                 query.append(limitOffset);
456         }
457         
458         LogDebug("execute condition [" << condition << "]");
459         LogDebug("execute projection [" << projection << "]");
460         LogDebug("execute sortMode [" << sortMode << "]");
461         LogDebug("execute limitOffset [" << limitOffset << "]");        
462         LogDebug("execute query [" << query << "]");
463         
464         int ret;
465     try
466         {
467                 sqlite3_stmt* pStmt = NULL;
468                 int content_type;
469                 ret = sqlite3_prepare_v2(Mediacontent::hDBCt, (char *)(query.c_str()), query.length(), &pStmt, NULL);
470                 while( sqlite3_step(pStmt) == SQLITE_ROW)
471                 {
472                         content_type = sqlite3_column_int(pStmt, 2);    
473
474                         if(content_type == MEDIA_TYPE_IMAGE)
475                         {
476
477                                 MediacontentImage *newImage(new MediacontentImage());
478                                 readImageFromDB(pStmt,newImage);
479                                 eBrowse->addMedia(newImage);
480                         }
481                         else if(content_type == MEDIA_TYPE_VIDEO)
482                         {
483                                 MediacontentVideo *newVideo(new MediacontentVideo());
484                                 readVideoFromDB(pStmt,newVideo);
485                                 eBrowse->addMedia(newVideo);
486                         }
487                         else if(content_type == MEDIA_TYPE_AUDIO)
488                         {
489                                 MediacontentAudio *newVAudio(new MediacontentAudio());
490                                 readAudioFromDB(pStmt,newVAudio);
491                                 eBrowse->addMedia(newVAudio);
492                         }
493                 }
494                 sqlite3_finalize(pStmt);
495                 eBrowse->setResult(true);
496
497         }
498     catch (const Exception &ex)
499     {
500         LogError("Exception: " << ex.DumpToString());
501         eBrowse->setResult(false);
502     }
503 }
504
505 bool Mediacontent::updateMediaToDB(MediacontentMediaPtr mediaPtr)
506 {
507         int err;
508         char *errMsg = NULL;
509
510         bool ret = false;
511         string type  = mediaPtr->getMediaType();
512                 
513         if(type.compare("IMAGE")==0)
514         {
515                 MediacontentImagePtr imagePtr = DPL::DynamicPointerCast<MediacontentImage>(mediaPtr);
516                 if(imagePtr != NULL)
517                 {
518                         string query;   
519                         if(imagePtr->getIsChangedFavorite())
520                         {
521                                 query.append("update visual_media set rating=");
522                                 std::stringstream offsetStream;
523                                 offsetStream << imagePtr->getFavorite();
524                                 query.append(offsetStream.str());
525                                 query.append(" where visual_uuid='");
526                                 query.append(imagePtr->getMediaUUID());
527                                 query.append("'");
528                                 LogDebug("QUERY : " << query);
529
530                                 err = sqlite3_exec(Mediacontent::hDBCt, query.c_str(), NULL, NULL, &errMsg);
531                                 if (SQLITE_OK != err)
532                                 {                                       
533                                         if(errMsg)
534                                         {
535                                                 LogDebug("DB opertations is failed:" << errMsg);
536                                                 sqlite3_free(errMsg);
537                                                 ThrowMsg(PlatformException, "DB opertations is failed");
538                                         }       
539                                 }
540                         }
541                 }
542                 ret = true;
543         }
544         if(type.compare("VIDEO")==0)
545         {
546                 MediacontentVideoPtr videoPtr = DPL::DynamicPointerCast<MediacontentVideo>(mediaPtr);
547                 if(videoPtr != NULL)
548                 {
549                         string query;   
550                         if(videoPtr->getIsChangedFavorite())
551                         {
552                                 std::stringstream offsetStream;
553                                 offsetStream << "update visual_media set rating=" << videoPtr->getFavorite() << " where visual_uuid=" << "'" << videoPtr->getMediaUUID() << "'";
554                                 query = offsetStream.str();                     
555                                 err = sqlite3_exec(Mediacontent::hDBCt, query.c_str(), NULL, NULL, &errMsg);
556                                 if (SQLITE_OK != err)
557                                 {                                       
558                                         if(errMsg)
559                                         {
560                                                 LogDebug("DB opertations is failed:" << errMsg);
561                                                 sqlite3_free(errMsg);
562                                                 ThrowMsg(PlatformException, "DB opertations is failed");
563                                         }
564                                 }
565                         }
566                         if(videoPtr->isChangedPlayedTime() || videoPtr->isChangedPlayCount())
567                         {
568                                 std::stringstream offsetStream;
569                                 offsetStream << "update video_meta set last_played_time=" << videoPtr->getVideoPlayedTime() << " where visual_uuid=" << "'" << videoPtr->getMediaUUID() << "'";
570                                 query = offsetStream.str();                     
571                                 err = sqlite3_exec(Mediacontent::hDBCt, query.c_str(), NULL, NULL, &errMsg);
572                                 if (SQLITE_OK != err)
573                                 {                                       
574                                         if(errMsg)
575                                         {
576                                                 LogDebug("DB opertations is failed:" << errMsg);
577                                                 sqlite3_free(errMsg);
578                                                 ThrowMsg(PlatformException, "DB opertations is failed");
579                                         }
580                                 }                                               
581                         }
582                 }       
583                 ret = true;
584         }
585         if(type.compare("AUDIO")==0)
586         {
587                 MediacontentAudioPtr audioPtr = DPL::DynamicPointerCast<MediacontentAudio>(mediaPtr);
588                 if(audioPtr != NULL)
589                 {
590                         string query;   
591                         if(audioPtr->getIsChangedFavorite())
592                         {
593                                 std::stringstream offsetStream;
594                                 offsetStream << "update audio_media set rating=" << audioPtr->getFavorite() << " where audio_uuid=" << "'" << audioPtr->getMediaUUID() << "'";
595                                 query = offsetStream.str();
596                                 err = sqlite3_exec(Mediacontent::hDBCt, query.c_str(), NULL, NULL, &errMsg);
597                                 if (SQLITE_OK != err)
598                                 {                                       
599                                         if(errMsg)
600                                         {
601                                                 LogDebug("DB opertations is failed:" << errMsg);
602                                                 sqlite3_free(errMsg);
603                                                 ThrowMsg(PlatformException, "DB opertations is failed");
604                                         }
605                                         
606                                 }                                       
607                         }
608                         if(audioPtr->isChangedPlayedTime() || audioPtr->isChangedPlayCount())
609                         {
610                                 std::stringstream offsetStream;
611                                 offsetStream << "update audio_media set last_played_time=" <<audioPtr->getAudioPlayedTime()<< ",played_count="<<audioPtr->getAudioPlayCount()<<" where audio_uuid=" << "'" << audioPtr->getMediaUUID() << "'";
612                                 query = offsetStream.str();                     
613                                 err = sqlite3_exec(Mediacontent::hDBCt, query.c_str(), NULL, NULL, &errMsg);
614                                 if (SQLITE_OK != err)
615                                 {                                       
616                                         if(errMsg)
617                                         {
618                                                 LogDebug("DB opertations is failed:" << errMsg);
619                                                 sqlite3_free(errMsg);
620                                                 ThrowMsg(PlatformException, "DB opertations is failed");
621                                         }
622                                 }                                               
623                         }
624                 }       
625                 ret = true;
626         }
627         return ret;
628 }
629
630 void Mediacontent::OnRequestReceived(const IEventUpdateMediaPtr &eMedia)
631 {
632     LogDebug("OnRequestReceived::IEventUpdateMediaPtr entered");
633         try
634         {
635                 MediacontentMediaPtr mediaPtr = eMedia->getMediaItem();
636
637                 if(updateMediaToDB(mediaPtr))
638                 {
639                         eMedia->setResult(true);
640                 }
641                 else
642                 {
643                         eMedia->setResult(false);
644                 }
645         }
646     catch (const Exception &ex)
647     {
648         LogError("Exception: " << ex.DumpToString());
649         eMedia->setResult(false);
650     }
651 }
652
653
654 void Mediacontent::OnRequestReceived(const IEventUpdateMediaItemsPtr &eItems)
655 {
656         LogDebug("OnRequestReceived::IEventUpdateMediaItemsPtr entered");
657
658         char *ErrMsg = NULL;
659         try
660         {
661                 MediacontentMediaListPtr mediaListPtr = eItems->getMediaItems();
662         if (mediaListPtr->empty()) 
663         {
664             ThrowMsg(PlatformException, "Item vector parameter is empty");
665         }
666
667                 if(SQLITE_OK != sqlite3_exec(Mediacontent::hDBCt, "BEGIN IMMEDIATE;", NULL, NULL, &ErrMsg)) 
668                 {
669                         LogError("Error:failed to begin transaction: error=" << ErrMsg);
670                         sqlite3_free(ErrMsg);
671                         ThrowMsg(PlatformException,"Error:failed to begin transaction\n");
672                 }
673                 sqlite3_free(ErrMsg);
674                 
675         for(unsigned int i=0; i<mediaListPtr->size(); i++)
676         {
677                         if (eItems->checkCancelled()) 
678                         {
679                 eItems->setCancelAllowed(true);
680                 eItems->setResult(true);
681                                 if (SQLITE_OK != sqlite3_exec(Mediacontent::hDBCt, "ROLLBACK;", NULL, NULL, &ErrMsg)) 
682                                 {
683                                         LogError("Error:failed to rollback transaction: error=" << ErrMsg);
684                                         sqlite3_free(ErrMsg);
685                                         ThrowMsg(PlatformException,"Error:failed to rollback transaction\n");
686                                 }    
687                                 sqlite3_free(ErrMsg);
688                 return;
689             }
690                         MediacontentMediaPtr mediaPtr = mediaListPtr->at(i);
691                         if(!updateMediaToDB(mediaPtr))
692                         {
693                                 ThrowMsg(PlatformException, "DB operation is failed");
694                         }
695                 }
696                 eItems->setResult(true);
697                 if (SQLITE_OK != sqlite3_exec(Mediacontent::hDBCt, "COMMIT;", NULL, NULL, &ErrMsg)) 
698                 {
699                         LogError("Error:failed to end transaction: error=" << ErrMsg);
700                         sqlite3_free(ErrMsg);
701                         ThrowMsg(PlatformException,"Error:failed to end transaction\n");
702                 }
703                 sqlite3_free(ErrMsg);
704          }
705          catch (const Exception &ex)
706          {
707                  LogError("Exception: " << ex.DumpToString());
708                  eItems->setResult(false);
709          }
710          eItems->setCancelAllowed(false);
711 }
712
713 void Mediacontent::readImageFromDB(sqlite3_stmt* pStmt, MediacontentImage* newImage)
714 {
715         char* tmp;
716         if(pStmt != NULL)
717         {
718
719                 tmp = (char*)sqlite3_column_text(pStmt,0);
720                 if( tmp != NULL)
721                         newImage->setMediaUUID(tmp);
722
723                 newImage->setMediaType("IMAGE");
724         
725                 //setMimeType //2
726                 tmp = (char*)sqlite3_column_text(pStmt,3);
727                 if( tmp != NULL)
728                         newImage->setDisplayName(tmp);
729
730                 tmp = (char*)sqlite3_column_text(pStmt,4);
731                 if( tmp != NULL)
732                         newImage->setFilePath(tmp);
733
734                 tmp = (char*)sqlite3_column_text(pStmt,5);
735                 if( tmp != NULL)
736                         newImage->setThumbnailPath(tmp);
737
738                 newImage->setCreatedDate(toDateTm(sqlite3_column_int(pStmt,6)));
739                 newImage->setReleasedDate(toDateTm(sqlite3_column_int(pStmt,7)));
740                 newImage->setModifiedDate(toDateTm(sqlite3_column_int(pStmt,8)));
741
742                 tmp = (char*)sqlite3_column_text(pStmt,9);
743                 if( tmp != NULL)
744                         newImage->setDescription(tmp);
745
746                 newImage->setFavorite(sqlite3_column_int(pStmt,10));    
747                 newImage->setImageLatitude(sqlite3_column_double(pStmt,15));
748                 newImage->setImageLongitude(sqlite3_column_double(pStmt,16));   
749                 newImage->setImageWidth(sqlite3_column_int(pStmt,17));
750                 newImage->setImageHeight(sqlite3_column_int(pStmt,18));
751                 int orientation = (sqlite3_column_int(pStmt,19));
752                 string orientationStr;
753                 switch(orientation)
754                 {
755                         case 1: 
756                                 orientationStr = "NORMAL";
757                                 break;
758                         case 2: 
759                                 orientationStr = "FLIP_HORIZONTAL";
760                                 break;
761                         case 3: 
762                                 orientationStr = "ROTATE_180";
763                                 break;
764                         case 4: 
765                                 orientationStr = "FLIP_VERTICAL";
766                                 break;
767                         case 5: 
768                                 orientationStr = "TRANSPOSE";
769                                 break;
770                         case 6: 
771                                 orientationStr = "ROTATE_90";
772                                 break;
773                         case 7: 
774                                 orientationStr = "TRANSVERSE";
775                                 break;
776                         case 8: 
777                                 orientationStr = "ROTATE_270";
778                                 break;
779                 }               
780                 newImage->setImageOrientation(orientationStr);
781                 newImage->setSize(sqlite3_column_double(pStmt,23));
782         }
783         
784 }
785
786
787
788 void Mediacontent::readVideoFromDB(sqlite3_stmt* pStmt, MediacontentVideo* newVideo)
789 {
790         char* tmp;
791         if(pStmt != NULL)
792         {
793
794                 tmp = (char*)sqlite3_column_text(pStmt,0);
795                 if( tmp != NULL)
796                         newVideo->setMediaUUID(tmp);
797
798                 newVideo->setMediaType("VIDEO");
799
800                 //setMimeType 
801                 tmp = (char*)sqlite3_column_text(pStmt,3);
802                 if( tmp != NULL)
803                         newVideo->setDisplayName(tmp);
804
805                 tmp = (char*)sqlite3_column_text(pStmt,4);
806                 if( tmp != NULL)
807                         newVideo->setFilePath(tmp);
808
809                 tmp = (char*)sqlite3_column_text(pStmt,5);
810                 if( tmp != NULL)
811                         newVideo->setThumbnailPath(tmp);
812
813                 newVideo->setCreatedDate(toDateTm(sqlite3_column_int(pStmt,6)));
814                 newVideo->setReleasedDate(toDateTm(sqlite3_column_int(pStmt,7)));
815                 newVideo->setModifiedDate(toDateTm(sqlite3_column_int(pStmt,8)));
816
817                 tmp = (char*)sqlite3_column_text(pStmt,9);
818                 if( tmp != NULL)
819                         newVideo->setDescription(tmp);
820
821                 newVideo->setFavorite(sqlite3_column_int(pStmt,10));    
822
823                 tmp = (char*)sqlite3_column_text(pStmt,11);
824                 if( tmp != NULL)
825                         newVideo->setVideoAlbum(tmp);
826
827                 tmp = (char*)sqlite3_column_text(pStmt,12);
828                 if( tmp != NULL)                        
829                         newVideo->setVideoArtist(tmp);
830
831                 newVideo->setVideoPlayedTime(sqlite3_column_int(pStmt,13));     
832                 newVideo->setVideoPlayCount(sqlite3_column_int(pStmt,14)); 
833
834                 newVideo->setVideoLongitude(sqlite3_column_double(pStmt,15));   
835                 newVideo->setVideoLatitude(sqlite3_column_double(pStmt,16));
836                 
837                 newVideo->setVideoWidth(sqlite3_column_int(pStmt,17));
838                 newVideo->setVideoHeight(sqlite3_column_int(pStmt,18));
839         
840                 newVideo->setVideoDuration(sqlite3_column_int(pStmt,22));
841                 newVideo->setSize(sqlite3_column_double(pStmt,23));
842         }               
843
844 }
845
846 void Mediacontent::readAudioFromDB(sqlite3_stmt* pStmt, MediacontentAudio* newAudio)
847 {
848         char* tmp;
849         if(pStmt != NULL)
850         {
851
852                 tmp = (char*)sqlite3_column_text(pStmt,0);
853                 if( tmp != NULL)
854                         newAudio->setMediaUUID(tmp);
855
856                 newAudio->setMediaType("AUDIO");
857
858                 //setMimeType 
859                 tmp = (char*)sqlite3_column_text(pStmt,3);
860                 if( tmp != NULL)
861                         newAudio->setDisplayName(tmp);
862
863                 tmp = (char*)sqlite3_column_text(pStmt,4);
864                 if( tmp != NULL)
865                         newAudio->setFilePath(tmp);
866
867                 tmp = (char*)sqlite3_column_text(pStmt,5);
868                 if( tmp != NULL)
869                         newAudio->setThumbnailPath(tmp);
870
871
872                 newAudio->setCreatedDate(toDateTm(sqlite3_column_int(pStmt,6)));
873
874                 tm tm_date;
875                 memset(&tm_date,0x00,sizeof(tm_date));
876                 tm_date.tm_year = sqlite3_column_int(pStmt,7);
877                 newAudio->setReleasedDate(tm_date);
878                 newAudio->setModifiedDate(toDateTm(sqlite3_column_int(pStmt,8)));
879
880                 tmp = (char*)sqlite3_column_text(pStmt,9);
881                 if( tmp != NULL)
882                         newAudio->setDescription(tmp);
883
884                 newAudio->setFavorite(sqlite3_column_int(pStmt,10));    
885
886                 tmp = (char*)sqlite3_column_text(pStmt,11);
887                 if( tmp != NULL)
888                         newAudio->setAudioAlbum(tmp);
889
890                 tmp = (char*)sqlite3_column_text(pStmt,12);
891                 if( tmp != NULL)                        
892                         newAudio->setAudioArtist(tmp);
893
894                 newAudio->setAudioPlayedTime(sqlite3_column_int(pStmt,13));     
895                 newAudio->setAudioPlayCount(sqlite3_column_int(pStmt,14));
896
897                 tmp = (char*)sqlite3_column_text(pStmt,20);
898                 if( tmp != NULL)                        
899                         newAudio->setAudioGenre(tmp);
900
901                 tmp = (char*)sqlite3_column_text(pStmt,21);
902                 if( tmp != NULL)                        
903                         newAudio->setAudioComposer(tmp);
904
905                 newAudio->setAudioDuration(sqlite3_column_int(pStmt,22));
906                 newAudio->setSize(sqlite3_column_double(pStmt,23));
907
908                 string queryExtraAudio(SELECT_EXTRA_AUDIO);
909                 queryExtraAudio.append("'");
910                 queryExtraAudio.append(newAudio->getMediaUUID());
911                 queryExtraAudio.append("'");
912                 sqlite3_stmt* pStmt1 = NULL;
913                 //"select copyright,bitrate,track_num,size from audio_media where audio_id=";
914                 if(sqlite3_prepare_v2(Mediacontent::hDBCt, (char *)(queryExtraAudio.c_str()), strlen((char *)(queryExtraAudio.c_str())), &pStmt1, NULL) == SQLITE_OK)
915                 {
916                         while( sqlite3_step(pStmt1) == SQLITE_ROW)
917                         {
918                                 tmp = (char*)sqlite3_column_text(pStmt,0);
919                                 if( tmp != NULL)                        
920                                         newAudio->setAudioCopyright(tmp);
921                                 newAudio->setAudioBitrate(sqlite3_column_int(pStmt,1));
922                                 newAudio->setAudioTrackNum(sqlite3_column_int(pStmt,2));
923                                 newAudio->setAudioSize(sqlite3_column_int(pStmt,3));
924                         }
925                         sqlite3_finalize(pStmt1);
926                 }
927
928                 MMHandleType tag_attrs = 0;
929                 LogDebug("AUDIO PATH: " << newAudio->getFilePath());
930                 int ret = mm_file_create_tag_attrs(&tag_attrs, newAudio->getFilePath().c_str());
931
932                 char *err_attr_name = NULL;
933                 char* unSyncText;
934                 int unSyncLen;
935                 int syncTextNum;                
936                 if (ret == MM_ERROR_NONE && tag_attrs) 
937                 {
938                         ret = mm_file_get_attrs( tag_attrs,
939                                                                                 &err_attr_name,
940                                                                                 MM_FILE_TAG_UNSYNCLYRICS, &unSyncText, &unSyncLen,
941                                                                                 MM_FILE_TAG_SYNCLYRICS_NUM, &syncTextNum,
942                                                                                 NULL);          
943                 }
944                 if (ret != MM_ERROR_NONE &&  err_attr_name)
945                 {
946                         MediacontentLyricsPtr lyricsPtr(new MediacontentLyrics());
947                         if(syncTextNum > 0)
948                         {
949                                 lyricsPtr->setMediaLyricsType("SYNCHRONIZED");
950                                 for(int i=0; i < syncTextNum; i++)
951                                 {       
952                                         unsigned long time_info = 0;
953                                         char * lyrics_info = NULL;
954                                         mm_file_get_synclyrics_info(tag_attrs,i,&time_info,&lyrics_info);
955                                         lyricsPtr->addMediaLyricsTimeStamp(time_info);
956                                         lyricsPtr->addMediaLyricsText(lyrics_info);
957                                         LogDebug("SYNCHRONIZED: " << lyrics_info);
958                                 }
959                         }
960                         else
961                         {
962                                 LogDebug("UNSYNCHRONIZED: " << unSyncText);
963                                 lyricsPtr->setMediaLyricsType("UNSYNCHRONIZED");
964                                 lyricsPtr->addMediaLyricsTimeStamp(0);
965                                 lyricsPtr->addMediaLyricsText(unSyncText);
966         
967                         }
968                         newAudio->setAudioLyrics(lyricsPtr);
969                 }
970                 
971         }
972
973 }
974
975
976 }
977 }
978 }