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( "C&lear" ) );
85 saveLogButton = new QPushButton( qtr( "&Save as..." ) );
86 saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
88 verbosityBox = new QSpinBox();
89 verbosityBox->setRange( 0, 2 );
90 verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
91 verbosityBox->setWrapping( true );
92 verbosityBox->setMaximumWidth( 50 );
94 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
96 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
97 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
98 mainLayout->addWidget( verbosityBox, 1, 1 );
99 mainLayout->setColumnStretch( 2, 10 );
100 mainLayout->addWidget( saveLogButton, 1, 3 );
101 mainLayout->addWidget( clearUpdateButton, 1, 4 );
102 mainLayout->addWidget( closeButton, 1, 5 );
104 BUTTONACT( closeButton, hide() );
105 BUTTONACT( clearUpdateButton, clearOrUpdate() );
106 BUTTONACT( saveLogButton, save() );
107 CONNECT( mainTab, currentChanged( int ),
108 this, updateTab( int ) );
111 readSettings( "Messages", QSize( 600, 450 ) );
114 void MessagesDialog::updateTab( int index )
116 /* Second tab : modules tree */
119 verbosityLabel->hide();
120 verbosityBox->hide();
121 clearUpdateButton->setText( qtr( "&Update" ) );
122 saveLogButton->hide();
125 /* First tab : messages */
128 verbosityLabel->show();
129 verbosityBox->show();
130 clearUpdateButton->setText( qtr( "&Clear" ) );
131 saveLogButton->show();
135 void MessagesDialog::updateLog()
138 msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
141 vlc_mutex_lock( p_sub->p_lock );
142 int i_stop = *p_sub->pi_stop;
143 vlc_mutex_unlock( p_sub->p_lock );
145 if( p_sub->i_start != i_stop )
147 messages->textCursor().movePosition( QTextCursor::End );
149 for( i_start = p_sub->i_start;
151 i_start = (i_start+1) % VLC_MSG_QSIZE )
153 if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
154 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
155 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
156 verbosityBox->value() >= 1 ||
157 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
158 verbosityBox->value() >= 2 )
160 messages->setFontItalic( true );
161 messages->setTextColor( "darkBlue" );
162 messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
167 switch( p_sub->p_msg[i_start].i_type )
170 messages->setTextColor( "blue" );
171 messages->insertPlainText( " info: " );
174 messages->setTextColor( "red" );
175 messages->insertPlainText( " error: " );
178 messages->setTextColor( "green" );
179 messages->insertPlainText( " warning: " );
183 messages->setTextColor( "grey" );
184 messages->insertPlainText( " debug: " );
188 /* Add message Regular black Font */
189 messages->setFontItalic( false );
190 messages->setTextColor( "black" );
191 messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
192 messages->insertPlainText( "\n" );
194 messages->ensureCursorVisible();
196 vlc_mutex_lock( p_sub->p_lock );
197 p_sub->i_start = i_start;
198 vlc_mutex_unlock( p_sub->p_lock );
203 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
204 vlc_object_t *p_obj )
206 QTreeWidgetItem *item;
209 item = new QTreeWidgetItem( parentItem );
211 item = new QTreeWidgetItem( modulesTree );
213 if( p_obj->psz_object_name )
214 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
215 qfu( p_obj->psz_object_name ) + "\" (" +
216 QString::number((uintptr_t)p_obj) + ")" );
218 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
219 QString::number((uintptr_t)p_obj) + ")" );
221 item->setExpanded( true );
223 vlc_list_t *l = vlc_list_children( p_obj );
224 for( int i=0; i < l->i_count; i++ )
225 buildTree( item, l->p_values[i].p_object );
226 vlc_list_release( l );
229 void MessagesDialog::clearOrUpdate()
231 if( mainTab->currentIndex() )
237 void MessagesDialog::updateTree()
239 modulesTree->clear();
240 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
243 void MessagesDialog::clear()
248 bool MessagesDialog::save()
250 QString saveLogFileName = QFileDialog::getSaveFileName(
251 this, qtr( "Save log file as..." ),
252 qfu( config_GetHomeDir() ),
253 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
255 if( !saveLogFileName.isNull() )
257 QFile file( saveLogFileName );
258 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
259 QMessageBox::warning( this, qtr( "Application" ),
260 qtr( "Cannot write to file %1:\n%2." )
261 .arg( saveLogFileName )
262 .arg( file.errorString() ) );
266 QTextStream out( &file );
267 out << messages->toPlainText() << "\n";