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>
42 #include <QPushButton>
47 MsgEvent_Type = QEvent::User + MsgEventType + 1,
50 class MsgEvent : public QEvent
53 MsgEvent( msg_item_t *msg )
54 : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
70 static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
72 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
73 : QVLCFrame( _p_intf )
75 setWindowTitle( qtr( "Messages" ) );
76 setWindowRole( "vlc-messages" );
79 QGridLayout *mainLayout = new QGridLayout( this );
80 mainTab = new QTabWidget( this );
81 mainTab->setTabPosition( QTabWidget::North );
85 QWidget *msgWidget = new QWidget;
86 QGridLayout *msgLayout = new QGridLayout( msgWidget );
88 messages = new QTextEdit();
89 messages->setReadOnly( true );
90 messages->setGeometry( 0, 0, 440, 600 );
91 messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
92 messages->setTextInteractionFlags( Qt::TextSelectableByMouse );
94 msgLayout->addWidget( messages, 0, 0, 1, 0 );
95 mainTab->addTab( msgWidget, qtr( "Messages" ) );
99 QWidget *treeWidget = new QWidget;
100 QGridLayout *treeLayout = new QGridLayout( treeWidget );
102 modulesTree = new QTreeWidget();
103 modulesTree->header()->hide();
105 treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
106 mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
109 /* Buttons and general layout */
110 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
111 closeButton->setDefault( true );
112 clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
113 saveLogButton = new QPushButton( qtr( "&Save as..." ) );
114 saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
116 verbosityBox = new QSpinBox();
117 verbosityBox->setRange( 0, 2 );
118 verbosityBox->setValue( var_InheritInteger( p_intf, "verbose" ) );
119 verbosityBox->setWrapping( true );
120 verbosityBox->setMaximumWidth( 50 );
122 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
124 vbobjectsEdit = new QLineEdit();
126 vbobjectsEdit->setMaximumWidth( 100 );
127 vbobjectsEdit->setText(config_GetPsz( p_intf, "verbose-objects"));
128 vbobjectsEdit->setToolTip( "verbose-objects usage: \n"
129 "--verbose-objects=+printthatobject,-dontprintthatone\n"
130 "(keyword 'all' to applies to all objects)");
132 vbobjectsLabel = new QLabel( qtr( "Message filter" ) );
134 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
135 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
136 mainLayout->addWidget( verbosityBox, 1, 1 );
137 mainLayout->addWidget( vbobjectsLabel, 1, 2, 1, 1 );
138 mainLayout->addWidget( vbobjectsEdit, 1, 3 );
139 mainLayout->setColumnStretch( 4, 10 );
140 mainLayout->addWidget( saveLogButton, 1, 5 );
141 mainLayout->addWidget( clearUpdateButton, 1, 6 );
142 mainLayout->addWidget( closeButton, 1, 7 );
144 BUTTONACT( closeButton, hide() );
145 BUTTONACT( clearUpdateButton, clearOrUpdate() );
146 BUTTONACT( saveLogButton, save() );
147 CONNECT( mainTab, currentChanged( int ),
148 this, updateTab( int ) );
149 CONNECT(vbobjectsEdit, editingFinished(), this, updateConfig());
152 readSettings( "Messages", QSize( 600, 450 ) );
155 /* Hook up to LibVLC messaging */
156 cbData = new msg_cb_data_t;
158 sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
161 MessagesDialog::~MessagesDialog()
163 writeSettings( "Messages" );
164 msg_Unsubscribe( sub );
168 void MessagesDialog::updateTab( int index )
170 /* Second tab : modules tree */
173 verbosityLabel->hide();
174 verbosityBox->hide();
175 vbobjectsLabel->hide();
176 vbobjectsEdit->hide();
177 clearUpdateButton->setText( qtr( "&Update" ) );
178 saveLogButton->hide();
181 /* First tab : messages */
184 verbosityLabel->show();
185 verbosityBox->show();
186 vbobjectsLabel->show();
187 vbobjectsEdit->show();
188 clearUpdateButton->setText( qtr( "&Clear" ) );
189 saveLogButton->show();
193 void MessagesDialog::updateConfig()
195 config_PutPsz(p_intf, "verbose-objects", qtu(vbobjectsEdit->text()));
196 //vbobjectsEdit->setText("vbEdit changed!");
198 if( !vbobjectsEdit->text().isEmpty() )
200 char * psz_verbose_objects = strdup(qtu(vbobjectsEdit->text()));
201 char * psz_object, * iter = psz_verbose_objects;
202 while( (psz_object = strsep( &iter, "," )) )
204 switch( psz_object[0] )
206 printf("%s\n", psz_object+1);
207 case '+': msg_EnableObjectPrinting(p_intf, psz_object+1); break;
208 case '-': msg_DisableObjectPrinting(p_intf, psz_object+1); break;
211 free( psz_verbose_objects );
215 msg_EnableObjectPrinting( p_intf, "all");
219 void MessagesDialog::sinkMessage( msg_item_t *item )
221 if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
222 || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
225 /* Copy selected text to the clipboard */
226 if( messages->textCursor().hasSelection() )
229 /* Fix selected text bug */
230 if( !messages->textCursor().atEnd() ||
231 messages->textCursor().anchor() != messages->textCursor().position() )
232 messages->moveCursor( QTextCursor::End );
234 messages->setFontItalic( true );
235 messages->setTextColor( "darkBlue" );
236 messages->insertPlainText( qfu( item->psz_module ) );
238 switch (item->i_type)
241 messages->setTextColor( "blue" );
242 messages->insertPlainText( " info: " );
245 messages->setTextColor( "red" );
246 messages->insertPlainText( " error: " );
249 messages->setTextColor( "green" );
250 messages->insertPlainText( " warning: " );
254 messages->setTextColor( "grey" );
255 messages->insertPlainText( " debug: " );
259 /* Add message Regular black Font */
260 messages->setFontItalic( false );
261 messages->setTextColor( "black" );
262 messages->insertPlainText( qfu(item->psz_msg) );
263 messages->insertPlainText( "\n" );
264 messages->ensureCursorVisible();
267 void MessagesDialog::customEvent( QEvent *event )
269 MsgEvent *msge = static_cast<MsgEvent *>(event);
272 sinkMessage( msge->msg );
275 void MessagesDialog::clearOrUpdate()
277 if( mainTab->currentIndex() )
283 void MessagesDialog::clear()
288 bool MessagesDialog::save()
290 QString saveLogFileName = QFileDialog::getSaveFileName(
291 this, qtr( "Save log file as..." ),
292 QVLCUserDir( VLC_DOCUMENTS_DIR ),
293 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
295 if( !saveLogFileName.isNull() )
297 QFile file( saveLogFileName );
298 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
299 QMessageBox::warning( this, qtr( "Application" ),
300 qtr( "Cannot write to file %1:\n%2." )
301 .arg( saveLogFileName )
302 .arg( file.errorString() ) );
306 QTextStream out( &file );
307 out << messages->toPlainText() << "\n";
314 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
315 vlc_object_t *p_obj )
317 QTreeWidgetItem *item;
320 item = new QTreeWidgetItem( parentItem );
322 item = new QTreeWidgetItem( modulesTree );
324 char *name = vlc_object_get_name( p_obj );
327 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
328 qfu( name ) + "\" (" +
329 QString::number((uintptr_t)p_obj) + ")" );
333 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
334 QString::number((uintptr_t)p_obj) + ")" );
336 item->setExpanded( true );
338 vlc_list_t *l = vlc_list_children( p_obj );
339 for( int i=0; i < l->i_count; i++ )
340 buildTree( item, l->p_values[i].p_object );
341 vlc_list_release( l );
344 void MessagesDialog::updateTree()
346 modulesTree->clear();
347 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
350 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
352 int canc = vlc_savecancel();
354 QApplication::postEvent( data->self, new MsgEvent( item ) );
356 vlc_restorecancel( canc );