kontact

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 Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 #include <qimage.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qtooltip.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( QWidget *parent, const char *name )
00042   : Kontact::Summary( parent, name ),
00043     DCOPObject( "WeatherSummaryWidget" ), mProc( 0 )
00044 {
00045   mLayout = new QVBoxLayout( this, 3, 3 );
00046   mLayout->setAlignment( Qt::AlignTop );
00047 
00048   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kweather", KIcon::Desktop, KIcon::SizeMedium );
00049   QWidget *header = createHeader( this, icon, i18n( "Weather Information" ) );
00050   mLayout->addWidget( header );
00051 
00052   QString error;
00053   QCString appID;
00054   bool serviceAvailable = true;
00055   if ( !kapp->dcopClient()->isApplicationRegistered( "KWeatherService" ) ) {
00056     if ( KApplication::startServiceByDesktopName( "kweatherservice", QStringList(), &error, &appID ) ) {
00057       QLabel *label = new QLabel( i18n( "No weather dcop service available;\nyou need KWeather to use this plugin." ), this );
00058       mLayout->addWidget( label, Qt::AlignHCenter | AlignVCenter );
00059       serviceAvailable = false;
00060     }
00061   }
00062 
00063   if ( serviceAvailable ) {
00064     connectDCOPSignal( 0, 0, "fileUpdate(QString)", "refresh(QString)", false );
00065     connectDCOPSignal( 0, 0, "stationRemoved(QString)", "stationRemoved(QString)", 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, SIGNAL( timeout() ), this, 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   QValueList<WeatherData> dataList = mWeatherMap.values();
00098   qHeapSort( dataList );
00099 
00100   QValueList<WeatherData>::Iterator it;
00101   for ( it = dataList.begin(); it != dataList.end(); ++it ) {
00102     QString cover;
00103     for ( uint i = 0; i < (*it).cover().count(); ++i )
00104       cover += QString( "- %1\n" ).arg( (*it).cover()[ i ] );
00105 
00106     QImage img;
00107     img = (*it).icon();
00108 
00109     QGridLayout *layout = new QGridLayout( 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, SIGNAL( leftClickedURL( const QString& ) ),
00121               this, SLOT( showReport( const QString& ) ) );
00122 
00123     QLabel* label = new QLabel( this );
00124     label->setText( QString( "%1 (%2)" ).arg( (*it).name() ).arg( (*it).temperature() ) );
00125     QFont 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     QString labelText;
00133     labelText = QString( "<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     QToolTip::add( label, labelText.replace( " ", "&nbsp;" ) );
00144 
00145     label = new QLabel( cover, this );
00146     label->setAlignment( AlignLeft );
00147     layout->addMultiCellWidget( label, 1, 1, 1, 2 );
00148     mLabels.append( label );
00149   }
00150 
00151   for ( QLabel *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( QString station )
00166 {
00167   DCOPRef dcopCall( "KWeatherService", "WeatherService" );
00168 
00169   mWeatherMap[ station ].setIcon( dcopCall.call( "currentIcon(QString)", station, true ) );
00170   mWeatherMap[ station ].setName( dcopCall.call( "stationName(QString)", station, true ) );
00171   mWeatherMap[ station ].setCover( dcopCall.call( "cover(QString)", station, true ) );
00172   mWeatherMap[ station ].setDate( dcopCall.call( "date(QString)", station, true ) );
00173   mWeatherMap[ station ].setTemperature( dcopCall.call( "temperature(QString)", station, true ) );
00174   mWeatherMap[ station ].setWindSpeed( dcopCall.call( "wind(QString)", station, true ) );
00175   mWeatherMap[ station ].setRelativeHumidity( dcopCall.call( "relativeHumidity(QString)", station, true ) );
00176   mWeatherMap[ station ].setStationID(station);
00177 
00178   updateView();
00179 }
00180 
00181 void SummaryWidget::stationRemoved( QString station )
00182 {
00183   mWeatherMap.remove( station );
00184   updateView();
00185 }
00186 
00187 bool SummaryWidget::eventFilter( QObject *obj, QEvent* e )
00188 {
00189   if ( obj->inherits( "KURLLabel" ) ) {
00190     if ( e->type() == QEvent::Enter )
00191       emit message(
00192         i18n( "View Weather Report for Station" ) );
00193     if ( e->type() == QEvent::Leave )
00194       emit message( QString::null );
00195   }
00196 
00197   return Kontact::Summary::eventFilter( obj, e );
00198 }
00199 
00200 QStringList SummaryWidget::configModules() const
00201 {
00202   return QStringList( "kcmweatherservice.desktop" );
00203 }
00204 
00205 void SummaryWidget::updateSummary( bool )
00206 {
00207   timeout();
00208 }
00209 
00210 void SummaryWidget::showReport( const QString &stationID )
00211 {
00212   mProc = new KProcess;
00213   QApplication::connect( mProc, SIGNAL( processExited( KProcess* ) ),
00214                          this, 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"
KDE Home | KDE Accessibility Home | Description of Access Keys