messagingapp/msgui/msgapp/src/msglistview.cpp
branchRCL_3
changeset 57 ebe688cedc25
equal deleted inserted replaced
54:fa1df4b99609 57:ebe688cedc25
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:Message List View for the messaging application.
       
    15  *
       
    16  */
       
    17 
       
    18 #include "msglistview.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <hbmenu.h>
       
    22 #include <hbaction.h>
       
    23 #include <hbtoolbar.h>
       
    24 #include <hbtoolbarextension.h>
       
    25 #include <hblistview.h>
       
    26 #include <hblistwidget.h>
       
    27 #include <hblistwidgetitem.h>
       
    28 #include <hbgroupbox.h>
       
    29 #include <hbmessagebox.h>
       
    30 #include <hbframebackground.h>
       
    31 #include <QSortFilterProxyModel>
       
    32 #include <QGraphicsLinearLayout>
       
    33 #include <hblistviewitem.h>
       
    34 #include <HbStyleLoader>
       
    35 #include <HbMainWindow>
       
    36 #include <xqaiwrequest.h>
       
    37 #include <xqappmgr.h>
       
    38 #include <qtcontacts.h>
       
    39 
       
    40 // USER INCLUDES
       
    41 #include "msgconversationviewinterface.h"
       
    42 #include "debugtraces.h"
       
    43 #include "conversationsengine.h"
       
    44 #include "conversationsenginedefines.h"
       
    45 #include "convergedmessage.h"
       
    46 #include "msglistviewitem.h"
       
    47 
       
    48 QTM_USE_NAMESPACE
       
    49 
       
    50 //Icons
       
    51 const QString POPUP_LIST_FRAME("qtg_fr_popup_list_normal");
       
    52 const QString NEW_MESSAGE_ICON("qtg_mono_create_message");
       
    53 const QString SORT_ICON("qtg_mono_sort");
       
    54 
       
    55 //Localized constants
       
    56 
       
    57 #define LOC_DIALOG_DELETE_CONVERSATION hbTrId("txt_messaging_dialog_delete_conversation")
       
    58 
       
    59 //itemspecific menu
       
    60 #define LOC_OPEN hbTrId("txt_common_menu_open")
       
    61 #define LOC_SAVETO_CONTACTS hbTrId("txt_messaging_menu_save_to_contacts")
       
    62 #define LOC_DELETE_CONVERSATION hbTrId("txt_messaging_menu_delete_conversation")
       
    63 #define LOC_OPEN_CONTACT_INFO hbTrId("txt_messaging_menu_contact_info")
       
    64 
       
    65 //main menu
       
    66 #define LOC_SETTINGS    hbTrId("txt_messaging_opt_settings")
       
    67 
       
    68 #define LOC_TB_VIEW_EXTN hbTrId("txt_messaging_button_view")
       
    69 #define LOC_TB_EXTN_DRAFTS hbTrId("txt_messaging_button_drafts")
       
    70 #define LOC_TB_EXTN_CONVERSATIONS hbTrId("txt_messaging_button_conversations")
       
    71 
       
    72 #define LOC_VIEW_HEADING hbTrId("txt_messaging_title_conversations")
       
    73 
       
    74 //---------------------------------------------------------------
       
    75 // MsgListView::MsgListView
       
    76 // @see header
       
    77 //---------------------------------------------------------------
       
    78 MsgListView::MsgListView(QGraphicsItem *parent) :
       
    79     MsgBaseView(parent)
       
    80     {
       
    81     //These changes are needed for splash implementation to avoid flicker
       
    82     setupToolBar();
       
    83     setupMenu();
       
    84 
       
    85     // Create parent layout.
       
    86     mMainLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
    87     mMainLayout->setContentsMargins(0, 0, 0, 0);
       
    88     mMainLayout->setSpacing(0);
       
    89 
       
    90     // Create view heading.
       
    91     HbGroupBox *viewHeading = new HbGroupBox();
       
    92     viewHeading->setHeading(LOC_VIEW_HEADING);
       
    93 
       
    94     // Add view heading widget to main layout.
       
    95     mMainLayout->addItem(viewHeading);
       
    96 
       
    97     this->setLayout(mMainLayout);
       
    98 
       
    99     connect(this->mainWindow(), SIGNAL(viewReady()), this,
       
   100             SLOT(doDelayedConstruction()));
       
   101     }
       
   102 
       
   103 //---------------------------------------------------------------
       
   104 // MsgListView::~MsgListView
       
   105 // @see header
       
   106 //---------------------------------------------------------------
       
   107 MsgListView::~MsgListView()
       
   108 {
       
   109     
       
   110 }
       
   111 
       
   112 //---------------------------------------------------------------
       
   113 // MsgListView::longPressed
       
   114 // @see header
       
   115 //---------------------------------------------------------------
       
   116 void MsgListView::longPressed(HbAbstractViewItem* viewItem, const QPointF& point)
       
   117 {
       
   118     if (this->isVisible()) {
       
   119         // Set the current index as the present Item's index.
       
   120         // By default it will not be set.
       
   121         mMsgList->setCurrentIndex(viewItem->modelIndex(), QItemSelectionModel::Select);
       
   122 
       
   123         // Create new menu
       
   124         HbMenu *contextMenu = new HbMenu();
       
   125         contextMenu->setAttribute(Qt::WA_DeleteOnClose);
       
   126         //open menu option
       
   127         contextMenu->addAction(LOC_OPEN,this,SLOT(openConversation()));
       
   128         
       
   129         //save to contacts for unresolved.
       
   130         int msgType = viewItem->modelIndex().data(MessageType).toInt();
       
   131         if(msgType == ConvergedMessage::Sms || 
       
   132            msgType == ConvergedMessage::Mms || 
       
   133            msgType == ConvergedMessage::BioMsg)
       
   134             {
       
   135             qint64 contactId = mMsgList->currentIndex().data(ContactId).toLongLong();
       
   136             if(contactId < 0)
       
   137                 {
       
   138                 contextMenu->addAction(LOC_SAVETO_CONTACTS,this,SLOT(saveToContacts()));
       
   139                 }
       
   140             else
       
   141                 {
       
   142                 contextMenu->addAction(LOC_OPEN_CONTACT_INFO,this,SLOT(contactInfo()));
       
   143                 }
       
   144             }
       
   145         
       
   146         //delete conversation
       
   147         contextMenu->addAction(LOC_DELETE_CONVERSATION,this,SLOT(deleteItem()));
       
   148 
       
   149         contextMenu->setPreferredPos(point);    
       
   150         contextMenu->show();
       
   151     }
       
   152 }
       
   153 
       
   154 //---------------------------------------------------------------
       
   155 // MsgListView::openConversation
       
   156 // @see header
       
   157 //---------------------------------------------------------------
       
   158 void MsgListView::openConversation(const QModelIndex& index)
       
   159 {
       
   160     //TODO: model populating possibilities.
       
   161     if (index.isValid()) {
       
   162         QVariant conversationId = index.data(ConversationId);
       
   163 
       
   164         // Launch conversation view
       
   165         QVariantList param;
       
   166         param << MsgBaseView::CV; // target view
       
   167         param << MsgBaseView::CLV; // source view
       
   168 
       
   169         param << conversationId;
       
   170 
       
   171         emit switchView(param);
       
   172     }
       
   173 }
       
   174 
       
   175 //---------------------------------------------------------------
       
   176 // MsgListView::sendNewMessage
       
   177 // @see header
       
   178 //---------------------------------------------------------------
       
   179 void MsgListView::sendNewMessage()
       
   180 {
       
   181     QVariantList param;
       
   182     param << MsgBaseView::UNIEDITOR; // target view
       
   183     param << MsgBaseView::CLV; // source view
       
   184 
       
   185     emit switchView(param);
       
   186 }
       
   187 
       
   188 //---------------------------------------------------------------
       
   189 // MsgListView::settings
       
   190 // @see header
       
   191 //---------------------------------------------------------------
       
   192 void MsgListView::settings()
       
   193 {
       
   194     // Launch message settings
       
   195     QVariantList param;
       
   196     param << MsgBaseView::MSGSETTINGS;
       
   197     param << MsgBaseView::CLV;
       
   198     emit switchView(param);
       
   199 }
       
   200 
       
   201 //---------------------------------------------------------------
       
   202 // MsgListView::openConversation
       
   203 // @see header
       
   204 //---------------------------------------------------------------
       
   205 void MsgListView::openConversation()
       
   206 {
       
   207     QModelIndex modelIndex = mMsgList->currentIndex();
       
   208     openConversation(modelIndex);
       
   209 }
       
   210 
       
   211 //---------------------------------------------------------------
       
   212 // MsgListView::showContact
       
   213 // @see header
       
   214 //---------------------------------------------------------------
       
   215 void MsgListView::showContact()
       
   216 {
       
   217 }
       
   218 
       
   219 //---------------------------------------------------------------
       
   220 // MsgListView::deleteItem
       
   221 // @see header
       
   222 //---------------------------------------------------------------
       
   223 void MsgListView::deleteItem()
       
   224 {
       
   225 #ifdef _DEBUG_TRACES_
       
   226     qDebug() << "Inside MsgListView::deleteItem";
       
   227 #endif
       
   228     //confirmation dialog.
       
   229     HbMessageBox::question(LOC_DIALOG_DELETE_CONVERSATION,
       
   230                            this,SLOT(onDialogDeleteMsg(HbAction*)),
       
   231                            HbMessageBox::Delete | HbMessageBox::Cancel);    
       
   232 #ifdef _DEBUG_TRACES_	
       
   233     qDebug() << " Leaving MsgConversationView::deleteItem";
       
   234 #endif
       
   235 }
       
   236 
       
   237 //---------------------------------------------------------------
       
   238 // MsgListView::setupView
       
   239 // @see header
       
   240 //---------------------------------------------------------------
       
   241 void MsgListView::setupListView()
       
   242     {
       
   243     // Register the custorm css path.
       
   244     HbStyleLoader::registerFilePath(":/clv");
       
   245 
       
   246     mMsgList = new HbListView(this);
       
   247     mMsgList->setScrollingStyle(HbScrollArea::PanOrFlick);
       
   248     mMsgList->setClampingStyle(HbScrollArea::BounceBackClamping);
       
   249 
       
   250     mMsgList->setLayoutName("custom");
       
   251     mMsgList->setItemRecycling(true);
       
   252     mMsgList->setUniformItemSizes(true);
       
   253 
       
   254     MsgListViewItem *prototype = new MsgListViewItem(this);
       
   255     mMsgList->setItemPrototype(prototype);
       
   256     mMsgList->setModel(ConversationsEngine::instance()->getConversationsSummaryModel());
       
   257     
       
   258     // Single tap list item
       
   259     connect(mMsgList, SIGNAL(activated(const QModelIndex&)), this,
       
   260             SLOT(openConversation(const QModelIndex&)));
       
   261 
       
   262     // Long tap list item
       
   263     connect(mMsgList,
       
   264             SIGNAL(longPressed(HbAbstractViewItem*, const QPointF&)), this,
       
   265             SLOT(longPressed(HbAbstractViewItem*, const QPointF&)));
       
   266 
       
   267     // enable tap after delete is completed
       
   268     connect(ConversationsEngine::instance(),
       
   269             SIGNAL(conversationListEntryDeleted( int )), this,
       
   270             SLOT(enableListitem( int )));
       
   271 
       
   272     // Add list view to main layout.
       
   273     mMainLayout->addItem(mMsgList);
       
   274     }
       
   275 
       
   276 //---------------------------------------------------------------
       
   277 // MsgListView::addMenu
       
   278 // @see header
       
   279 //---------------------------------------------------------------
       
   280 void MsgListView::setupMenu()
       
   281 {
       
   282     // Main menu
       
   283     HbMenu* mainMenu = this->menu();
       
   284     mainMenu->addAction(LOC_SETTINGS,this,SLOT(settings()));
       
   285 }
       
   286 
       
   287 //---------------------------------------------------------------
       
   288 // MsgListView::setupToolBar
       
   289 // @see header
       
   290 //---------------------------------------------------------------
       
   291 void MsgListView::setupToolBar()
       
   292 {
       
   293     // ToolBar
       
   294     HbToolBar *toolBar = this->toolBar();
       
   295     toolBar->setOrientation(Qt::Horizontal);
       
   296 
       
   297     // Create & setup ToolBar Extension
       
   298     HbToolBarExtension *viewExtn = new HbToolBarExtension();
       
   299     HbAction *viewAction = toolBar->addExtension(viewExtn);
       
   300     viewAction->setIcon(HbIcon(SORT_ICON));
       
   301 
       
   302     mViewExtnList = new HbListWidget();
       
   303     mViewExtnList->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
       
   304     mViewExtnList->addItem(LOC_TB_EXTN_DRAFTS);
       
   305     mViewExtnList->addItem(LOC_TB_EXTN_CONVERSATIONS);
       
   306 
       
   307     HbListViewItem *prototype = mViewExtnList->listItemPrototype();
       
   308     HbFrameBackground frame(POPUP_LIST_FRAME, HbFrameDrawer::NinePieces);
       
   309     prototype->setDefaultFrame(frame);
       
   310 
       
   311     connect(mViewExtnList, SIGNAL(activated(HbListWidgetItem*)),
       
   312             this,SLOT(handleViewExtnActivated(HbListWidgetItem*)));
       
   313     connect(mViewExtnList, SIGNAL(released(HbListWidgetItem*)), viewExtn, SLOT(close()));
       
   314 
       
   315     viewExtn->setContentWidget(mViewExtnList);
       
   316     
       
   317     // Create & setup 2nd ToolBar button.
       
   318     toolBar->addAction(HbIcon(NEW_MESSAGE_ICON),"",this,SLOT(sendNewMessage()));
       
   319 }
       
   320 
       
   321 //---------------------------------------------------------------
       
   322 // MsgListView::handleViewExtnActivated
       
   323 // @see header
       
   324 //---------------------------------------------------------------
       
   325 void MsgListView::handleViewExtnActivated(HbListWidgetItem *item)
       
   326 {
       
   327     int row = mViewExtnList->row(item);
       
   328     if (DRAFTS_EXTN == row) {
       
   329         QVariantList param;
       
   330         param << MsgBaseView::DLV; // target view
       
   331         param << MsgBaseView::CLV; // source view
       
   332 
       
   333         emit switchView(param);
       
   334     }
       
   335 }
       
   336 
       
   337 //---------------------------------------------------------------
       
   338 // MsgListView::doDelayedConstruction
       
   339 // @see header
       
   340 //---------------------------------------------------------------
       
   341 void MsgListView::doDelayedConstruction()
       
   342     {    
       
   343     setupListView();
       
   344     
       
   345     disconnect(this->mainWindow(),SIGNAL(viewReady()),this,SLOT(doDelayedConstruction()));
       
   346     }
       
   347 
       
   348 //---------------------------------------------------------------
       
   349 // MsgListView::saveToContacts
       
   350 // @see header
       
   351 //---------------------------------------------------------------
       
   352 void MsgListView::saveToContacts()
       
   353     {
       
   354     //save to contacts with phone number field prefilled.
       
   355     QList<QVariant> args;  
       
   356 
       
   357     QString data = mMsgList->currentIndex().data(DisplayName).toString();
       
   358 
       
   359     QString type = QContactPhoneNumber::DefinitionName;
       
   360 
       
   361     args << type;
       
   362     args << data;
       
   363 
       
   364     //service stuff.
       
   365     QString serviceName("com.nokia.services.phonebookservices");
       
   366     QString operation("editCreateNew(QString,QString)");
       
   367 
       
   368     XQAiwRequest* request;
       
   369     XQApplicationManager appManager;
       
   370     request = appManager.create(serviceName, "Fetch", operation, true); // embedded
       
   371     if ( request == NULL )
       
   372         {
       
   373         return;       
       
   374         }
       
   375 
       
   376     request->setArguments(args);
       
   377     request->send();
       
   378     
       
   379     delete request;
       
   380     }
       
   381 
       
   382 //---------------------------------------------------------------
       
   383 // MsgListView::contactInfo
       
   384 // @see header
       
   385 //---------------------------------------------------------------
       
   386 void MsgListView::contactInfo()
       
   387     {
       
   388     //open contact info.
       
   389     QList<QVariant> args;  
       
   390 
       
   391     int contactId = mMsgList->currentIndex().data(ContactId).toInt();
       
   392 
       
   393     args << contactId;
       
   394 
       
   395     //service stuff.
       
   396     QString serviceName("com.nokia.services.phonebookservices");
       
   397     QString operation("open(int)");
       
   398 
       
   399     XQAiwRequest* request;
       
   400     XQApplicationManager appManager;
       
   401     request = appManager.create(serviceName, "Fetch", operation, true); // embedded
       
   402     if ( request == NULL )
       
   403         {
       
   404         return;       
       
   405         }
       
   406 
       
   407     request->setArguments(args);
       
   408     request->send();
       
   409     
       
   410     delete request;
       
   411     }
       
   412 
       
   413 //---------------------------------------------------------------
       
   414 // MsgListView::onDialogDeleteMsg
       
   415 // @see header
       
   416 //---------------------------------------------------------------
       
   417 void MsgListView::onDialogDeleteMsg(HbAction* action)
       
   418     {
       
   419     HbMessageBox *dlg = qobject_cast<HbMessageBox*> (sender());
       
   420     if (action == dlg->actions().at(0))
       
   421         {
       
   422         QModelIndex index = mMsgList->currentIndex();
       
   423         if (index.isValid())
       
   424             {
       
   425             HbAbstractViewItem *item = mMsgList->currentViewItem();
       
   426             item->ungrabGesture(Qt::TapGesture);
       
   427             qint64 conversationId = index.data(ConversationId).toLongLong();
       
   428 
       
   429             ConversationsEngine::instance()->deleteConversations(
       
   430                     conversationId);
       
   431 
       
   432             }
       
   433 
       
   434         }
       
   435     }
       
   436 
       
   437 //---------------------------------------------------------------
       
   438 // MsgListView::enableListitem
       
   439 // @see header
       
   440 //---------------------------------------------------------------
       
   441 void MsgListView::enableListitem(int conversationId)
       
   442     {
       
   443     QAbstractItemModel* proxyModel = mMsgList->model();
       
   444     QModelIndexList indexList = proxyModel->match(proxyModel->index(0, 0),
       
   445             ConversationId, conversationId, 1, Qt::MatchExactly);
       
   446     HbAbstractViewItem* item = NULL;
       
   447     int count = indexList.count();
       
   448 	// only item is expected to be the result of the above match function
       
   449     if (1 == count)
       
   450         {
       
   451         item = mMsgList->itemByIndex(indexList[0]);
       
   452         if (item)
       
   453             {
       
   454             item->grabGesture(Qt::TapGesture);
       
   455             }
       
   456         }
       
   457 
       
   458     }
       
   459 
       
   460 //EOF