summaryrefslogtreecommitdiffstats
path: root/noatun/modules/systray/systray.cpp
blob: 2493bdbc6abf68c34e02831bbca98e38a0666724 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// systray.h
//
// Copyright (C) 2000 Neil Stevens <multivac@fcmail.com>
// Copyright (C) 1999 Charles Samuels <charles@kde.org>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name(s) of the author(s) shall not be
// used in advertising or otherwise to promote the sale, use or other dealings
// in this Software without prior written authorization from the author(s).

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "systray.h"
#include "kitsystemtray.h"
#include "cmodule.h"
#include "yhconfig.h"

#include <noatun/app.h>
#include <noatun/pref.h>
#include <noatun/player.h>
#include <noatun/stdaction.h>

#include <kaction.h>
#include <kconfig.h>
#include <tqfile.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kpassivepopup.h>
#include <kpixmapeffect.h>
#include <kstdaction.h>
#include <tqbitmap.h>
#include <tqhbox.h>
#include <tqpainter.h>
#include <tqpushbutton.h>
#include <tqtooltip.h>
#include <tqvbox.h>

#include <tqimage.h>
#include <kurl.h>
#include <kio/netaccess.h>
#include <kdebug.h>
#include <kstandarddirs.h>

#include <netwm.h>
#include <kglobalsettings.h>

// TODO: Maybe make this value configurable?
const int COVER_MAXW = 128;
const int COVER_MAXH = 128;
#define BASEICON "noatun"

// From JuK
class PassivePopup : public KPassivePopup
{
public:
	PassivePopup(TQWidget *parent = 0, const char *name = 0) : KPassivePopup(parent, name) {}

protected:
	virtual void enterEvent(TQEvent *)
	{
		setTimeout(3000000); // Make timeout damn near infinite
	}

	virtual void leaveEvent(TQEvent *)
	{
		setTimeout(250); // Close quickly
	}
};


//NoatunSystray *NoatunSystray::self = 0;


NoatunSystray::NoatunSystray() : KMainWindow(0, "NoatunSystray"), Plugin(),
	mTray(0), trayStatus(0), trayBase(0), mPassivePopup(0L)
{
	//self = this;
	hide();

	tmpCoverPath = locateLocal("tmp", "youngHickoryCover.png");

	removeCover(); // make sure any old temp cover is gone

	KStdAction::quit(napp, TQT_SLOT(quit()), actionCollection());
	KStdAction::open(napp, TQT_SLOT(fileOpen()), actionCollection());
	KStdAction::preferences(napp, TQT_SLOT(preferences()), actionCollection());
	NoatunStdAction::back(actionCollection(), "back");
	NoatunStdAction::stop(actionCollection(), "stop");
	NoatunStdAction::playpause(actionCollection(), "play");
	NoatunStdAction::forward(actionCollection(), "forward");
	NoatunStdAction::playlist(actionCollection(), "show_playlist");
	NoatunStdAction::loop(actionCollection(), "loop_style");
	NoatunStdAction::effects(actionCollection(), "effects");
	NoatunStdAction::equalizer(actionCollection(), "equalizer");

	createGUI("systrayui.rc");

	mTray = new KitSystemTray("tray", this);
	mTray->show();

	trayBase = renderIcon(BASEICON, TQString::null);
	trayStatus = renderIcon(BASEICON, "player_stop");

	mTray->changeTitle(*trayBase, i18n("Noatun"));
	showingTrayStatus = false;

	mBlinkTimer = new TQTimer(this);
	connect(mBlinkTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotBlinkTimer()));

	connect(napp->player(), TQT_SIGNAL(playing()), this, TQT_SLOT(slotPlayPause()));
	connect(napp->player(), TQT_SIGNAL(paused()), this, TQT_SLOT(slotPlayPause()));
	connect(napp->player(), TQT_SIGNAL(stopped()), this, TQT_SLOT(slotStopped()));
	//napp->player()->handleButtons();
}


NoatunSystray::~NoatunSystray()
{
	//kdDebug(66666) << k_funcinfo << "Called." << endl;
	removeCover();
	delete trayBase;
	delete trayStatus;
	napp->showInterfaces();
}


void NoatunSystray::init()
{
	YHModule *cmod = new YHModule(this);
	connect(cmod, TQT_SIGNAL(saved()), this, TQT_SLOT(slotLoadSettings()));
	slotLoadSettings();
}


