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 @@ ...@@ -39,13 +39,35 @@
#include <QHeaderView> #include <QHeaderView>
#include <QMutex> #include <QMutex>
#include <assert.h>
MessagesDialog *MessagesDialog::instance = NULL; 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 struct msg_cb_data_t
{ {
MessagesDialog *self; 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) MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
: QVLCFrame( _p_intf ) : QVLCFrame( _p_intf )
...@@ -116,16 +138,16 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf) ...@@ -116,16 +138,16 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
/* Hook up to LibVLC messaging */ /* Hook up to LibVLC messaging */
cb_data = new msg_cb_data_t; cbData = new msg_cb_data_t;
cb_data->self = this; cbData->self = this;
sub = msg_Subscribe (_p_intf->p_libvlc, sinkMessage, cb_data); sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
} }
MessagesDialog::~MessagesDialog () MessagesDialog::~MessagesDialog()
{ {
writeSettings( "Messages" ); writeSettings( "Messages" );
msg_Unsubscribe (sub); msg_Unsubscribe( sub );
delete cb_data; delete cbData;
}; };
void MessagesDialog::updateTab( int index ) void MessagesDialog::updateTab( int index )
...@@ -149,17 +171,6 @@ 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) void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
{ {
if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1) if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
...@@ -210,6 +221,13 @@ void MessagesDialog::sinkMessage (msg_item_t *item, unsigned) ...@@ -210,6 +221,13 @@ void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos ); cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos );
messages->setTextCursor( cur ); 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, void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
vlc_object_t *p_obj ) vlc_object_t *p_obj )
...@@ -253,7 +271,6 @@ void MessagesDialog::updateTree() ...@@ -253,7 +271,6 @@ void MessagesDialog::updateTree()
void MessagesDialog::clear() void MessagesDialog::clear()
{ {
QMutexLocker locker (&cb_data->lock);
messages->clear(); messages->clear();
} }
...@@ -276,10 +293,19 @@ bool MessagesDialog::save() ...@@ -276,10 +293,19 @@ bool MessagesDialog::save()
} }
QTextStream out( &file ); QTextStream out( &file );
QMutexLocker locker (&cb_data->lock);
out << messages->toPlainText() << "\n"; out << messages->toPlainText() << "\n";
return true; return true;
} }
return false; 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: ...@@ -64,9 +64,10 @@ private:
QPushButton *clearUpdateButton; QPushButton *clearUpdateButton;
QPushButton *saveLogButton; QPushButton *saveLogButton;
msg_subscription_t *sub; msg_subscription_t *sub;
msg_cb_data_t *cb_data; msg_cb_data_t *cbData;
static void sinkMessage (msg_cb_data_t *, msg_item_t *, unsigned); static void sinkMessage( msg_cb_data_t *, msg_item_t *, unsigned );
void sinkMessage (msg_item_t *item, unsigned); void customEvent( QEvent * );
void sinkMessage( msg_item_t *item, unsigned );
private slots: private slots:
void updateTab( int ); void updateTab( int );
......
...@@ -48,7 +48,8 @@ enum { ...@@ -48,7 +48,8 @@ enum {
enum { enum {
DialogEventType = 0, DialogEventType = 0,
IMEventType = 100, IMEventType = 100,
PLEventType = 200 PLEventType = 200,
MsgEventType = 300,
}; };
class QApplication; 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