phonebookui/pbkcommonui/src/cntcollectionview.cpp
changeset 25 76a2435edfd4
parent 24 0ba2181d7c28
child 27 de1630741fbe
--- a/phonebookui/pbkcommonui/src/cntcollectionview.cpp	Fri Mar 19 09:27:18 2010 +0200
+++ b/phonebookui/pbkcommonui/src/cntcollectionview.cpp	Fri Apr 16 14:53:18 2010 +0300
@@ -16,31 +16,70 @@
 */
 
 #include "cntcollectionview.h"
+#include "cntgroupselectionpopup.h"
+#include "cntgroupdeletepopup.h"
+#include "cntcollectionlistmodel.h"
+#include "qtpbkglobal.h"
 
 #include <hblistview.h>
 #include <hblistviewitem.h>
 #include <hbmenu.h>
 #include <hbnotificationdialog.h>
-#include <hblabel.h>
-#include "hbinputdialog.h"
-#include "hbdialog.h"
-#include "hbaction.h"
-#include "cntgroupselectionpopup.h"
+#include <hbinputdialog.h>
+#include <hbdialog.h>
+#include <hbaction.h>
+#include <hbview.h>
+#include <hbframebackground.h>
+#include <hbgroupbox.h>
+#include <hbmessagebox.h>
 
-const char *CNT_COLLECTIONVIEW_ACTIONS_XML = ":/xml/contacts_actions.docml";
+const char *CNT_COLLECTIONVIEW_XML = ":/xml/contacts_collections.docml";
 
 /*!
 
 */
-CntCollectionView::CntCollectionView(CntViewManager *viewManager, QGraphicsItem *parent)
-    : CntBaseListView(viewManager, parent),
+CntCollectionView::CntCollectionView() :
+    mView(0),
+    mSoftkey(0),
+    mViewManager(0),
     mModel(0),
-    mReorderAction(0), 
-    mDeleteGroupAction(0), 
-    mDisconnectAllAction(0),
-    mOptionsMenu(0)
+    mListView(0),
+    mNamesAction(0),
+    mRefreshAction(0),
+    mNewGroupAction(0),
+    mDeleteGroupsAction(0)
 {
-    setBannerName(hbTrId("txt_phob_subtitle_groups"));
+    bool ok = false;
+    mDocumentLoader.load(CNT_COLLECTIONVIEW_XML, &ok);
+
+    if (ok)
+    {
+        mView = static_cast<HbView*>(mDocumentLoader.findWidget(QString("view")));
+    }
+    else
+    {
+        qFatal("Unable to read :/xml/contacts_collections.docml");
+    }
+    
+    //back button
+    mSoftkey = new HbAction(Hb::BackNaviAction, mView);
+    connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
+    
+    // menu actions
+    mDeleteGroupsAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:deletegroups"));
+    mDeleteGroupsAction->setParent(mView);
+    connect(mDeleteGroupsAction, SIGNAL(triggered()), this, SLOT(deleteGroups()));
+    
+    // toolbar actions
+    mNamesAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:names"));
+    mNamesAction->setParent(mView);
+    connect(mNamesAction, SIGNAL(triggered()), this, SLOT(showPreviousView()));
+    mRefreshAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:refresh"));
+    mRefreshAction->setParent(mView);
+    connect(mRefreshAction, SIGNAL(triggered()), this, SLOT(refreshDataModel()));
+    mNewGroupAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:newgroup"));
+    mNewGroupAction->setParent(mView);
+    connect(mNewGroupAction, SIGNAL(triggered()), this, SLOT(newGroup()));
 }
 
 /*!
@@ -48,118 +87,145 @@
 */
 CntCollectionView::~CntCollectionView()
 {
-delete mModel;
-mModel = 0;
-delete mOptionsMenu;
-mOptionsMenu = 0;
+    mView->deleteLater();
 }
 
 /*!
-
+Called when activating the view
 */
