summaryrefslogtreecommitdiffstats
path: root/kmail/treebase.cpp
blob: 1e23a00712ae9a98beb0237d1476c7ed7745df1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
    Copyright (c) 2008 Pradeepto K. Bhattacharya <pradeepto@kde.org>
      ( adapted from tdepim/kmail/kmfolderseldlg.cpp and simplefoldertree.h  )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "treebase.h"
#include "kmfolder.h"
#include "kmfoldertree.h"
#include "simplefoldertree.h"

#include <kdebug.h>
#include <tdelistview.h>

using namespace KMail;

TreeBase::TreeBase( TQWidget *parent, KMFolderTree *folderTree,
        const TQString &preSelection, bool mustBeReadWrite )
       : TDEListView( parent ), mFolderTree( folderTree )
{
     Q_UNUSED( preSelection );
     Q_UNUSED( mustBeReadWrite );
     kdDebug(5006) << k_funcinfo << endl;

     connect(this, TQT_SIGNAL(collapsed(TQListViewItem*)), TQT_SLOT(recolorRows()));
     connect(this, TQT_SIGNAL(expanded(TQListViewItem*)),  TQT_SLOT(recolorRows()));
     connect( this, TQT_SIGNAL( contextMenuRequested( TQListViewItem*, const TQPoint &, int ) ),
           this, TQT_SLOT( slotContextMenuRequested( TQListViewItem*, const TQPoint & ) ) );

}

const KMFolder * TreeBase::folder() const
{
    TQListViewItem * item = currentItem();
    if( item ) {
      TreeItemBase *base = dynamic_cast<TreeItemBase*>( item );
      assert(base);
      const KMFolder * folder = base->folder();
      return folder;
    }
    return 0;
}

void TreeBase::setFolder( KMFolder *folder )
 {
      for ( TQListViewItemIterator it( this ) ; it.current() ; ++it )
      {
        const KMFolder *fld = dynamic_cast<TreeItemBase*>( it.current() )->folder();
        if ( fld == folder )
        {
           setSelected( it.current(), true );
           ensureItemVisible( it.current() );
        }
      }
}

void TreeBase::addChildFolder()
{
     kdDebug(5006) << k_funcinfo << endl;

      const KMFolder *fld = folder();
      if ( fld ) {
         mFolderTree->addChildFolder( (KMFolder *) fld, parentWidget() );
	reload( mLastMustBeReadWrite, mLastShowOutbox, mLastShowImapFolders );
	setFolder( (KMFolder *) fld );
      }
}

void TreeBase::slotContextMenuRequested( TQListViewItem *lvi,  const TQPoint &p )
{
     kdDebug(5006) << k_funcinfo << endl;

      if (!lvi)
        return;
        setCurrentItem( lvi );
        setSelected( lvi, TRUE );

      const KMFolder * folder = dynamic_cast<TreeItemBase*>( lvi )->folder();
      if ( !folder || folder->noContent() || folder->noChildren() )
        return;

      TDEPopupMenu *folderMenu = new TDEPopupMenu;
      folderMenu->insertTitle( folder->label() );
      folderMenu->insertSeparator();
      folderMenu->insertItem(SmallIconSet("folder-new"),
                          i18n("&New Subfolder..."), this,
                          TQT_SLOT(addChildFolder()));
      kmkernel->setContextMenuShown( true );
      folderMenu->exec (p, 0);
      kmkernel->setContextMenuShown( false );
      delete folderMenu;

}

void TreeBase::recolorRows()
{
     kdDebug(5006) << k_funcinfo << endl;

       // Iterate through the list to set the alternate row flags.
       int alt = 0;
       TQListViewItemIterator it ( this );
       while ( it.current() ) {
        TQListViewItem * item = it.current() ;
        if ( item->isVisible() ) {
           bool visible = true;
           TQListViewItem * parent = item->parent();
           while ( parent ) {
           if (!parent->isOpen()) {
             visible = false;
             break;
           }
           parent = parent->parent();
         }

         if ( visible ) {
          TreeItemBase * treeItemBase = dynamic_cast<TreeItemBase*>( item );
          treeItemBase->setAlternate( alt );
          alt = !alt;
         }
       }
       ++it;
      }
}

void TreeBase::reload( bool mustBeReadWrite, bool showOutbox, bool showImapFolders,
                   const TQString& preSelection )
{
      clear();

      mLastMustBeReadWrite = mustBeReadWrite;
      mLastShowOutbox = showOutbox;
      mLastShowImapFolders = showImapFolders;

      TQListViewItem * lastItem = 0;
      TQListViewItem * lastTopItem = 0;
      TQListViewItem * selectedItem = 0;
      int lastDepth = 0;

      mFilter = "";
      TQString path;

      for ( TQListViewItemIterator it( mFolderTree ) ; it.current() ; ++it ) {
        KMFolderTreeItem * fti = dynamic_cast<KMFolderTreeItem *>( it.current() );

        if ( !fti || fti->protocol() == KFolderTreeItem::Search )
          continue;

        int depth = fti->depth();// - 1;
        //kdDebug( 5006 ) << "LastDepth=" << lastDepth << "\tdepth=" << depth
        //                << "\tname=" << fti->text( 0 ) << endl;
        TQListViewItem * item = 0;
        if ( depth <= 0 ) {
          // top level - first top level item or after last existing top level item
          if ( lastTopItem )
            item = createItem( this, lastTopItem );
          else
            item = createItem( this );
          lastTopItem = item;
          depth = 0;
          path  = "";
        }
        else {
          if ( depth > lastDepth ) {
            // next lower level - parent node will get opened
            item = createItem( lastItem );
            lastItem->setOpen( true );
          }
          else {

            path = path.section( '/', 0, -2 - (lastDepth-depth) );
            if ( depth == lastDepth )
              // same level - behind previous item
              item = createItem( lastItem->parent(), lastItem );
            else if ( depth < lastDepth ) {
              // above previous level - might be more than one level difference
              // but highest possibility is top level
              while ( ( depth <= --lastDepth ) && lastItem->parent() ) {
                lastItem = static_cast<TQListViewItem *>( lastItem->parent() );
              }
              if ( lastItem->parent() )
                item = createItem( lastItem->parent(), lastItem );
              else {
                // chain somehow broken - what does cause this ???
                kdDebug( 5006 ) << "You shouldn't get here: depth=" << depth
                                << "folder name=" << fti->text( 0 ) << endl;
                item = createItem( this );
                lastTopItem = item;
              }
            }
          }
        }

        if ( depth > 0 )
          path += "/";
        path += fti->text( 0 );


        item->setText( mFolderColumn, fti->text( 0 ) );
        item->setText( mPathColumn, path );
        // Make items without folders and top level items unselectable
        // (i.e. root item Local Folders and IMAP accounts)
        if ( !fti->folder() || depth == 0 || ( mustBeReadWrite && fti->folder()->isReadOnly() ) ) {
          item->setSelectable( false );
        } else {
          TreeItemBase * treeItemBase = dynamic_cast<TreeItemBase*>( item );
          assert(treeItemBase);
          treeItemBase->setFolder( fti->folder() );
          if ( preSelection == treeItemBase->folder()->idString() )
            selectedItem = item;
        }
        lastItem = item;
        lastDepth = depth;
      }

      if ( selectedItem ) {
        setSelected( selectedItem, true );
        ensureItemVisible( selectedItem );
      }

}

#include "treebase.moc"