00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 <kurllabel.h>
00037
00038
#include "summarywidget.h"
00039
00040 SummaryWidget::SummaryWidget(
QWidget *parent,
const char *name )
00041 : Kontact::
Summary( parent, name ),
00042 DCOPObject( "WeatherSummaryWidget" ), mProc( 0 )
00043 {
00044 mLayout =
new QVBoxLayout(
this );
00045 mLayout->setAlignment( Qt::AlignTop );
00046
00047
QPixmap icon = KGlobal::iconLoader()->loadIcon(
"kweather", KIcon::Desktop, KIcon::SizeMedium );
00048
QWidget *header =
createHeader(
this, icon, i18n(
"Weather Information" ) );
00049 mLayout->addWidget( header );
00050
00051
QString error;
00052
QCString appID;
00053
bool serviceAvailable =
true;
00054
if ( !kapp->dcopClient()->isApplicationRegistered(
"KWeatherService" ) ) {
00055
if ( KApplication::startServiceByDesktopName(
"kweatherservice",
QStringList(), &error, &appID ) ) {
00056
QLabel *label =
new QLabel( i18n(
"No weather dcop service available;\nyou need KWeather to use this plugin." ),
this );
00057 mLayout->addWidget( label, Qt::AlignHCenter );
00058 serviceAvailable =
false;
00059 }
00060 }
00061
00062
if ( serviceAvailable ) {
00063 connectDCOPSignal( 0, 0,
"fileUpdate(QString)",
"refresh(QString)",
false );
00064 connectDCOPSignal( 0, 0,
"stationRemoved(QString)",
"stationRemoved(QString)",
false );
00065
00066 DCOPRef dcopCall(
"KWeatherService",
"WeatherService" );
00067 DCOPReply reply = dcopCall.call(
"listStations()",
true );
00068
if ( reply.isValid() ) {
00069 mStations = reply;
00070
00071 connect( &mTimer, SIGNAL( timeout() ),
this, SLOT( timeout() ) );
00072 mTimer.start( 0 );
00073 }
else {
00074 kdDebug(5602) <<
"ERROR: dcop reply not valid..." << endl;
00075 }
00076 }
00077 }
00078
00079
00080
void SummaryWidget::updateView()
00081 {
00082 mLayouts.setAutoDelete(
true );
00083 mLayouts.clear();
00084 mLayouts.setAutoDelete(
false );
00085
00086 mLabels.setAutoDelete(
true );
00087 mLabels.clear();
00088 mLabels.setAutoDelete(
false );
00089
00090
if ( mStations.count() == 0 ) {
00091 kdDebug(5602) <<
"No weather stations defined..." << endl;
00092
return;
00093 }
00094
00095
00096
QValueList<WeatherData> dataList = mWeatherMap.values();
00097 qHeapSort( dataList );
00098
00099
QValueList<WeatherData>::Iterator it;
00100
for ( it = dataList.begin(); it != dataList.end(); ++it ) {
00101
QString cover;
00102
for ( uint i = 0; i < (*it).cover().count(); ++i )
00103 cover +=
QString(
"- %1\n" ).arg( (*it).cover()[ i ] );
00104
00105
QImage img;
00106 img = (*it).icon();
00107
00108
QGridLayout *layout =
new QGridLayout( mLayout, 3, 3, 3 );
00109 mLayouts.append( layout );
00110
00111 KURLLabel* urlLabel =
new KURLLabel(
this);
00112 urlLabel->installEventFilter(
this);
00113 urlLabel->setURL((*it).stationID());
00114 urlLabel->setPixmap( img.smoothScale( 32, 32 ) );
00115 urlLabel->setMaximumSize(urlLabel->sizeHint());
00116 urlLabel->setAlignment( AlignTop );
00117 layout->addMultiCellWidget( urlLabel, 0, 1, 0, 0 );
00118 mLabels.append( urlLabel );
00119 connect (urlLabel, SIGNAL(leftClickedURL(
const QString&) ),
00120
this, SLOT(slotShowReport(
const QString& )));
00121
00122
QLabel* label =
new QLabel(
this );
00123 label->setText( QString(
"%1 (%2)" ).arg( (*it).name() ).arg( (*it).temperature() ) );
00124
QFont font = label->font();
00125 font.setBold(
true );
00126 label->setFont( font );
00127 label->setAlignment( AlignLeft );
00128 layout->addMultiCellWidget( label, 0, 0, 1, 2 );
00129 mLabels.append( label );
00130
00131 QString labelText;
00132 labelText = QString(
"<b>%1:</b> %2<br>"
00133
"<b>%3:</b> %4" )
00134 .arg( i18n(
"Wind Speed" ) )
00135 .arg( (*it).windSpeed() )
00136 .arg( i18n(
"Rel. Humidity" ) )
00137 .arg( (*it).relativeHumidity() );
00138
00139 QToolTip::add( label, labelText.replace(
" ",
" " ) );
00140
00141 label =
new QLabel( cover,
this );
00142 label->setAlignment( AlignLeft );
00143 layout->addMultiCellWidget( label, 1, 1, 1, 2 );
00144 mLabels.append( label );
00145 }
00146
00147
for ( QLabel *label = mLabels.first(); label; label = mLabels.next() )
00148 label->show();
00149
00150 mLayout->addStretch( 1 );
00151 }
00152
00153
void SummaryWidget::timeout()
00154 {
00155 mTimer.stop();
00156
00157 DCOPRef dcopCall(
"KWeatherService",
"WeatherService" );
00158 dcopCall.send(
"updateAll()" );
00159
00160 mTimer.start( 15 * 60000 );
00161 }
00162
00163
void SummaryWidget::refresh( QString station )
00164 {
00165 DCOPRef dcopCall(
"KWeatherService",
"WeatherService" );
00166
00167 mWeatherMap[ station ].setIcon( dcopCall.call(
"currentIcon(QString)", station,
true ) );
00168 mWeatherMap[ station ].setName( dcopCall.call(
"stationName(QString)", station,
true ) );
00169 mWeatherMap[ station ].setCover( dcopCall.call(
"cover(QString)", station,
true ) );
00170 mWeatherMap[ station ].setTemperature( dcopCall.call(
"temperature(QString)", station,
true ) );
00171 mWeatherMap[ station ].setWindSpeed( dcopCall.call(
"wind(QString)", station,
true ) );
00172 mWeatherMap[ station ].setRelativeHumidity( dcopCall.call(
"relativeHumidity(QString)", station,
true ) );
00173 mWeatherMap[ station ].setStationID(station);
00174
00175 updateView();
00176 }
00177
00178
void SummaryWidget::stationRemoved( QString station )
00179 {
00180 mWeatherMap.remove( station );
00181 updateView();
00182 }
00183
00184
QStringList SummaryWidget::configModules()
const
00185
{
00186
return QStringList(
"kcmweatherservice.desktop" );
00187 }
00188
00189
void SummaryWidget::slotShowReport(
const QString &stationID)
00190 {
00191 mProc =
new KProcess;
00192 QApplication::connect(mProc, SIGNAL(processExited(KProcess *)),
00193
this, SLOT(slotReportFinished(KProcess* )));
00194 *mProc <<
"kweatherreport";
00195 *mProc << stationID;
00196
if ( !mProc->start() )
00197 {
00198
delete mProc;
00199 mProc=0;
00200 }
00201 }
00202
00203
void SummaryWidget::slotReportFinished(KProcess* ){
00204
delete mProc;
00205 mProc = 0;
00206 }
00207
00208
#include "summarywidget.moc"