-void CntCollectionView::aboutToCloseView()
+void CntCollectionView::activate( CntAbstractViewManager* aMgr, const CntViewParameters& aArgs )
 {
-    CntViewParameters viewParameters(CntViewParameters::namesView);
-    viewManager()->onActivateView(viewParameters);
+    Q_UNUSED(aArgs)
+    
+    if (mView->navigationAction() != mSoftkey)
+        mView->setNavigationAction(mSoftkey);   
+    
+    mViewManager = aMgr;
+
+    // disable delete group(s) button if only favorites group is present
+    QContactDetailFilter groupFilter;
+    groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
+    groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
+    QList<QContactLocalId> groupContactIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(groupFilter);
+    if (groupContactIds.count() < 2)
+    {
+        mDeleteGroupsAction->setEnabled(false);
+    }
+    
+    // set up the list
+    mListView = static_cast<HbListView*>(mDocumentLoader.findWidget(QString("listView")));
+    
+    connect(mListView, SIGNAL(activated(const QModelIndex&)), this,
+        SLOT(openGroup(const QModelIndex&)));
+    connect(mListView, SIGNAL(longPressed(HbAbstractViewItem*,QPointF)), this,
+        SLOT(showContextMenu(HbAbstractViewItem*,QPointF)));
+    
+    HbFrameBackground frame;
+    frame.setFrameGraphicsName("qtg_fr_list_normal");
+    frame.setFrameType(HbFrameDrawer::NinePieces);
+    mListView->itemPrototypes().first()->setDefaultFrame(frame);
+    
+    mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::LargeIcon);
+    
+    mModel = new CntCollectionListModel(mViewManager->contactManager(SYMBIAN_BACKEND), this);
+    mListView->setModel(mModel);
+}
+
+void CntCollectionView::deactivate()
+{
+
+}
+
+/*!
+Go back to previous view
+*/
+void CntCollectionView::showPreviousView()
+{
+    CntViewParameters viewParameters;
+    mViewManager->back(viewParameters);
 }
 
 /*!
 Called after user clicked on the listview.
 */
-void CntCollectionView::onListViewActivated(const QModelIndex &index)
+void CntCollectionView::openGroup(const QModelIndex &index)
 {
-    int id  = index.data(Qt::UserRole).toInt();
-    
-    if (id == -1)
+    int id = index.data(Qt::UserRole).toInt();
+    int favoriteGrpId = mModel->favoriteGroupId();
+
+    if (id == favoriteGrpId )
     {
-        CntViewParameters viewParameters(CntViewParameters::collectionFavoritesView);
-        viewManager()->onActivateView(viewParameters);
+        QContact favoriteGroup = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(favoriteGrpId);
+        QContactRelationshipFilter rFilter;
+        rFilter.setRelationshipType(QContactRelationship::HasMember);
+        rFilter.setRelatedContactRole(QContactRelationshipFilter::First);
+        rFilter.setRelatedContactId(favoriteGroup.id());
+        // group members and their count
+        QList<QContactLocalId> groupMemberIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(rFilter);
+        
+        if (groupMemberIds.isEmpty())
+        {
+            CntViewParameters viewParameters(CntViewParameters::collectionFavoritesView);
+            viewParameters.setSelectedContact(favoriteGroup);
+            mViewManager->changeView(viewParameters);
+        }
+        else 
+        {
+            CntViewParameters viewParameters(CntViewParameters::FavoritesMemberView);
+            viewParameters.setSelectedContact(favoriteGroup);
+            mViewManager->changeView(viewParameters);
+        }
     }
     else
     {
-        QContact groupContact = contactManager()->contact(id);
-        CntViewParameters viewParameters(CntViewParameters::groupActionsView);
+        QContact groupContact = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(id);
+        CntViewParameters viewParameters(CntViewParameters::groupMemberView);
         viewParameters.setSelectedContact(groupContact);
-        viewManager()->onActivateView(viewParameters);
+        mViewManager->changeView(viewParameters);
     }
 }
 
-void CntCollectionView::onLongPressed(HbAbstractViewItem *item, const QPointF &coords)
+void CntCollectionView::showContextMenu(HbAbstractViewItem *item, const QPointF &coords)
 {
+    int id = item->modelIndex().data(Qt::UserRole).toInt();
+    int favoriteGrpId = mModel->favoriteGroupId();
+    
     HbMenu *menu = new HbMenu();
     HbAction *openAction = 0;
     HbAction *deleteAction = 0;
-    HbAction *homeScreenAction = 0;
     
     openAction = menu->addAction(hbTrId("txt_common_menu_open"));
     
-    if (item->modelIndex().data(Qt::UserRole).toInt() > -1)
+    if (id != favoriteGrpId)
     {
         deleteAction = menu->addAction(hbTrId("txt_phob_menu_delete_group"));
     }
     
-    homeScreenAction = menu->addAction(hbTrId("txt_phob_opt_send_to_homescreen_as_widget"));
-    homeScreenAction->setEnabled(false);
-    
     HbAction *selectedAction = menu->exec(coords);
 
     if (selectedAction)
     {
         if (selectedAction == openAction)
         {
-            onListViewActivated(item->modelIndex());
+            openGroup(item->modelIndex());
         }
         else if (selectedAction == deleteAction)
         {
-            connect(commands(), SIGNAL(commandExecuted(QString, QContact)), this, 
-                    SLOT(handleExecutedCommand(QString, QContact)));
-            QContact groupContact = contactManager()->contact(item->modelIndex().data(Qt::UserRole).toInt());
-            commands()->deleteContact(groupContact);
-        }
-        else if (selectedAction == homeScreenAction)
-        {
-
+            QContact groupContact = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(id);
+            deleteGroup(groupContact);
         }
     }
     menu->deleteLater();
 }
 
