2
#include <qtextstream.h>
7
#include <kiconloader.h>
9
#include <kpopupmenu.h>
11
#include <kwinmodule.h>
17
const int XFocusOut = FocusOut;
18
const int XFocusIn = FocusIn;
25
extern Time qt_x_time;
28
#include "ksystraycmd.h"
29
#include "ksystraycmd.moc"
32
KSysTrayCmd::KSysTrayCmd()
33
: QLabel( 0, "systray_cmd" ),
34
isVisible(true), lazyStart( false ), noquit( false ),
35
win(0), client(0), kwinmodule(0)
37
setAlignment( AlignCenter );
38
kwinmodule = new KWinModule( this );
42
KSysTrayCmd::~KSysTrayCmd()
48
// Main entry point to the class
51
bool KSysTrayCmd::start()
54
kwinmodule->doNotManage( window );
56
// If we have no command we must catching an existing window
58
checkExistingWindows();
62
errStr = i18n( "No window matching pattern '%1' and no command specified.\n" )
67
// Run the command and watch for its window
68
if ( !startClient() ) {
69
errStr = i18n( "KSysTrayCmd: KShellProcess can't find a shell." );
78
// Window related functions.
81
void KSysTrayCmd::showWindow()
86
XMapWindow( qt_xdisplay(), win );
87
KWin::setActiveWindow( win );
90
void KSysTrayCmd::hideWindow()
95
XUnmapWindow( qt_xdisplay(), win );
98
void KSysTrayCmd::setTargetWindow( WId w )
100
setTargetWindow( KWin::info( w ) );
103
void KSysTrayCmd::setTargetWindow( const KWin::Info &info )
105
disconnect( kwinmodule, SIGNAL(windowAdded(WId)), this, SLOT(windowAdded(WId)) );
106
connect( kwinmodule, SIGNAL(windowChanged(WId)), SLOT(windowChanged(WId)) );
108
KWin::setSystemTrayWindowFor( winId(), win );
113
KWin::setActiveWindow( win );
119
// Refresh the tray icon
122
void KSysTrayCmd::refresh()
124
KWin::setSystemTrayWindowFor( winId(), win ? win : winId() );
127
setPixmap( KWin::icon( win, 20, 20, true ) );
128
QToolTip::add( this, KWin::info( win ).name );
132
QToolTip::add( this, tooltip );
134
QToolTip::add( this, command );
136
QToolTip::add( this, window );
140
img = img.smoothScale( 20, 20 );
148
// Client related functions.
151
bool KSysTrayCmd::startClient()
153
client = new KShellProcess();
155
connect( kwinmodule, SIGNAL(windowAdded(WId)), SLOT(windowAdded(WId)) );
156
connect( client, SIGNAL( processExited(KProcess *) ),
157
this, SLOT( clientExited() ) );
159
return client->start();
162
void KSysTrayCmd::clientExited()
168
if ( lazyStart && noquit )
174
void KSysTrayCmd::quit()
177
NETRootInfo ri( qt_xdisplay(), NET::CloseWindow );
178
ri.closeWindowRequest( win );
186
void KSysTrayCmd::execContextMenu( const QPoint &pos )
188
KPopupMenu *menu = new KPopupMenu();
189
menu->insertTitle( *pixmap(), i18n( "KSysTrayCmd" ) );
190
int hideShowId = menu->insertItem( isVisible ? i18n( "&Hide" ) : i18n( "&Restore" ) );
191
int quitId = menu->insertItem( SmallIcon("exit"), i18n( "&Quit" ) );
193
int cmd = menu->exec( pos );
197
else if ( cmd == hideShowId )
203
void KSysTrayCmd::checkExistingWindows()
205
QValueList<WId>::ConstIterator it;
206
for ( it = kwinmodule->windows().begin(); it != kwinmodule->windows().end(); ++it ) {
213
void KSysTrayCmd::windowAdded(WId w)
215
if ( window && ( QRegExp( window ).match( KWin::info(w).name ) == -1 ) )
217
setTargetWindow( w );
220
void KSysTrayCmd::windowChanged( WId w )
228
// Tray icon event handlers
231
void KSysTrayCmd::mousePressEvent( QMouseEvent *e )
233
if ( e->button() == RightButton )
234
execContextMenu( e->globalPos() );
235
else if ( lazyStart && ( !hasRunningClient() ) )
241
void KSysTrayCmd::enterEvent( QEvent * )
243
// Fake a focus event when the mouse points at our tray icon. This is
244
// needed for our tooltips to work.
245
if ( !qApp->focusWidget() ) {
247
memset(&ev, 0, sizeof(ev));
248
ev.xfocus.display = qt_xdisplay();
249
ev.xfocus.type = XFocusIn;
250
ev.xfocus.window = winId();
251
ev.xfocus.mode = NotifyNormal;
252
ev.xfocus.detail = NotifyAncestor;
253
Time time = qt_x_time;
255
qApp->x11ProcessEvent( &ev );