00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "qrr.h"
00022
00023 #include <qtopia/qcopenvelope_qws.h>
00024 #include <qfile.h>
00025 #include <qtimer.h>
00026 #include <qdialog.h>
00027 #include <qlayout.h>
00028 #include <qlabel.h>
00029 #include <qprogressbar.h>
00030 #include <qapplication.h>
00031 #include <qevent.h>
00032
00033
00034 class CenteringDialog : public QDialog
00035 {
00036 public:
00037 CenteringDialog( QWidget *parent = 0, char *name = 0, bool modal = FALSE, WFlags f = 0 );
00038 virtual ~CenteringDialog();
00039
00040 protected:
00041 void resizeEvent( QResizeEvent *e );
00042 };
00043
00044 CenteringDialog::CenteringDialog( QWidget *parent, char *name, bool modal, WFlags f )
00045 : QDialog( parent, name, modal, f )
00046 {
00047 }
00048
00049 CenteringDialog::~CenteringDialog()
00050 {
00051 }
00052
00053 void CenteringDialog::resizeEvent( QResizeEvent *e )
00054 {
00055 int dist = -((width() - e->oldSize().width()) / 2);
00056 qDebug( "move %d", dist );
00057 move( pos().x() + dist, pos().y() );
00058 }
00059
00060
00061
00062 QueuedRequestRunner::QueuedRequestRunner( QFile *f, QWidget *parent )
00063 : readyToDelete( FALSE ), waitingForMessages( FALSE ), file( 0 )
00064 {
00065 file = f;
00066 waitMsgs.setAutoDelete( TRUE );
00067 if ( parent ) {
00068 progressDialog = new CenteringDialog( parent, 0, TRUE );
00069 QVBoxLayout *l = new QVBoxLayout( progressDialog );
00070 l->setMargin( 6 );
00071 l->setSpacing( 6 );
00072 progressLabel = new QLabel( progressDialog );
00073 progressLabel->setText( tr("Processing Queued Requests") );
00074 progressBar = new QProgressBar( progressDialog );
00075 l->addWidget( progressLabel );
00076 l->addWidget( progressBar );
00077
00078 progressDialog->show();
00079 }
00080 int totalSteps = countSteps();
00081 if ( parent ) {
00082 qDebug( "%d steps", totalSteps );
00083 progressBar->setTotalSteps( totalSteps );
00084 progressBar->setProgress( 0 );
00085 }
00086 file->open( IO_ReadOnly );
00087 }
00088
00089 QueuedRequestRunner::~QueuedRequestRunner()
00090 {
00091 delete progressDialog;
00092 delete file;
00093 }
00094
00095 void QueuedRequestRunner::process()
00096 {
00097 if ( process( FALSE ) ) {
00098 if ( !waitingForMessages || action == "wait" )
00099 QTimer::singleShot( 100, this, SLOT(process()) );
00100 } else {
00101 file->remove();
00102 emit finished();
00103 }
00104
00105 }
00106
00107 int QueuedRequestRunner::countSteps()
00108 {
00109 int totalSteps = 0;
00110 bool more = TRUE;
00111 file->open( IO_ReadOnly );
00112 while ( more ) {
00113 steps = 0;
00114 more = process( TRUE );
00115 totalSteps += steps;
00116 }
00117 file->close();
00118 waitingForMessages = FALSE;
00119 return totalSteps;
00120 }
00121
00122 bool QueuedRequestRunner::process( bool counting )
00123 {
00124 QDataStream stream( file );
00125 stream >> action;
00126 if ( action == "info" ) {
00127 QString message;
00128 stream >> message;
00129 qDebug( "info %s", message.latin1() );
00130 if ( counting ) {
00131 steps++;
00132 } else {
00133 progressLabel->setText( message );
00134 }
00135 } else if ( action == "qcop" ) {
00136 QCString channel;
00137 QCString message;
00138 int args;
00139 stream >> channel >> message >> args;
00140 qDebug( "qcop %s %s", channel.data(), message.data() );
00141 #ifndef QT_NO_COP
00142 QCopEnvelope *e = 0;
00143 if ( !counting ) {
00144 e = new QCopEnvelope( channel, message );
00145 }
00146 #endif
00147 QCString type;
00148 for ( int i = 0; i < args; ++i ) {
00149 stream >> type;
00150 if ( type == "QString" ) {
00151 QString arg;
00152 stream >> arg;
00153 qDebug( " %s %s", type.data(), arg.latin1() );
00154 #ifndef QT_NO_COP
00155 if ( !counting )
00156 (*e) << arg;
00157 #endif
00158 } else if ( type == "int" ) {
00159 int arg;
00160 stream >> arg;
00161 qDebug( " %s %d", type.data(), arg );
00162 #ifndef QT_NO_COP
00163 if ( !counting )
00164 (*e) << arg;
00165 #endif
00166 } else {
00167 qDebug( "\tBUG unknown type '%s'!", type.data() );
00168 }
00169 }
00170 if ( counting ) {
00171 steps++;
00172 } else {
00173 #ifndef QT_NO_COP
00174
00175 delete e;
00176 #endif
00177 }
00178 } else if ( action == "wait" ) {
00179 int messageCount;
00180 QCString message;
00181 waitMsgs.clear();
00182 stream >> messageCount;
00183 for ( int i = 0; i < messageCount; ++i ) {
00184 stream >> message;
00185 qDebug( "wait %s", message.data() );
00186 if ( !counting ) {
00187 waitMsgs.append( new QCString( message ) );
00188 }
00189 }
00190 if ( counting )
00191 steps++;
00192 waitingForMessages = TRUE;
00193 } else {
00194 qDebug( "\tBUG unknown action '%s'!", action.data() );
00195 }
00196
00197 if ( !counting ) {
00198 progressBar->setProgress( progressBar->progress() + 1 );
00199 }
00200
00201 return !file->atEnd();
00202 }
00203
00204 void QueuedRequestRunner::desktopMessage( const QCString &message, const QByteArray & )
00205 {
00206 bool found = FALSE;
00207 QCString *msg;
00208 for ( QListIterator<QCString> iter( waitMsgs ); ( msg = iter.current() ) != 0; ++iter ) {
00209 if ( *msg == message ) {
00210 found = TRUE;
00211 break;
00212 }
00213 }
00214 if ( found ) {
00215 waitMsgs.clear();
00216 waitingForMessages = FALSE;
00217 QTimer::singleShot( 100, this, SLOT(process()) );
00218 }
00219 }
00220