1 /******************************************************************************
3 * Copyright 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
21 #ifndef __MESSAGELIST_CORE_ITEM_H__
22 #define __MESSAGELIST_CORE_ITEM_H__
28 #include <time.h> // for time_t
30 #include <kmime/kmime_headers.h>
32 #include <akonadi/kmime/messagestatus.h>
34 #include <messagelist/core/model.h>
35 #include <messagelist/messagelist_export.h>
47 * A single item of the MessageList tree managed by MessageList::Model.
49 * This class stores basic information needed in all the subclasses which
50 * at the moment of writing are MessageItem and GroupHeaderItem.
52 class MESSAGELIST_EXPORT Item
55 friend class ModelPrivate
;
59 * The type of the Item.
63 GroupHeader
, ///< This item is a GroupHeaderItem
64 Message
, ///< This item is a MessageItem
65 InvisibleRoot
///< This item is just Item and it's the only InvisibleRoot per Model.
69 * Specifies the initial expand status for the item that should be applied
70 * when it's attacched to the viewable tree. Needed as a workaround for
71 * QTreeView limitations in handling item expansion.
73 enum InitialExpandStatus
75 ExpandNeeded
, ///< Must expand when this item becomes viewable
76 NoExpandNeeded
, ///< No expand needed at all
77 ExpandExecuted
///< Item already expanded
82 * Creates an Item. Only derived classes and MessageList::Model should access this.
85 Item( Type type
, ItemPrivate
*dd
);
89 * Destroys the Item. Should be protected just like the constructor but the QList<>
90 * helpers need to access it, so it's public actually.
95 * Returns the type of this item. The Type can be set only in the constructor.
100 * The initial expand status we have to honor when attacching to the viewable root.
102 InitialExpandStatus
initialExpandStatus() const;
105 * Set the initial expand status we have to honor when attacching to the viewable root.
107 void setInitialExpandStatus( InitialExpandStatus initialExpandStatus
);
110 * Is this item attached to the viewable root ?
112 bool isViewable() const;
115 * Return true if Item pointed by it is an ancestor of this item (that is,
116 * if it is it's parent, parent of it's parent, parent of it's parent of it's parent etc...
118 bool hasAncestor( const Item
* it
) const;
121 * Makes this item viewable, that is, notifies it's existence to any listener
122 * attacched to the "rowsInserted()" signal, most notably QTreeView.
124 * This will also make all the children viewable.
126 void setViewable( Model
*model
, bool bViewable
);
129 * Return the list of child items. May be null.
131 QList
< Item
* > * childItems() const;
134 * Returns the child item at position idx or 0 if idx is out of the allowable range.
136 Item
* childItem( int idx
) const;
139 * Returns the first child item, if any.
141 Item
* firstChildItem() const;
144 * Returns the item that is visually below the specified child if this item.
145 * Note that the returned item may belong to a completely different subtree.
147 Item
* itemBelowChild( Item
* child
);
150 * Returns the item that is visually above the specified child if this item.
151 * Note that the returned item may belong to a completely different subtree.
153 Item
* itemAboveChild( Item
* child
);
156 * Returns the deepest item in the subtree originating at this item.
158 Item
* deepestItem();
161 * Returns the item that is visually below this item in the tree.
162 * Note that the returned item may belong to a completely different subtree.
167 * Returns the item that is visually above this item in the tree.
168 * Note that the returned item may belong to a completely different subtree.
173 * Debug helper. Dumps the structure of this subtree.
175 void dump( const QString
&prefix
);
178 * Returns the number of children of this Item.
180 int childItemCount() const;
183 * Convenience function that returns true if this item has children.
185 bool hasChildren() const;
188 * A structure used with MessageList::Item::childItemStats().
189 * Contains counts of total and unread messages in a subtree.
194 unsigned int mTotalChildCount
; // total
195 unsigned int mUnreadChildCount
; // unread only
198 : mTotalChildCount( 0 ), mUnreadChildCount( 0 )
203 * Gathers statistics about child items.
204 * For performance purposes assumes that this item has children.
205 * You MUST check it before calling it.
207 void childItemStats( ChildItemStats
&stats
) const;
210 * Returns the actual index of the child Item item or -1 if
211 * item is not a child of this Item.
213 int indexOfChildItem( Item
*item
) const;
216 * Returns the cached guess for the index of this item in the parent's child list.
218 * This is used to speed up the index lookup with the following algorithm:
219 * Ask the parent if this item is at the position specified by index guess (this costs ~O(1)).
220 * If the position matches we have finished, if it doesn't then perform
221 * a linear search via indexOfChildItem() (which costs ~O(n)).
223 int indexGuess() const;
226 * Updates the cached guess for the index of this item in the parent's child list.
227 * See indexGuess() for more information.
229 void setIndexGuess( int index
);
232 * Returns true if the specified Item is a position idx in the list of children.
233 * See indexGuess() for more information.
235 bool childItemHasIndex( const Item
*item
, int idx
) const;
238 * Returns the parent Item in the tree, or 0 if this item isn't attached to the tree.
239 * Please note that even if this item has a non-zero parent, it can be still non viewable.
240 * That is: the topmost parent of this item may be not attached to the viewable root.
242 Item
* parent() const;
245 * Sets the parent for this item. You should also take care of inserting
246 * this item in the parent's child list.
248 void setParent( Item
*pParent
);
251 * Returns the topmost parent item that is not a Root item (that is, is a Message or GroupHeader).
253 Item
* topmostNonRoot();
256 * Returns the status associated to this Item.
258 const Akonadi::MessageStatus
& status() const;
261 * Sets the status associated to this Item.
263 void setStatus( const Akonadi::MessageStatus
&status
);
266 * Returns a string describing the status e.g: "Read, Forwarded, Important"
268 QString
statusDescription() const;
271 * Returns the size of this item (size of the Message, mainly)
276 * Sets the size of this item (size of the Message, mainly)
278 void setSize( size_t size
);
281 * A string with a text rappresentation of size(). This is computed on-the-fly
282 * and cached until the size() changes.
284 const QString
& formattedSize();
287 * Returns the date of this item
292 * Sets the date of this item
294 void setDate( time_t date
);
297 * A string with a text rappresentation of date() obtained via Manager. This is computed on-the-fly
298 * and cached until the size() changes.
300 const QString
& formattedDate();
303 * Returns the maximum date in the subtree originating from this item.
304 * This is kept up-to-date by MessageList::Model.
306 time_t maxDate() const;
309 * Sets the maximum date in the subtree originating from this item.
311 void setMaxDate( time_t date
);
314 * A string with a text rappresentation of maxDate() obtained via Manager. This is computed on-the-fly
315 * and cached until the size() changes.
317 const QString
& formattedMaxDate();
320 * Recompute the maximum date from the current children list.
321 * Return true if the current max date changed and false otherwise.
323 bool recomputeMaxDate();
326 * Returns the sender associated to this item.
328 const QString
& sender() const;
331 * Sets the sender associated to this item.
333 void setSender( const QString
&sender
);
336 * Returns the receiver associated to this item.
338 const QString
& receiver() const;
341 * Sets the sender associated to this item.
343 void setReceiver( const QString
&receiver
);
346 * Returns the sender or the receiver, depending on the underlying StorageModel settings.
348 const QString
& senderOrReceiver() const;
351 * Sets the sender or the receiver: this should depend on the underlying StorageModel settings.
353 void setSenderOrReceiver( const QString
&senderOrReceiver
);
356 * Returns the subject associated to this Item.
358 const QString
& subject() const;
361 * Sets the subject associated to this Item.
363 void setSubject( const QString
&subject
);
366 * This is meant to be called right after the constructor.
367 * It sets up several items at once (so even if not inlined it's still a single call)
368 * and it skips some calls that can be avoided at constructor time.
370 void initialSetup( time_t date
, size_t size
,
371 const QString
&sender
,
372 const QString
&receiver
,
373 const QString
&senderOrReceiver
);
376 * This is meant to be called right after the constructor for MessageItem objects.
377 * It sets up several items at once (so even if not inlined it's still a single call).
379 void setSubjectAndStatus( const QString
&subject
,
380 const Akonadi::MessageStatus
&status
);
383 * Appends an Item to this item's child list.
384 * The Model is used for beginInsertRows()/endInsertRows() calls.
386 int appendChildItem( Model
*model
, Item
*child
);
389 * Appends a child item without inserting it via the model.
390 * This is useful in ThemeEditor which doesn't use a custom model for the items.
391 * You shouldn't need to use this function...
393 void rawAppendChildItem( Item
* child
);
396 * Removes a child from this item's child list without deleting it.
397 * The Model is used for beginRemoveRows()/endRemoveRows() calls.
399 void takeChildItem( Model
*model
, Item
*child
);
402 * Kills all the child items without emitting any signal, recursively.
403 * It should be used only when MessageList::Model is reset() afterwards.
405 void killAllChildItems();
408 ItemPrivate
* const d_ptr
;
409 Q_DECLARE_PRIVATE( Item
)
414 } // namespace MessageList
416 #endif //!__MESSAGELIST_CORE_ITEM_H__