weather/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#include <tqimage.h>
24#include <tqlabel.h>
25#include <tqlayout.h>
26#include <tqtooltip.h>
27
28#include <dcopclient.h>
29#include <dcopref.h>
30#include <tdeapplication.h>
31#include <kdebug.h>
32#include <tdeglobal.h>
33#include <tdeglobalsettings.h>
34#include <kiconloader.h>
35#include <tdelocale.h>
36#include <tdeprocess.h>
37#include <kurllabel.h>
38
39#include "summarywidget.h"
40
41SummaryWidget::SummaryWidget( TQWidget *parent, const char *name )
42 : Kontact::Summary( parent, name ),
43 DCOPObject( "WeatherSummaryWidget" ), mProc( 0 )
44{
45 mLayout = new TQVBoxLayout( this, 3, 3 );
46 mLayout->setAlignment( TQt::AlignTop );
47
48 TQPixmap icon = TDEGlobal::iconLoader()->loadIcon( "kweather", TDEIcon::Desktop, TDEIcon::SizeMedium );
49 TQWidget *header = createHeader( this, icon, i18n( "Weather Service" ) );
50 mLayout->addWidget( header );
51
52 TQString error;
53 TQCString appID;
54 bool serviceAvailable = true;
55 if ( !tdeApp->dcopClient()->isApplicationRegistered( "KWeatherService" ) ) {
56 if ( TDEApplication::startServiceByDesktopName( "kweatherservice", TQStringList(), &error, &appID ) ) {
57 TQLabel *label = new TQLabel( i18n( "No weather dcop service available;\nyou need KWeather to use this plugin." ), this );
58 mLayout->addWidget( label, TQt::AlignHCenter | AlignVCenter );
59 serviceAvailable = false;
60 }
61 }
62
63 if ( serviceAvailable ) {
64 connectDCOPSignal( 0, 0, "fileUpdate(TQString)", "refresh(TQString)", false );
65 connectDCOPSignal( 0, 0, "stationRemoved(TQString)", "stationRemoved(TQString)", false );
66
67 DCOPRef dcopCall( "KWeatherService", "WeatherService" );
68 DCOPReply reply = dcopCall.call( "listStations()", true );
69 if ( reply.isValid() ) {
70 mStations = reply;
71
72 connect( &mTimer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( timeout() ) );
73 mTimer.start( 0 );
74 } else {
75 kdDebug(5602) << "ERROR: dcop reply not valid..." << endl;
76 }
77 }
78}
79
80
81void SummaryWidget::updateView()
82{
83 mLayouts.setAutoDelete( true );
84 mLayouts.clear();
85 mLayouts.setAutoDelete( false );
86
87 mLabels.setAutoDelete( true );
88 mLabels.clear();
89 mLabels.setAutoDelete( false );
90
91 if ( mStations.count() == 0 ) {
92 kdDebug(5602) << "No weather stations defined..." << endl;
93 return;
94 }
95
96
97 TQValueList<WeatherData> dataList = mWeatherMap.values();
98 qHeapSort( dataList );
99
100 TQValueList<WeatherData>::Iterator it;
101 for ( it = dataList.begin(); it != dataList.end(); ++it ) {
102 TQString cover;
103 for ( uint i = 0; i < (*it).cover().count(); ++i )
104 cover += TQString( "- %1\n" ).arg( (*it).cover()[ i ] );
105
106 TQImage img;
107 img = (*it).icon();
108
109 TQGridLayout *layout = new TQGridLayout( mLayout, 3, 3, 3 );
110 mLayouts.append( layout );
111
112 KURLLabel* urlLabel = new KURLLabel( this );
113 urlLabel->installEventFilter( this );
114 urlLabel->setURL( (*it).stationID() );
115 urlLabel->setPixmap( img.smoothScale( 32, 32 ) );
116 urlLabel->setMaximumSize( urlLabel->sizeHint() );
117 urlLabel->setAlignment( AlignTop );
118 layout->addMultiCellWidget( urlLabel, 0, 1, 0, 0 );
119 mLabels.append( urlLabel );
120 connect ( urlLabel, TQ_SIGNAL( leftClickedURL( const TQString& ) ),
121 this, TQ_SLOT( showReport( const TQString& ) ) );
122
123 TQLabel* label = new TQLabel( this );
124 label->setText( TQString( "%1 (%2)" ).arg( (*it).name() ).arg( (*it).temperature() ) );
125 TQFont font = label->font();
126 font.setBold( true );
127 label->setFont( font );
128 label->setAlignment( AlignLeft );
129 layout->addMultiCellWidget( label, 0, 0, 1, 2 );
130 mLabels.append( label );
131
132 TQString labelText;
133 labelText = TQString( "<b>%1:</b> %2<br>"
134 "<b>%3:</b> %4<br>"
135 "<b>%5:</b> %6" )
136 .arg( i18n( "Last updated on" ) )
137 .arg( (*it).date() )
138 .arg( i18n( "Wind Speed" ) )
139 .arg( (*it).windSpeed() )
140 .arg( i18n( "Rel. Humidity" ) )
141 .arg( (*it).relativeHumidity() );
142
143 TQToolTip::add( label, labelText.replace( " ", "&nbsp;" ) );
144
145 label = new TQLabel( cover, this );
146 label->setAlignment( AlignLeft );
147 layout->addMultiCellWidget( label, 1, 1, 1, 2 );
148 mLabels.append( label );
149 }
150
151 for ( TQLabel *label = mLabels.first(); label; label = mLabels.next() )
152 label->show();
153}
154
155void SummaryWidget::timeout()
156{
157 mTimer.stop();
158
159 DCOPRef dcopCall( "KWeatherService", "WeatherService" );
160 dcopCall.send( "updateAll()" );
161
162 mTimer.start( 15 * 60000 );
163}
164
165void SummaryWidget::refresh( TQString station )
166{
167 DCOPRef dcopCall( "KWeatherService", "WeatherService" );
168
169 mWeatherMap[ station ].setIcon( dcopCall.call( "currentIcon(TQString)", station, true ) );
170 mWeatherMap[ station ].setName( dcopCall.call( "stationName(TQString)", station, true ) );
171 mWeatherMap[ station ].setCover( dcopCall.call( "cover(TQString)", station, true ) );
172 mWeatherMap[ station ].setDate( dcopCall.call( "date(TQString)", station, true ) );
173 mWeatherMap[ station ].setTemperature( dcopCall.call( "temperature(TQString)", station, true ) );
174 mWeatherMap[ station ].setWindSpeed( dcopCall.call( "wind(TQString)", station, true ) );
175 mWeatherMap[ station ].setRelativeHumidity( dcopCall.call( "relativeHumidity(TQString)", station, true ) );
176 mWeatherMap[ station ].setStationID(station);
177
178 updateView();
179}
180
181void SummaryWidget::stationRemoved( TQString station )
182{
183 mWeatherMap.remove( station );
184 updateView();
185}
186
187bool SummaryWidget::eventFilter( TQObject *obj, TQEvent* e )
188{
189 if ( obj->inherits( "KURLLabel" ) ) {
190 if ( e->type() == TQEvent::Enter )
191 emit message(
192 i18n( "View Weather Report for Station" ) );
193 if ( e->type() == TQEvent::Leave )
194 emit message( TQString() );
195 }
196
197 return Kontact::Summary::eventFilter( obj, e );
198}
199
200TQStringList SummaryWidget::configModules() const
201{
202 return TQStringList( "kcmweatherservice.desktop" );
203}
204
205void SummaryWidget::updateSummary( bool )
206{
207 timeout();
208}
209
210void SummaryWidget::showReport( const TQString &stationID )
211{
212 mProc = new TDEProcess;
213 TQApplication::connect( mProc, TQ_SIGNAL( processExited( TDEProcess* ) ),
214 this, TQ_SLOT( reportFinished( TDEProcess* ) ) );
215 *mProc << "kweatherreport";
216 *mProc << stationID;
217
218 if ( !mProc->start() ) {
219 delete mProc;
220 mProc = 0;
221 }
222}
223
224void SummaryWidget::reportFinished( TDEProcess* )
225{
226 mProc->deleteLater();
227 mProc = 0;
228}
229
230#include "summarywidget.moc"