File indexing completed on 2024-12-22 04:57:39

0001 /*
0002  *   SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
0003  *   SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  */
0008 
0009 #pragma once
0010 
0011 #include "kmindexreader_export.h"
0012 
0013 #include <Akonadi/MessageStatus>
0014 using Akonadi::MessageStatus;
0015 
0016 #include <QFile>
0017 #include <QList>
0018 #include <QSharedPointer>
0019 #include <QString>
0020 #include <QStringList>
0021 
0022 class KMINDEXREADER_EXPORT KMIndexData
0023 {
0024     Q_DISABLE_COPY(KMIndexData)
0025 public:
0026     KMIndexData();
0027     /** Status object of the message. */
0028     MessageStatus &status();
0029     QStringList tagList() const;
0030     quint64 uid() const;
0031     bool isEmpty() const;
0032 
0033 private:
0034     QString mCachedStringParts[20];
0035     unsigned long mCachedLongParts[20];
0036     bool mPartsCacheBuilt;
0037 
0038     MessageStatus mStatus;
0039     friend class KMIndexReader;
0040     friend class TestIdxReader;
0041 };
0042 
0043 using KMIndexDataPtr = QSharedPointer<KMIndexData>;
0044 
0045 /**
0046  * @short A class to read legacy kmail (< 4.5) index files
0047  *
0048  * This class provides read-only access to legacy kmail index files.
0049  * It uses old kmfolderindex code, authors attributed as appropriate.
0050  * @author Casey Link <unnamedrambler@gmail.com>
0051  */
0052 class KMINDEXREADER_EXPORT KMIndexReader
0053 {
0054 public:
0055     explicit KMIndexReader(const QString &indexFile);
0056     ~KMIndexReader();
0057 
0058     bool error() const;
0059 
0060     /**
0061      * begins the index reading process
0062      */
0063     bool readIndex();
0064 
0065     enum MsgPartType {
0066         MsgNoPart = 0,
0067         // unicode strings
0068         MsgFromPart = 1,
0069         MsgSubjectPart = 2,
0070         MsgToPart = 3,
0071         MsgReplyToIdMD5Part = 4,
0072         MsgIdMD5Part = 5,
0073         MsgXMarkPart = 6,
0074         // unsigned long
0075         MsgOffsetPart = 7,
0076         MsgLegacyStatusPart = 8,
0077         MsgSizePart = 9,
0078         MsgDatePart = 10,
0079         // unicode string
0080         MsgFilePart = 11,
0081         // unsigned long
0082         MsgCryptoStatePart = 12,
0083         MsgMDNSentPart = 13,
0084         // another two unicode strings
0085         MsgReplyToAuxIdMD5Part = 14,
0086         MsgStrippedSubjectMD5Part = 15,
0087         // and another unsigned long
0088         MsgStatusPart = 16,
0089         MsgSizeServerPart = 17,
0090         MsgUIDPart = 18,
0091         // unicode string
0092         MsgTagPart = 19
0093     };
0094 
0095     KMIndexDataPtr dataByOffset(quint64 offset) const;
0096 
0097     KMIndexDataPtr dataByFileName(const QString &fileName) const;
0098 
0099 private:
0100     /**
0101      * Reads the header of an index
0102      */
0103     bool readHeader(int *version);
0104 
0105     /**
0106      * creates a message object from an old index files
0107      */
0108     bool fromOldIndexString(KMIndexData *msg, const QByteArray &str, bool toUtf8);
0109 
0110     bool fillPartsCache(KMIndexData *msg, off_t off, short int len);
0111 
0112     QList<KMIndexDataPtr> messages();
0113 
0114     QString mIndexFileName;
0115     QFile mIndexFile;
0116     FILE *mFp = nullptr;
0117 
0118     bool mConvertToUtf8;
0119     bool mIndexSwapByteOrder; // Index file was written with swapped byte order
0120     int mIndexSizeOfLong; // Index file was written with longs of this size
0121     off_t mHeaderOffset;
0122 
0123     bool mError;
0124 
0125     /** list of index entries or messages */
0126     QList<KMIndexDataPtr> mMsgList;
0127     QHash<QString, KMIndexDataPtr> mMsgByFileName;
0128     QHash<quint64, KMIndexDataPtr> mMsgByOffset;
0129     friend class TestIdxReader;
0130 };