File indexing completed on 2024-06-23 05:20:26

0001 /*
0002     Copyright (c) 2007 Till Adam <adam@kde.org>
0003     Copyright (c) 2017 Christian Mollekopf <mollekopf@kolabsys.com>
0004 
0005     This library is free software; you can redistribute it and/or modify it
0006     under the terms of the GNU Library General Public License as published by
0007     the Free Software Foundation; either version 2 of the License, or (at your
0008     option) any later version.
0009 
0010     This library is distributed in the hope that it will be useful, but WITHOUT
0011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0013     License for more details.
0014 
0015     You should have received a copy of the GNU Library General Public License
0016     along with this library; see the file COPYING.LIB.  If not, write to the
0017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018     02110-1301, USA.
0019 */
0020 
0021 #ifndef MAILDIR_H
0022 #define MAILDIR_H
0023 
0024 #include <QString>
0025 #include <QStringList>
0026 
0027 class QDateTime;
0028 
0029 namespace KPIM {
0030 
0031 class Maildir
0032 {
0033 public:
0034     /**
0035       Create a new Maildir object.
0036       @param path The path to the maildir, if @p isRoot is @c false, that's the path
0037       to the folder containing the cur/new/tmp folders, if @p isRoot is @c true this
0038       is the path to a folder containing a number of maildirs.
0039       @param isRoot Indicate whether this is a maildir containing mails and various
0040       sub-folders or a container only containing maildirs.
0041     */
0042     explicit Maildir( const QString& path = QString(), bool isRoot = false );
0043     /* Copy constructor */
0044     Maildir(const Maildir & rhs);
0045     /* Copy operator */
0046     Maildir& operator=(const Maildir & rhs);
0047     /** Equality comparison */
0048     bool operator==(const Maildir & rhs) const;
0049     /* Destructor */
0050     ~Maildir();
0051 
0052     /** Returns whether the maildir has all the necessary subdirectories,
0053      * that they are readable, etc.
0054      * @param createMissingFolders if true (the default), the cur/new/tmp folders are created if they are missing
0055      */
0056     bool isValid( bool createMissingFolders = true ) const;
0057 
0058     /**
0059      * Returns whether this is a normal maildir or a container containing maildirs.
0060      */
0061     bool isRoot() const;
0062 
0063     /**
0064      * Make a valid maildir at the path of this Maildir object. This involves
0065      * creating the necessary subdirs, etc. Note that an empty Maildir is
0066      * not valid, unless it is given  valid path, or until create( ) is
0067      * called on it.
0068      */
0069     bool create();
0070 
0071     /**
0072      * Remove the maildir and everything it contains.
0073      */
0074     bool remove();
0075 
0076     /**
0077      * Returns the path of this maildir.
0078      */
0079     QString path() const;
0080 
0081     /**
0082      * Returns the name of this maildir.
0083      */
0084     QString name() const;
0085 
0086     /**
0087      * Returns the list of items (mails) in the maildir. These are keys, which
0088      * map to filenames, internally, but that's an implementation detail, which
0089      * should not be relied on.
0090      */
0091     QStringList entryList() const;
0092 
0093     /** Returns the list of items (mails) in the maildirs "new" folder. These are keys, which
0094      * map to filenames, internally, but that's an implementation detail, which
0095      * should not be relied on.
0096      */
0097     QStringList listNew() const;
0098 
0099     /** Returns the list of items (mails) in the maildirs "cur" folder. These are keys, which
0100      * map to filenames, internally, but that's an implementation detail, which
0101      * should not be relied on.
0102      */
0103     QStringList listCurrent() const;
0104 
0105     /** Return the path to the "new" directory */
0106     QString pathToNew() const;
0107 
0108     /** Return the path to the "cur" directory */
0109     QString pathToCurrent() const;
0110 
0111     /**
0112      * Returns the full path to the subdir (the NAME.directory folder ).
0113      **/
0114     QString subDirPath() const;
0115 
0116     /**
0117      * Return the full path to the file identified by key (it can be either in the "new" or "cur" folder
0118      **/
0119     QString findRealKey( const QString& key ) const;
0120 
0121     /**
0122      * Returns the list of subfolders, as names (relative paths). Use the
0123      * subFolder method to get Maildir objects representing them.
0124      */
0125     QStringList subFolderList() const;
0126 
0127     /**
0128      * Adds subfolder with the given @p folderName.
0129      * @return an empty string on failure or the full path of the new subfolder
0130      *         on success
0131      */
0132     QString addSubFolder( const QString& folderName );
0133 
0134     /**
0135      * Removes subfolder with the given @p folderName. Returns success or failure.
0136      */
0137     bool removeSubFolder( const QString& folderName );
0138 
0139     /**
0140      * Returns a Maildir object for the given @p folderName. If such a folder
0141      * exists, the Maildir object will be valid, otherwise you can call create()
0142      * on it, to make a subfolder with that name.
0143      */
0144     Maildir subFolder( const QString& folderName ) const;
0145 
0146     /**
0147      * Returns the parent Maildir object for this Maildir, if there is one (ie. this is not the root).
0148      */
0149     Maildir parent() const;
0150 
0151     /**
0152      * Returns the size of the file in the maildir with the given @p key or \c -1 if key is not valid.
0153      * @since 4.2
0154      */
0155     qint64 size( const QString& key ) const;
0156 
0157     /**
0158      * Returns the modification time of the file in the maildir with the given @p key.
0159      * @since 4.7
0160      */
0161     QDateTime lastModified( const QString &key ) const;
0162 
0163     /**
0164      * Move all mails in new to cur
0165      */
0166     void importNewMails();
0167 
0168     /**
0169      * Return the contents of the file in the maildir with the given @p key.
0170      */
0171     QByteArray readEntry( const QString& key ) const;
0172 
0173     enum Flag {
0174         Forwarded = 0x1,
0175         Replied = 0x2,
0176         Seen = 0x4,
0177         Flagged = 0x8,
0178         Deleted = 0x10
0179     };
0180     Q_DECLARE_FLAGS(Flags, Flag);
0181 
0182     /**
0183      * Return the flags encoded in the maildir file name for an entry
0184      **/
0185     static Flags readEntryFlags( const QString& key );
0186 
0187     /**
0188      * Return the contents of the headers section of the file the maildir with the given @p file, that
0189      * is a full path to the file. You can get it by using findRealKey(key) .
0190      */
0191     static QByteArray readEntryHeadersFromFile( const QString& file );
0192 
0193     /**
0194      * Return the contents of the headers section of the file the maildir with the given @p key.
0195      */
0196     QByteArray readEntryHeaders( const QString& key ) const;
0197 
0198     /**
0199      * Write the given @p data to a file in the maildir with the given  @p key.
0200      * Returns true in case of success, false in case of any error.
0201      */
0202     bool writeEntry( const QString& key, const QByteArray& data );
0203 
0204     /**
0205      * Adds the given @p data to the maildir. Returns the key of the entry.
0206      */
0207     QString addEntry( const QByteArray& data );
0208     QString addEntryFromPath(const QString& path);
0209 
0210     /**
0211      * Removes the entry with the given @p key. Returns success or failure.
0212      */
0213     bool removeEntry( const QString& key );
0214 
0215     /**
0216      * Change the flags for an entry specified by @p key. Returns the new key of the entry (the key might change because
0217      * flags are stored in the unique filename).
0218      */
0219     QString changeEntryFlags( const QString& key, const Flags& flags );
0220 
0221     /**
0222      * Moves this maildir into @p destination.
0223      */
0224     bool moveTo( const Maildir &destination );
0225 
0226     /**
0227      * Renames this maildir to @p newName.
0228      */
0229     bool rename( const QString &newName );
0230 
0231     /**
0232      * Moves the file with the given @p key into the Maildir @p destination.
0233      * @returns The new file name inside @p destination.
0234      */
0235     QString moveEntryTo( const QString& key, const KPIM::Maildir& destination );
0236 
0237     /**
0238      * Creates the maildir tree structure specific directory path that the
0239      * given @p folderPath folder would have for its sub folders
0240      * @param folderPath a maildir folder path
0241      * @return the relative subDirPath for the given @p folderPath
0242      *
0243      * @see subDirNameForFolderName()
0244      */
0245     static QString subDirPathForFolderPath( const QString &folderPath );
0246 
0247     /**
0248      * Creates the maildir tree structure specific directory name that the
0249      * given @p folderName folder would have for its sub folders
0250      * @param folderName a maildir folder name
0251      * @return the relative subDirName for the given @p folderMame
0252      *
0253      * @see subDirPathForFolderPath()
0254      */
0255     static QString subDirNameForFolderName( const QString &folderName );
0256 
0257     /**
0258      * Returns the key from the file identified by the full path @param file.
0259      */
0260     static QString getKeyFromFile( const QString& file );
0261 
0262     /**
0263      * Returns the directory from a file.
0264      * 
0265      * Strips key and new/cur/tmp.
0266      * The returned path is ended with a trailing slash.
0267      */
0268     static QString getDirectoryFromFile( const QString& file );
0269 
0270 private:
0271     void swap( const Maildir& );
0272     class Private;
0273     Private *d;
0274 };
0275 
0276 }
0277 #endif // __MAILDIR_H__