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 char * psz_verbose_objects = strdup(qtu(vbobjectsEdit->text()));
199 msg_EnableObjectPrinting(p_intf, "all");
200 if( psz_verbose_objects )
202 char * psz_object, * iter = psz_verbose_objects;
203 while( (psz_object = strsep( &iter, "," )) )
205 switch( psz_object[0] )
207 printf("%s\n", psz_object+1);
208 case '+': msg_EnableObjectPrinting(p_intf, psz_object+1); break;
209 case '-': msg_DisableObjectPrinting(p_intf, psz_object+1); break;
212 free( psz_verbose_objects );
216 void MessagesDialog::sinkMessage( msg_item_t *item )
218 if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
219 || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
222 /* Copy selected text to the clipboard */
223 if( messages->textCursor().hasSelection() )
226 /* Fix selected text bug */
227 if( !messages->textCursor().atEnd() ||
228 messages->textCursor().anchor() != messages->textCursor().position() )
229 messages->moveCursor( QTextCursor::End );
231 messages->setFontItalic( true );
232 messages->setTextColor( "darkBlue" );
233 messages->insertPlainText( qfu( item->psz_module ) );
235 switch (item->i_type)
238 messages->setTextColor( "blue" );
239 messages->insertPlainText( " info: " );
242 messages->setTextColor( "red" );
243 messages->insertPlainText( " error: " );
246 messages->setTextColor( "green" );
247 messages->insertPlainText( " warning: " );
251 messages->setTextColor( "grey" );
252 messages->insertPlainText( " debug: " );
256 /* Add message Regular black Font */
257 messages->setFontItalic( false );
258 messages->setTextColor( "black" );
259 messages->insertPlainText( qfu(item->psz_msg) );
260 messages->insertPlainText( "\n" );
261 messages->ensureCursorVisible();
264 void MessagesDialog::customEvent( QEvent *event )
266 MsgEvent *msge = static_cast<MsgEvent *>(event);
269 sinkMessage( msge->msg );
272 void MessagesDialog::clearOrUpdate()
274 if( mainTab->currentIndex() )
280 void MessagesDialog::clear()
285 bool MessagesDialog::save()
287 QString saveLogFileName = QFileDialog::getSaveFileName(
288 this, qtr( "Save log file as..." ),
289 QVLCUserDir( VLC_DOCUMENTS_DIR ),
290 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
292 if( !saveLogFileName.isNull() )
294 QFile file( saveLogFileName );
295 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
296 QMessageBox::warning( this, qtr( "Application" ),
297 qtr( "Cannot write to file %1:\n%2." )
298 .arg( saveLogFileName )
299 .arg( file.errorString() ) );
303 QTextStream out( &file );
304 out << messages->toPlainText() << "\n";
311 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
312 vlc_object_t *p_obj )
314 QTreeWidgetItem *item;
317 item = new QTreeWidgetItem( parentItem );
319 item = new QTreeWidgetItem( modulesTree );
321 char *name = vlc_object_get_name( p_obj );
324 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
325 qfu( name ) + "\" (" +
326 QString::number((uintptr_t)p_obj) + ")" );
330 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
331 QString::number((uintptr_t)p_obj) + ")" );
333 item->setExpanded( true );
335 vlc_list_t *l = vlc_list_children( p_obj );
336 for( int i=0; i < l->i_count; i++ )
337 buildTree( item, l->p_values[i].p_object );
338 vlc_list_release( l );
341 void MessagesDialog::updateTree()
343 modulesTree->clear();
344 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
347 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
349 int canc = vlc_savecancel();
351 QApplication::postEvent( data->self, new MsgEvent( item ) );
353 vlc_restorecancel( canc );