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