messagingapp/msgui/unifiededitor/src/msgunieditorattachment.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:
       
    15  *
       
    16  */
       
    17 
       
    18 // INCLUDES
       
    19 #include "debugtraces.h"
       
    20 #include <HbTextItem>
       
    21 #include <HbIconItem>
       
    22 #include <QFileInfo>
       
    23 #include <QFont>
       
    24 #include <HbFrameDrawer>
       
    25 #include <HbMenu>
       
    26 #include <MsgMimeTypes.h>
       
    27 #include <HbFrameItem>
       
    28 #include <HbInstantFeedback>
       
    29 #include <HbTapGesture>
       
    30 #include <QGraphicsSceneMouseEvent>
       
    31 #include <HbWidgetFeedback>
       
    32 #include <HbEffect>
       
    33 #include <QTimer>
       
    34 
       
    35 // USER INCLUDES
       
    36 #include "msgcontactsutil.h"
       
    37 #include "msgunieditorattachment.h"
       
    38 #include "UniEditorGenUtils.h"
       
    39 #include <xqconversions.h>
       
    40 #include "msgunieditorutils.h"
       
    41 
       
    42 // Constants
       
    43 #define BYTES_TO_KBYTES_FACTOR 1024
       
    44 #define BG_FRAME "qtg_fr_groupbox"
       
    45 
       
    46 //Localized Constants for item specific menu
       
    47 #define LOC_OPEN    hbTrId("txt_common_menu_open")
       
    48 #define LOC_REMOVE  hbTrId("txt_common_menu_remove")
       
    49 #define LOC_DETAILS hbTrId("txt_common_menu_details")
       
    50 
       
    51 const QString LIST_ITEM_BG_FRAME_NORMAL ("qtg_fr_list_normal");
       
    52 const QString LIST_ITEM_BG_FRAME_PRESSED("qtg_fr_list_pressed");
       
    53 
       
    54 const QString ATTACHMENT_ICON("qtg_small_attachment");
       
    55 
       
    56 MsgUnifiedEditorAttachment::MsgUnifiedEditorAttachment( const QString& attachmentpath,
       
    57                                                         const int filesize,
       
    58                                                         QGraphicsItem *parent ) :
       
    59 HbWidget(parent),
       
    60 mPath(attachmentpath),
       
    61 mSize(filesize),
       
    62 mAttachmentIcon(0),
       
    63 mAttachmentName(0),
       
    64 mMaxSmsSize(KFirstNormalSmsLength),
       
    65 mEditorUtils(0)
       
    66 {
       
    67     this->grabGesture(Qt::TapGesture);
       
    68     setProperty("state", "normal");
       
    69     
       
    70     //back ground
       
    71     mBackGround = new HbFrameItem(this);
       
    72     mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
       
    73     mBackGround->frameDrawer().setFrameType(HbFrameDrawer::NinePieces);
       
    74     this->setBackgroundItem(mBackGround);        
       
    75 
       
    76     mAttachmentIcon = new HbIconItem(ATTACHMENT_ICON, this);
       
    77     HbStyle::setItemName(mAttachmentIcon,"attachmentIcon");
       
    78 
       
    79     int at_size = 0;
       
    80     TMsgMediaType mediaType = EMsgMediaUnknown;
       
    81     
       
    82             
       
    83     UniEditorGenUtils* genUtils = q_check_ptr(new UniEditorGenUtils);
       
    84     
       
    85     TRAP_IGNORE(genUtils->getFileInfoL(mPath,at_size,
       
    86         mMimeType,mediaType));
       
    87     TRAP_IGNORE(mMaxSmsSize = genUtils->MaxSmsMsgSizeL()); 
       
    88     delete genUtils;
       
    89     QFileInfo fileinfo(attachmentpath);
       
    90     QString filename = fileinfo.fileName();
       
    91     mAttachmentName = new HbTextItem(filename,this);
       
    92     HbStyle::setItemName(mAttachmentName,"attachmentName");
       
    93     mAttachmentName->setElideMode(Qt::ElideRight);
       
    94 
       
    95     // for sms, pure size should be shown
       
    96     // for mms, additional mimeheader size must be included
       
    97     qreal displaySize = mSize;
       
    98     if(!isMultimediaContent())
       
    99     {
       
   100         displaySize = fileinfo.size();
       
   101     }
       
   102     int sizeInKb = displaySize/BYTES_TO_KBYTES_FACTOR;
       
   103     QString fileDetails;
       
   104     // if size exceeds 1kb, then show kb or else only bytes
       
   105     if(sizeInKb >= 1)
       
   106     {
       
   107         fileDetails = QString().append(QString("(%1 Kb)").arg(sizeInKb));
       
   108     }
       
   109     else
       
   110     {
       
   111         fileDetails = QString().append(QString("(%1 B)").arg(displaySize));
       
   112     }
       
   113 
       
   114     mAttachmentDetails = new HbTextItem(fileDetails, this);
       
   115     HbStyle::setItemName(mAttachmentDetails,"attachmentDetails");
       
   116     mAttachmentDetails->setElideMode(Qt::ElideNone);
       
   117     
       
   118     HbEffect::add("attachmentWidget", "listviewitem_press", "pressed");
       
   119     HbEffect::add("attachmentWidget", "listviewitem_release", "released");
       
   120 }
       
   121 
       
   122 MsgUnifiedEditorAttachment::~MsgUnifiedEditorAttachment()
       
   123 {
       
   124 }
       
   125 
       
   126 const QString& MsgUnifiedEditorAttachment::path()
       
   127 {
       
   128     return mPath;
       
   129 }
       
   130 
       
   131 qreal MsgUnifiedEditorAttachment::size()
       
   132 {
       
   133     return mSize;
       
   134 }
       
   135 
       
   136 const QString& MsgUnifiedEditorAttachment::mimeType()
       
   137 {
       
   138     return mMimeType;
       
   139 }
       
   140 
       
   141 void MsgUnifiedEditorAttachment::handleLongTap(QPointF position)
       
   142 {
       
   143     HbMenu* menu = new HbMenu;
       
   144     menu->addAction(LOC_OPEN, this, SLOT(handleOpen()));
       
   145     menu->addAction(LOC_REMOVE, this, SLOT(handleRemove()));
       
   146     menu->addAction(LOC_DETAILS, this, SLOT(viewDetails()));
       
   147     menu->setDismissPolicy(HbPopup::TapAnywhere);
       
   148     menu->setAttribute(Qt::WA_DeleteOnClose, true);
       
   149     menu->setPreferredPos(position);
       
   150     menu->show();
       
   151 }
       
   152 
       
   153 void MsgUnifiedEditorAttachment::handleRemove()
       
   154 {
       
   155     emit deleteMe(this);
       
   156 }
       
   157 
       
   158 void MsgUnifiedEditorAttachment::handleOpen()
       
   159 {
       
   160     this->ungrabGesture(Qt::TapGesture);
       
   161     
       
   162     //open corresponding viewer app
       
   163 
       
   164     if (!mEditorUtils) 
       
   165     {
       
   166         mEditorUtils = new MsgUnifiedEditorUtils(this);
       
   167     }
       
   168     mEditorUtils->launchContentViewer(mMimeType, mPath);
       
   169     
       
   170     //fire timer to regrab gesture after some delay.
       
   171     QTimer::singleShot(300,this,SLOT(regrabGesture()));
       
   172 }
       
   173 
       
   174 void MsgUnifiedEditorAttachment::viewDetails()
       
   175 {
       
   176     //open details view.
       
   177 }
       
   178 
       
   179 bool MsgUnifiedEditorAttachment::isMultimediaContent()
       
   180 {
       
   181     bool ret = true;
       
   182     QString vcard = XQConversions::s60Desc8ToQString(KMsgMimeVCard());
       
   183     QString vcal = XQConversions::s60Desc8ToQString(KMsgMimeVCal());
       
   184     QString ical = XQConversions::s60Desc8ToQString(KMsgMimeICal());
       
   185     if( !QString::compare(mMimeType, vcard, Qt::CaseInsensitive) ||
       
   186         !QString::compare(mMimeType, vcal, Qt::CaseInsensitive) ||
       
   187         !QString::compare(mMimeType, ical, Qt::CaseInsensitive) )
       
   188     {
       
   189         QFileInfo fileinfo(mPath);
       
   190         int fSize = fileinfo.size();
       
   191         
       
   192         // if filesize is within sms size-limit, then
       
   193         // it is not mm content, else it is mm attachment
       
   194         if(fSize <= mMaxSmsSize)
       
   195         {
       
   196             ret = false;
       
   197         }
       
   198     }
       
   199     return ret;
       
   200 }
       
   201 
       
   202 void MsgUnifiedEditorAttachment::gestureEvent(QGestureEvent *event)
       
   203 {
       
   204     HbTapGesture *tapGesture = qobject_cast<HbTapGesture*> (event->gesture(Qt::TapGesture));
       
   205     if (tapGesture) {
       
   206         switch (tapGesture->state()) {
       
   207         case Qt::GestureStarted:
       
   208         {
       
   209             // Trigger haptic feedback.
       
   210             HbInstantFeedback::play(HbFeedback::Basic);
       
   211             setPressed(true);
       
   212             break;
       
   213         }
       
   214         case Qt::GestureUpdated:
       
   215         {
       
   216             if (HbTapGesture::TapAndHold == tapGesture->tapStyleHint()) {
       
   217                 // Handle longtap.
       
   218                 setPressed(false);
       
   219                 handleLongTap(tapGesture->scenePosition());
       
   220             }
       
   221             break;
       
   222         }
       
   223         case Qt::GestureFinished:
       
   224         {
       
   225             HbInstantFeedback::play(HbFeedback::Basic);
       
   226             if (HbTapGesture::Tap == tapGesture->tapStyleHint()) {
       
   227                 // Handle short tap.
       
   228                 setPressed(false);
       
   229                 handleShortTap();
       
   230             }
       
   231             break;
       
   232         }
       
   233         case Qt::GestureCanceled:
       
   234         {
       
   235             HbInstantFeedback::play(HbFeedback::Basic);
       
   236             setPressed(false);
       
   237             break;
       
   238         }
       
   239         }
       
   240     }
       
   241     else {
       
   242         HbWidget::gestureEvent(event);
       
   243     }
       
   244 }
       
   245 
       
   246 void MsgUnifiedEditorAttachment::handleShortTap()
       
   247 {
       
   248     handleOpen();
       
   249 }
       
   250 
       
   251 void MsgUnifiedEditorAttachment::setPressed(bool pressed)
       
   252 {
       
   253     if (pressed) 
       
   254     {
       
   255         setProperty("state", "pressed");
       
   256         mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_PRESSED);
       
   257         HbEffect::cancel(mBackGround, "released");
       
   258         HbEffect::start(mBackGround, "attachmentWidget", "pressed");
       
   259 
       
   260     }
       
   261     else 
       
   262     {
       
   263         setProperty("state", "normal");
       
   264         mBackGround->frameDrawer().setFrameGraphicsName(LIST_ITEM_BG_FRAME_NORMAL);
       
   265         HbEffect::cancel(mBackGround, "pressed");
       
   266         HbEffect::start(mBackGround, "attachmentWidget", "released");
       
   267     }    
       
   268 }
       
   269 
       
   270 void MsgUnifiedEditorAttachment::regrabGesture()
       
   271 {
       
   272     this->grabGesture(Qt::TapGesture);
       
   273 }
       
   274 // EOF