messagingapp/msgui/appengine/src/conversationsmodel.cpp
changeset 34 84197e66a4bd
parent 31 ebfee66fde93
child 43 35b64624a9e7
equal deleted inserted replaced
31:ebfee66fde93 34:84197e66a4bd
    30 #include "debugtraces.h"
    30 #include "debugtraces.h"
    31 
    31 
    32 #include <QFile>
    32 #include <QFile>
    33 #include <QFileInfo>
    33 #include <QFileInfo>
    34 #include <s32mem.h>
    34 #include <s32mem.h>
       
    35 #include <s32strm.h>
       
    36 #include <fbs.h>
    35 #include <ccsdefs.h>
    37 #include <ccsdefs.h>
    36 
    38 
    37 //CONSTANTS
    39 //CONSTANTS
    38 _LIT(KDbFileName, "c:[2002A542]conversations.db");
    40 _LIT(KDbFileName, "c:[2002A542]conversations.db");
       
    41 
    39 // preview sql query
    42 // preview sql query
    40 _LIT(KSelectConvMsgsStmt, "SELECT message_id, subject, body_text, preview_path, msg_property FROM conversation_messages WHERE message_id=:message_id ");
    43 _LIT(KSelectConvMsgsStmt, "SELECT message_id, msg_processingstate, subject, body_text, preview_path, msg_property, preview_icon FROM conversation_messages WHERE message_id=:message_id ");
    41 
    44 
       
    45 //selecet preview-icon query
       
    46 _LIT(KSelectPreviewIconStmt,"SELECT  message_id, preview_icon FROM conversation_messages WHERE message_id = :message_id ");
       
    47 
       
    48 // preview-cache max cost (items)
       
    49 const int CACHE_COST =  50;
    42 //---------------------------------------------------------------
    50 //---------------------------------------------------------------
    43 // ConversationsModel::ConversationsModel
    51 // ConversationsModel::ConversationsModel
    44 // Constructor
    52 // Constructor
    45 //---------------------------------------------------------------
    53 //---------------------------------------------------------------
    46 ConversationsModel::ConversationsModel(ConversationMsgStoreHandler* msgStoreHandler,
    54 ConversationsModel::ConversationsModel(ConversationMsgStoreHandler* msgStoreHandler,
    50     //Open SQL DB
    58     //Open SQL DB
    51     if (KErrNone == iSqlDb.Open(KDbFileName))
    59     if (KErrNone == iSqlDb.Open(KDbFileName))
    52     {
    60     {
    53         iSqlDbOpen = ETrue;
    61         iSqlDbOpen = ETrue;
    54     }
    62     }
       
    63     previewIconCache.setMaxCost(CACHE_COST);
       
    64 
       
    65     int err = connect(this, SIGNAL(retrievePreviewIcon(int, QString&)), this,
       
    66         SLOT(updatePreviewIcon(int, QString&)));
       
    67     QCRITICAL_WRITE_FORMAT("Error from connect()", err)
    55     iDataModelPluginLoader = new UniDataModelLoader;
    68     iDataModelPluginLoader = new UniDataModelLoader;
    56     iMmsDataPlugin = iDataModelPluginLoader->getDataModelPlugin(ConvergedMessage::Mms);
    69     iMmsDataPlugin = iDataModelPluginLoader->getDataModelPlugin(ConvergedMessage::Mms);
    57     iBioMsgPlugin = iDataModelPluginLoader->getDataModelPlugin(ConvergedMessage::BioMsg);
    70     iBioMsgPlugin = iDataModelPluginLoader->getDataModelPlugin(ConvergedMessage::BioMsg);
    58 }
    71 }
    59 
    72 
    63 //---------------------------------------------------------------
    76 //---------------------------------------------------------------
    64 ConversationsModel::~ConversationsModel()
    77 ConversationsModel::~ConversationsModel()
    65 {
    78 {
    66     //Close SQL-DB
    79     //Close SQL-DB
    67     iSqlDb.Close();
    80     iSqlDb.Close();
    68 
    81 	
       
    82 	//clear preview-cache
       
    83     previewIconCache.clear();
       
    84 	
    69     if (iDataModelPluginLoader) {
    85     if (iDataModelPluginLoader) {
    70         delete iDataModelPluginLoader;
    86         delete iDataModelPluginLoader;
    71         iDataModelPluginLoader = NULL;
    87         iDataModelPluginLoader = NULL;
    72     }
    88     }
    73 }
    89 }
   143     }
   159     }
   144     case SendingState:
   160     case SendingState:
   145     {
   161     {
   146         value = item->data(SendingState);
   162         value = item->data(SendingState);
   147         break;
   163         break;
       
   164     }
       
   165     case PreviewIcon:
       
   166     {
       
   167         QString filepath(item->data(Attachments).toString());
       
   168         int msgId = item->data(ConvergedMsgId).toInt();
       
   169         HbIcon *icon = getPreviewIconItem(msgId, filepath);
       
   170         return *icon;
   148     }
   171     }
   149     case MessagePriority:
   172     case MessagePriority:
   150     {
   173     {
   151         value = item->data(MessagePriority);
   174         value = item->data(MessagePriority);
   152         break;
   175         break;
   355     //msg_id
   378     //msg_id
   356     int msgId = entry.EntryId();
   379     int msgId = entry.EntryId();
   357 
   380 
   358     bool isEntryInDb = false;
   381     bool isEntryInDb = false;
   359     TInt err = KErrNone;
   382     TInt err = KErrNone;
   360     
   383 
   361     //check if db is open and query db
   384     //check if db is open and query db
   362     if (iSqlDbOpen)
   385     if (iSqlDbOpen)
   363     {
   386     {
   364         RSqlStatement sqlSelectStmt;
   387         RSqlStatement sqlSelectStmt;
   365         err = sqlSelectStmt.Prepare(iSqlDb, KSelectConvMsgsStmt);
   388         err = sqlSelectStmt.Prepare(iSqlDb, KSelectConvMsgsStmt);
   366 
   389 
   367         // move to fallback option
   390         // move to fallback option
   368         if (KErrNone == err)
   391         if (KErrNone == err)
   369             {
   392             {
   370             TInt msgIdIndex = sqlSelectStmt.ParameterIndex(_L(":message_id"));
   393             TInt msgIdIndex = sqlSelectStmt.ParameterIndex(_L(":message_id"));
       
   394             TInt msgProcessingStateIndex = sqlSelectStmt.ColumnIndex(_L("msg_processingstate"));
   371             TInt subjectIndex = sqlSelectStmt.ColumnIndex(_L("subject"));
   395             TInt subjectIndex = sqlSelectStmt.ColumnIndex(_L("subject"));
   372             TInt bodyIndex = sqlSelectStmt.ColumnIndex(_L("body_text"));
   396             TInt bodyIndex = sqlSelectStmt.ColumnIndex(_L("body_text"));
   373             TInt previewPathIndex = sqlSelectStmt.ColumnIndex(
   397             TInt previewPathIndex = sqlSelectStmt.ColumnIndex(
   374                     _L("preview_path"));
   398                     _L("preview_path"));
   375             TInt msgpropertyIndex = sqlSelectStmt.ColumnIndex(
   399             TInt msgpropertyIndex = sqlSelectStmt.ColumnIndex(
   376                     _L("msg_property"));
   400                 _L("msg_property"));
       
   401             TInt previewIconIndex = sqlSelectStmt.ColumnIndex(
       
   402                 _L("preview_icon"));
   377 
   403 
   378             err = sqlSelectStmt.BindInt(msgIdIndex, msgId);
   404             err = sqlSelectStmt.BindInt(msgIdIndex, msgId);
   379             
   405 
   380             // populate item
   406             // populate item
   381             if ((KErrNone == err) && (sqlSelectStmt.Next() == KSqlAtRow))
   407             if ((KErrNone == err) && (sqlSelectStmt.Next() == KSqlAtRow))
   382                 {
   408                 {
   383                 RBuf subjectBuffer;
   409                 int msgProcessingState = 0;
   384                 subjectBuffer.Create(sqlSelectStmt.ColumnSize(subjectIndex));
   410                 msgProcessingState = sqlSelectStmt.ColumnInt(
   385                 sqlSelectStmt.ColumnText(subjectIndex, subjectBuffer);
   411                         msgProcessingStateIndex);
   386 
   412                 if (msgProcessingState == EPreviewMsgProcessed)
   387                 item.setData(
   413                     {
   388                         S60QConversions::s60DescToQString(subjectBuffer),
   414                     // use entry to populate model only when,
   389                         Subject);
   415                     // entry is present in DB and its processing is over.
   390                 subjectBuffer.Close();
   416                     RBuf subjectBuffer;
   391 
   417                     subjectBuffer.Create(sqlSelectStmt.ColumnSize(
   392                 RBuf bodyBuffer;
   418                             subjectIndex));
   393                 bodyBuffer.Create(sqlSelectStmt.ColumnSize(bodyIndex));
   419                     sqlSelectStmt.ColumnText(subjectIndex, subjectBuffer);
   394                 sqlSelectStmt.ColumnText(bodyIndex, bodyBuffer);
   420 
   395 
   421                     item.setData(S60QConversions::s60DescToQString(
   396                 item.setData(S60QConversions::s60DescToQString(bodyBuffer),
   422                             subjectBuffer), Subject);
   397                         BodyText);
   423                     subjectBuffer.Close();
   398                 bodyBuffer.Close();
   424 
   399 
   425                     RBuf bodyBuffer;
   400                 RBuf previewPathBuffer;
   426                     bodyBuffer.Create(sqlSelectStmt.ColumnSize(bodyIndex));
   401                 previewPathBuffer.Create(sqlSelectStmt.ColumnSize(
   427                     sqlSelectStmt.ColumnText(bodyIndex, bodyBuffer);
   402                         previewPathIndex));
   428 
   403                 sqlSelectStmt.ColumnText(previewPathIndex, previewPathBuffer);
   429                     item.setData(
   404 
   430                             S60QConversions::s60DescToQString(bodyBuffer),
   405                 //Rightnow set inside attachments
   431                             BodyText);
   406                 item.setData(S60QConversions::s60DescToQString(
   432                     bodyBuffer.Close();
   407                         previewPathBuffer), Attachments);
   433 
   408                 previewPathBuffer.Close();
   434                     RBuf previewPathBuffer;
   409 
   435                     previewPathBuffer.Create(sqlSelectStmt.ColumnSize(
   410                 int msgProperty = 0;
   436                             previewPathIndex));
   411                 msgProperty = sqlSelectStmt.ColumnInt(msgpropertyIndex);
   437                     sqlSelectStmt.ColumnText(previewPathIndex,
   412                 item.setData(msgProperty, MessageProperty);
   438                             previewPathBuffer);
   413 
   439 
   414                 //set flag to disable fallback option
   440                     //Rightnow set inside attachments
   415                 isEntryInDb = true;
   441                     QString attachmentPath(S60QConversions::s60DescToQString(
       
   442                             previewPathBuffer));
       
   443 
       
   444                     item.setData(attachmentPath, Attachments);
       
   445                     previewPathBuffer.Close();
       
   446 
       
   447                     int msgProperty = 0;
       
   448                     msgProperty = sqlSelectStmt.ColumnInt(msgpropertyIndex);
       
   449                     item.setData(msgProperty, MessageProperty);
       
   450 
       
   451                     RSqlColumnReadStream stream;
       
   452                     //Get data from binary column BLOB
       
   453                     TInt err = stream.ColumnBinary(sqlSelectStmt,
       
   454                             previewIconIndex);
       
   455 
       
   456                     QCRITICAL_WRITE_FORMAT("Error from ColumnBinary()", err)
       
   457 
       
   458                     if (err == KErrNone)
       
   459                         {
       
   460                         CFbsBitmap *bitmap = new CFbsBitmap;
       
   461                         TRAPD(err,bitmap->InternalizeL(stream));
       
   462                         QCRITICAL_WRITE_FORMAT("Error from bitmap InternalizeL()", err)
       
   463 
       
   464                         //convert bitmap to pixmap
       
   465                         if (err == KErrNone)
       
   466                             {
       
   467                             TSize size = bitmap->SizeInPixels();
       
   468                             int bytesPerLine = bitmap->ScanLineLength(
       
   469                                     size.iWidth, bitmap->DisplayMode());
       
   470                             const uchar* dataPtr =
       
   471                                     (const uchar*) bitmap->DataAddress();
       
   472 
       
   473                             QPixmap pixmap = QPixmap::fromImage(QImage(
       
   474                                     dataPtr, size.iWidth, size.iHeight,
       
   475                                     bytesPerLine, QImage::Format_RGB16));
       
   476 
       
   477                             setPreviewIcon(pixmap, attachmentPath, msgId,
       
   478                                     true);
       
   479 
       
   480                             }
       
   481                         //remove bitmap
       
   482                         delete bitmap;
       
   483                         }
       
   484 
       
   485                     //set flag to disable fallback option
       
   486                     isEntryInDb = true;
       
   487                     }
   416                 }
   488                 }
   417             }
   489             }
   418         sqlSelectStmt.Close();
   490         sqlSelectStmt.Close();
   419         }
   491         }
   420 
   492 
   481             foreach(UniMessageInfo* slide,objectList)
   553             foreach(UniMessageInfo* slide,objectList)
   482                 {
   554                 {
   483                     delete slide;
   555                     delete slide;
   484                 }
   556                 }
   485         }
   557         }
       
   558         QPixmap pixmap;
   486         //populate item  with the attachment list
   559         //populate item  with the attachment list
   487         if (isVideoSet)
   560         if (isVideoSet)
   488         {
   561         {
   489             item.setData(videoPath, Attachments);
   562             item.setData(videoPath, Attachments);
       
   563             setPreviewIcon(pixmap, videoPath, msgId, false);
   490         }
   564         }
   491         else if (isImageSet)
   565         else if (isImageSet)
   492         {
   566         {
   493             item.setData(imagePath, Attachments);
   567             item.setData(imagePath, Attachments);
       
   568             setPreviewIcon(pixmap, imagePath, msgId, false);
   494         }
   569         }
   495         //populate msgProperty
   570         //populate msgProperty
   496         item.setData(msgProperty, MessageProperty);
   571         item.setData(msgProperty, MessageProperty);
   497     }
   572     }
   498 
   573 
   628             //get display-name and set as bodytext
   703             //get display-name and set as bodytext
   629             QString displayName = 
   704             QString displayName = 
   630                     MsgContactHandler::getVCardDisplayName(
   705                     MsgContactHandler::getVCardDisplayName(
   631                             attachmentPath);
   706                             attachmentPath);
   632             item.setData(displayName, BodyText);
   707             item.setData(displayName, BodyText);
       
   708             item.setData(attachmentPath, Attachments);
   633 
   709 
   634             // clear attachement list : its allocated at data model
   710             // clear attachement list : its allocated at data model
   635             while (!attList.isEmpty()) {
   711             while (!attList.isEmpty()) {
   636                 delete attList.takeFirst();
   712                 delete attList.takeFirst();
   637             }
   713             }
   672 RSqlDatabase& ConversationsModel::getDBHandle(TBool& isOpen)
   748 RSqlDatabase& ConversationsModel::getDBHandle(TBool& isOpen)
   673 {
   749 {
   674     isOpen = iSqlDbOpen;
   750     isOpen = iSqlDbOpen;
   675     return iSqlDb;
   751     return iSqlDb;
   676 }
   752 }
       
   753 
       
   754 //---------------------------------------------------------------
       
   755 // ConversationsModel::setPreviewIcon()
       
   756 // @see header
       
   757 //---------------------------------------------------------------
       
   758 void ConversationsModel::setPreviewIcon(QPixmap& pixmap, QString& filePath,
       
   759     int msgId, bool inDb)
       
   760 {
       
   761 
       
   762     //Since the population happens in reverse this check is needed so that
       
   763     //most recent items have their icons present in cache
       
   764     if (previewIconCache.totalCost() >= previewIconCache.maxCost())
       
   765         return;
       
   766 
       
   767     // if not found in db, set from file path
       
   768     if (!inDb)
       
   769     {
       
   770         QPixmap pixmap(filePath);
       
   771         QPixmap scaledPixmap = pixmap.scaled(63.65, 63.65, Qt::IgnoreAspectRatio);
       
   772         HbIcon *previewIcon = new HbIcon(pixmap);
       
   773 
       
   774         previewIconCache.insert(msgId, previewIcon);
       
   775 
       
   776     }
       
   777     else
       
   778     {
       
   779         HbIcon *previewIcon = new HbIcon(pixmap);
       
   780         previewIconCache.insert(msgId, previewIcon);
       
   781     }
       
   782 }
       
   783 
       
   784 //---------------------------------------------------------------
       
   785 // ConversationsModel::getPreviewIconItem()
       
   786 // @see header
       
   787 //---------------------------------------------------------------
       
   788 HbIcon* ConversationsModel::getPreviewIconItem(int msgId, 
       
   789     QString& filepath) const
       
   790 {
       
   791     QCRITICAL_WRITE("ConversationsModel::getPreviewIconItem start.")
       
   792 
       
   793     //Initialize icon from the Cache will be NULL if Item not present        
       
   794     HbIcon* previewIcon = previewIconCache[msgId]; 
       
   795     if (!previewIcon)
       
   796     {
       
   797         //This is done in this way as non-const function call cant be done here
       
   798         emit retrievePreviewIcon(msgId, filepath);
       
   799 
       
   800         previewIcon = previewIconCache[msgId];
       
   801     }
       
   802 
       
   803     QCRITICAL_WRITE("ConversationsModel::getPreviewIconItem start.")
       
   804 
       
   805     return previewIcon;
       
   806 }
       
   807 
       
   808 //---------------------------------------------------------------
       
   809 // ConversationsModel::updatePreviewIcon()
       
   810 // @see header
       
   811 //---------------------------------------------------------------
       
   812 void ConversationsModel::updatePreviewIcon(int msgId, QString& filePath)
       
   813 {
       
   814     QCRITICAL_WRITE("ConversationsModel::updatePreviewIcon start.")
       
   815 
       
   816     //sql query to get preview-icon from DB
       
   817     bool imagePreviewed = false;
       
   818     QPixmap pixmap;
       
   819 
       
   820     if (iSqlDbOpen)
       
   821     {
       
   822         RSqlStatement sqlSelectPreviewIconStmt;
       
   823         TInt err = sqlSelectPreviewIconStmt.Prepare(iSqlDb,
       
   824             KSelectPreviewIconStmt);
       
   825 
       
   826         QCRITICAL_WRITE_FORMAT("Error from Prepare()", err)
       
   827 
       
   828         if (err == KErrNone)
       
   829         {
       
   830             //msg_id
       
   831             TInt msgIdIndex = sqlSelectPreviewIconStmt.ParameterIndex(
       
   832                 _L(":message_id"));
       
   833             sqlSelectPreviewIconStmt.BindInt(msgIdIndex, msgId);
       
   834 
       
   835             // get preview-icon from DB
       
   836             err = sqlSelectPreviewIconStmt.Next();
       
   837             QCRITICAL_WRITE_FORMAT("Error from Next()", err)
       
   838 
       
   839             if (err == KSqlAtRow)
       
   840             {
       
   841                 TInt previewIconIndex = sqlSelectPreviewIconStmt.ColumnIndex(
       
   842                     _L("preview_icon"));
       
   843 
       
   844                 RSqlColumnReadStream stream;
       
   845 
       
   846                 //Get data from binary column BLOB
       
   847                 err = stream.ColumnBinary(sqlSelectPreviewIconStmt,
       
   848                     previewIconIndex);
       
   849 
       
   850                 QCRITICAL_WRITE_FORMAT("Error from ColumnBinary()", err)
       
   851 
       
   852                 if (err == KErrNone)
       
   853                 {
       
   854                     CFbsBitmap *bitmap = new CFbsBitmap;
       
   855                     TRAPD(err,bitmap->InternalizeL(stream));
       
   856                     QCRITICAL_WRITE_FORMAT("Error from bitmap InternalizeL()", err)
       
   857 
       
   858                     //convert bitmap to pixmap
       
   859                     if (err == KErrNone)
       
   860                     {
       
   861                         TSize size = bitmap->SizeInPixels();
       
   862                         int bytesPerLine = bitmap->ScanLineLength(size.iWidth,
       
   863                             bitmap->DisplayMode());
       
   864                         const uchar* dataPtr =
       
   865                                 (const uchar*) bitmap->DataAddress();
       
   866 
       
   867                         pixmap = QPixmap::fromImage(QImage(dataPtr,
       
   868                             size.iWidth, size.iHeight, bytesPerLine,
       
   869                             QImage::Format_RGB16));
       
   870 
       
   871                         imagePreviewed = true;
       
   872 
       
   873                         QCRITICAL_WRITE("Bitmap Conversion completed")
       
   874                     }
       
   875                     //remove bitmap
       
   876                     delete bitmap;
       
   877                 }
       
   878                 //close stream
       
   879                 stream.Close();
       
   880             }
       
   881         }
       
   882         sqlSelectPreviewIconStmt.Close();
       
   883     }
       
   884 
       
   885     // if not found in db, set from file path
       
   886     if (!imagePreviewed)
       
   887     {
       
   888         QPixmap orgPixmap(filePath);
       
   889         pixmap = orgPixmap.scaled(63.65, 63.65, Qt::IgnoreAspectRatio);
       
   890     }
       
   891     HbIcon * previewIcon = new HbIcon(pixmap);
       
   892 
       
   893     previewIconCache.insert(msgId, previewIcon);
       
   894 
       
   895     QCRITICAL_WRITE("ConversationsModel::updatePreviewIcon end.")
       
   896 
       
   897 }
       
   898 
       
   899 //---------------------------------------------------------------
       
   900 // ConversationsModel::clearModel()
       
   901 // @see header
       
   902 //---------------------------------------------------------------
       
   903 void ConversationsModel::clearModel()
       
   904 {
       
   905     clear(); 
       
   906     previewIconCache.clear();
       
   907 }
       
   908 
   677 //EOF
   909 //EOF