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>
43 MessagesDialog *MessagesDialog::instance = NULL;
45 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
46 : QVLCFrame( _p_intf )
48 setWindowTitle( qtr( "Messages" ) );
51 QGridLayout *mainLayout = new QGridLayout( this );
52 mainTab = new QTabWidget( this );
53 mainTab->setTabPosition( QTabWidget::North );
57 QWidget *msgWidget = new QWidget;
58 QGridLayout *msgLayout = new QGridLayout( msgWidget );
60 messages = new QTextEdit();
61 messages->setReadOnly( true );
62 messages->setGeometry( 0, 0, 440, 600 );
63 messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
65 msgLayout->addWidget( messages, 0, 0, 1, 0 );
66 mainTab->addTab( msgWidget, qtr( "Messages" ) );
67 ON_TIMEOUT( updateLog() );
71 QWidget *treeWidget = new QWidget;
72 QGridLayout *treeLayout = new QGridLayout( treeWidget );
74 modulesTree = new QTreeWidget();
75 modulesTree->header()->hide();
77 treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
78 mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
81 /* Buttons and general layout */
82 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
83 closeButton->setDefault( true );
84 clearUpdateButton = new QPushButton( qtr( "&Clear" ) );
85 saveLogButton = new QPushButton( qtr( "&Save as..." ) );
87 verbosityBox = new QSpinBox();
88 verbosityBox->setRange( 0, 2 );
89 verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
90 verbosityBox->setWrapping( true );
91 verbosityBox->setMaximumWidth( 50 );
93 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
95 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
96 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
97 mainLayout->addWidget( verbosityBox, 1, 1 );
98 mainLayout->addWidget( saveLogButton, 1, 3 );
99 mainLayout->addWidget( clearUpdateButton, 1, 4 );
100 mainLayout->addWidget( closeButton, 1, 5 );
102 BUTTONACT( closeButton, hide() );
103 BUTTONACT( clearUpdateButton, clearOrUpdate() );
104 BUTTONACT( saveLogButton, save() );
105 CONNECT( mainTab, currentChanged( int ),
106 this, updateTab( int ) );
109 readSettings( "Messages", QSize( 600, 450 ) );
112 void MessagesDialog::updateTab( int index )
114 /* Second tab : modules tree */
117 verbosityLabel->hide();
118 verbosityBox->hide();
119 clearUpdateButton->setText( qtr( "&Update" ) );
120 saveLogButton->hide();
123 /* First tab : messages */
126 verbosityLabel->show();
127 verbosityBox->show();
128 clearUpdateButton->setText( qtr( "&Clear" ) );
129 saveLogButton->show();
133 void MessagesDialog::updateLog()
135 msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
138 vlc_mutex_lock( p_sub->p_lock );
139 int i_stop = *p_sub->pi_stop;
140 vlc_mutex_unlock( p_sub->p_lock );
142 if( p_sub->i_start != i_stop )
144 messages->textCursor().movePosition( QTextCursor::End );
146 for( i_start = p_sub->i_start;
148 i_start = (i_start+1) % VLC_MSG_QSIZE )
150 if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
151 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
152 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
153 verbosityBox->value() >= 1 ||
154 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
155 verbosityBox->value() >= 2 )
157 messages->setFontItalic( true );
158 messages->setTextColor( "darkBlue" );
159 messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
164 switch( p_sub->p_msg[i_start].i_type )
167 messages->setTextColor( "blue" );
168 messages->insertPlainText( " info: " );
171 messages->setTextColor( "red" );
172 messages->insertPlainText( " error: " );
175 messages->setTextColor( "green" );
176 messages->insertPlainText( " warning: " );
180 messages->setTextColor( "grey" );
181 messages->insertPlainText( " debug: " );
185 /* Add message Regular black Font */
186 messages->setFontItalic( false );
187 messages->setTextColor( "black" );
188 messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
189 messages->insertPlainText( "\n" );
191 messages->ensureCursorVisible();
193 vlc_mutex_lock( p_sub->p_lock );
194 p_sub->i_start = i_start;
195 vlc_mutex_unlock( p_sub->p_lock );
199 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
200 vlc_object_t *p_obj )
202 QTreeWidgetItem *item;
205 item = new QTreeWidgetItem( parentItem );
207 item = new QTreeWidgetItem( modulesTree );
209 if( p_obj->psz_object_name )
210 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
211 qfu( p_obj->psz_object_name ) + "\" (" +
212 QString::number(p_obj->i_object_id) + ")" );
214 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
215 QString::number(p_obj->i_object_id) + ")" );
217 item->setExpanded( true );
219 vlc_list_t *l = vlc_list_children( p_obj );
220 vlc_object_release( p_obj );
222 for( int i=0; i < l->i_count; i++ )
223 buildTree( item, l->p_values[i].p_object );
226 void MessagesDialog::clearOrUpdate()
228 if( mainTab->currentIndex() )
234 void MessagesDialog::updateTree()
236 modulesTree->clear();
238 vlc_object_yield( p_intf->p_libvlc );
239 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
242 void MessagesDialog::clear()
247 bool MessagesDialog::save()
249 QString saveLogFileName = QFileDialog::getSaveFileName(
250 this, qtr( "Choose a filename to save the logs under..." ),
251 qfu( config_GetHomeDir() ),
252 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
254 if( !saveLogFileName.isNull() )
256 QFile file( saveLogFileName );
257 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
258 QMessageBox::warning( this, qtr( "Application" ),
259 qtr( "Cannot write file %1:\n%2." )
260 .arg( saveLogFileName )
261 .arg( file.errorString() ) );
265 QTextStream out( &file );
266 out << messages->toPlainText() << "\n";