1 /*****************************************************************************
2 * Messages.cpp : Information about an item
3 ****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
7 * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #include "dialogs/messages.hpp"
28 #include "dialogs_provider.hpp"
30 #include <QSpacerItem>
34 #include <QTextCursor>
35 #include <QFileDialog>
36 #include <QTextStream>
37 #include <QMessageBox>
39 #include <QTreeWidget>
40 #include <QTreeWidgetItem>
41 #include <QHeaderView>
44 MessagesDialog *MessagesDialog::instance = NULL;
49 QMutex lock; /**< protects MessagesDialog::messages */
52 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
53 : QVLCFrame( _p_intf )
55 setWindowTitle( qtr( "Messages" ) );
58 QGridLayout *mainLayout = new QGridLayout( this );
59 mainTab = new QTabWidget( this );
60 mainTab->setTabPosition( QTabWidget::North );
64 QWidget *msgWidget = new QWidget;
65 QGridLayout *msgLayout = new QGridLayout( msgWidget );
67 messages = new QTextEdit();
68 messages->setReadOnly( true );
69 messages->setGeometry( 0, 0, 440, 600 );
70 messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
72 msgLayout->addWidget( messages, 0, 0, 1, 0 );
73 mainTab->addTab( msgWidget, qtr( "Messages" ) );
77 QWidget *treeWidget = new QWidget;
78 QGridLayout *treeLayout = new QGridLayout( treeWidget );
80 modulesTree = new QTreeWidget();
81 modulesTree->header()->hide();
83 treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
84 mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
87 /* Buttons and general layout */
88 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
89 closeButton->setDefault( true );
90 clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
91 saveLogButton = new QPushButton( qtr( "&Save as..." ) );
92 saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
94 verbosityBox = new QSpinBox();
95 verbosityBox->setRange( 0, 2 );
96 verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
97 verbosityBox->setWrapping( true );
98 verbosityBox->setMaximumWidth( 50 );
100 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
102 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
103 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
104 mainLayout->addWidget( verbosityBox, 1, 1 );
105 mainLayout->setColumnStretch( 2, 10 );
106 mainLayout->addWidget( saveLogButton, 1, 3 );
107 mainLayout->addWidget( clearUpdateButton, 1, 4 );
108 mainLayout->addWidget( closeButton, 1, 5 );
110 BUTTONACT( closeButton, hide() );
111 BUTTONACT( clearUpdateButton, clearOrUpdate() );
112 BUTTONACT( saveLogButton, save() );
113 CONNECT( mainTab, currentChanged( int ),
114 this, updateTab( int ) );
117 readSettings( "Messages", QSize( 600, 450 ) );
120 /* Hook up to LibVLC messaging */
121 cb_data = new msg_cb_data_t;
122 cb_data->self = this;
123 sub = msg_Subscribe (_p_intf->p_libvlc, sinkMessage, cb_data);
126 MessagesDialog::~MessagesDialog ()
128 writeSettings( "messages" );
129 msg_Unsubscribe (sub);
133 void MessagesDialog::updateTab( int index )
135 /* Second tab : modules tree */
138 verbosityLabel->hide();
139 verbosityBox->hide();
140 clearUpdateButton->setText( qtr( "&Update" ) );
141 saveLogButton->hide();
144 /* First tab : messages */
147 verbosityLabel->show();
148 verbosityBox->show();
149 clearUpdateButton->setText( qtr( "&Clear" ) );
150 saveLogButton->show();
154 void MessagesDialog::sinkMessage (msg_cb_data_t *data, msg_item_t *item,
157 MessagesDialog *self = data->self;
158 QMutexLocker locker (&data->lock);
160 self->sinkMessage (item, overruns);
163 void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
165 if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
166 || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
169 // Saving cursor selection
170 int startPos = messages->textCursor().selectionStart();
171 int endPos = messages->textCursor().selectionEnd();
173 messages->moveCursor( QTextCursor::End );
174 messages->setFontItalic( true );
175 messages->setTextColor( "darkBlue" );
176 messages->insertPlainText( qfu( item->psz_module ) );
178 switch (item->i_type)
181 messages->setTextColor( "blue" );
182 messages->insertPlainText( " info: " );
185 messages->setTextColor( "red" );
186 messages->insertPlainText( " error: " );
189 messages->setTextColor( "green" );
190 messages->insertPlainText( " warning: " );
194 messages->setTextColor( "grey" );
195 messages->insertPlainText( " debug: " );
199 /* Add message Regular black Font */
200 messages->setFontItalic( false );
201 messages->setTextColor( "black" );
202 messages->insertPlainText( qfu(item->psz_msg) );
203 messages->insertPlainText( "\n" );
204 messages->ensureCursorVisible();
206 // Restoring saved cursor selection
207 QTextCursor cur = messages->textCursor();
208 cur.movePosition( QTextCursor::Start );
209 cur.movePosition( QTextCursor::NextCharacter, QTextCursor::MoveAnchor, startPos );
210 cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos );
211 messages->setTextCursor( cur );
214 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
215 vlc_object_t *p_obj )
217 QTreeWidgetItem *item;
220 item = new QTreeWidgetItem( parentItem );
222 item = new QTreeWidgetItem( modulesTree );
224 if( p_obj->psz_object_name )
225 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
226 qfu( p_obj->psz_object_name ) + "\" (" +
227 QString::number((uintptr_t)p_obj) + ")" );
229 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
230 QString::number((uintptr_t)p_obj) + ")" );
232 item->setExpanded( true );
234 vlc_list_t *l = vlc_list_children( p_obj );
235 for( int i=0; i < l->i_count; i++ )
236 buildTree( item, l->p_values[i].p_object );
237 vlc_list_release( l );
240 void MessagesDialog::clearOrUpdate()
242 if( mainTab->currentIndex() )
248 void MessagesDialog::updateTree()
250 modulesTree->clear();
251 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
254 void MessagesDialog::clear()
256 QMutexLocker locker (&cb_data->lock);
260 bool MessagesDialog::save()
262 QString saveLogFileName = QFileDialog::getSaveFileName(
263 this, qtr( "Save log file as..." ),
264 qfu( config_GetHomeDir() ),
265 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
267 if( !saveLogFileName.isNull() )
269 QFile file( saveLogFileName );
270 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
271 QMessageBox::warning( this, qtr( "Application" ),
272 qtr( "Cannot write to file %1:\n%2." )
273 .arg( saveLogFileName )
274 .arg( file.errorString() ) );
278 QTextStream out( &file );
279 QMutexLocker locker (&cb_data->lock);
280 out << messages->toPlainText() << "\n";