void NoatunSystray::slotLoadSettings()
{
	kdDebug(66666) << k_funcinfo << endl;

	YHConfig *c = YHConfig::self();

	if(c->stateIconDisplay() == YHConfig::FlashingIcon)
		mBlinkTimer->start(1000);
	else
		mBlinkTimer->stop();
	slotBlinkTimer();


	if(c->tip())
		TQToolTip::add(mTray, tipText);
	else
		TQToolTip::remove(mTray);

	if (!c->passivePopupCovers())
		removeCover();

	if(c->passivePopup())
	{
		mPassivePopup = new PassivePopup(mTray, "NoatunPassivePopup");
	}
	else
	{
		delete mPassivePopup;
		mPassivePopup = 0L;
	}
}


void NoatunSystray::closeEvent(TQCloseEvent*)
{
	//kdDebug(66666) << k_funcinfo << "Called." << endl;
	disconnect(napp->player(), 0, 0, 0);
	unload();
}


void NoatunSystray::slotPlayPause()
{
	TQString status;

	if(napp->player()->isPaused())
	{
		changeTray("player_pause");
		status = i18n("Noatun - Paused");
	}
	else
	{
		changeTray("player_play");
		status = i18n("Noatun - Playing");
	}

	const PlaylistItem item = napp->player()->current();
	TQString s;

	if(!item.isProperty("title"))
	{
		// No metadata
		s = TQString("<nobr>%1</nobr>").arg(item.title());
	}
	else
	{
		s = TQString("<h2><nobr>%1</nobr></h2>").arg(item.property("title"));

		if(item.isProperty("author"))
			s += TQString("<nobr>%1</nobr><br>").arg(item.property("author"));

		if(item.isProperty("album"))
		{
			if(item.isProperty("date"))
				s += TQString("<nobr>%1 (%2)</nobr><br>").arg(item.property("album")).arg(item.property("date"));
			else
				s += TQString("<nobr>%1</nobr><br>").arg(item.property("album"));
		}
	}

	// prepare cover image for display
	if (YHConfig::self()->passivePopupCovers())
		updateCover();

	if(YHConfig::self()->passivePopupCovers() && TQFile::exists(tmpCoverPath))
	{
		// QT always adds an empty line after the table so we add en empty line before the
		// table to get equal spacing on top and bottom
		setTipText(TQString("<qt><br><table cellspacing=0 cellpadding=0><tr>" \
			"<td align=center valign=center><h4><nobr>%1</nobr></h4>%2</td>" \
			"<td valign=center><img src='%3'></td>" \
			"</qt></tr></table>").arg(status).arg(s).arg(tmpCoverPath));
	}
	else
	{
		setTipText(TQString("<qt><center><h4><nobr>%1</nobr></h4>%2</center></qt>").arg(status).arg(s));
	}
}


void NoatunSystray::slotStopped()
{
	if(!napp->player()->current())
		return;
	changeTray("player_stop");
	setTipText(TQString("<qt><nobr><h4>%1</h4></nobr></qt>").arg(i18n("Noatun - Stopped")));
}



void NoatunSystray::changeTray(const TQString &pm)
{
	delete trayStatus;
	trayStatus = renderIcon(BASEICON, pm);
	if(showingTrayStatus)
		slotBlinkTimer();
}


void NoatunSystray::slotBlinkTimer()
{
	switch(YHConfig::self()->stateIconDisplay())
	{
		case (YHConfig::FlashingIcon):
			showingTrayStatus ^= true;
			break;
		case (YHConfig::StaticIcon):
			showingTrayStatus = true;
			break;
		case (YHConfig::NoIcon):
			showingTrayStatus = false;
			break;
	}

	if(showingTrayStatus)
		mTray->setPixmap(*trayStatus);
	else
		mTray->setPixmap(*trayBase);
}


// taken from patched karamba xmmssensor
// modified heavily to work in this place
void NoatunSystray::updateCover()
{
	//kdDebug(66666) << k_funcinfo << endl;
	TQString dir = napp->player()->current().url().directory();
	TQString cover;

	// TODO: Maybe make these filenames configurable?
	if(TQFile::exists(dir + "/folder.png"))
		cover = dir + "/folder.png";
	else if(TQFile::exists(dir + "/.folder.png"))
		cover = dir + "/.folder.png";
	else if(TQFile::exists(dir + "/cover.png"))
		cover = dir + "/cover.png";
	else if(TQFile::exists(dir + "/cover.jpg"))
		cover = dir + "/cover.jpg";
	else if(TQFile::exists(dir + "/cover.jpeg"))
		cover = dir + "/cover.jpeg";
	else // no cover
	{
		//kdDebug(66666) << k_funcinfo << "NO COVER" <<  endl;
		removeCover();
		return;
	}

	TQString title = napp->player()->current().title();

	TQImage previmg;
	previmg.load(tmpCoverPath);

	if(previmg.text("Title") != title)
	{ //Verify song change to limit CPU usage
		/*kdDebug(66666) << k_funcinfo << "Creating new temp cover for '" <<
			cover << "'" << endl;*/

		TQImage src;
		TQImage tmpimg;

		if(src.load(cover))
		{
			if(src.width() >= COVER_MAXW || src.height() >= COVER_MAXH)
				tmpimg = src.scale(COVER_MAXW, COVER_MAXH, TQImage::ScaleMin);
			else
				tmpimg = src;

			tmpimg.setText("Title", 0, title); //add Title in the image text for cache usage
			tmpimg.save(tmpCoverPath, "PNG", 0);
		}
		else
		{
			removeCover();
		}
	}
}


