File indexing completed on 2024-11-24 04:53:08

0001 /*
0002     Certain enhancements (www.xtuple.com/trojita-enhancements)
0003     are copyright © 2010 by OpenMFG LLC, dba xTuple.  All rights reserved.
0004 
0005     Redistribution and use in source and binary forms, with or without
0006     modification, are permitted provided that the following conditions are met:
0007 
0008     - Redistributions of source code must retain the above copyright notice, this
0009     list of conditions and the following disclaimer.
0010     - Redistributions in binary form must reproduce the above copyright notice,
0011     this list of conditions and the following disclaimer in the documentation
0012     and/or other materials provided with the distribution.
0013     - Neither the name of xTuple nor the names of its contributors may be used to
0014     endorse or promote products derived from this software without specific prior
0015     written permission.
0016 
0017     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0020     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
0021     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0022     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0023     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
0024     ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0026     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 
0028 */
0029 
0030 #include "FindInterestingPart.h"
0031 #include "Imap/Model/ItemRoles.h"
0032 #include "Imap/Model/Model.h"
0033 #include "Imap/Model/MailboxTree.h"
0034 
0035 namespace Imap {
0036 namespace Mailbox {
0037 
0038 QString FindInterestingPart::findMainPart( QModelIndex &part )
0039 {
0040     if ( ! part.isValid() )
0041         return QStringLiteral("Invalid index");
0042 
0043     QString mimeType = part.data( Imap::Mailbox::RolePartMimeType ).toString().toLower();
0044 
0045     if ( mimeType == QLatin1String("text/plain") ) {
0046         // found it, no reason to do anything else
0047         return QString();
0048     }
0049 
0050     if ( mimeType == QLatin1String("text/html") ) {
0051         // HTML without a text/plain counterpart is not supported
0052         part = QModelIndex();
0053         return QStringLiteral("A HTML message without a plaintext counterpart");
0054     }
0055 
0056     if ( mimeType == QLatin1String("message/rfc822") ) {
0057         if ( part.model()->rowCount( part ) != 1 ) {
0058             part = QModelIndex();
0059             return QStringLiteral("Unsupported message/rfc822 formatting");
0060         }
0061         part = part.model()->index( 0, 0, part );
0062         return findMainPart( part );
0063     }
0064 
0065     if ( mimeType.startsWith( QLatin1String("multipart/") ) ) {
0066         QModelIndex target;
0067         QString str;
0068         for ( int i = 0; i < part.model()->rowCount( part ); ++i ) {
0069             // Walk through all children, try to find a first usable item
0070             target = part.model()->index( i, 0, part );
0071             str = findMainPart( target );
0072             if ( target.isValid() ) {
0073                 // Found a usable item
0074                 part = target;
0075                 return QString();
0076             }
0077 
0078         }
0079         part = QModelIndex();
0080         return QStringLiteral("This is a %1 formatted message whose parts are not suitable for diplaying here").arg(mimeType);
0081     }
0082 
0083     part = QModelIndex();
0084     return QStringLiteral("MIME type %1 is not supported").arg(mimeType);
0085 }
0086 
0087 FindInterestingPart::MainPartReturnCode FindInterestingPart::findMainPartOfMessage(
0088         const QModelIndex &message, QModelIndex &mainPartIndex, QString &partMessage, QString *partData)
0089 {
0090     mainPartIndex = message.model()->index( 0, 0, message );
0091     if ( ! mainPartIndex.isValid() ) {
0092         return MAINPART_MESSAGE_NOT_LOADED;
0093     }
0094 
0095     partMessage = findMainPart( mainPartIndex );
0096     if ( ! mainPartIndex.isValid() ) {
0097         return MAINPART_PART_CANNOT_DETERMINE;
0098     }
0099 
0100     if (partData) {
0101         QVariant data = mainPartIndex.data( Imap::Mailbox::RolePartData );
0102         if ( ! data.isValid() ) {
0103             return MAINPART_PART_LOADING;
0104         }
0105 
0106         *partData = data.toString();
0107         return MAINPART_FOUND;
0108     } else {
0109         return mainPartIndex.data(Imap::Mailbox::RoleIsFetched).toBool() ? MAINPART_FOUND : MAINPART_PART_LOADING;
0110     }
0111 }
0112 
0113 }
0114 }