phonebookui/cntcommonui/views/cntimageeditorview.cpp
changeset 72 6abfb1094884
parent 61 d30183af6ca6
child 81 640d30f4fb64
equal deleted inserted replaced
67:59984e68247d 72:6abfb1094884
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cntimageeditorview.h"
       
    19 #include "cntthumbnailmanager.h"
       
    20 #include "cntimageutility.h"
       
    21 #include "cntsavemanager.h"
       
    22 #include "cntimagelabel.h"
       
    23 
       
    24 #include <hblabel.h>
       
    25 #include <xqaiwrequest.h>
       
    26 #include <xqaiwdecl.h>
       
    27 
       
    28 #include "cntdebug.h"
       
    29 #include "cntglobal.h"
       
    30 
       
    31 #include <hbaction.h>
       
    32 #include <hbview.h>
       
    33 #include <hbmainwindow.h>
       
    34 #include <hblistview.h>
       
    35 #include <hblistviewitem.h>
       
    36 #include <hbframebackground.h>
       
    37 #include <hbdevicenotificationdialog.h>
       
    38 #include <hbparameterlengthlimiter.h>
       
    39 
       
    40 #include <QStandardItemModel>
       
    41 #include <QApplication>
       
    42 
       
    43 const char *CNT_IMAGE_XML = ":/xml/contacts_if.docml";
       
    44 
       
    45 /*!
       
    46 Constructor
       
    47 */
       
    48 CntImageEditorView::CntImageEditorView() :
       
    49     mContact(NULL),
       
    50     mAvatar(NULL),
       
    51     mImageLabel(NULL),
       
    52     mRequest(NULL),
       
    53     mViewManager(NULL),
       
    54     mListView(NULL),
       
    55     mModel(NULL),
       
    56     mSaveManager(NULL)
       
    57 {
       
    58     bool ok = false;
       
    59     mDocumentLoader.load(CNT_IMAGE_XML, &ok);
       
    60 
       
    61     if (ok)
       
    62     {
       
    63         mView = static_cast<HbView*>(mDocumentLoader.findWidget(QString("view")));
       
    64     }
       
    65     else
       
    66     {
       
    67         qFatal("Unable to read :/xml/contacts_if.docml");
       
    68     }
       
    69     
       
    70     //back button
       
    71     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    72     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    73     
       
    74     // menu action
       
    75     mRemoveImage = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:removeimage"));
       
    76     connect(mRemoveImage, SIGNAL(triggered()), this, SLOT(removeImage()));
       
    77 
       
    78     // closing the application from task swapper or end key will cause the contact to be saved
       
    79     connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(saveContact()));
       
    80 }
       
    81 
       
    82 /*!
       
    83 Destructor
       
    84 */
       
    85 CntImageEditorView::~CntImageEditorView()
       
    86 {
       
    87     CNT_ENTRY
       
    88     
       
    89     mView->deleteLater();
       
    90 
       
    91     delete mAvatar;
       
    92     mAvatar = NULL;
       
    93     delete mContact;
       
    94     mContact = NULL;
       
    95     delete mRequest;
       
    96     mRequest = NULL;
       
    97     delete mRemoveImage;
       
    98     mRemoveImage = NULL;
       
    99     delete mModel;
       
   100     mModel = NULL;
       
   101     delete mSaveManager;
       
   102     mSaveManager = NULL;
       
   103     
       
   104     CNT_EXIT
       
   105 }
       
   106 
       
   107 /*!
       
   108 Called when activating the view
       
   109 */
       
   110 void CntImageEditorView::activate( const CntViewParameters aArgs )
       
   111 {
       
   112     mArgs = aArgs;
       
   113     
       
   114     mViewManager = &mEngine->viewManager();
       
   115     mThumbnailManager = &mEngine->thumbnailManager();
       
   116     connect( mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void*, int, int)),
       
   117             this, SLOT(thumbnailReady(QPixmap, void*, int, int)) );
       
   118        
       
   119     if (mView->navigationAction() != mSoftkey)
       
   120         mView->setNavigationAction(mSoftkey);
       
   121     
       
   122     HbMainWindow* window = mView->mainWindow();
       
   123     if ( window )
       
   124     {
       
   125         connect(window, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(setOrientation(Qt::Orientation)));
       
   126         setOrientation(window->orientation());
       
   127     }
       
   128     
       
   129     if ( mArgs.contains(ESelectedContact))
       
   130     {
       
   131         mContact = new QContact(mArgs.value(ESelectedContact).value<QContact>());
       
   132     }
       
   133     
       
   134     if ( mArgs.contains(ESelectedGroupContact) && !mContact ) 
       
   135     {
       
   136         mContact = new QContact(mArgs.value(ESelectedGroupContact).value<QContact>());
       
   137     }
       
   138     
       
   139     QString myCard = mArgs.value( EMyCard ).toString();
       
   140     QContactLocalId localId = mContact->localId();
       
   141     QContactLocalId selfContactId = mEngine->contactManager(SYMBIAN_BACKEND).selfContactId();
       
   142     bool isMyCard = ( localId == selfContactId && localId != 0 ) || !myCard.isEmpty();
       
   143     
       
   144     if (isMyCard)
       
   145     {
       
   146         mSaveManager = new CntSaveManager(CntSaveManager::EMyCard);
       
   147     }
       
   148     else if (mContact->type() == QContactType::TypeGroup)
       
   149     {
       
   150         mSaveManager = new CntSaveManager(CntSaveManager::EGroup);
       
   151     }
       
   152     else
       
   153     {
       
   154         mSaveManager = new CntSaveManager();
       
   155     }
       
   156     
       
   157     // set the correct image if the contact already has an image set
       
   158     mImageLabel = static_cast<CntImageLabel*>(mDocumentLoader.findWidget(QString("cnt_image_label")));
       
   159     mImageLabel->ungrabGesture(Qt::TapGesture);
       
   160     QList<QContactAvatar> details = mContact->details<QContactAvatar>();
       
   161     if (details.count() > 0)
       
   162         {
       
   163         for (int i = 0;i < details.count();i++)
       
   164             {
       
   165                 if (details.at(i).imageUrl().isValid())
       
   166                     {
       
   167                     mAvatar = new QContactAvatar(details.at(i));
       
   168                     mThumbnailManager->getThumbnail(ThumbnailManager::ThumbnailLarge, mAvatar->imageUrl().toString());
       
   169                     break;
       
   170                     }
       
   171             }
       
   172         }
       
   173     else
       
   174         {
       
   175         mAvatar = new QContactAvatar();
       
   176         mRemoveImage->setEnabled(false);
       
   177         if (mContact->type() == QContactType::TypeGroup)
       
   178             {
       
   179             mImageLabel->setAvatarIcon(HbIcon("qtg_large_add_group_picture"));
       
   180             }
       
   181         }
       
   182     
       
   183     // set up the list
       
   184     mListView = static_cast<HbListView*>(mDocumentLoader.findWidget(QString("cnt_listview")));
       
   185     
       
   186     connect(mListView, SIGNAL(activated(const QModelIndex&)), this,
       
   187         SLOT(listViewActivated(const QModelIndex&)));
       
   188     
       
   189     HbFrameBackground frame;
       
   190     frame.setFrameGraphicsName("qtg_fr_list_normal");
       
   191     frame.setFrameType(HbFrameDrawer::NinePieces);
       
   192     mListView->itemPrototypes().first()->setDefaultFrame(frame);
       
   193     
       
   194     mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::LargeIcon);
       
   195     mListView->setUniformItemSizes(true);
       
   196     
       
   197     mModel = new QStandardItemModel();
       
   198     populateModel(mModel);
       
   199     mListView->setModel(mModel);
       
   200 }
       
   201 
       
   202 void CntImageEditorView::deactivate()
       
   203 {
       
   204 
       
   205 }
       
   206 
       
   207 void CntImageEditorView::populateModel(QStandardItemModel *model)
       
   208 {
       
   209     QStandardItem *newPhoto = new QStandardItem();
       
   210     newPhoto->setText(hbTrId("txt_phob_list_take_a_new_photo"));
       
   211     newPhoto->setData(HbIcon("qtg_large_camera"), Qt::DecorationRole);
       
   212     model->appendRow(newPhoto);
       
   213     
       
   214     QStandardItem *fromGallery = new QStandardItem();
       
   215     fromGallery->setText(hbTrId("txt_phob_list_chooce_from_gallery"));
       
   216     fromGallery->setData(HbIcon("qtg_large_photos"), Qt::DecorationRole);
       
   217     model->appendRow(fromGallery);
       
   218 }
       
   219 
       
   220 /*!
       
   221 Open camera and get new image for contact
       
   222 */
       
   223 void CntImageEditorView::openCamera()
       
   224 {
       
   225     CNT_ENTRY
       
   226     
       
   227     if (mRequest)
       
   228     {
       
   229         delete mRequest;
       
   230         mRequest = 0;
       
   231     }
       
   232     
       
   233     mRequest = mAppManager.create(XQI_CAMERA_CAPTURE, XQOP_CAMERA_CAPTURE, false);
       
   234     if ( mRequest ) 
       
   235     {
       
   236         int mode = 0; //image mode
       
   237         
       
   238         QVariantMap map;
       
   239         map.insert(XQCAMERA_INDEX, 0);
       
   240         map.insert(XQCAMERA_QUALITY, 0);
       
   241         map.insert(XQCAMERA_MODE_SWITCH, false);
       
   242         map.insert(XQCAMERA_INDEX_SWITCH, false);
       
   243         map.insert(XQCAMERA_QUALITY_CHANGE, true);
       
   244         
       
   245         // Set function parameters
       
   246         QList<QVariant> args;
       
   247         args << mode;
       
   248         args << map;
       
   249         mRequest->setArguments(args);
       
   250         
       
   251         connect(mRequest, SIGNAL(requestOk(const QVariant&)), this, SLOT(handleImageChange(const QVariant&)));
       
   252         connect(mRequest, SIGNAL(requestError(int,const QString&)), this, SLOT(handleError(int,const QString&)));
       
   253 
       
   254         mRequest->send();
       
   255     }
       
   256     
       
   257     CNT_EXIT
       
   258 }
       
   259 
       
   260 /*!
       
   261 Open gallery and select image for contact
       
   262 */
       
   263 void CntImageEditorView::openGallery()
       
   264 {
       
   265     if (mRequest)
       
   266     {
       
   267         delete mRequest;
       
   268         mRequest = 0;
       
   269     }
       
   270     
       
   271     mRequest = mAppManager.create(XQI_IMAGE_FETCH, XQOP_IMAGE_FETCH, true);
       
   272     if ( mRequest ) 
       
   273     {
       
   274         connect(mRequest, SIGNAL(requestOk(const QVariant&)), this, SLOT(handleImageChange(const QVariant&)));
       
   275         mRequest->send();
       
   276     }
       
   277 }
       
   278 
       
   279 /*!
       
   280 Called when user closes the view
       
   281 */
       
   282 void CntImageEditorView::showPreviousView()
       
   283 {
       
   284     mContact->saveDetail(mAvatar);
       
   285 
       
   286     if ( mAvatar->imageUrl().isEmpty())
       
   287     {
       
   288         mContact->removeDetail(mAvatar);
       
   289     }
       
   290     
       
   291     QVariant var;
       
   292     var.setValue(*mContact);
       
   293     
       
   294     // If the arguments didn't originally contain "normal" contact,
       
   295     // then the group contact should be updated. This case applies only
       
   296     // when group image is edited
       
   297     if ( !mArgs.contains(ESelectedContact) )
       
   298     {
       
   299         mArgs.insert(ESelectedGroupContact, var);
       
   300     }
       
   301     else
       
   302     {
       
   303         mArgs.insert(ESelectedContact, var);
       
   304     }
       
   305     
       
   306     mArgs.insert(ECustomParam, viewId());
       
   307     mViewManager->back( mArgs );
       
   308 }
       
   309 
       
   310 void CntImageEditorView::removeImage()
       
   311 {
       
   312     QString filePath=mAvatar->imageUrl().toString();
       
   313     if(!filePath.isEmpty())
       
   314     {
       
   315         // Check if image removable.
       
   316         CntImageUtility imageUtility;
       
   317         if(imageUtility.isImageRemovable(filePath))
       
   318         {
       
   319             imageUtility.removeImage(filePath);
       
   320         }
       
   321         
       
   322         mAvatar->setImageUrl(QUrl());
       
   323         mImageLabel->clear();
       
   324         if (mContact->type() == QContactType::TypeGroup)
       
   325             mImageLabel->setAvatarIcon(HbIcon("qtg_large_add_group_picture"));
       
   326         else
       
   327             mImageLabel->setAvatarIcon(HbIcon("qtg_large_add_contact_picture"));
       
   328         mRemoveImage->setEnabled(false);
       
   329     }
       
   330 }
       
   331 
       
   332 /*!
       
   333 Set the selected image as new QContactAvatar::SubTypeImage
       
   334 */
       
   335 void CntImageEditorView::handleImageChange(const QVariant &value)
       
   336 {
       
   337     CNT_ENTRY_ARGS("image path = " << value.toString())
       
   338     
       
   339     if(value.canConvert<QString>())
       
   340     {
       
   341         CntImageUtility imageUtility;
       
   342         if(imageUtility.isMassStorageAvailable())
       
   343         {
       
   344             CNT_LOG_ARGS("mass storage available")
       
   345             /* Copy image and create thumbnail
       
   346              * When contact image removed only copy is deleted
       
   347              */
       
   348             QString imagepath;
       
   349             if(imageUtility.createImage(value.toString(),imagepath))
       
   350             {
       
   351                 
       
   352                 // If image already assigned, delete
       
   353                 QString filePath=mAvatar->imageUrl().toString();
       
   354                 if(!filePath.isEmpty()
       
   355                    && imageUtility.isImageRemovable(filePath))
       
   356                     imageUtility.removeImage(filePath);
       
   357             
       
   358                 CNT_LOG_ARGS("image created, image = " << filePath)
       
   359                 
       
   360                 mAvatar->setImageUrl(QUrl(imagepath));
       
   361                 mThumbnailManager->getThumbnail(ThumbnailManager::ThumbnailLarge, imagepath);
       
   362                 mRemoveImage->setEnabled(true);
       
   363             }
       
   364         }
       
   365         else
       
   366         {
       
   367             // No mass memory, use the orginal image
       
   368             mAvatar->setImageUrl(QUrl(value.toString()));
       
   369             mThumbnailManager->getThumbnail(ThumbnailManager::ThumbnailLarge, value.toString());
       
   370             mRemoveImage->setEnabled(true);
       
   371         }
       
   372     }
       
   373     
       
   374     CNT_EXIT
       
   375 }
       
   376 
       
   377 void CntImageEditorView::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
       
   378 {
       
   379     CNT_ENTRY_ARGS("error code = " << error)
       
   380     
       
   381     Q_UNUSED(data);
       
   382     Q_UNUSED(id);
       
   383     if (!error)
       
   384     {
       
   385         mImageLabel->clear();
       
   386         mImageLabel->setIcon(pixmap);
       
   387     }
       
   388     
       
   389     CNT_EXIT
       
   390 }
       
   391 
       
   392 void CntImageEditorView::setOrientation(Qt::Orientation orientation)
       
   393 {
       
   394     if (orientation == Qt::Vertical) 
       
   395     {
       
   396         // reading "portrait" section
       
   397         mDocumentLoader.load(CNT_IMAGE_XML, "portrait");
       
   398     } 
       
   399     else 
       
   400     {
       
   401         // reading "landscape" section
       
   402         mDocumentLoader.load(CNT_IMAGE_XML, "landscape");
       
   403     }
       
   404 }
       
   405 
       
   406 /*!
       
   407 Figure out which item was selected according to row number
       
   408 */
       
   409 void CntImageEditorView::listViewActivated(const QModelIndex &index)
       
   410 {
       
   411     if (index.isValid())
       
   412     {
       
   413         int row = index.row();
       
   414         switch(row)
       
   415         {
       
   416         case 0: // first item is always "take photo"
       
   417             openCamera();
       
   418             break;
       
   419         case 1: // and the second one is always "choose from gallery"
       
   420             openGallery();
       
   421             break;
       
   422         default:
       
   423             break;
       
   424         }
       
   425     }
       
   426 }
       
   427 
       
   428 void CntImageEditorView::handleError(int errorCode, const QString& errorMessage)
       
   429 {
       
   430     Q_UNUSED(errorCode);
       
   431     Q_UNUSED(errorMessage);
       
   432     CNT_LOG_ARGS("error code = " << errorCode << "errorMessage=" << errorMessage)
       
   433 }
       
   434 
       
   435 void CntImageEditorView::saveContact()
       
   436 {
       
   437     mContact->saveDetail(mAvatar);
       
   438 
       
   439     if ( mAvatar->imageUrl().isEmpty())
       
   440     {
       
   441         mContact->removeDetail(mAvatar);
       
   442     }
       
   443     
       
   444     QString name = mEngine->contactManager(SYMBIAN_BACKEND).synthesizedContactDisplayLabel(*mContact);
       
   445     
       
   446     if (name.isEmpty())
       
   447     {
       
   448         name = hbTrId("txt_phob_list_unnamed");
       
   449     }
       
   450     
       
   451     CntSaveManager::CntSaveResult result = mSaveManager->saveContact(mContact, &mEngine->contactManager(SYMBIAN_BACKEND));
       
   452     
       
   453     if (mContact->type() != QContactType::TypeGroup)
       
   454     {
       
   455         switch (result)
       
   456         {
       
   457         case CntSaveManager::ESaved:
       
   458             HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contact_1_saved")).arg(name));
       
   459             break;
       
   460         case CntSaveManager::EUpdated:
       
   461             HbDeviceNotificationDialog::notification(QString(),HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_contacts_1_updated")).arg(name));
       
   462             break;
       
   463         case CntSaveManager::EFailed:
       
   464             HbDeviceNotificationDialog::notification(QString(),hbTrId("SAVING FAILED!"));
       
   465             break;
       
   466         case CntSaveManager::EDeleted:
       
   467         case CntSaveManager::ENothingDone:
       
   468         default:
       
   469             break;
       
   470         }
       
   471     }
       
   472 }
       
   473