summaryrefslogtreecommitdiff
path: root/core/launcher/qrr.cpp
blob: 5809ca93c53007c9a5e773bb07bffbce25dc610f (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
/**********************************************************************
** 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.
**
**********************************************************************/

#include "qrr.h"

#include <qtopia/qcopenvelope_qws.h>
#include <qfile.h>
#include <qtimer.h>
#include <qdialog.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qprogressbar.h>
#include <qapplication.h>
#include <qevent.h>


class CenteringDialog : public QDialog
{
public:
    CenteringDialog( QWidget *parent = 0, char *name = 0, bool modal = FALSE, WFlags f = 0 );
    virtual ~CenteringDialog();

protected:
    void resizeEvent( QResizeEvent *e );
};

CenteringDialog::CenteringDialog( QWidget *parent, char *name, bool modal, WFlags f )
    : QDialog( parent, name, modal, f )
{
}

CenteringDialog::~CenteringDialog()
{
}

void CenteringDialog::resizeEvent( QResizeEvent *e )
{
    int dist = -((width() - e->oldSize().width()) / 2);
    qDebug( "move %d", dist );
    move( pos().x() + dist, pos().y() );
}

// =====================================================================

QueuedRequestRunner::QueuedRequestRunner( QFile *f, QWidget *parent )
    : readyToDelete( FALSE ), waitingForMessages( FALSE ), file( 0 )
{
    file = f;
    waitMsgs.setAutoDelete( TRUE );
    if ( parent ) {
	progressDialog = new CenteringDialog( parent, 0, TRUE );
	QVBoxLayout *l = new QVBoxLayout( progressDialog );
	l->setMargin( 6 );
	l->setSpacing( 6 );
	progressLabel = new QLabel( progressDialog );
	progressLabel->setText( tr("Processing Queued Requests") );
	progressBar = new QProgressBar( progressDialog );
	l->addWidget( progressLabel );
	l->addWidget( progressBar );
	//progressDialog->setFixedSize( qApp->desktop()->width(), qApp->desktop()->height() );
	progressDialog->show();
    }
    int totalSteps = countSteps();
    if ( parent ) {
	qDebug( "%d steps", totalSteps );
	progressBar->setTotalSteps( totalSteps );
	progressBar->setProgress( 0 );
    }
    file->open( IO_ReadOnly );
}

QueuedRequestRunner::~QueuedRequestRunner()
{
    delete progressDialog;
    delete file;
}

void QueuedRequestRunner::process()
{
    if ( process( FALSE ) ) {
	if ( !waitingForMessages || action == "wait" )
	    QTimer::singleShot( 100, this, SLOT(process()) );
    } else {
	file->remove();
	emit finished();
    }

}

int QueuedRequestRunner::countSteps()
{
    int totalSteps = 0;
    bool more = TRUE;
    file->open( IO_ReadOnly );
    while ( more ) {
	steps = 0;
	more = process( TRUE );
	totalSteps += steps;
    }
    file->close();
    waitingForMessages = FALSE;
    return totalSteps;
}

bool QueuedRequestRunner::process( bool counting )
{
    QDataStream stream( file );
    stream >> action;
    if ( action == "info" ) {
	QString message;
	stream >> message;
	qDebug( "info %s", message.latin1() );
	if ( counting ) {
	    steps++;
	} else {
	    progressLabel->setText( message );
	}
    } else if ( action == "qcop" ) {
	QCString channel;
	QCString message;
	int args;
	stream >> channel >> message >> args;
	qDebug( "qcop %s %s", channel.data(), message.data() );
#ifndef QT_NO_COP
	QCopEnvelope *e = 0;
	if ( !counting ) {
	    e = new QCopEnvelope( channel, message );
	}
#endif
	QCString type;
	for ( int i = 0; i < args; ++i ) {
	    stream >> type;
	    if ( type == "QString" ) {
		QString arg;
		stream >> arg;
		qDebug( "     %s %s", type.data(), arg.latin1() );
#ifndef QT_NO_COP
		if ( !counting )
		    (*e) << arg;
#endif
	    } else if ( type == "int" ) {
		int arg;
		stream >> arg;
		qDebug( "     %s %d", type.data(), arg );
#ifndef QT_NO_COP
		if ( !counting )
		    (*e) << arg;
#endif
	    } else {
		qDebug( "\tBUG unknown type '%s'!", type.data() );
	    }
	}
	if ( counting ) {
	    steps++;
	} else {
#ifndef QT_NO_COP
	    // this causes the QCop message to be sent
	    delete e;
#endif
	}
    } else if ( action == "wait" ) {
	int messageCount;
	QCString message;
	waitMsgs.clear();
	stream >> messageCount;
	for ( int i = 0; i < messageCount; ++i ) {
	    stream >> message;
	    qDebug( "wait %s", message.data() );
	    if ( !counting ) {
		waitMsgs.append( new QCString( message ) );
	    }
	}
	if ( counting )
	    steps++;
	waitingForMessages = TRUE;
    } else {
	qDebug( "\tBUG unknown action '%s'!", action.data() );
    }

    if ( !counting ) {
	progressBar->setProgress( progressBar->progress() + 1 );
    }

    return !file->atEnd();
}

void QueuedRequestRunner::desktopMessage( const QCString &message, const QByteArray & )
{
    bool found = FALSE;
    QCString *msg;
    for ( QListIterator<QCString> iter( waitMsgs ); ( msg = iter.current() ) != 0; ++iter ) {
	if ( *msg == message ) {
	    found = TRUE;
	    break;
	}
    }
    if ( found ) {
	waitMsgs.clear();
	waitingForMessages = FALSE;
	QTimer::singleShot( 100, this, SLOT(process()) );
    }
}