File indexing completed on 2024-05-12 05:17:23

0001 /*
0002     Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #ifndef KIMAP2_MESSAGE_P_H
0021 #define KIMAP2_MESSAGE_P_H
0022 
0023 #include <QtCore/QByteArray>
0024 #include <QtCore/QList>
0025 #include <QtCore/QMetaType>
0026 
0027 namespace KIMAP2
0028 {
0029 
0030 struct Message {
0031     class Part
0032     {
0033     public:
0034         enum Type { String = 0, List };
0035 
0036         explicit Part(const QByteArray &string)
0037             : m_type(String), m_string(string) { }
0038         explicit Part(const QList<QByteArray> &list)
0039             : m_type(List), m_list(list) { }
0040 
0041         inline Type type() const
0042         {
0043             return m_type;
0044         }
0045         inline QByteArray toString() const
0046         {
0047             return m_string;
0048         }
0049         inline QList<QByteArray> toList() const
0050         {
0051             return m_list;
0052         }
0053 
0054     private:
0055         Type m_type;
0056         QByteArray m_string;
0057         QList<QByteArray> m_list;
0058     };
0059 
0060     inline QByteArray toString() const
0061     {
0062         QByteArray result;
0063 
0064         foreach (const Part &part, content) {
0065             if (part.type() == Part::List) {
0066                 result += '(';
0067                 foreach (const QByteArray &item, part.toList()) {
0068                     result += ' ';
0069                     result += item;
0070                 }
0071                 result += " ) ";
0072             } else {
0073                 result += part.toString() + ' ';
0074             }
0075         }
0076 
0077         if (!responseCode.isEmpty()) {
0078             result += "[ ";
0079             foreach (const Part &part, responseCode) {
0080                 if (part.type() == Part::List) {
0081                     result += '(';
0082                     foreach (const QByteArray &item, part.toList()) {
0083                         result += ' ';
0084                         result += item;
0085                     }
0086                     result += " ) ";
0087                 } else {
0088                     result += part.toString() + ' ';
0089                 }
0090             }
0091             result += " ]";
0092         }
0093 
0094         return result;
0095     }
0096 
0097     QList<Part> content;
0098     QList<Part> responseCode;
0099 };
0100 
0101 }
0102 
0103 Q_DECLARE_METATYPE(KIMAP2::Message)
0104 static const int _kimap_messageTypeId = qRegisterMetaType<KIMAP2::Message>();
0105 
0106 #endif