kmail/summarywidget.cpp
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 
3  This file is part of Kontact.
4  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqlabel.h>
26 #include <tqlayout.h>
27 
28 #include <dcopref.h>
29 #include <tdeapplication.h>
30 #include <tdeconfig.h>
31 #include <kdebug.h>
32 #include <kdialog.h>
33 #include <tdeglobal.h>
34 #include <kiconloader.h>
35 #include <tdelocale.h>
36 #include <tdeparts/part.h>
37 
38 #include "core.h"
39 #include "summary.h"
40 #include "summarywidget.h"
41 
42 #include <time.h>
43 
44 SummaryWidget::SummaryWidget( Kontact::Plugin *plugin, TQWidget *parent, const char *name )
45  : Kontact::Summary( parent, name ),
46  DCOPObject( TQCString("MailSummary") ),
47  mPlugin( plugin )
48 {
49  TQVBoxLayout *mainLayout = new TQVBoxLayout( this, 3, 3 );
50 
51  TQPixmap icon = TDEGlobal::iconLoader()->loadIcon( "kontact_mail", TDEIcon::Desktop,
52  TDEIcon::SizeMedium );
53  TQWidget *header = createHeader(this, icon, i18n("E-Mail"));
54  mLayout = new TQGridLayout( 1, 3, 3 );
55 
56  mainLayout->addWidget(header);
57  mainLayout->addLayout(mLayout);
58 
59  slotUnreadCountChanged();
60  connectDCOPSignal( 0, 0, "unreadCountChanged()", "slotUnreadCountChanged()",
61  false );
62 }
63 
64 void SummaryWidget::selectFolder( const TQString& folder )
65 {
66  if ( mPlugin->isRunningStandalone() )
67  mPlugin->bringToForeground();
68  else
69  mPlugin->core()->selectPlugin( mPlugin );
70  TQByteArray data;
71  TQDataStream arg( data, IO_WriteOnly );
72  arg << folder;
73  emitDCOPSignal( "kmailSelectFolder(TQString)", data );
74 }
75 
76 void SummaryWidget::updateSummary( bool )
77 {
78  // check whether we need to update the message counts
79  DCOPRef kmail( "kmail", "KMailIface" );
80  const int timeOfLastMessageCountChange =
81  kmail.call( "timeOfLastMessageCountChange()" );
82  if ( timeOfLastMessageCountChange > mTimeOfLastMessageCountUpdate )
83  slotUnreadCountChanged();
84 }
85 
86 void SummaryWidget::slotUnreadCountChanged()
87 {
88  DCOPRef kmail( "kmail", "KMailIface" );
89  DCOPReply reply = kmail.call( "folderList" );
90  if ( reply.isValid() ) {
91  TQStringList folderList = reply;
92  updateFolderList( folderList );
93  }
94  else {
95  kdDebug(5602) << "Calling kmail->KMailIface->folderList() via DCOP failed."
96  << endl;
97  }
98  mTimeOfLastMessageCountUpdate = ::time( 0 );
99 }
100 
101 void SummaryWidget::updateFolderList( const TQStringList& folders )
102 {
103  mLabels.setAutoDelete( true );
104  mLabels.clear();
105  mLabels.setAutoDelete( false );
106 
107  TDEConfig config( "kcmkmailsummaryrc" );
108  config.setGroup( "General" );
109 
110  TQStringList activeFolders;
111  if ( !config.hasKey( "ActiveFolders" ) )
112  activeFolders << "/Local/inbox";
113  else
114  activeFolders = config.readListEntry( "ActiveFolders" );
115 
116  int counter = 0;
117  TQStringList::ConstIterator it;
118  DCOPRef kmail( "kmail", "KMailIface" );
119  for ( it = folders.begin(); it != folders.end(); ++it ) {
120  if ( activeFolders.contains( *it ) ) {
121  DCOPRef folderRef = kmail.call( "getFolder(TQString)", *it );
122  const int numMsg = folderRef.call( "messages()" );
123  const int numUnreadMsg = folderRef.call( "unreadMessages()" );
124 
125  if ( numUnreadMsg == 0 ) continue;
126 
127  TQString folderPath;
128  if ( config.readBoolEntry( "ShowFullPath", true ) )
129  folderRef.call( "displayPath()" ).get( folderPath );
130  else
131  folderRef.call( "displayName()" ).get( folderPath );
132 
133  KURLLabel *urlLabel = new KURLLabel( *it, folderPath, this );
134  urlLabel->installEventFilter( this );
135  urlLabel->setAlignment( AlignLeft );
136  urlLabel->show();
137  connect( urlLabel, TQT_SIGNAL( leftClickedURL( const TQString& ) ),
138  TQT_SLOT( selectFolder( const TQString& ) ) );
139  mLayout->addWidget( urlLabel, counter, 0 );
140  mLabels.append( urlLabel );
141 
142  TQLabel *label =
143  new TQLabel( TQString( i18n("%1: number of unread messages "
144  "%2: total number of messages", "%1 / %2") )
145  .arg( numUnreadMsg ).arg( numMsg ), this );
146  label->setAlignment( AlignLeft );
147  label->show();
148  mLayout->addWidget( label, counter, 2 );
149  mLabels.append( label );
150 
151  counter++;
152  }
153  }
154 
155  if ( counter == 0 ) {
156  TQLabel *label = new TQLabel( i18n( "No unread messages in your monitored folders" ), this );
157  label->setAlignment( AlignHCenter | AlignVCenter );
158  mLayout->addMultiCellWidget( label, 0, 0, 0, 2 );
159  label->show();
160  mLabels.append( label );
161  }
162 }
163 
164 bool SummaryWidget::eventFilter( TQObject *obj, TQEvent* e )
165 {
166  if ( obj->inherits( "KURLLabel" ) ) {
167  KURLLabel* label = static_cast<KURLLabel*>( TQT_TQWIDGET(obj) );
168  if ( e->type() == TQEvent::Enter )
169  emit message( i18n( "Open Folder: \"%1\"" ).arg( label->text() ) );
170  if ( e->type() == TQEvent::Leave )
171  emit message( TQString() );
172  }
173 
174  return Kontact::Summary::eventFilter( obj, e );
175 }
176 
177 TQStringList SummaryWidget::configModules() const
178 {
179  return TQStringList( "kcmkmailsummary.desktop" );
180 }
181 
182 #include "summarywidget.moc"
TQWidget * createHeader(TQWidget *parent, const TQPixmap &icon, const TQString &heading)
Creates a heading for a typical summary view with an icon and a heading.
Definition: summary.cpp:47
Definition: core.h:33
Base class for all Plugins in Kontact.
Definition: plugin.h:58