kmail

treebase.cpp
1/*
2 Copyright (c) 2008 Pradeepto K. Bhattacharya <pradeepto@kde.org>
3 ( adapted from tdepim/kmail/kmfolderseldlg.cpp and simplefoldertree.h )
4
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.
9
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.
14
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.
18*/
19
20#include "treebase.h"
21#include "kmfolder.h"
22#include "kmfoldertree.h"
23#include "simplefoldertree.h"
24
25#include <kdebug.h>
26#include <tdelistview.h>
27
28using namespace KMail;
29
30TreeBase::TreeBase( TQWidget *parent, KMFolderTree *folderTree,
31 const TQString &preSelection, bool mustBeReadWrite )
32 : TDEListView( parent ), mFolderTree( folderTree )
33{
34 Q_UNUSED( preSelection );
35 Q_UNUSED( mustBeReadWrite );
36 kdDebug(5006) << k_funcinfo << endl;
37
38 connect(this, TQ_SIGNAL(collapsed(TQListViewItem*)), TQ_SLOT(recolorRows()));
39 connect(this, TQ_SIGNAL(expanded(TQListViewItem*)), TQ_SLOT(recolorRows()));
40 connect( this, TQ_SIGNAL( contextMenuRequested( TQListViewItem*, const TQPoint &, int ) ),
41 this, TQ_SLOT( slotContextMenuRequested( TQListViewItem*, const TQPoint & ) ) );
42
43}
44
45const KMFolder * TreeBase::folder() const
46{
47 TQListViewItem * item = currentItem();
48 if( item ) {
49 TreeItemBase *base = dynamic_cast<TreeItemBase*>( item );
50 assert(base);
51 const KMFolder * folder = base->folder();
52 return folder;
53 }
54 return 0;
55}
56
57void TreeBase::setFolder( KMFolder *folder )
58 {
59 for ( TQListViewItemIterator it( this ) ; it.current() ; ++it )
60 {
61 const KMFolder *fld = dynamic_cast<TreeItemBase*>( it.current() )->folder();
62 if ( fld == folder )
63 {
64 setSelected( it.current(), true );
65 ensureItemVisible( it.current() );
66 }
67 }
68}
69
70void TreeBase::addChildFolder()
71{
72 kdDebug(5006) << k_funcinfo << endl;
73
74 const KMFolder *fld = folder();
75 if ( fld ) {
76 mFolderTree->addChildFolder( (KMFolder *) fld, parentWidget() );
77 reload( mLastMustBeReadWrite, mLastShowOutbox, mLastShowImapFolders );
78 setFolder( (KMFolder *) fld );
79 }
80}
81
82void TreeBase::slotContextMenuRequested( TQListViewItem *lvi, const TQPoint &p )
83{
84 kdDebug(5006) << k_funcinfo << endl;
85
86 if (!lvi)
87 return;
88 setCurrentItem( lvi );
89 setSelected( lvi, TRUE );
90
91 const KMFolder * folder = dynamic_cast<TreeItemBase*>( lvi )->folder();
92 if ( !folder || folder->noContent() || folder->noChildren() )
93 return;
94
95 TDEPopupMenu *folderMenu = new TDEPopupMenu;
96 folderMenu->insertTitle( folder->label() );
97 folderMenu->insertSeparator();
98 folderMenu->insertItem(SmallIconSet("folder-new"),
99 i18n("&New Subfolder..."), this,
100 TQ_SLOT(addChildFolder()));
101 kmkernel->setContextMenuShown( true );
102 folderMenu->exec (p, 0);
103 kmkernel->setContextMenuShown( false );
104 delete folderMenu;
105
106}
107
108void TreeBase::recolorRows()
109{
110 kdDebug(5006) << k_funcinfo << endl;
111
112 // Iterate through the list to set the alternate row flags.
113 int alt = 0;
114 TQListViewItemIterator it ( this );
115 while ( it.current() ) {
116 TQListViewItem * item = it.current() ;
117 if ( item->isVisible() ) {
118 bool visible = true;
119 TQListViewItem * parent = item->parent();
120 while ( parent ) {
121 if (!parent->isOpen()) {
122 visible = false;
123 break;
124 }
125 parent = parent->parent();
126 }
127
128 if ( visible ) {
129 TreeItemBase * treeItemBase = dynamic_cast<TreeItemBase*>( item );
130 treeItemBase->setAlternate( alt );
131 alt = !alt;
132 }
133 }
134 ++it;
135 }
136}
137
138void TreeBase::reload( bool mustBeReadWrite, bool showOutbox, bool showImapFolders,
139 const TQString& preSelection )
140{
141 clear();
142
143 mLastMustBeReadWrite = mustBeReadWrite;
144 mLastShowOutbox = showOutbox;
145 mLastShowImapFolders = showImapFolders;
146
147 TQListViewItem * lastItem = 0;
148 TQListViewItem * lastTopItem = 0;
149 TQListViewItem * selectedItem = 0;
150 int lastDepth = 0;
151
152 mFilter = "";
153 TQString path;
154
155 for ( TQListViewItemIterator it( mFolderTree ) ; it.current() ; ++it ) {
156 KMFolderTreeItem * fti = dynamic_cast<KMFolderTreeItem *>( it.current() );
157
158 if ( !fti || fti->protocol() == KFolderTreeItem::Search )
159 continue;
160
161 int depth = fti->depth();// - 1;
162 //kdDebug( 5006 ) << "LastDepth=" << lastDepth << "\tdepth=" << depth
163 // << "\tname=" << fti->text( 0 ) << endl;
164 TQListViewItem * item = 0;
165 if ( depth <= 0 ) {
166 // top level - first top level item or after last existing top level item
167 if ( lastTopItem )
168 item = createItem( this, lastTopItem );
169 else
170 item = createItem( this );
171 lastTopItem = item;
172 depth = 0;
173 path = "";
174 }
175 else {
176 if ( depth > lastDepth ) {
177 // next lower level - parent node will get opened
178 item = createItem( lastItem );
179 lastItem->setOpen( true );
180 }
181 else {
182
183 path = path.section( '/', 0, -2 - (lastDepth-depth) );
184 if ( depth == lastDepth )
185 // same level - behind previous item
186 item = createItem( lastItem->parent(), lastItem );
187 else if ( depth < lastDepth ) {
188 // above previous level - might be more than one level difference
189 // but highest possibility is top level
190 while ( ( depth <= --lastDepth ) && lastItem->parent() ) {
191 lastItem = static_cast<TQListViewItem *>( lastItem->parent() );
192 }
193 if ( lastItem->parent() )
194 item = createItem( lastItem->parent(), lastItem );
195 else {
196 // chain somehow broken - what does cause this ???
197 kdDebug( 5006 ) << "You shouldn't get here: depth=" << depth
198 << "folder name=" << fti->text( 0 ) << endl;
199 item = createItem( this );
200 lastTopItem = item;
201 }
202 }
203 }
204 }
205
206 if ( depth > 0 )
207 path += "/";
208 path += fti->text( 0 );
209
210
211 item->setText( mFolderColumn, fti->text( 0 ) );
212 item->setText( mPathColumn, path );
213 // Make items without folders and top level items unselectable
214 // (i.e. root item Local Folders and IMAP accounts)
215 if ( !fti->folder() || depth == 0 || ( mustBeReadWrite && fti->folder()->isReadOnly() ) ) {
216 item->setSelectable( false );
217 } else {
218 TreeItemBase * treeItemBase = dynamic_cast<TreeItemBase*>( item );
219 assert(treeItemBase);
220 treeItemBase->setFolder( fti->folder() );
221 if ( preSelection == treeItemBase->folder()->idString() )
222 selectedItem = item;
223 }
224 lastItem = item;
225 lastDepth = depth;
226 }
227
228 if ( selectedItem ) {
229 setSelected( selectedItem, true );
230 ensureItemVisible( selectedItem );
231 }
232
233}
234
235#include "treebase.moc"
Mail folder.
Definition: kmfolder.h:69
virtual TQString label() const
Returns the label of the folder for visualization.
Definition: kmfolder.cpp:581
bool noChildren() const
Returns, if the folder can't have children.
Definition: kmfolder.cpp:311
bool noContent() const
Returns, if the folder can't contain mails, but only subfolder.
Definition: kmfolder.cpp:301
folderdiaquotatab.h
Definition: aboutdata.cpp:40