Commit 92e66f6e authored by Laurent Aimar's avatar Laurent Aimar

Fixed QT4 message dialogs thread safety.

I used customEvent to implement it.
(It renders the dedicated message thread per subscriber useless with qt4).
parent 5aa95193
......@@ -39,13 +39,35 @@
#include <QHeaderView>
#include <QMutex>
#include <assert.h>
MessagesDialog *MessagesDialog::instance = NULL;
enum {
MsgEvent_Type = QEvent::User + MsgEventType + 1,
};
class MsgEvent : public QEvent
{
public:
MsgEvent( msg_item_t *msg )
: msg(msg), QEvent( (QEvent::Type)MsgEvent_Type )
{
msg_Hold( msg );
}
virtual ~MsgEvent()
{
msg_Release( msg );
}
msg_item_t *msg;
};
struct msg_cb_data_t
{
MessagesDialog *self;
QMutex lock; /**< protects MessagesDialog::messages */
};
static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
: QVLCFrame( _p_intf )
......@@ -116,16 +138,16 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
/* Hook up to LibVLC messaging */
cb_data = new msg_cb_data_t;
cb_data->self = this;
sub = msg_Subscribe (_p_intf->p_libvlc, sinkMessage, cb_data);
cbData = new msg_cb_data_t;
cbData->self = this;
sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
}
MessagesDialog::~MessagesDialog ()
MessagesDialog::~MessagesDialog()
{
writeSettings( "Messages" );
msg_Unsubscribe (sub);
delete cb_data;
msg_Unsubscribe( sub );
delete cbData;
};
void MessagesDialog::updateTab( int index )
......@@ -149,17 +171,6 @@ void MessagesDialog::updateTab( int index )
}
}
void MessagesDialog::sinkMessage (msg_cb_data_t *data, msg_item_t *item,
unsigned overruns)
{
MessagesDialog *self = data->self;
int canc = vlc_savecancel ();
QMutexLocker locker (&data->lock);
self->sinkMessage (item, overruns);
vlc_restorecancel (canc);
}
void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
{
if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
......@@ -210,6 +221,13 @@ void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos );
messages->setTextCursor( cur );
}
void MessagesDialog::customEvent( QEvent *event )
{
MsgEvent *msg = dynamic_cast<MsgEvent*>(event);
assert( msg );
sinkMessage( msg->msg, 0 );
}
void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
vlc_object_t *p_obj )
......@@ -253,7 +271,6 @@ void MessagesDialog::updateTree()
void MessagesDialog::clear()
{
QMutexLocker locker (&cb_data->lock);
messages->clear();
}
......@@ -276,10 +293,19 @@ bool MessagesDialog::save()
}
QTextStream out( &file );
QMutexLocker locker (&cb_data->lock);
out << messages->toPlainText() << "\n";
return true;
}
return false;
}
static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
{
int canc = vlc_savecancel();
QApplication::postEvent( data->self, new MsgEvent( item ) );
vlc_restorecancel( canc );
}
......@@ -64,9 +64,10 @@ private:
QPushButton *clearUpdateButton;
QPushButton *saveLogButton;
msg_subscription_t *sub;
msg_cb_data_t *cb_data;
static void sinkMessage (msg_cb_data_t *, msg_item_t *, unsigned);
void sinkMessage (msg_item_t *item, unsigned);
msg_cb_data_t *cbData;
static void sinkMessage( msg_cb_data_t *, msg_item_t *, unsigned );
void customEvent( QEvent * );
void sinkMessage( msg_item_t *item, unsigned );
private slots:
void updateTab( int );
......
......@@ -48,7 +48,8 @@ enum {
enum {
DialogEventType = 0,
IMEventType = 100,
PLEventType = 200
PLEventType = 200,
MsgEventType = 300,
};
class QApplication;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment