Index: dialogs/kdialog.cpp
===================================================================
--- dialogs/kdialog.cpp	(revision 888795)
+++ dialogs/kdialog.cpp	(working copy)
@@ -3,6 +3,7 @@
  *  Additions 1999-2000 by Espen Sand (espen@kde.org)
  *                      by Holger Freyther <freyther@kde.org>
  *            2005-2006 by Olivier Goffart (ogoffart at kde.org)
+ *            2008 by Sebastian Trueg (trueg at kde.org)
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -35,6 +36,10 @@
 #include <QTimer>
 #include <QVBoxLayout>
 #include <QWhatsThis>
+#include <QtCore/QThread>
+#include <QtCore/QMutex>
+#include <QtCore/QMutexLocker>
+#include <QtCore/QQueue>
 
 #include <klocale.h>
 #include <kpushbutton.h>
@@ -1009,9 +1014,9 @@
     void slotShowQueuedDialog();
 
     KDialogQueue *q;
-    QList< QPointer<QDialog> > queue;
+    QQueue< QPointer<QDialog> > queue;
     bool busy;
-
+    QMutex mutex;
 };
 
 KDialogQueue* KDialogQueue::self()
@@ -1023,6 +1028,13 @@
 KDialogQueue::KDialogQueue()
   : d( new Private(this) )
 {
+  // we want KDialogQueue to be thread-safe, i.e. it should be possible
+  // to queue dialogs from any thread, not only the GUI thread. For that
+  // to work, the KDialogQueue has to live in the GUI thread. Only then
+  // the queued connections used below will end up there.
+  if(thread() != QApplication::instance()->thread())
+      moveToThread(QApplication::instance()->thread());
+
   d->busy = false;
 }
 
@@ -1035,9 +1047,17 @@
 void KDialogQueue::queueDialog( QDialog *dialog )
 {
   KDialogQueue *_this = self();
-  _this->d->queue.append( dialog );
+  QMutexLocker lock( &_this->d->mutex );
+  _this->d->queue.enqueue( dialog );
 
-  QTimer::singleShot( 0, _this, SLOT( slotShowQueuedDialog() ) );
+  // move the dialog to the GUI thread
+  if(dialog->thread() != QApplication::instance()->thread() &&
+     dialog->thread() == QThread::currentThread())
+      dialog->moveToThread(QApplication::instance()->thread());
+
+  // We use a queued connection to be able to use queued dialogs
+  // from threads other than the GUI thread (example: KMessageBox)
+  QMetaObject::invokeMethod( _this, "slotShowQueuedDialog", Qt::QueuedConnection );
 }
 
 void KDialogQueue::Private::slotShowQueuedDialog()
@@ -1047,10 +1067,10 @@
 
   QDialog *dialog;
   do {
+    QMutexLocker lock( &mutex );
     if ( queue.isEmpty() )
       return;
-    dialog = queue.first();
-    queue.pop_front();
+    dialog = queue.dequeue();
   } while( !dialog );
 
   busy = true;
@@ -1058,8 +1078,11 @@
   busy = false;
   delete dialog;
 
-  if ( !queue.isEmpty() )
-    QTimer::singleShot( 20, q, SLOT( slotShowQueuedDialog() ) );
+  if ( !queue.isEmpty() ) {
+      // We use a queued connection to be able to use queued dialogs
+      // from threads other than the GUI thread (example: KMessageBox)
+      QMetaObject::invokeMethod( q, "slotShowQueuedDialog", Qt::QueuedConnection );
+  }
 }
 
 #include "kdialog.moc"
