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>
48 MsgEvent_Type = QEvent::User + MsgEventType + 1,
51 class MsgEvent : public QEvent
54 MsgEvent( msg_item_t *msg )
55 : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
71 static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
73 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
74 : QVLCFrame( _p_intf )
76 setWindowTitle( qtr( "Messages" ) );
77 setWindowRole( "vlc-messages" );
80 QGridLayout *mainLayout = new QGridLayout( this );
81 mainTab = new QTabWidget( this );
82 mainTab->setTabPosition( QTabWidget::North );
86 QWidget *msgWidget = new QWidget;
87 QGridLayout *msgLayout = new QGridLayout( msgWidget );
89 messages = new QTextEdit();
90 messages->setReadOnly( true );
91 messages->setGeometry( 0, 0, 440, 600 );
92 messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
93 messages->setTextInteractionFlags( Qt::TextSelectableByMouse );
95 msgLayout->addWidget( messages, 0, 0, 1, 0 );
96 mainTab->addTab( msgWidget, qtr( "Messages" ) );
100 QWidget *treeWidget = new QWidget;
101 QGridLayout *treeLayout = new QGridLayout( treeWidget );
103 modulesTree = new QTreeWidget();
104 modulesTree->header()->hide();
106 treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
107 mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
110 /* Buttons and general layout */
111 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
112 closeButton->setDefault( true );
113 clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
114 saveLogButton = new QPushButton( qtr( "&Save as..." ) );
115 saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
117 verbosityBox = new QSpinBox();
118 verbosityBox->setRange( 0, 2 );
119 verbosityBox->setValue( var_InheritInteger( p_intf, "verbose" ) );
120 verbosityBox->setWrapping( true );
121 verbosityBox->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
123 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
125 vbobjectsEdit = new QLineEdit();
127 vbobjectsEdit->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
128 vbobjectsEdit->setText(config_GetPsz( p_intf, "verbose-objects"));
129 vbobjectsEdit->setToolTip( "verbose-objects usage: \n"
130 "--verbose-objects=+printthatobject,-dontprintthatone\n"
131 "(keyword 'all' to applies to all objects)");
133 vbobjectsLabel = new QLabel( qtr( "Filter:" ) );
135 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
136 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
137 mainLayout->addWidget( verbosityBox, 1, 1 );
138 mainLayout->addWidget( vbobjectsLabel, 1, 2, 1, 1 );
139 mainLayout->addWidget( vbobjectsEdit, 1, 3 );
140 mainLayout->setColumnStretch( 4, 10 );
141 mainLayout->addWidget( saveLogButton, 1, 5 );
142 mainLayout->addWidget( clearUpdateButton, 1, 6 );
143 mainLayout->addWidget( closeButton, 1, 7 );
145 BUTTONACT( closeButton, hide() );
146 BUTTONACT( clearUpdateButton, clearOrUpdate() );
147 BUTTONACT( saveLogButton, save() );
148 CONNECT( mainTab, currentChanged( int ),
149 this, updateTab( int ) );
150 CONNECT(vbobjectsEdit, editingFinished(), this, updateConfig());
151 CONNECT( verbosityBox, valueChanged( int ),
152 this, changeVerbosity( int ) );
155 readSettings( "Messages", QSize( 600, 450 ) );
158 /* Hook up to LibVLC messaging */
159 cbData = new msg_cb_data_t;
161 sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
162 changeVerbosity( verbosityBox->value() );
165 MessagesDialog::~MessagesDialog()
167 writeSettings( "Messages" );
168 msg_Unsubscribe( sub );
172 void MessagesDialog::changeVerbosity( int verbosity )
174 msg_SubscriptionSetVerbosity( sub , verbosity );
177 void MessagesDialog::updateTab( int index )
179 /* Second tab : modules tree */
182 verbosityLabel->hide();
183 verbosityBox->hide();
184 vbobjectsLabel->hide();
185 vbobjectsEdit->hide();
186 clearUpdateButton->setText( qtr( "&Update" ) );
187 saveLogButton->hide();
190 /* First tab : messages */
193 verbosityLabel->show();
194 verbosityBox->show();
195 vbobjectsLabel->show();
196 vbobjectsEdit->show();
197 clearUpdateButton->setText( qtr( "&Clear" ) );
198 saveLogButton->show();
202 void MessagesDialog::updateConfig()
204 config_PutPsz(p_intf, "verbose-objects", qtu(vbobjectsEdit->text()));
205 //vbobjectsEdit->setText("vbEdit changed!");
207 if( !vbobjectsEdit->text().isEmpty() )
209 /* if user sets filter, go with the idea that user just wants that to be shown,
210 so disable all by default and enable those that user wants */
211 msg_DisableObjectPrinting( p_intf, "all");
212 char * psz_verbose_objects = strdup(qtu(vbobjectsEdit->text()));
213 char * psz_object, * iter = psz_verbose_objects;
214 while( (psz_object = strsep( &iter, "," )) )
216 switch( psz_object[0] )
218 printf("%s\n", psz_object+1);
219 case '+': msg_EnableObjectPrinting(p_intf, psz_object+1); break;
220 case '-': msg_DisableObjectPrinting(p_intf, psz_object+1); break;
221 /* user can but just 'lua,playlist' on filter */
222 default: msg_EnableObjectPrinting(p_intf, psz_object); break;
225 free( psz_verbose_objects );
229 msg_EnableObjectPrinting( p_intf, "all");
233 void MessagesDialog::sinkMessage( msg_item_t *item )
235 /* Only scroll if the viewport is at the end.
236 Don't bug user by auto-changing/loosing viewport on insert(). */
237 bool b_autoscroll = ( messages->verticalScrollBar()->value()
238 + messages->verticalScrollBar()->pageStep()
239 >= messages->verticalScrollBar()->maximum() );
241 /* Copy selected text to the clipboard */
242 if( messages->textCursor().hasSelection() )
245 /* Fix selected text bug */
246 if( !messages->textCursor().atEnd() ||
247 messages->textCursor().anchor() != messages->textCursor().position() )
248 messages->moveCursor( QTextCursor::End );
250 messages->setFontItalic( true );
251 messages->setTextColor( "darkBlue" );
252 messages->insertPlainText( qfu( item->psz_module ) );
254 switch (item->i_type)
257 messages->setTextColor( "blue" );
258 messages->insertPlainText( " info: " );
261 messages->setTextColor( "red" );
262 messages->insertPlainText( " error: " );
265 messages->setTextColor( "green" );
266 messages->insertPlainText( " warning: " );
270 messages->setTextColor( "grey" );
271 messages->insertPlainText( " debug: " );
275 /* Add message Regular black Font */
276 messages->setFontItalic( false );
277 messages->setTextColor( "black" );
278 messages->insertPlainText( qfu(item->psz_msg) );
279 messages->insertPlainText( "\n" );
280 if ( b_autoscroll ) messages->ensureCursorVisible();
283 void MessagesDialog::customEvent( QEvent *event )
285 MsgEvent *msge = static_cast<MsgEvent *>(event);
288 sinkMessage( msge->msg );
291 void MessagesDialog::clearOrUpdate()
293 if( mainTab->currentIndex() )
299 void MessagesDialog::clear()
304 bool MessagesDialog::save()
306 QString saveLogFileName = QFileDialog::getSaveFileName(
307 this, qtr( "Save log file as..." ),
308 QVLCUserDir( VLC_DOCUMENTS_DIR ),
309 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
311 if( !saveLogFileName.isNull() )
313 QFile file( saveLogFileName );
314 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
315 QMessageBox::warning( this, qtr( "Application" ),
316 qtr( "Cannot write to file %1:\n%2." )
317 .arg( saveLogFileName )
318 .arg( file.errorString() ) );
322 QTextStream out( &file );
323 out << messages->toPlainText() << "\n";
330 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
331 vlc_object_t *p_obj )
333 QTreeWidgetItem *item;
336 item = new QTreeWidgetItem( parentItem );
338 item = new QTreeWidgetItem( modulesTree );
340 char *name = vlc_object_get_name( p_obj );
343 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
344 qfu( name ) + "\" (" +
345 QString::number((uintptr_t)p_obj) + ")" );
349 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
350 QString::number((uintptr_t)p_obj) + ")" );
352 item->setExpanded( true );
354 vlc_list_t *l = vlc_list_children( p_obj );
355 for( int i=0; i < l->i_count; i++ )
356 buildTree( item, l->p_values[i].p_object );
357 vlc_list_release( l );
360 void MessagesDialog::updateTree()
362 modulesTree->clear();
363 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
366 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
368 int canc = vlc_savecancel();
370 QApplication::postEvent( data->self, new MsgEvent( item ) );
372 vlc_restorecancel( canc );