summaryrefslogtreecommitdiffstats
path: root/kweather/weatherbar.cpp
blob: 3570c1be9b55f0f067aed54cf70b4fc1c240db94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 *  This file is part of the KDE project
 *  Copyright (C) 2002 Ian Reinhart Geiser <geiseri@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2.0 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 *  $Id$
 */

#include "weatherbar.h"
#include "dockwidget.h"
#include "sidebarwidget.h"

#include <tdeapplication.h>
#include <tqlabel.h>
#include <tqfont.h>
#include <tqlayout.h>
#include <tqscrollview.h>
#include <tqgroupbox.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <kstandarddirs.h>
#include <tdeglobalsettings.h>
#include <tdemessagebox.h>
#include <dcopclient.h>
#include <dcopref.h>


KonqSidebarWeather::KonqSidebarWeather(TDEInstance* inst, TQObject* parent,
                                         TQWidget* widgetParent,
                                         TQString& desktopName, const char* name)

    : KonqSidebarPlugin(inst, parent, widgetParent, desktopName, name),
    DCOPObject(name)
{

	m_container = new sidebarwidget(widgetParent,"sidebarwidget");

	kdDebug() << "Calling Get Widget" << endl;

	kdDebug() << "Get weatherstation list... " << endl;

	if (!(
		connectDCOPSignal(0, 0, "fileUpdate(TQString)",     "refresh(TQString)", false) &&
		connectDCOPSignal(0, 0, "stationAdded(TQString)",   "updateWidgets()",   false) &&
		connectDCOPSignal(0, 0, "stationRemoved(TQString)", "updateWidgets()",   false)
	))
		kdDebug() << "Could not attach signal..." << endl;
	else
		kdDebug() << "attached dcop signals..." << endl;

	timeOut = new TQTimer(this, "timeOut" );
	connect(timeOut, TQ_SIGNAL(timeout()), this, TQ_SLOT(updateWidgets()));

	updateWidgets();
}


KonqSidebarWeather::~KonqSidebarWeather()
{
}

void* KonqSidebarWeather::provides(const TQString&)
{
    return 0;
}

void KonqSidebarWeather::emitStatusBarText(const TQString& s)
{
}

TQWidget* KonqSidebarWeather::getWidget()
{
	return m_container;
}

void KonqSidebarWeather::refresh(TQString stationID)
{
	kdDebug() << "refresh " << stationID << endl;
	if(m_widgets.find(stationID))
	{
		/* These updates are seemingly now handled by dockwidget itself
		DCOPRef dcopCall( "KWeatherService", "WeatherService" );
		m_widgets[stationID]->setWeatherIcon(dcopCall.call("currentIcon(TQString)", stationID ,true ));
		m_widgets[stationID]->setTemperature(dcopCall.call("temperature(TQString)", stationID,true ));
		m_widgets[stationID]->setPressure(dcopCall.call("pressure(TQString)", stationID,true ));
		m_widgets[stationID]->setWind(dcopCall.call("wind(TQString)", stationID,true ));
		*/

		m_widgets[stationID]->showWeather();
	}
}

void KonqSidebarWeather::handleURL(const KURL& url)
{
}

void KonqSidebarWeather::handlePreview(const KFileItemList& items)
{
}

void KonqSidebarWeather::handlePreviewOnMouseOver(const KFileItem& item)
{
}

void KonqSidebarWeather::updateWidgets()
{
	kdDebug() << "updating station widgets" << endl;

	timeOut->stop();

	DCOPRef dcopCall( "KWeatherService", "WeatherService" );
	DCOPReply reply = dcopCall.call("listStations()", true );

	// KWeatherService might not be active at this point
	if( !reply.isValid() ) {
		kdDebug() << "Starting KWeatherService" << endl;
		if( !startWeatherService() ) {
			KMessageBox::sorry(0, i18n("Could not start the weather service!"));
			return;
		}

		reply = dcopCall.call("listStations()", true );
	}

	if ( reply.isValid() ) {
		TQStringList replyList = reply;

		// Check for new cities
		for(int i = 0; i < replyList.size(); i++)
		{
			if(!m_widgets.find(replyList[i]))
			{
				TQLabel *city = new TQLabel(
					dcopCall.call("stationName(TQString)", replyList[i], true),
					m_container->viewport()
				);
				city->setPaletteBackgroundColor(TDEGlobalSettings::highlightColor());
				city->setPaletteForegroundColor(TDEGlobalSettings::highlightedTextColor());
				dockwidget *d = new dockwidget(replyList[i], m_container->viewport());
				d->resizeView(TQSize(d->width(), 48));
				city->show();
				d->show();
				m_labels.insert(replyList[i], city);
				m_widgets.insert(replyList[i], d);
			}
			dcopCall.send("update(TQString)", replyList[i]);
		}

		// Check for removed cities
		TQDictIterator<dockwidget> it(m_widgets);
		for(; it.current(); ++it)
		{
			TQString current(it.currentKey());
			if(!replyList.contains(current))
			{
				m_widgets[current]->~dockwidget();
				m_labels[current]->~TQLabel();
				m_widgets.remove(current);
				m_labels.remove(current);
			}
		}
	} else {
		KMessageBox::sorry(0, i18n("The weather service is unreachable!"));
	}

	timeOut->start(15*60000);
}

bool KonqSidebarWeather::startWeatherService()
{
	TQByteArray data, replyData;
	TQCString replyType;
	TQDataStream arg(data, IO_WriteOnly);
	arg << TQString("KWeatherService") << TQStringList();

	if ( !kapp->dcopClient()->call(
			"tdelauncher", "tdelauncher",
			"start_service_by_name(TQString,TQStringList)",
			data, replyType, replyData) ) {
		kdDebug() << "Cannot start weather service: tdelauncher call failed." << endl;
		return false;
	}

	TQDataStream reply(replyData, IO_ReadOnly);
	if ( replyType != "serviceResult" )
	{
		kdDebug() << "Cannot start weather service: unknown reply type by tdelauncher." << endl;
		return false;
	}

	int result;
	TQCString dcopName;
	TQString error;
	reply >> result >> dcopName >> error;
	if (result != 0)
	{
		kdDebug() << "Cannot start weather service: " << error.local8Bit().data() << endl;
		return false;
	}

	return true;
}

extern "C"
{
    KDE_EXPORT void* create_weather_sidebar(TDEInstance* inst, TQObject* par, TQWidget*widp,
                                      TQString& desktopname, const char* name)
    {
        return new KonqSidebarWeather(inst, par, widp, desktopname, name);
    }

    KDE_EXPORT bool add_weather_sidebar(TQString* fn, TQString* /*param*/,
                                  TQMap<TQString, TQString>* map)
    {
                    map->insert("Type","Link");
                        map->insert("Icon","kweather");
                        map->insert("Name",i18n("Weather"));
                        map->insert("Open","false");
                          map->insert("X-TDE-KonqSidebarModule","weather_sidebar");
                        fn->setLatin1("weatherbar%1.desktop");
                        return true;
    }
}

#include "weatherbar.moc"