emailuis/nmailui/src/nmattachmentlist.cpp
changeset 18 578830873419
child 20 ecc8def7944a
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     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: Class for handling the attachment list
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmuiheaders.h"
       
    19 
       
    20 /*!
       
    21 	\class NmAttachmentList
       
    22 	\brief Class for handling the attachment list
       
    23 */
       
    24 
       
    25 /*!
       
    26     Constructor
       
    27 */
       
    28 NmAttachmentList::NmAttachmentList(NmAttachmentListWidget *listWidget)
       
    29 : mListWidget(listWidget)
       
    30 {
       
    31     updateLayout();
       
    32 }
       
    33 
       
    34 /*!
       
    35     Destructor
       
    36 */
       
    37 NmAttachmentList::~NmAttachmentList()
       
    38 { 
       
    39     clearList();
       
    40 }
       
    41 
       
    42 /*!
       
    43     Insert new list item. Returns the count of the attachment in list
       
    44 */
       
    45 int NmAttachmentList::insertAttachment(
       
    46     const QString &fullFileName,
       
    47     const QString &fileSize,
       
    48     const NmId &attachmentPartId)
       
    49 {
       
    50     QString displayName = fullNameToDisplayName(fullFileName);
       
    51     mFullFileName.append(fullFileName);
       
    52     mDisplayFileName.append(displayName);
       
    53     mAttachmentPartId.append(attachmentPartId);
       
    54     mFileSize.append(fileSize);
       
    55     mListWidget->insertAttachment(count() - 1, displayName, createSizeString(fileSize));
       
    56     updateLayout();
       
    57     return count() - 1;
       
    58 }
       
    59 
       
    60 /*!
       
    61     Set attachmentPartId of the list item. Because there can be several attachments with
       
    62     same filename, function will search the one which nmid is not set.
       
    63 */
       
    64 void NmAttachmentList::setAttachmentPartId(const QString fullFileName, const NmId &attachmentPartId)
       
    65 {
       
    66     for (int i=0; i<count(); ++i) {
       
    67         if (mFullFileName.at(i) == fullFileName && mAttachmentPartId.at(i).id() == 0) {
       
    68             mAttachmentPartId.replace(i, attachmentPartId);
       
    69         }
       
    70     }
       
    71 }
       
    72 
       
    73 /*!
       
    74     Set fileSize of the attachment.
       
    75 */
       
    76 void NmAttachmentList::setAttachmentSize(const NmId &attachmentPartId, const QString &size)
       
    77 {
       
    78     for (int i=0; i<count(); ++i) {
       
    79         if (mAttachmentPartId.at(i) == attachmentPartId) {
       
    80             mFileSize.replace(i, size);
       
    81         }
       
    82     }
       
    83 }
       
    84 
       
    85 /*!
       
    86     Remove attachment from list position
       
    87 */
       
    88 void NmAttachmentList::removeAttachment(int arrayIndex)
       
    89 {
       
    90     if (arrayIndex < count()) {
       
    91         // Remove UI
       
    92         mListWidget->removeAttachment(arrayIndex);
       
    93         // Remove from data structure
       
    94         mFullFileName.removeAt(arrayIndex);
       
    95         mDisplayFileName.removeAt(arrayIndex);
       
    96         mAttachmentPartId.removeAt(arrayIndex);
       
    97         updateLayout();
       
    98     }
       
    99 }
       
   100 
       
   101 /*!
       
   102     Remove attachment which have same fullFileName from list
       
   103 */
       
   104 void NmAttachmentList::removeAttachment(const QString &fullFileName)
       
   105 {
       
   106     for (int i=0; i<count(); ++i) {
       
   107         if (mFullFileName.at(i) == fullFileName) {
       
   108             removeAttachment(i);
       
   109         }
       
   110     }
       
   111 }
       
   112 
       
   113 /*!
       
   114     Remove attachment which have same attachmentPartId from list
       
   115 */
       
   116 void NmAttachmentList::removeAttachment(const NmId &attachmentPartId)
       
   117 {
       
   118     for (int i=0; i<count(); ++i) {
       
   119         if (mAttachmentPartId.at(i) == attachmentPartId) {
       
   120             removeAttachment(i);
       
   121         }
       
   122     }
       
   123 }
       
   124 
       
   125 /*!
       
   126     Clear attachment list from UI and from data structure
       
   127 */
       
   128 void NmAttachmentList::clearList()
       
   129 {
       
   130     for (int i=count()-1; i>=0; --i) {
       
   131         // Remove from UI
       
   132         mListWidget->removeAttachment(i);
       
   133         // Remove from data structure
       
   134         mFullFileName.removeAt(i);
       
   135         mDisplayFileName.removeAt(i);
       
   136         mAttachmentPartId.removeAt(i);
       
   137         updateLayout();
       
   138     }
       
   139 }
       
   140 
       
   141 /*!
       
   142     Return attachment list widget
       
   143 */
       
   144 NmAttachmentListWidget* NmAttachmentList::listWidget()
       
   145 {
       
   146     return mListWidget;
       
   147 }
       
   148 
       
   149 /*!
       
   150     Return attachment count
       
   151 */
       
   152 int NmAttachmentList::count()
       
   153 {
       
   154     return mFullFileName.count();
       
   155 }
       
   156 
       
   157 /*!
       
   158     Return attachment part id
       
   159 */
       
   160 NmId NmAttachmentList::nmIdByIndex(int listIndex)
       
   161 {
       
   162     return mAttachmentPartId.at(listIndex);
       
   163 }
       
   164 
       
   165 /*!
       
   166     Return array index of attachment
       
   167 */
       
   168 int NmAttachmentList::indexByNmId(const NmId &id)
       
   169 {
       
   170     for (int i=0; i<count(); ++i) {
       
   171         if (mAttachmentPartId.at(i) == id) {
       
   172             return i;
       
   173         }
       
   174     }
       
   175     return -1;
       
   176 }
       
   177 
       
   178 /*!
       
   179     Remove file path from full filename
       
   180 */
       
   181 QString NmAttachmentList::fullNameToDisplayName(const QString &fullName)
       
   182 {
       
   183     return fullName.section('\\', -1);
       
   184 }
       
   185 
       
   186 /*!
       
   187     Create string for showing the size information
       
   188 */
       
   189 QString NmAttachmentList::createSizeString(const QString &sizeInBytes)
       
   190 {
       
   191     double sizeMb = sizeInBytes.toDouble() / 1000000;
       
   192     if (sizeMb < 0.1) {
       
   193         // 0.1 Mb is the minimum size shown for attachment
       
   194         sizeMb = 0.1;
       
   195     }
       
   196     return QString().sprintf("(%.1f Mb)", sizeMb); // Use loc string when available
       
   197 }
       
   198 
       
   199 /*!
       
   200     Update the list layout height
       
   201 */
       
   202 void NmAttachmentList::updateLayout()
       
   203 {
       
   204     // Fix this when progress bar is used
       
   205     mListWidget->setMaximumHeight(count() * 29); 
       
   206     QTimer::singleShot(1, this, SLOT(delayedLayoutChangeInfo()));
       
   207 }
       
   208 
       
   209 /*!
       
   210     Send delayed signal about attachment list layout change
       
   211 */
       
   212 void NmAttachmentList::delayedLayoutChangeInfo()
       
   213 {
       
   214     emit attachmentListLayoutChanged();
       
   215 }