-void CntCollectionView::handleExecutedCommand(QString command, QContact contact)
-{
-    if (command == "delete")
-    {
-        static_cast<CntCollectionListModel*>(listView()->model())->removeGroup(contact.localId());
-    }
-}
-
-/*!
-Add actions also to toolbar
-*/
-void CntCollectionView::addActionsToToolBar()
-{
-    actions()->clearActionList();
-    actions()->actionList() << actions()->baseAction("cnt:nameslist") << actions()->baseAction("cnt:collections")
-        << actions()->baseAction("cnt:refresh") << actions()->baseAction("cnt:newgroup");
-    actions()->addActionsToToolBar(toolBar());
-
-    connect(actions()->baseAction("cnt:nameslist"), SIGNAL(triggered()),
-       this, SLOT (aboutToCloseView()));
-
-    actions()->baseAction("cnt:collections")->setEnabled(false);
-
-    connect(actions()->baseAction("cnt:newgroup"), SIGNAL(triggered()),
-               this, SLOT (newGroup()));
-}
-
 void CntCollectionView::newGroup()
 {
     QString mTextOfNewItem("");
     
     HbInputDialog popup;
     
-    HbGroupBox *headingLabel = new HbGroupBox();
-    HbLabel *label = new HbLabel(hbTrId("txt_phob_title_new_group_name"));    
-    headingLabel->setContentWidget(label);
+    HbGroupBox *headingLabel = new HbGroupBox(&popup);  
+    headingLabel->setHeading("txt_phob_title_new_group_name");
     popup.setHeadingWidget(headingLabel);
     popup.setPrimaryAction(new HbAction(hbTrId("txt_phob_button_create"),&popup));
     popup.setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"),&popup));
@@ -176,97 +242,46 @@
         QContact groupContact;
         groupContact.setType(QContactType::TypeGroup);
         
-        
         QContactName groupName;
         groupName.setCustomLabel(mTextOfNewItem);
         
         groupContact.saveDetail(&groupName);
-        contactManager()->saveContact(&groupContact);
+        mViewManager->contactManager(SYMBIAN_BACKEND)->saveContact(&groupContact);
         
         // call a dialog to display the contacts
         
-        CntGroupSelectionPopup *groupSelectionPopup = new CntGroupSelectionPopup(contactManager(),contactModel(),&groupContact);
+        CntGroupSelectionPopup *groupSelectionPopup = 
+            new CntGroupSelectionPopup(mViewManager->contactManager(SYMBIAN_BACKEND), &groupContact);
         groupSelectionPopup->populateListOfContact();
         HbAction* action = groupSelectionPopup->exec();
         if (action == groupSelectionPopup->primaryAction())
         {
             groupSelectionPopup->saveNewGroup();
-            CntViewParameters viewParameters(CntViewParameters::groupActionsView);
+            CntViewParameters viewParameters(CntViewParameters::groupMemberView);
             viewParameters.setSelectedContact(groupContact);
-            viewManager()->onActivateView(viewParameters);
+            mViewManager->changeView(viewParameters);
             delete groupSelectionPopup;
         }
         else if (action == groupSelectionPopup->secondaryAction())
         {
             delete groupSelectionPopup;
-            QString groupNameCreated(groupName.value( QContactName::FieldCustomLabel ));
+            QString groupNameCreated(groupName.customLabel());
             HbNotificationDialog::launchDialog(hbTrId("txt_phob_dpophead_new_group_1_created").arg(groupNameCreated));
             //refresh the page 
             refreshDataModel();
-            //reconstruct the menu items 
-            addMenuItems();
+            mDeleteGroupsAction->setEnabled(true);
         }
         
     }
 }
 
