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"
32 #include <QTextCursor>
33 #include <QFileDialog>
34 #include <QTextStream>
35 #include <QMessageBox>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItem>
39 #include <QHeaderView>
46 MsgEvent_Type = QEvent::User + MsgEventType + 1,
49 class MsgEvent : public QEvent
52 MsgEvent( msg_item_t *msg )
53 : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
69 static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
71 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
72 : QVLCFrame( _p_intf )
74 setWindowTitle( qtr( "Messages" ) );
75 setWindowRole( "vlc-messages" );
78 QGridLayout *mainLayout = new QGridLayout( this );
79 mainTab = new QTabWidget( this );
80 mainTab->setTabPosition( QTabWidget::North );
84 QWidget *msgWidget = new QWidget;
85 QGridLayout *msgLayout = new QGridLayout( msgWidget );
87 messages = new QTextEdit();
88 messages->setReadOnly( true );
89 messages->setGeometry( 0, 0, 440, 600 );
90 messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
91 messages->setTextInteractionFlags( Qt::TextSelectableByMouse );
93 msgLayout->addWidget( messages, 0, 0, 1, 0 );
94 mainTab->addTab( msgWidget, qtr( "Messages" ) );
98 QWidget *treeWidget = new QWidget;
99 QGridLayout *treeLayout = new QGridLayout( treeWidget );
101 modulesTree = new QTreeWidget();
102 modulesTree->header()->hide();
104 treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
105 mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
108 /* Buttons and general layout */
109 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
110 closeButton->setDefault( true );
111 clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
112 saveLogButton = new QPushButton( qtr( "&Save as..." ) );
113 saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
115 verbosityBox = new QSpinBox();
116 verbosityBox->setRange( 0, 2 );
117 verbosityBox->setValue( var_InheritInteger( p_intf, "verbose" ) );
118 verbosityBox->setWrapping( true );
119 verbosityBox->setMaximumWidth( 50 );
121 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
123 vbobjectsEdit = new QLineEdit();
125 vbobjectsEdit->setMaximumWidth( 100 );
126 vbobjectsEdit->setText(config_GetPsz( p_intf, "verbose-objects"));
127 vbobjectsLabel = new QLabel( qtr( "Message filter" ) );
129 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
130 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
131 mainLayout->addWidget( verbosityBox, 1, 1 );
132 mainLayout->addWidget( vbobjectsLabel, 1, 2, 1, 1 );
133 mainLayout->addWidget( vbobjectsEdit, 1, 3 );
134 mainLayout->setColumnStretch( 4, 10 );
135 mainLayout->addWidget( saveLogButton, 1, 5 );
136 mainLayout->addWidget( clearUpdateButton, 1, 6 );
137 mainLayout->addWidget( closeButton, 1, 7 );
139 BUTTONACT( closeButton, hide() );
140 BUTTONACT( clearUpdateButton, clearOrUpdate() );
141 BUTTONACT( saveLogButton, save() );
142 CONNECT( mainTab, currentChanged( int ),
143 this, updateTab( int ) );
144 CONNECT(vbobjectsEdit, editingFinished(), this, updateConfig());
147 readSettings( "Messages", QSize( 600, 450 ) );
150 /* Hook up to LibVLC messaging */
151 cbData = new msg_cb_data_t;
153 sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
156 MessagesDialog::~MessagesDialog()
158 writeSettings( "Messages" );
159 msg_Unsubscribe( sub );
163 void MessagesDialog::updateTab( int index )
165 /* Second tab : modules tree */
168 verbosityLabel->hide();
169 verbosityBox->hide();
170 vbobjectsLabel->hide();
171 vbobjectsEdit->hide();
172 clearUpdateButton->setText( qtr( "&Update" ) );
173 saveLogButton->hide();
176 /* First tab : messages */
179 verbosityLabel->show();
180 verbosityBox->show();
181 vbobjectsLabel->show();
182 vbobjectsEdit->show();
183 clearUpdateButton->setText( qtr( "&Clear" ) );
184 saveLogButton->show();
188 void MessagesDialog::updateConfig()
190 config_PutPsz(p_intf, "verbose-objects", qtu(vbobjectsEdit->text()));
191 //vbobjectsEdit->setText("vbEdit changed!");
192 msg_Dbg( p_intf, "Here" );
194 char * psz_verbose_objects = strdup(qtu(vbobjectsEdit->text()));
195 msg_EnableObjectPrinting(p_intf, "all");
196 if( psz_verbose_objects )
198 char * psz_object, * iter = psz_verbose_objects;
199 while( (psz_object = strsep( &iter, "," )) )
201 switch( psz_object[0] )
203 printf("%s\n", psz_object+1);
204 case '+': msg_EnableObjectPrinting(p_intf, psz_object+1); break;
205 case '-': msg_DisableObjectPrinting(p_intf, psz_object+1); break;
207 msg_Err( p_intf, "verbose-objects usage: \n"
208 "--verbose-objects=+printthatobject,"
209 "-dontprintthatone\n"
210 "(keyword 'all' to applies to all objects)");
213 free( psz_verbose_objects );
217 void MessagesDialog::sinkMessage( msg_item_t *item )
219 if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
220 || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
223 /* Copy selected text to the clipboard */
224 if( messages->textCursor().hasSelection() )
227 /* Fix selected text bug */
228 if( !messages->textCursor().atEnd() ||
229 messages->textCursor().anchor() != messages->textCursor().position() )
230 messages->moveCursor( QTextCursor::End );
232 messages->setFontItalic( true );
233 messages->setTextColor( "darkBlue" );
234 messages->insertPlainText( qfu( item->psz_module ) );
236 switch (item->i_type)
239 messages->setTextColor( "blue" );
240 messages->insertPlainText( " info: " );
243 messages->setTextColor( "red" );
244 messages->insertPlainText( " error: " );
247 messages->setTextColor( "green" );
248 messages->insertPlainText( " warning: " );
252 messages->setTextColor( "grey" );
253 messages->insertPlainText( " debug: " );
257 /* Add message Regular black Font */
258 messages->setFontItalic( false );
259 messages->setTextColor( "black" );
260 messages->insertPlainText( qfu(item->psz_msg) );
261 messages->insertPlainText( "\n" );
262 messages->ensureCursorVisible();
265 void MessagesDialog::customEvent( QEvent *event )
267 MsgEvent *msge = static_cast<MsgEvent *>(event);
270 sinkMessage( msge->msg );
273 void MessagesDialog::clearOrUpdate()
275 if( mainTab->currentIndex() )
281 void MessagesDialog::clear()
286 bool MessagesDialog::save()
288 QString saveLogFileName = QFileDialog::getSaveFileName(
289 this, qtr( "Save log file as..." ),
290 QVLCUserDir( VLC_DOCUMENTS_DIR ),
291 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
293 if( !saveLogFileName.isNull() )
295 QFile file( saveLogFileName );
296 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
297 QMessageBox::warning( this, qtr( "Application" ),
298 qtr( "Cannot write to file %1:\n%2." )
299 .arg( saveLogFileName )
300 .arg( file.errorString() ) );
304 QTextStream out( &file );
305 out << messages->toPlainText() << "\n";
312 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
313 vlc_object_t *p_obj )
315 QTreeWidgetItem *item;
318 item = new QTreeWidgetItem( parentItem );
320 item = new QTreeWidgetItem( modulesTree );
322 char *name = vlc_object_get_name( p_obj );
325 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
326 qfu( name ) + "\" (" +
327 QString::number((uintptr_t)p_obj) + ")" );
331 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
332 QString::number((uintptr_t)p_obj) + ")" );
334 item->setExpanded( true );
336 vlc_list_t *l = vlc_list_children( p_obj );
337 for( int i=0; i < l->i_count; i++ )
338 buildTree( item, l->p_values[i].p_object );
339 vlc_list_release( l );
342 void MessagesDialog::updateTree()
344 modulesTree->clear();
345 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
348 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
350 int canc = vlc_savecancel();
352 QApplication::postEvent( data->self, new MsgEvent( item ) );
354 vlc_restorecancel( canc );