KMbox

mbox.h
1/*
2 SPDX-FileCopyrightText: 2009 Bertjan Broeksema <broeksema@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kmbox_export.h"
10#include "mboxentry.h"
11#include <memory>
12
13#include <KMime/KMimeMessage>
14
15namespace KMBox
16{
17class MBoxPrivate;
18
19/**
20 * @short A class to access mail storages in MBox format.
21 *
22 * @author Bertjan Broeksema <broeksema@kde.org>
23 * @since 4.6
24 */
25class KMBOX_EXPORT MBox
26{
27public:
28 /**
29 * Describes the type of locking that will be used.
30 */
31 enum LockType { ProcmailLockfile, MuttDotlock, MuttDotlockPrivileged, None };
32
33 /**
34 * Creates a new mbox object.
35 */
36 MBox();
37
38 /**
39 * Destroys the mbox object.
40 *
41 * The file will be unlocked if it is still open.
42 */
43 ~MBox();
44
45 /**
46 * Appends @p message to the MBox and returns the corresponding mbox entry for it.
47 * You must load a mbox file by making a call to load( const QString& ) before
48 * appending entries.
49 * The returned mbox entry is <em>only</em> valid for that particular file.
50 *
51 * @param message The message to append to the mbox.
52 * @return the corresponding mbox entry for the message in the file or an invalid mbox entry
53 * if the message was not added.
54 */
55 [[nodiscard]] MBoxEntry appendMessage(const KMime::Message::Ptr &message);
56
57 /**
58 * Retrieve the mbox entry objects for all emails from the file except the
59 * @p deleteEntries.
60 * The @p deletedEntries should be a list of mbox entries with offsets of deleted messages.
61 * @param deletedEntries list of mbox entries that have been deleted and need not be retrieved
62 * Note: One <em>must</em> call load() before calling this method.
63 */
64 [[nodiscard]] MBoxEntry::List entries(const MBoxEntry::List &deletedEntries = MBoxEntry::List()) const;
65
66 /**
67 * Returns the file name that was passed to the last call to load().
68 */
69 [[nodiscard]] QString fileName() const;
70
71 /**
72 * Loads the raw mbox data from disk into the current MBox object. Messages
73 * already present are <em>not</em> preserved. This method does not load the
74 * full messages into memory but only the offsets of the messages and their
75 * sizes. If the file currently is locked this method will do nothing and
76 * return false. Appended messages that are not written yet will get lost.
77 *
78 * @param fileName the name of the mbox on disk.
79 * @return true, if successful, false on error.
80 *
81 * @see save( const QString & )
82 */
83 [[nodiscard]] bool load(const QString &fileName);
84
85 /**
86 * Locks the mbox file using the configured lock method. This can be used
87 * for consecutive calls to readMessage and readMessageHeaders. Calling lock()
88 * before these calls prevents the mbox file being locked for every call.
89 *
90 * NOTE: Even when the lock method is None the mbox is internally marked as
91 * locked. This means that it must be unlocked before calling load().
92 *
93 * @return true if locked successful, false on error.
94 *
95 * @see setLockType( LockType ), unlock()
96 */
97 [[nodiscard]] bool lock();
98
99 /**
100 * Returns whether or not the mbox currently is locked.
101 */
102 [[nodiscard]] bool locked() const;
103
104 /**
105 * Removes all messages for the given mbox entries from the current reference file
106 * (the file that is loaded with load( const QString & ).
107 * This method will first check if all lines at the offsets are actually
108 * separator lines if this is not then no message will be deleted to prevent
109 * corruption.
110 *
111 * @param deletedEntries The mbox entries of the messages that should be removed from
112 * the file.
113 * @param movedEntries Optional list for storing pairs of mbox entries that got moved
114 * within the file due to the deletions.
115 * The @c first member of the pair is the entry with the original offsets
116 * the @c second member is the entry with the new (current) offset
117 *
118 * @return true if all offsets refer to a mbox separator line and a file was
119 * loaded, false otherwise. If the latter, the physical file has
120 * not changed.
121 */
122 [[nodiscard]] bool purge(const MBoxEntry::List &deletedEntries, QList<MBoxEntry::Pair> *movedEntries = nullptr);
123
124 /**
125 * Reads the entire message from the file for the given mbox @p entry. If the
126 * mbox file is not locked this method will lock the file before reading and
127 * unlock it after reading. If the file already is locked, it will not
128 * unlock the file after reading the entry.
129 *
130 * @param entry The entry in the mbox file.
131 * @return Message for the given entry or 0 if the file could not be locked
132 * or the entry offset > fileSize.
133 *
134 * @see lock(), unlock()
135 */
136 KMime::Message *readMessage(const MBoxEntry &entry);
137
138 /**
139 * Reads the headers of the message for the given mbox @p entry. If the
140 * mbox file is not locked this method will lock the file before reading and
141 * unlock it after reading. If the file already is locked, it will not
142 * unlock the file after reading the entry.
143 *
144 * @param entry The entry in the mbox file.
145 * @return QByteArray containing the raw message header data.
146 *
147 * @see lock(), unlock()
148 */
149 [[nodiscard]] QByteArray readMessageHeaders(const MBoxEntry &entry);
150
151 /**
152 * Reads the entire message from the file for the given mbox @p entry. If the
153 * mbox file is not locked this method will lock the file before reading and
154 * unlock it after reading. If the file already is locked, it will not
155 * unlock the file after reading the entry.
156 *
157 * @param entry The entry in the mbox file.
158 * @return QByteArray containing the raw message data.
159 *
160 * @see lock(), unlock()
161 */
162 [[nodiscard]] QByteArray readRawMessage(const MBoxEntry &entry);
163
164 /**
165 * Writes the mbox to disk. If the fileName is empty only appended messages
166 * will be written to the file that was passed to load( const QString & ).
167 * Otherwise the contents of the file that was loaded with load is copied to
168 * @p fileName first.
169 *
170 * @param fileName the name of the file
171 * @return true if the save was successful; false otherwise.
172 *
173 * @see load( const QString & )
174 */
175 [[nodiscard]] bool save(const QString &fileName = QString());
176
177 /**
178 * Sets the locktype that should be used for locking the mbox file. If the
179 * new LockType cannot be used (e.g. the lockfile executable could not be
180 * found) the LockType will not be changed.
181 * @param ltype the locktype to set
182 * This method will not do anything if the mbox object is currently locked
183 * to make sure that it doesn't leave a locked file for one of the lockfile
184 * / mutt_dotlock methods.
185 */
186 [[nodiscard]] bool setLockType(LockType ltype);
187
188 /**
189 * Sets the lockfile that should be used by the procmail or the KDE lock
190 * file method. If this method is not called and one of the before mentioned
191 * lock methods is used the name of the lock file will be equal to
192 * MBOXFILENAME.lock.
193 * @param lockFile the lockfile to set
194 */
195 void setLockFile(const QString &lockFile);
196
197 /**
198 * By default the unlock method will directly unlock the file. However this
199 * is expensive in case of many consecutive calls to readEntry. Setting the
200 * time out to a non zero value will keep the lock open until the timeout has
201 * passed. On each read the timer will be reset.
202 * @param msec the time out to set for file lock
203 */
204 void setUnlockTimeout(int msec);
205
206 /**
207 * Unlock the mbox file.
208 *
209 * @return true if the unlock was successful, false otherwise.
210 *
211 * @see lock()
212 */
213 [[nodiscard]] bool unlock();
214 /**
215 * Set the access mode of the mbox file to read only.
216 *
217 * If this is set to true, the mbox file can only be read from disk.
218 * When the mbox file given in load() can not be opened in readWrite mode,
219 * but can be opened in readOnly mode, this flag is automatically set to true.
220 * You can still append messages, which are stored in memory
221 * until save() is called, but the mbox can not be saved/purged to itself.
222 * However it is possible to save it to a different file.
223 * @param ro the readOnly flag to use
224 *
225 * @see save( const QString & )
226 *
227 * @since 4.14.5
228 */
229 void setReadOnly(bool ro = true);
230
231 /**
232 * Returns if the current access mode is set to readOnly.
233 *
234 * The access mode can either be set explicitly with setReadOnly() or
235 * implicitly by calling load() on a readOnly file.
236 *
237 * @since 4.14.5
238 */
239 [[nodiscard]] bool isReadOnly() const;
240
241private:
242 //@cond PRIVATE
243 Q_DISABLE_COPY(MBox)
244 std::unique_ptr<class MBoxPrivate> const d;
245 //@endcond
246};
247}
A class that encapsulates an entry of a MBox.
Definition mboxentry.h:25
A class to access mail storages in MBox format.
Definition mbox.h:26
LockType
Describes the type of locking that will be used.
Definition mbox.h:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:04 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.