kmail

foldertreebase.cpp
1 /*
2  Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #include "foldertreebase.h"
20 
21 #include "globalsettings.h"
22 #include "kmfolder.h"
23 #include "kmfolderdir.h"
24 #include "kmfoldertree.h"
25 #include "kmheaders.h"
26 #include "kmmainwidget.h"
27 #include "messagecopyhelper.h"
28 #include "folderstorage.h"
29 
30 #include <libtdepim/maillistdrag.h>
31 using KPIM::MailList;
32 using KPIM::MailListDrag;
33 
34 #include <tdeconfig.h>
35 #include <kiconloader.h>
36 #include <tdepopupmenu.h>
37 
38 #include <tqcursor.h>
39 
40 using namespace KMail;
41 
42 FolderTreeBase::FolderTreeBase(KMMainWidget *mainWidget, TQWidget * parent, const char * name) :
43  KFolderTree( parent, name ),
44  mMainWidget( mainWidget )
45 {
46  addAcceptableDropMimetype(MailListDrag::format(), false);
47 }
48 
49 void FolderTreeBase::contentsDropEvent(TQDropEvent * e)
50 {
51  TQListViewItem *item = itemAt( contentsToViewport(e->pos()) );
52  KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>(item);
53  if ( fti && fti->folder() && e->provides( MailListDrag::format() ) ) {
54  if ( e->source() == mMainWidget->headers()->viewport() ) {
55  int action;
56  if ( mMainWidget->headers()->folder() && mMainWidget->headers()->folder()->isReadOnly() )
57  action = DRAG_COPY;
58  else
59  action = dndMode();
60  // KMHeaders does copy/move itself
61  if ( action == DRAG_MOVE && fti->folder() )
62  emit folderDrop( fti->folder() );
63  else if ( action == DRAG_COPY && fti->folder() )
64  emit folderDropCopy( fti->folder() );
65  } else {
66  handleMailListDrop( e, fti->folder() );
67  }
68  e->accept( true );
69  } else {
70  KFolderTree::contentsDropEvent( e );
71  }
72 }
73 
74 int FolderTreeBase::dndMode(bool alwaysAsk)
75 {
76  int action = -1;
77  int keybstate = kapp->keyboardModifiers();
78  if ( keybstate & TDEApplication::ControlModifier ) {
79  action = DRAG_COPY;
80  } else if ( keybstate & TDEApplication::ShiftModifier ) {
81  action = DRAG_MOVE;
82  } else {
83  if ( GlobalSettings::self()->showPopupAfterDnD() || alwaysAsk ) {
84  TDEPopupMenu menu;
85  menu.insertItem( i18n("&Move Here"), DRAG_MOVE, 0 );
86  menu.insertItem( SmallIcon("edit-copy"), i18n("&Copy Here"), DRAG_COPY, 1 );
87  menu.insertSeparator();
88  menu.insertItem( SmallIcon("cancel"), i18n("C&ancel"), DRAG_CANCEL, 3 );
89  action = menu.exec( TQCursor::pos(), 0 );
90  }
91  else
92  action = DRAG_MOVE;
93  }
94  return action;
95 }
96 
97 bool FolderTreeBase::event(TQEvent * e)
98 {
99  if (e->type() == TQEvent::ApplicationPaletteChange) {
100  readColorConfig();
101  return true;
102  }
103  return KFolderTree::event(e);
104 }
105 
106 void FolderTreeBase::readColorConfig()
107 {
108  TDEConfig* conf = KMKernel::config();
109  // Custom/System color support
110  TDEConfigGroupSaver saver(conf, "Reader");
111  TQColor c1=TQColor(kapp->palette().active().text());
112  TQColor c2=TQColor("blue");
113  TQColor c4=TQColor(kapp->palette().active().base());
114  TQColor c5=TQColor("red");
115 
116  if (!conf->readBoolEntry("defaultColors",true)) {
117  mPaintInfo.colFore = conf->readColorEntry("ForegroundColor",&c1);
118  mPaintInfo.colUnread = conf->readColorEntry("UnreadMessage",&c2);
119  mPaintInfo.colBack = conf->readColorEntry("BackgroundColor",&c4);
120  mPaintInfo.colCloseToQuota = conf->readColorEntry("CloseToQuotaColor",&c5);
121  }
122  else {
123  mPaintInfo.colFore = c1;
124  mPaintInfo.colUnread = c2;
125  mPaintInfo.colBack = c4;
126  mPaintInfo.colCloseToQuota = c5;
127  }
128  TQPalette newPal = kapp->palette();
129  newPal.setColor( TQColorGroup::Base, mPaintInfo.colBack );
130  newPal.setColor( TQColorGroup::Text, mPaintInfo.colFore );
131  setPalette( newPal );
132 }
133 
134 bool FolderTreeBase::hideLocalInbox() const
135 {
136  if ( !GlobalSettings::self()->hideLocalInbox() )
137  return false;
138  KMFolder *localInbox = kmkernel->inboxFolder();
139  assert( localInbox );
140  // check if it is empty
141  localInbox->open( "foldertreebase" );
142  if ( localInbox->count() > 0 ) {
143  localInbox->close( "foldertreebase" );
144  return false;
145  }
146  localInbox->close( "foldertreebase" );
147  // check if it has subfolders
148  if ( localInbox->child() && !localInbox->child()->isEmpty() )
149  return false;
150  // check if it is an account target
151  if ( localInbox->hasAccounts() )
152  return false;
153  return true;
154 }
155 
156 
157 void FolderTreeBase::slotUpdateCounts(KMFolder * folder, bool force /* = false*/)
158 {
159  // kdDebug(5006) << "KMFolderTree::slotUpdateCounts()" << endl;
160  TQListViewItem * current;
161  if (folder)
162  current = indexOfFolder(folder);
163  else
164  current = currentItem();
165 
166  KMFolderTreeItem* fti = static_cast<KMFolderTreeItem*>(current);
167 
168  // sanity check
169  if (!fti) return;
170  if (!fti->folder()) fti->setTotalCount(-1);
171 
172  // get the unread count
173  int count = 0;
174  if (folder && folder->noContent()) // always empty
175  count = -1;
176  else {
177  if ( fti->folder() )
178  count = fti->folder()->countUnread();
179  }
180 
181  // set it
182  bool repaint = false;
183  if (fti->unreadCount() != count) {
184  fti->adjustUnreadCount( count );
185  repaint = true;
186  }
187  if (isTotalActive() || force)
188  {
189  // get the total-count
190  if (fti->folder()->noContent())
191  count = -1;
192  else {
193  // get the cached count if the folder is not open
194  count = fti->folder()->count( !fti->folder()->isOpened() );
195  }
196  // set it
197  if ( count != fti->totalCount() ) {
198  fti->setTotalCount(count);
199  repaint = true;
200  }
201  }
202  if ( isSizeActive() || force ) {
203  if ( !fti->folder()->noContent()) {
204  TQ_INT64 size = folder->storage()->folderSize();
205  if ( size != fti->folderSize() ) {
206  fti->setFolderSize( size );
207  repaint = true;
208  }
209  }
210  }
211  if ( fti->folderIsCloseToQuota() != folder->storage()->isCloseToQuota() ) {
212  fti->setFolderIsCloseToQuota( folder->storage()->isCloseToQuota() );
213  }
214 
215  if (fti->parent() && !fti->parent()->isOpen())
216  repaint = false; // we're not visible
217  if (repaint) {
218  fti->setNeedsRepaint( true );
219  emit triggerRefresh();
220  }
221  // tell the kernel that one of the counts has changed
222  kmkernel->messageCountChanged();
223 }
224 
225 void FolderTreeBase::handleMailListDrop(TQDropEvent * event, KMFolder *destination )
226 {
227  MailList list;
228  if ( !MailListDrag::decode( event, list ) ) {
229  kdWarning() << k_funcinfo << "Could not decode drag data!" << endl;
230  } else {
231  TQValueList<TQ_UINT32> serNums = MessageCopyHelper::serNumListFromMailList( list );
232  int action;
233  if ( MessageCopyHelper::inReadOnlyFolder( serNums ) )
234  action = DRAG_COPY;
235  else
236  action = dndMode();
237  if ( action == DRAG_COPY || action == DRAG_MOVE ) {
238  new MessageCopyHelper( serNums, destination, action == DRAG_MOVE, this );
239  }
240  }
241 }
242 
243 #include "foldertreebase.moc"
virtual bool isCloseToQuota() const
Return whether the folder is close to its quota limit, which can be reflected in the UI.
TQ_INT64 folderSize() const
Total size of the contents of this folder.
Mail folder.
Definition: kmfolder.h:69
KMFolderDir * child() const
Returns the folder directory associated with this node or 0 if no such directory exists.
Definition: kmfolder.h:157
void close(const char *owner, bool force=false)
Close folder.
Definition: kmfolder.cpp:489
int count(bool cache=false) const
Number of messages in this folder.
Definition: kmfolder.cpp:445
int open(const char *owner)
Open folder for access.
Definition: kmfolder.cpp:479
bool noContent() const
Returns, if the folder can't contain mails, but only subfolder.
Definition: kmfolder.cpp:301
bool hasAccounts() const
Returns TRUE if accounts are associated with this folder.
Definition: kmfolder.h:128
Helper class to copy/move a set of messages defined by their serial numbers from arbitrary folders in...
folderdiaquotatab.h
Definition: aboutdata.cpp:40