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