summarywidget.cpp
00001 /* 00002 This file is part of Kontact. 00003 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 00019 As a special exception, permission is given to link this program 00020 with any edition of TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 #include <tqimage.h> 00024 #include <tqlabel.h> 00025 #include <tqlayout.h> 00026 #include <tqtooltip.h> 00027 00028 #include <dcopclient.h> 00029 #include <dcopref.h> 00030 #include <kapplication.h> 00031 #include <kdebug.h> 00032 #include <kglobal.h> 00033 #include <kglobalsettings.h> 00034 #include <kiconloader.h> 00035 #include <klocale.h> 00036 #include <kprocess.h> 00037 #include <kurllabel.h> 00038 00039 #include "summarywidget.h" 00040 00041 SummaryWidget::SummaryWidget( TQWidget *parent, const char *name ) 00042 : Kontact::Summary( parent, name ), 00043 DCOPObject( "WeatherSummaryWidget" ), mProc( 0 ) 00044 { 00045 mLayout = new TQVBoxLayout( this, 3, 3 ); 00046 mLayout->setAlignment( TQt::AlignTop ); 00047 00048 TQPixmap icon = KGlobal::iconLoader()->loadIcon( "kweather", KIcon::Desktop, KIcon::SizeMedium ); 00049 TQWidget *header = createHeader( this, icon, i18n( "Weather Service" ) ); 00050 mLayout->addWidget( header ); 00051 00052 TQString error; 00053 TQCString appID; 00054 bool serviceAvailable = true; 00055 if ( !kapp->dcopClient()->isApplicationRegistered( "KWeatherService" ) ) { 00056 if ( KApplication::startServiceByDesktopName( "kweatherservice", TQStringList(), &error, &appID ) ) { 00057 TQLabel *label = new TQLabel( i18n( "No weather dcop service available;\nyou need KWeather to use this plugin." ), this ); 00058 mLayout->addWidget( label, TQt::AlignHCenter | AlignVCenter ); 00059 serviceAvailable = false; 00060 } 00061 } 00062 00063 if ( serviceAvailable ) { 00064 connectDCOPSignal( 0, 0, "fileUpdate(TQString)", "refresh(TQString)", false ); 00065 connectDCOPSignal( 0, 0, "stationRemoved(TQString)", "stationRemoved(TQString)", false ); 00066 00067 DCOPRef dcopCall( "KWeatherService", "WeatherService" ); 00068 DCOPReply reply = dcopCall.call( "listStations()", true ); 00069 if ( reply.isValid() ) { 00070 mStations = reply; 00071 00072 connect( &mTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( timeout() ) ); 00073 mTimer.start( 0 ); 00074 } else { 00075 kdDebug(5602) << "ERROR: dcop reply not valid..." << endl; 00076 } 00077 } 00078 } 00079 00080 00081 void SummaryWidget::updateView() 00082 { 00083 mLayouts.setAutoDelete( true ); 00084 mLayouts.clear(); 00085 mLayouts.setAutoDelete( false ); 00086 00087 mLabels.setAutoDelete( true ); 00088 mLabels.clear(); 00089 mLabels.setAutoDelete( false ); 00090 00091 if ( mStations.count() == 0 ) { 00092 kdDebug(5602) << "No weather stations defined..." << endl; 00093 return; 00094 } 00095 00096 00097 TQValueList<WeatherData> dataList = mWeatherMap.values(); 00098 qHeapSort( dataList ); 00099 00100 TQValueList<WeatherData>::Iterator it; 00101 for ( it = dataList.begin(); it != dataList.end(); ++it ) { 00102 TQString cover; 00103 for ( uint i = 0; i < (*it).cover().count(); ++i ) 00104 cover += TQString( "- %1\n" ).arg( (*it).cover()[ i ] ); 00105 00106 TQImage img; 00107 img = (*it).icon(); 00108 00109 TQGridLayout *layout = new TQGridLayout( mLayout, 3, 3, 3 ); 00110 mLayouts.append( layout ); 00111 00112 KURLLabel* urlLabel = new KURLLabel( this ); 00113 urlLabel->installEventFilter( this ); 00114 urlLabel->setURL( (*it).stationID() ); 00115 urlLabel->setPixmap( img.smoothScale( 32, 32 ) ); 00116 urlLabel->setMaximumSize( urlLabel->sizeHint() ); 00117 urlLabel->setAlignment( AlignTop ); 00118 layout->addMultiCellWidget( urlLabel, 0, 1, 0, 0 ); 00119 mLabels.append( urlLabel ); 00120 connect ( urlLabel, TQT_SIGNAL( leftClickedURL( const TQString& ) ), 00121 this, TQT_SLOT( showReport( const TQString& ) ) ); 00122 00123 TQLabel* label = new TQLabel( this ); 00124 label->setText( TQString( "%1 (%2)" ).arg( (*it).name() ).arg( (*it).temperature() ) ); 00125 TQFont font = label->font(); 00126 font.setBold( true ); 00127 label->setFont( font ); 00128 label->setAlignment( AlignLeft ); 00129 layout->addMultiCellWidget( label, 0, 0, 1, 2 ); 00130 mLabels.append( label ); 00131 00132 TQString labelText; 00133 labelText = TQString( "<b>%1:</b> %2<br>" 00134 "<b>%3:</b> %4<br>" 00135 "<b>%5:</b> %6" ) 00136 .arg( i18n( "Last updated on" ) ) 00137 .arg( (*it).date() ) 00138 .arg( i18n( "Wind Speed" ) ) 00139 .arg( (*it).windSpeed() ) 00140 .arg( i18n( "Rel. Humidity" ) ) 00141 .arg( (*it).relativeHumidity() ); 00142 00143 TQToolTip::add( label, labelText.replace( " ", " " ) ); 00144 00145 label = new TQLabel( cover, this ); 00146 label->setAlignment( AlignLeft ); 00147 layout->addMultiCellWidget( label, 1, 1, 1, 2 ); 00148 mLabels.append( label ); 00149 } 00150 00151 for ( TQLabel *label = mLabels.first(); label; label = mLabels.next() ) 00152 label->show(); 00153 } 00154 00155 void SummaryWidget::timeout() 00156 { 00157 mTimer.stop(); 00158 00159 DCOPRef dcopCall( "KWeatherService", "WeatherService" ); 00160 dcopCall.send( "updateAll()" ); 00161 00162 mTimer.start( 15 * 60000 ); 00163 } 00164 00165 void SummaryWidget::refresh( TQString station ) 00166 { 00167 DCOPRef dcopCall( "KWeatherService", "WeatherService" ); 00168 00169 mWeatherMap[ station ].setIcon( dcopCall.call( "currentIcon(TQString)", station, true ) ); 00170 mWeatherMap[ station ].setName( dcopCall.call( "stationName(TQString)", station, true ) ); 00171 mWeatherMap[ station ].setCover( dcopCall.call( "cover(TQString)", station, true ) ); 00172 mWeatherMap[ station ].setDate( dcopCall.call( "date(TQString)", station, true ) ); 00173 mWeatherMap[ station ].setTemperature( dcopCall.call( "temperature(TQString)", station, true ) ); 00174 mWeatherMap[ station ].setWindSpeed( dcopCall.call( "wind(TQString)", station, true ) ); 00175 mWeatherMap[ station ].setRelativeHumidity( dcopCall.call( "relativeHumidity(TQString)", station, true ) ); 00176 mWeatherMap[ station ].setStationID(station); 00177 00178 updateView(); 00179 } 00180 00181 void SummaryWidget::stationRemoved( TQString station ) 00182 { 00183 mWeatherMap.remove( station ); 00184 updateView(); 00185 } 00186 00187 bool SummaryWidget::eventFilter( TQObject *obj, TQEvent* e ) 00188 { 00189 if ( obj->inherits( "KURLLabel" ) ) { 00190 if ( e->type() == TQEvent::Enter ) 00191 emit message( 00192 i18n( "View Weather Report for Station" ) ); 00193 if ( e->type() == TQEvent::Leave ) 00194 emit message( TQString() ); 00195 } 00196 00197 return Kontact::Summary::eventFilter( obj, e ); 00198 } 00199 00200 TQStringList SummaryWidget::configModules() const 00201 { 00202 return TQStringList( "kcmweatherservice.desktop" ); 00203 } 00204 00205 void SummaryWidget::updateSummary( bool ) 00206 { 00207 timeout(); 00208 } 00209 00210 void SummaryWidget::showReport( const TQString &stationID ) 00211 { 00212 mProc = new KProcess; 00213 TQApplication::connect( mProc, TQT_SIGNAL( processExited( KProcess* ) ), 00214 this, TQT_SLOT( reportFinished( KProcess* ) ) ); 00215 *mProc << "kweatherreport"; 00216 *mProc << stationID; 00217 00218 if ( !mProc->start() ) { 00219 delete mProc; 00220 mProc = 0; 00221 } 00222 } 00223 00224 void SummaryWidget::reportFinished( KProcess* ) 00225 { 00226 mProc->deleteLater(); 00227 mProc = 0; 00228 } 00229 00230 #include "summarywidget.moc"