messagingapp/msgui/unifiedviewer/src/univiewerattachmentwidget.cpp
branchRCL_3
changeset 57 ebe688cedc25
child 62 fdbe8253b596
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: Widget for displaying attachment media objects.
       
    15  *
       
    16  */
       
    17 
       
    18 #include "univiewerattachmentwidget.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <HbIconItem>
       
    22 #include <HbTextItem>
       
    23 #include <HbFrameItem>
       
    24 #include <HbMenu>
       
    25 #include <HbEffect>
       
    26 #include <HbTapGesture>
       
    27 #include <HbInstantFeedback>
       
    28 
       
    29 #include <QFileInfo>
       
    30 #include <QTimer>
       
    31 
       
    32 // USER INCLUDES
       
    33 #include "univiewerutils.h"
       
    34 #include "unidatamodelplugininterface.h"
       
    35 
       
    36 // LOCAL CONSTANTS
       
    37 #define LOC_OPEN hbTrId("txt_common_menu_open")
       
    38 #define LOC_SAVE hbTrId("txt_common_menu_save")
       
    39 #define LOC_SAVE_TO_CONTACTS hbTrId("txt_messaging_menu_save_to_contacts")
       
    40 
       
    41 const QString BG_FRAME_NORMAL("qtg_fr_list_normal");
       
    42 const QString BG_FRAME_PRESSED("qtg_fr_list_pressed");
       
    43 const QString ATTACHMENT_ICON("qtg_small_attachment");
       
    44 const QString CORRUPTED_ATTACH_ICON("qtg_small_corrupted");
       
    45 const QString VCARD_MIMETYPE("text/X-vCard");
       
    46 
       
    47 const int KILOBYTE = 1024;
       
    48 
       
    49 //----------------------------------------------------------------------------
       
    50 // UniViewerAttachmentWidget::UniViewerAttachmentWidget
       
    51 // @see header file
       
    52 //----------------------------------------------------------------------------
       
    53 UniViewerAttachmentWidget::UniViewerAttachmentWidget(QGraphicsItem *parent) :
       
    54     HbWidget(parent), mMediaIcon(0), mName(0), mInfo(0), mFrameItem(0), mViewerUtils(0)
       
    55 {
       
    56     this->grabGesture(Qt::TapGesture);
       
    57 
       
    58     setProperty("state", "normal");
       
    59 
       
    60     mMediaIcon = new HbIconItem(this);
       
    61     HbStyle::setItemName(mMediaIcon, "mediaIcon");
       
    62 
       
    63     mName = new HbTextItem(this);
       
    64     HbStyle::setItemName(mName, "text-1");
       
    65 
       
    66     mInfo = new HbTextItem(this);
       
    67     HbStyle::setItemName(mInfo, "text-2");
       
    68 
       
    69     mFrameItem = new HbFrameItem(BG_FRAME_NORMAL, HbFrameDrawer::NinePieces, this);
       
    70     HbStyle::setItemName(mFrameItem, "bgFrame");
       
    71 
       
    72     HbEffect::add("attachmentWidget", "listviewitem_press", "pressed");
       
    73     HbEffect::add("attachmentWidget", "listviewitem_release", "released");
       
    74 }
       
    75 
       
    76 //----------------------------------------------------------------------------
       
    77 // UniViewerAttachmentWidget::~UniViewerAttachmentWidget
       
    78 // @see header file
       
    79 //----------------------------------------------------------------------------
       
    80 UniViewerAttachmentWidget::~UniViewerAttachmentWidget()
       
    81 {
       
    82 }
       
    83 
       
    84 //----------------------------------------------------------------------------
       
    85 // UniViewerAttachmentWidget::populate
       
    86 // @see header file
       
    87 //----------------------------------------------------------------------------
       
    88 void UniViewerAttachmentWidget::populate(UniMessageInfo *info)
       
    89 {
       
    90     mMimeType = info->mimetype();
       
    91     mMediaPath = info->path();
       
    92 
       
    93     QString attachIcon;
       
    94     if (info->isProtected()) {
       
    95         attachIcon = ATTACHMENT_ICON;
       
    96     }
       
    97     else if (info->isCorrupted()) {
       
    98         attachIcon = CORRUPTED_ATTACH_ICON;
       
    99     }
       
   100     else {
       
   101         attachIcon = ATTACHMENT_ICON;
       
   102     }
       
   103 
       
   104     mMediaIcon->setIconName(attachIcon);
       
   105     QFileInfo fileInfo(mMediaPath);
       
   106     mName->setText(fileInfo.fileName());
       
   107 
       
   108     QString sizeString('B');
       
   109     int fileSize = fileInfo.size();
       
   110     if (fileSize > KILOBYTE) {
       
   111         // Convert to Kilobytes.
       
   112         fileSize /= KILOBYTE;
       
   113         sizeString = "Kb";
       
   114     }
       
   115     QString fileDetails = "(" + QString::number(fileSize) + sizeString + ")";
       
   116     mInfo->setText(fileDetails);
       
   117 }
       
   118 
       
   119 //----------------------------------------------------------------------------
       
   120 // UniViewerAttachmentWidget::resizeEvent
       
   121 // @see header file
       
   122 //----------------------------------------------------------------------------
       
   123 void UniViewerAttachmentWidget::gestureEvent(QGestureEvent *event)
       
   124 {
       
   125     HbTapGesture *tapGesture = qobject_cast<HbTapGesture*> (event->gesture(Qt::TapGesture));
       
   126     if (tapGesture) {
       
   127         switch (tapGesture->state()) {
       
   128         case Qt::GestureStarted:
       
   129         {
       
   130             // Trigger haptic feedback.
       
   131             HbInstantFeedback::play(HbFeedback::Basic);
       
   132             setPressed(true);
       
   133             break;
       
   134         }
       
   135         case Qt::GestureUpdated:
       
   136         {
       
   137             if (HbTapGesture::TapAndHold == tapGesture->tapStyleHint()) {
       
   138                 // Handle longtap.
       
   139                 setPressed(false);
       
   140                 handleLongTap(tapGesture->scenePosition());
       
   141             }
       
   142             break;
       
   143         }
       
   144         case Qt::GestureFinished:
       
   145         {
       
   146             HbInstantFeedback::play(HbFeedback::Basic);
       
   147             if (HbTapGesture::Tap == tapGesture->tapStyleHint()) {
       
   148                 // Handle short tap.
       
   149                 setPressed(false);
       
   150                 handleShortTap();
       
   151             }
       
   152             break;
       
   153         }
       
   154         case Qt::GestureCanceled:
       
   155         {
       
   156             HbInstantFeedback::play(HbFeedback::Basic);
       
   157             setPressed(false);
       
   158             break;
       
   159         }
       
   160         }
       
   161     }
       
   162     else {
       
   163         HbWidget::gestureEvent(event);
       
   164     }
       
   165 }
       
   166 
       
   167 //----------------------------------------------------------------------------
       
   168 // UniViewerAttachmentWidget::handleOpen
       
   169 // @see header file
       
   170 //----------------------------------------------------------------------------
       
   171 void UniViewerAttachmentWidget::handleOpen()
       
   172 {
       
   173     this->ungrabGesture(Qt::TapGesture);
       
   174     
       
   175     if (!mViewerUtils) {
       
   176         mViewerUtils = new UniViewerUtils(this);
       
   177     }
       
   178     mViewerUtils->launchContentViewer(mMimeType, mMediaPath);
       
   179     
       
   180     //fire timer to regrab gesture after some delay.
       
   181     QTimer::singleShot(300,this,SLOT(regrabGesture()));
       
   182 }
       
   183 
       
   184 //----------------------------------------------------------------------------
       
   185 // UniViewerAttachmentWidget::handleSave
       
   186 // @see header file
       
   187 //----------------------------------------------------------------------------
       
   188 void UniViewerAttachmentWidget::handleSave()
       
   189 {
       
   190 
       
   191 }
       
   192 
       
   193 //----------------------------------------------------------------------------
       
   194 // UniViewerAttachmentWidget::handleShortTap
       
   195 // @see header file
       
   196 //----------------------------------------------------------------------------
       
   197 void UniViewerAttachmentWidget::handleShortTap()
       
   198 {
       
   199     emit shortTap(mMediaPath);
       
   200 
       
   201     // Open the media.
       
   202     handleOpen();
       
   203 }
       
   204 
       
   205 //----------------------------------------------------------------------------
       
   206 // UniViewerAttachmentWidget::handleLongTap
       
   207 // @see header file
       
   208 //----------------------------------------------------------------------------
       
   209 void UniViewerAttachmentWidget::handleLongTap(const QPointF &position)
       
   210 {
       
   211     emit longTap(position);
       
   212 
       
   213     // Display context sensitive menu.
       
   214     HbMenu* menu = new HbMenu;
       
   215     menu->setAttribute(Qt::WA_DeleteOnClose);
       
   216     menu->setPreferredPos(position);
       
   217 
       
   218     if (mMimeType.contains(VCARD_MIMETYPE, Qt::CaseInsensitive)) {
       
   219         // For vcard opening & saving is same.
       
   220         menu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(handleOpen()));
       
   221     }
       
   222     else {
       
   223         menu->addAction(LOC_OPEN, this, SLOT(handleOpen()));
       
   224         menu->addAction(LOC_SAVE, this, SLOT(handleSave()));
       
   225     }
       
   226 
       
   227     menu->show();
       
   228 }
       
   229 
       
   230 //----------------------------------------------------------------------------
       
   231 // UniViewerAttachmentWidget::setPressed
       
   232 // @see header file
       
   233 //----------------------------------------------------------------------------
       
   234 void UniViewerAttachmentWidget::setPressed(bool pressed)
       
   235 {
       
   236     if (pressed) {
       
   237         setProperty("state", "pressed");
       
   238         mFrameItem->frameDrawer().setFrameGraphicsName(BG_FRAME_PRESSED);
       
   239         HbEffect::cancel(mFrameItem, "released");
       
   240         HbEffect::start(mFrameItem, "attachmentWidget", "pressed");
       
   241 
       
   242     }
       
   243     else {
       
   244         setProperty("state", "normal");
       
   245         mFrameItem->frameDrawer().setFrameGraphicsName(BG_FRAME_NORMAL);
       
   246         HbEffect::cancel(mFrameItem, "pressed");
       
   247         HbEffect::start(mFrameItem, "attachmentWidget", "released");
       
   248     }
       
   249 }
       
   250 
       
   251 //---------------------------------------------------------------
       
   252 // UniViewerAttachmentWidget::regrabGesture
       
   253 // @see header file
       
   254 //---------------------------------------------------------------
       
   255 void UniViewerAttachmentWidget::regrabGesture()
       
   256 {
       
   257     this->grabGesture(Qt::TapGesture);
       
   258 }
       
   259 // EOF