-void CntCollectionView::setDataModel()
-{
-    HbListViewItem *prototype = listView()->listItemPrototype();
-    prototype->setGraphicsSize(HbListViewItem::LargeIcon);
-    mModel = new CntCollectionListModel(contactManager(), this);
-    listView()->setModel(mModel);
-}
-
 void CntCollectionView::refreshDataModel()
 {
+    mListView->setModel(0);
     delete mModel;
     mModel = 0;
-    mModel = new CntCollectionListModel(contactManager(), this);
-    listView()->setModel(mModel);
-}
-
-void CntCollectionView::addMenuItems()
-{
-    bool ok = false;
-    HbDocumentLoader documentLoader;
-    documentLoader.load(CNT_COLLECTIONVIEW_ACTIONS_XML, &ok);
-    if (!ok)
-    {
-        qFatal("Unable to read :/xml/contacts_actions.docml");
-    }
-    
-    //Uncomment this once spec for this is ready
-  //  mReorderAction = qobject_cast<HbAction *>(documentLoader.findObject("cnt:reordergroups"));
-    mDeleteGroupAction = qobject_cast<HbAction* >(documentLoader.findObject("cnt:deletegroups"));
- 
-    //Uncomment this once spec for this is ready
-  //  mDisconnectAllAction =qobject_cast<HbAction *>(documentLoader.findObject("cnt:disconnectall"));
-    
-    delete mOptionsMenu;
-    mOptionsMenu = 0;
-    mOptionsMenu = new HbMenu();
-    //Uncomment this once spec for this is ready
-    //mOptionsMenu->addAction(mReorderAction);
-    
-    //add deletegroup action to option menu if group presents 
-    QContactDetailFilter groupFilter;
-    groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
-    groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup)));
-    QList<QContactLocalId> groupContactIds = contactManager()->contacts(groupFilter);
-    if (!groupContactIds.isEmpty())
-        {
-        mOptionsMenu->addAction(mDeleteGroupAction);
-        }
-    
-    //Uncomment this once spec for this is ready
- //   mOptionsMenu->addAction(mDisconnectAllAction);
-    
- //   connect(mReorderAction, SIGNAL(triggered()), this, SLOT (reorderGroup()));
-    connect(mDeleteGroupAction, SIGNAL(triggered()), this, SLOT (deleteGroups()));  
-   // connect(mDisconnectAllAction, SIGNAL(triggered()), this, SLOT (disconnectAll()));
-
-    setMenu(mOptionsMenu);
+    mModel = new CntCollectionListModel(mViewManager->contactManager(SYMBIAN_BACKEND), this);
+    mListView->setModel(mModel);
 }
 
 void CntCollectionView::reorderGroup()
@@ -274,27 +289,57 @@
     // wait for specs
 }
 
+void CntCollectionView::deleteGroup(QContact group)
+{
+    QString name = group.displayLabel();
+
+    HbMessageBox *note = new HbMessageBox(hbTrId("txt_phob_info_delete_1").arg(name), 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())
+    {
+        mViewManager->contactManager(SYMBIAN_BACKEND)->removeContact(group.localId());
+        mModel->removeGroup(group.localId());
+        
+        // disable delete group(s) button if only favorites group is present
+        QContactDetailFilter groupFilter;
+        groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
+        groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
+        QList<QContactLocalId> groupContactIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(groupFilter);
+        if (groupContactIds.count() < 2)
+        {
+            mDeleteGroupsAction->setEnabled(false);
+        }
+    }
+    delete note;
+}
+
 void CntCollectionView::deleteGroups()
 {
     // save the group here
-    QContact groupContact;
-    groupContact.setType(QContactType::TypeGroup);
-    
-    CntGroupSelectionPopup *groupSelectionPopup = new CntGroupSelectionPopup(contactManager(),contactModel(),&groupContact);
+    CntGroupDeletePopup *groupDeletePopup = new CntGroupDeletePopup(mViewManager->contactManager(SYMBIAN_BACKEND));
     
-    groupSelectionPopup->populateListOfGroup();
-    HbAction* action = groupSelectionPopup->exec();
-    if (action == groupSelectionPopup->primaryAction())
+    groupDeletePopup->populateListOfGroup();
+    HbAction* action = groupDeletePopup->exec();
+    if (action == groupDeletePopup->primaryAction())
     {   
-        groupSelectionPopup->deleteGroup();
+        groupDeletePopup->deleteGroup();
     }
     
-    delete groupSelectionPopup;
-    
+    delete groupDeletePopup;
     //refresh the page 
     refreshDataModel();
-    //reconstruct the menu items 
-    addMenuItems();
+    
+    // disable delete group(s) button if only favorites group is present
+    QContactDetailFilter groupFilter;
+    groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
+    groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
+    QList<QContactLocalId> groupContactIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(groupFilter);
+    if (groupContactIds.count() < 2)
+    {
+        mDeleteGroupsAction->setEnabled(false);
+    }
 }
 
 void CntCollectionView::disconnectAll()