phonebookui/pbkcommonui/src/cnthistoryview.cpp
changeset 27 de1630741fbe
parent 25 76a2435edfd4
child 31 2a11b5b00470
--- a/phonebookui/pbkcommonui/src/cnthistoryview.cpp	Fri Apr 16 14:53:18 2010 +0300
+++ b/phonebookui/pbkcommonui/src/cnthistoryview.cpp	Mon May 03 12:24:20 2010 +0300
@@ -17,36 +17,44 @@
 
 #include "cnthistoryview.h"
 
-#include <QSqlDatabase>
 #include <hblistview.h>
 #include <hbgroupbox.h>
-#include <hblistviewitem.h>
-#include <hbabstractviewitem.h>
+#include <hbdocumentloader.h>
 #include <hbmenu.h>
+#include <hbview.h>
+#include <hbmessagebox.h>
+#include <hbaction.h>
 #include <xqservicerequest.h>
-#include <mobhistorymodel.h>
-#include "cnthistoryviewitem.h" 
+#include <cnthistorymodel.h>
+
+#include "cnthistoryviewitem.h"
+#include "qtpbkglobal.h"
 
 const char *CNT_HISTORYVIEW_XML = ":/xml/contacts_history.docml";
 
 /*!
 Constructor, initialize member variables.
-\a viewManager is the parent that creates this view. \a parent is a pointer to parent QGraphicsItem (by default this is 0)
 */
