summaryrefslogtreecommitdiff
path: root/core/launcher/runningappbar.cpp
blob: 11d10dc127855182795961bf2e2d1855fb28e271 (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
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
***********************************************************************/

#define QTOPIA_INTERNAL_PRELOADACCESS


#include <stdlib.h>

#include <qpainter.h>

#include <qtopia/qcopenvelope_qws.h>

#include "runningappbar.h"
#include "serverinterface.h"

RunningAppBar::RunningAppBar(QWidget* parent)
  : QFrame(parent), selectedAppIndex(-1)
{
    QCopChannel* channel = new QCopChannel( "QPE/System", this );
    connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
	     this, SLOT(received(const QCString&, const QByteArray&)) );

    spacing = AppLnk::smallIconSize()+3;
}

RunningAppBar::~RunningAppBar()
{
}

void RunningAppBar::received(const QCString& msg, const QByteArray& data) {
    // Since fast apps appear and disappear without disconnecting from their
    // channel we need to watch for the showing/hiding events and update according.
    QDataStream stream( data, IO_ReadOnly );
    if ( msg == "fastAppShowing(QString)") {
	QString appName;
	stream >> appName;
	//    qDebug("fastAppShowing %s", appName.data() );
	const AppLnk* f = ServerInterface::appLnks().findExec(appName);
	if ( f ) addTask(*f);
    } else if ( msg == "fastAppHiding(QString)") {
	QString appName;
	stream >> appName;
	const AppLnk* f = ServerInterface::appLnks().findExec(appName);
	if ( f ) removeTask(*f);
    }
}

void RunningAppBar::addTask(const AppLnk& appLnk) {
    qDebug("Added %s to app list.", appLnk.name().latin1());
    AppLnk* newApp = new AppLnk(appLnk);
    newApp->setExec(appLnk.exec());
    appList.prepend(newApp);
    update();
}

void RunningAppBar::removeTask(const AppLnk& appLnk) {
  unsigned int i = 0;
  for (; i < appList.count() ; i++) {
    AppLnk* target = appList.at(i);
    if (target->exec() == appLnk.exec()) {
       qDebug("Removing %s from app list.", appLnk.name().latin1());
      appList.remove();
      delete target;
    }
  }
  update();
}

void RunningAppBar::mousePressEvent(QMouseEvent *e)
{
  // Find out if the user is clicking on an app icon...
  // If so, snag the index so when we repaint we show it
  // as highlighed.
  selectedAppIndex = 0;
  int x=0;
  QListIterator<AppLnk> it( appList );
  for ( ; it.current(); ++it,++selectedAppIndex,x+=spacing ) {
    if ( x + spacing <= width() ) {
      if ( e->x() >= x && e->x() < x+spacing ) {
	if ( selectedAppIndex < (int)appList.count() ) {
	  repaint(FALSE);
	  return;
	}
      }
    } else {
      break;
    }
  }
  selectedAppIndex = -1;
  repaint( FALSE );
}

void RunningAppBar::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == QMouseEvent::RightButton)
	return;
    if ( selectedAppIndex >= 0 ) {
	QString app = appList.at(selectedAppIndex)->exec();
	QCopEnvelope e("QPE/System", "raise(QString)");
	e << app;
	selectedAppIndex = -1;
	update();
    }
}

void RunningAppBar::paintEvent( QPaintEvent * )
{
    QPainter p( this );
    AppLnk *curApp;
    int x = 0;
    int y = (height() - AppLnk::smallIconSize()) / 2;
    int i = 0;

    p.fillRect( 0, 0, width(), height(), colorGroup().background() );

    QListIterator<AppLnk> it(appList);

    for (; it.current(); i++, ++it ) {
      if ( x + spacing <= width() ) {
	curApp = it.current();
        qWarning("Drawing %s", curApp->name().latin1() );
 	if ( (int)i == selectedAppIndex )
 	  p.fillRect( x, y, spacing, curApp->pixmap().height()+1, colorGroup().highlight() );
  	else
	  p.eraseRect( x, y, spacing, curApp->pixmap().height()+1 );
	p.drawPixmap( x, y, curApp->pixmap() );
	x += spacing;
      }
    }
}

QSize RunningAppBar::sizeHint() const
{
    return QSize( frameWidth(), AppLnk::smallIconSize()+frameWidth()*2+3 );
}

void RunningAppBar::applicationLaunched(const QString &appName)
{
    qDebug("desktop:: app: %s launched with pid ", appName.data() );
    const AppLnk* newGuy = ServerInterface::appLnks().findExec(appName);
    if ( newGuy && !newGuy->isPreloaded() ) {
	addTask( *newGuy );
    }
}

void RunningAppBar::applicationTerminated(const QString &app)
{
    const AppLnk* gone = ServerInterface::appLnks().findExec(app);
    if ( gone ) {
	removeTask(*gone);
    }
}