void NoatunSystray::removeCover()
{
	if(TQFile::exists(tmpCoverPath))
		KIO::NetAccess::del(KURL(tmpCoverPath), this);
}


void NoatunSystray::setTipText(const TQString& text)
{
	if(text == tipText) // save the planet, save cpu cycles ;)
		return;
	tipText = text;

	YHConfig *c = YHConfig::self();
	if(c->passivePopup())
		TQTimer::singleShot(0, this, TQT_SLOT(showPassivePopup()));

	if(c->tip())
		TQToolTip::add(mTray, tipText);
}


void NoatunSystray::showPassivePopup()
{
	if (!mPassivePopup)
	{
		kdDebug(66666) << k_funcinfo << "Called but no KPassivePopup created yet!" << endl;
		return;
	}

	mPassivePopup->reparent(0L, TQPoint(0,0));

	if (YHConfig::self()->passivePopupButtons() && !napp->player()->isStopped())
	{
		TQVBox *widget = mPassivePopup->standardView(TQString::null, tipText, TQPixmap());
		TQHBox *box = new TQHBox(mPassivePopup, "popupbox");

		box->setSpacing(8);

		// Algorithm for determining popup location from kpassivepopup.cpp via JuK
		NETWinInfo ni(qt_xdisplay(), mTray->winId(), qt_xrootwin(),
			NET::WMIconGeometry | NET::WMKDESystemTrayWinFor);
		NETRect frame, win;
		ni.kdeGeometry(frame, win);

		TQRect bounds = KGlobalSettings::desktopGeometry(TQPoint(win.pos.x, win.pos.y));

		if(win.pos.x < bounds.center().x())
		{
			// Buttons to the left

			TQVBox *buttonBox = new TQVBox(box);
			buttonBox->setSpacing(3);

			TQPushButton *forwardButton = new TQPushButton(action("forward")->iconSet(), 0, buttonBox, "popup_forward");
			forwardButton->setFlat(true);
			connect(forwardButton, TQT_SIGNAL(clicked()), action("forward"), TQT_SLOT(activate()));

			TQPushButton *backButton = new TQPushButton(action("back")->iconSet(), 0, buttonBox, "popup_back");
			backButton->setFlat(true);
			connect(backButton, TQT_SIGNAL(clicked()), action("back"), TQT_SLOT(activate()));

			TQFrame *line = new TQFrame(box);
			line->setFrameShape(TQFrame::VLine);

			widget->reparent(box, TQPoint(0, 0));
		}
		else
		{
			// Buttons to the right
			widget->reparent(box, TQPoint(0, 0));

			TQFrame *line = new TQFrame(box);
			line->setFrameShape(TQFrame::VLine);

			TQVBox *buttonBox = new TQVBox(box);
			buttonBox->setSpacing(3);

			TQPushButton *forwardButton = new TQPushButton(action("forward")->iconSet(), 0, buttonBox, "popup_forward");
			forwardButton->setFlat(true);
			connect(forwardButton, TQT_SIGNAL(clicked()), action("forward"), TQT_SLOT(activate()));

			TQPushButton *backButton = new TQPushButton(action("back")->iconSet(), 0, buttonBox, "popup_back");
			backButton->setFlat(true);
			connect(backButton, TQT_SIGNAL(clicked()), action("back"), TQT_SLOT(activate()));
		}
		mPassivePopup->setView(box);
	}
	else
	{
		mPassivePopup->setView(TQString::null, tipText);
	}

	mPassivePopup->setTimeout(YHConfig::self()->passivePopupTimeout()*1000);
	mPassivePopup->show();
}


TQPixmap *NoatunSystray::renderIcon(const TQString& baseIcon, const TQString &overlayIcon) const
{
	TQPixmap *base = new TQPixmap(KSystemTray::loadIcon(baseIcon));

	if(!(overlayIcon.isNull())) // otherwise leave the base as-is
	{
		TQPixmap overlay = KSystemTray::loadIcon(overlayIcon);
		if(!overlay.isNull())
		{
			// draw the overlay on top of it
			TQPainter p(base);
			p.drawPixmap(0, 0, overlay);
		}
	}
	return base;
}

#include "systray.moc"