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