src/messaging/qmessagecontentcontainer_qmf.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "qmessagecontentcontainer_p.h"
       
    42 
       
    43 #include <QTextCodec>
       
    44 #include <QDebug>
       
    45 
       
    46 using namespace QTM_PREPEND_NAMESPACE(QmfHelpers);
       
    47 
       
    48 QTM_BEGIN_NAMESPACE
       
    49 
       
    50 namespace {
       
    51 
       
    52 struct PartLocator
       
    53 {
       
    54     const QMailMessagePart::Location &_location;
       
    55     QMailMessagePart *_part;
       
    56 
       
    57     PartLocator(const QMailMessagePart::Location &location)
       
    58         : _location(location),
       
    59           _part(0)
       
    60     {
       
    61     }
       
    62 
       
    63     bool operator()(QMailMessagePart &part)
       
    64     {
       
    65         if (part.location() == _location) {
       
    66             _part = &part;
       
    67             return false;
       
    68         }
       
    69 
       
    70         return true;
       
    71     }
       
    72 };
       
    73 
       
    74 struct SizeAccumulator
       
    75 {
       
    76     int _size;
       
    77 
       
    78     SizeAccumulator() : _size(0) {}
       
    79 
       
    80     bool operator()(QMailMessagePart &part)
       
    81     {
       
    82         if (part.hasBody()) {
       
    83             _size += part.body().length();
       
    84         }
       
    85 
       
    86         return true;
       
    87     }
       
    88 };
       
    89 
       
    90 }
       
    91 
       
    92 QMessageContentContainer::QMessageContentContainer()
       
    93     : d_ptr(new QMessageContentContainerPrivate)
       
    94 {
       
    95 }
       
    96 
       
    97 QMessageContentContainer::QMessageContentContainer(const QMessageContentContainer &other)
       
    98     : d_ptr(new QMessageContentContainerPrivate)
       
    99 {
       
   100     this->operator=(other);
       
   101 }
       
   102 
       
   103 QMessageContentContainer& QMessageContentContainer::operator=(const QMessageContentContainer& other)
       
   104 {
       
   105     if (&other != this) {
       
   106         if (other.d_ptr->_container == &other.d_ptr->_part) {
       
   107             d_ptr->_part = other.d_ptr->_part;
       
   108             d_ptr->_container = &d_ptr->_part;
       
   109         } else {
       
   110             d_ptr->_message = other.d_ptr->_message;
       
   111             d_ptr->_part = QMailMessagePart();
       
   112             d_ptr->_container = convert(d_ptr->_message);
       
   113         }
       
   114     }
       
   115 
       
   116     return *this;
       
   117 }
       
   118 
       
   119 QMessageContentContainer::~QMessageContentContainer()
       
   120 {
       
   121     delete d_ptr;
       
   122 }
       
   123 
       
   124 QByteArray QMessageContentContainer::contentType() const
       
   125 {
       
   126     if (!d_ptr->_type.isEmpty()) {
       
   127         return d_ptr->_type;
       
   128     }
       
   129     return d_ptr->_container->contentType().type();
       
   130 }
       
   131 
       
   132 QByteArray QMessageContentContainer::contentSubType() const
       
   133 {
       
   134     if (!d_ptr->_subType.isEmpty()) {
       
   135         return d_ptr->_subType;
       
   136     }
       
   137     return d_ptr->_container->contentType().subType();
       
   138 }
       
   139 
       
   140 QByteArray QMessageContentContainer::contentCharset() const
       
   141 {
       
   142     if (!d_ptr->_charset.isEmpty()) {
       
   143         return d_ptr->_charset;
       
   144     }
       
   145     return d_ptr->_container->contentType().charset();
       
   146 }
       
   147 
       
   148 QByteArray QMessageContentContainer::suggestedFileName() const
       
   149 {
       
   150     if (d_ptr->_container == &d_ptr->_part) {
       
   151         // Return the display name for the part
       
   152         return d_ptr->_part.displayName().toAscii();
       
   153     }
       
   154 
       
   155     return QByteArray();
       
   156 }
       
   157 
       
   158 bool QMessageContentContainer::isContentAvailable() const
       
   159 {
       
   160     if (!d_ptr->_content.isEmpty()) {
       
   161         return true;
       
   162     }
       
   163     return (d_ptr->_container->partCount() > 0) || d_ptr->_container->contentAvailable();
       
   164 }
       
   165 
       
   166 int QMessageContentContainer::size() const
       
   167 {
       
   168     if (d_ptr->_container->hasBody()) {
       
   169         return d_ptr->_container->body().length();
       
   170     }
       
   171 
       
   172     SizeAccumulator accumulator;
       
   173     d_ptr->_container->foreachPart<SizeAccumulator&>(accumulator);
       
   174 
       
   175     return accumulator._size;
       
   176 }
       
   177 
       
   178 QString QMessageContentContainer::textContent() const
       
   179 {
       
   180     return d_ptr->_container->body().data();
       
   181 }
       
   182 
       
   183 QByteArray QMessageContentContainer::content() const
       
   184 {
       
   185     return d_ptr->_container->body().data(QMailMessageBody::Decoded);
       
   186 }
       
   187 
       
   188 void QMessageContentContainer::writeTextContent(QTextStream& out) const
       
   189 {
       
   190     if (d_ptr->_container->hasBody()) {
       
   191         d_ptr->_container->body().toStream(out);
       
   192     }
       
   193 }
       
   194 
       
   195 void QMessageContentContainer::writeContent(QDataStream& out) const
       
   196 {
       
   197     if (d_ptr->_container->hasBody()) {
       
   198         d_ptr->_container->body().toStream(out, QMailMessageBody::Decoded);
       
   199     }
       
   200 }
       
   201 
       
   202 QMessageContentContainerIdList QMessageContentContainer::contentIds() const
       
   203 {
       
   204     QMessageContentContainerIdList ids;
       
   205 
       
   206     for (uint i = 0; i < d_ptr->_container->partCount(); ++i) {
       
   207         ids.append(convert(d_ptr->_container->partAt(i).location()));
       
   208     }
       
   209 
       
   210     return ids;
       
   211 }
       
   212 
       
   213 QMessageContentContainer QMessageContentContainer::find(const QMessageContentContainerId &id) const
       
   214 {
       
   215     QMessageContentContainer container;
       
   216 
       
   217     QMailMessagePart::Location location(convert(id));
       
   218 
       
   219     if (location.isValid(false)) {
       
   220         PartLocator locator(location);
       
   221         d_ptr->_container->foreachPart<PartLocator&>(locator);
       
   222 
       
   223         if (locator._part) {
       
   224             container.d_ptr->_part = *locator._part;
       
   225             container.d_ptr->_container = &container.d_ptr->_part;
       
   226         }
       
   227     } else {
       
   228         if (location.containingMessageId() == convert(d_ptr->_message)->id()) {
       
   229             // Create a part containing the body content of this message
       
   230             QMailMessage *message(convert(d_ptr->_message));
       
   231             QMailMessageContentDisposition cd(QMailMessageContentDisposition::Inline);
       
   232             container.d_ptr->_part = QMailMessagePart::fromData(message->body().data(), cd, message->contentType(), QMailMessageBody::Base64);
       
   233             container.d_ptr->_container = &container.d_ptr->_part;
       
   234         }
       
   235     }
       
   236 
       
   237     return container;
       
   238 }
       
   239 
       
   240 bool QMessageContentContainer::contains(const QMessageContentContainerId &id) const
       
   241 {
       
   242     QMailMessagePart::Location location(convert(id));
       
   243 
       
   244     if (location.isValid(false)) {
       
   245         PartLocator locator(location);
       
   246         d_ptr->_container->foreachPart<PartLocator&>(locator);
       
   247         return (locator._part != 0);
       
   248     } else {
       
   249         return (location.containingMessageId() == convert(d_ptr->_message)->id());
       
   250     }
       
   251 }
       
   252 
       
   253 QString QMessageContentContainer::headerFieldValue(const QByteArray &name) const
       
   254 {
       
   255     return d_ptr->_container->headerFieldText(name);
       
   256 }
       
   257 
       
   258 QStringList QMessageContentContainer::headerFieldValues(const QByteArray &name) const
       
   259 {
       
   260     return d_ptr->_container->headerFieldsText(name);
       
   261 }
       
   262 
       
   263 QList<QByteArray> QMessageContentContainer::headerFields() const
       
   264 {
       
   265     QList<QByteArray> fields;
       
   266     foreach (const QMailMessageHeaderField &field, d_ptr->_container->headerFields()) {
       
   267         fields.append(field.id());
       
   268     }
       
   269 
       
   270     return fields;
       
   271 }
       
   272 
       
   273 void QMessageContentContainer::setDerivedMessage(QMessage *derived)
       
   274 {
       
   275     d_ptr->setDerivedMessage(derived);
       
   276 }
       
   277 
       
   278 QTM_END_NAMESPACE