-CntHistoryView::CntHistoryView(CntViewManager *viewManager, QGraphicsItem *parent) : 
-    CntBaseView(viewManager, parent),
-    mHistoryListView(0),
-    mHistoryModel(0),
-    mContact(0),
-    mIsMyCard(false)
+CntHistoryView::CntHistoryView() : 
+    mHistoryListView(NULL),
+    mHistoryModel(NULL),
+    mDocumentLoader(NULL),
+    mViewMgr(NULL),
+    mContact(NULL)
 {
     bool ok = false;
-    ok = loadDocument(CNT_HISTORYVIEW_XML);
-
+    
+    docLoader()->load(CNT_HISTORYVIEW_XML, &ok);
+    
     if (ok)
     {
-        QGraphicsWidget *content = findWidget(QString("content"));
-        setWidget(content);
+        mView = static_cast<HbView*>(docLoader()->findWidget(QString("view")));
+        
+        // Create a back key action an set it as the default navigation
+        // action once the back key is pressed
+        mBackKey = new HbAction(Hb::BackNaviAction, mView);
+        mView->setNavigationAction(mBackKey);        
+        connect(mBackKey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
     }
     else
     {
@@ -59,56 +67,63 @@
 */
 CntHistoryView::~CntHistoryView()
 {
-    delete mContact;
-    delete mHistoryModel;
+    if (mDocumentLoader) {
+        delete mDocumentLoader;
+        mDocumentLoader = NULL;
+    }
+    if (mHistoryModel) {
+        delete mHistoryModel;
+        mHistoryModel = NULL;
+    }
+    if (mContact) {
+        delete mContact;
+        mContact = NULL;
+    }
 }
 
 /*!
-Activates a previous view
-*/
-void CntHistoryView::aboutToCloseView()
+ * Deactivate the view
+ */
+void CntHistoryView::deactivate()
 {
-    CntViewParameters args;//( CntViewParameters::commLauncherView );
-    args.setSelectedContact( *mContact );
-    viewManager()->back( args );
-        /*
-    viewManager()->previousViewParameters().setSelectedContact(*mContact);
-    viewManager()->onActivatePreviousView();
-    */
 }
 
-void CntHistoryView::activateView(const CntViewParameters &viewParameters)
-{ 
-    QContact contact = viewParameters.selectedContact();
-    mContact = new QContact(contact);
+/**
+ * Activate the view
+ */
+void CntHistoryView::activate( CntAbstractViewManager* aMgr, const CntViewParameters aArgs )
+{
+    mViewMgr = aMgr;
+    mContact = new QContact(aArgs.value(ESelectedContact).value<QContact>());
     
-    //group box
-    HbGroupBox* groupBox = static_cast<HbGroupBox *>(findWidget(QString("cnt_groupbox_history")));
-    QString text = hbTrId("History with %1").arg(contactManager()->synthesizedDisplayLabel(contact));
-    groupBox->setHeading(text);
+    // Set history view heading
+    HbGroupBox* groupBox = static_cast<HbGroupBox*>(docLoader()->findWidget(QString("groupBox")));
+    groupBox->setHeading(hbTrId("txt_phob_subtitle_history_with_1").arg(mContact->displayLabel()));
     
     //construct listview
-    mHistoryListView = static_cast<HbListView*>(findWidget(QString("cnt_listview_history")));
-    
-    //bubble graphics - create our custom list view item to have different bubbles
-    //for incoming and outgoing messages 
+    mHistoryListView = static_cast<HbListView*>(docLoader()->findWidget(QString("listView")));
     CntHistoryViewItem *item = new CntHistoryViewItem;
     mHistoryListView->setItemPrototype(item); //ownership is taken
-    connect(mHistoryListView, SIGNAL(longPressed(HbAbstractViewItem *, const QPointF &)),
-                      this,  SLOT(longPressed(HbAbstractViewItem *, const QPointF &)));
+    mHistoryListView->setUniformItemSizes(true);
+    
+    // Connect listview items to respective slots
     connect(mHistoryListView, SIGNAL(activated(const QModelIndex &)),
-                      this,  SLOT(pressed(const QModelIndex &)));
-    
-    mHistoryModel = new MobHistoryModel(contact.localId(), contactManager());
+                      this,  SLOT(itemActivated(const QModelIndex &)));
+    mHistoryModel = new CntHistoryModel(mContact->localId(),
+                                        mViewMgr->contactManager(SYMBIAN_BACKEND));
     mHistoryListView->setModel(mHistoryModel); //ownership is not taken
     
     //start listening to the events amount changing in the model
     connect(mHistoryModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
         this, SLOT(updateScrollingPosition()));
     connect(mHistoryModel, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
-        this, SLOT(rowsRemoved(const QModelIndex &, int, int)));
-    connect(mHistoryModel, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
+        this, SLOT(updateScrollingPosition()));
+    connect(mHistoryModel, SIGNAL(layoutChanged()),
         this, SLOT(updateScrollingPosition()));
+    
+    // Connect the menu options to respective slots
+    HbAction* clearHistory = static_cast<HbAction*>(docLoader()->findObject("cnt:clearhistory"));
+    connect(clearHistory, SIGNAL(triggered()), this, SLOT(clearHistory()));
 }
 
 /*!
@@ -117,59 +132,50 @@
 void CntHistoryView::updateScrollingPosition()
 {
     int rowCnt = mHistoryModel->rowCount();
+    
+    // Scroll to the last item in the list
     mHistoryListView->scrollTo(mHistoryModel->index(rowCnt - 1, 0), 
         HbAbstractItemView::PositionAtBottom);
 }
 
-/*!
-Add actions to menu
-*/
-void CntHistoryView::addMenuItems()
-{
-    CntActions* acts = actions();
-    acts->clearActionList();
-    acts->actionList() << acts->baseAction("cnt:clearhistory");
-    acts->addActionsToMenu(menu());
-        
-    connect(acts->baseAction("cnt:clearhistory"), SIGNAL(triggered()),
-            this, SLOT (clearHistory()));
-}
-
 /*
 Clear communications history
 */
 void CntHistoryView::clearHistory()
 {
-    mHistoryModel->clearHistory();
+    // Ask the use if they want to clear the history
+    HbMessageBox *note = new HbMessageBox(hbTrId("txt_phob_info_clear_communications_history_with_1"),
+                HbMessageBox::MessageTypeQuestion);
+    
+    note->setPrimaryAction(new HbAction(hbTrId("txt_phob_button_delete"), note));
+    note->setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"), note));
+    HbAction *selected = note->exec();
+    if (selected == note->primaryAction())
+    {
+        // Clear comm history
+        mHistoryModel->clearHistory();
+    }
+    delete note;
 }
 
 /*!
-Called when a list item is longpressed
-*/
-void CntHistoryView::longPressed(HbAbstractViewItem *item, const QPointF &coords)
+Once list item is pressed on the list view this slot handles the 
+emitted signal
+ */
+void CntHistoryView::itemActivated(const QModelIndex &index)
 {
-    Q_UNUSED(item);
-    
-    HbMenu* menu = new HbMenu();
-    HbAction* clearAction = menu->addAction(hbTrId("txt_phob_menu_clear_history"));
-    
-    HbAction* selectedAction = menu->exec(coords);
+    QVariant itemType = index.data(CntHistoryModel::ItemTypeRole);
     
-    if (selectedAction && selectedAction == clearAction) {
-        // Clear comm history
-        mHistoryModel->clearHistory();
-    }
+    if (!itemType.isValid())
+        return;
     
-    menu->deleteLater();
-}
-
-void CntHistoryView::pressed(const QModelIndex &index)
-{
-    QVariant itemType = mHistoryModel->data(index, MobHistoryModel::ItemTypeRole);
-    
-    if (itemType.toInt() == MobHistoryModel::CallLog) {
+    // If the list item is a call log a call is made to that item
+    if (itemType.toInt() == CntHistoryModel::CallLog) {
         // Make a call
-        QVariant v = mHistoryModel->data(index, MobHistoryModel::PhoneNumberRole);
+        QVariant v = index.data(CntHistoryModel::PhoneNumberRole);
+        if (!v.isValid())
+            return;
+        
         QString service("com.nokia.services.telephony");
         QString type("dial(QString)");
         XQServiceRequest snd(service, type, false);
@@ -178,4 +184,27 @@
     }
 }
 
+/*!
+Go back to previous view
+*/
+void CntHistoryView::showPreviousView()
+{
+    CntViewParameters viewParameters;
+    QVariant var;
+    var.setValue(*mContact);
+    viewParameters.insert(ESelectedContact, var);
+    mViewMgr->back(viewParameters);
+}
+
+/*!
+ * Document loader
+ */
+HbDocumentLoader* CntHistoryView::docLoader()
+{
+    if (!mDocumentLoader) {
+        mDocumentLoader = new HbDocumentLoader();
+    }
+    return mDocumentLoader;
+}
+
 // end of file