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>
44 MessagesDialog *MessagesDialog::instance = NULL;
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" ) );
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( config_GetInt( p_intf, "verbose" ) );
118 verbosityBox->setWrapping( true );
119 verbosityBox->setMaximumWidth( 50 );
121 verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
123 mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
124 mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
125 mainLayout->addWidget( verbosityBox, 1, 1 );
126 mainLayout->setColumnStretch( 2, 10 );
127 mainLayout->addWidget( saveLogButton, 1, 3 );
128 mainLayout->addWidget( clearUpdateButton, 1, 4 );
129 mainLayout->addWidget( closeButton, 1, 5 );
131 BUTTONACT( closeButton, hide() );
132 BUTTONACT( clearUpdateButton, clearOrUpdate() );
133 BUTTONACT( saveLogButton, save() );
134 CONNECT( mainTab, currentChanged( int ),
135 this, updateTab( int ) );
138 readSettings( "Messages", QSize( 600, 450 ) );
141 /* Hook up to LibVLC messaging */
142 cbData = new msg_cb_data_t;
144 sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
147 MessagesDialog::~MessagesDialog()
149 writeSettings( "Messages" );
150 msg_Unsubscribe( sub );
154 void MessagesDialog::updateTab( int index )
156 /* Second tab : modules tree */
159 verbosityLabel->hide();
160 verbosityBox->hide();
161 clearUpdateButton->setText( qtr( "&Update" ) );
162 saveLogButton->hide();
165 /* First tab : messages */
168 verbosityLabel->show();
169 verbosityBox->show();
170 clearUpdateButton->setText( qtr( "&Clear" ) );
171 saveLogButton->show();
175 void MessagesDialog::sinkMessage( msg_item_t *item )
177 if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
178 || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
181 /* Copy selected text to the clipboard */
182 if( messages->textCursor().hasSelection() )
185 /* Fix selected text bug */
186 if( !messages->textCursor().atEnd() ||
187 messages->textCursor().anchor() != messages->textCursor().position() )
188 messages->moveCursor( QTextCursor::End );
190 messages->setFontItalic( true );
191 messages->setTextColor( "darkBlue" );
192 messages->insertPlainText( qfu( item->psz_module ) );
194 switch (item->i_type)
197 messages->setTextColor( "blue" );
198 messages->insertPlainText( " info: " );
201 messages->setTextColor( "red" );
202 messages->insertPlainText( " error: " );
205 messages->setTextColor( "green" );
206 messages->insertPlainText( " warning: " );
210 messages->setTextColor( "grey" );
211 messages->insertPlainText( " debug: " );
215 /* Add message Regular black Font */
216 messages->setFontItalic( false );
217 messages->setTextColor( "black" );
218 messages->insertPlainText( qfu(item->psz_msg) );
219 messages->insertPlainText( "\n" );
220 messages->ensureCursorVisible();
223 void MessagesDialog::customEvent( QEvent *event )
225 MsgEvent *msge = static_cast<MsgEvent *>(event);
228 sinkMessage( msge->msg );
231 void MessagesDialog::clearOrUpdate()
233 if( mainTab->currentIndex() )
239 void MessagesDialog::clear()
244 bool MessagesDialog::save()
246 QString saveLogFileName = QFileDialog::getSaveFileName(
247 this, qtr( "Save log file as..." ),
248 qfu( config_GetHomeDir() ),
249 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
251 if( !saveLogFileName.isNull() )
253 QFile file( saveLogFileName );
254 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
255 QMessageBox::warning( this, qtr( "Application" ),
256 qtr( "Cannot write to file %1:\n%2." )
257 .arg( saveLogFileName )
258 .arg( file.errorString() ) );
262 QTextStream out( &file );
263 out << messages->toPlainText() << "\n";
270 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
271 vlc_object_t *p_obj )
273 QTreeWidgetItem *item;
276 item = new QTreeWidgetItem( parentItem );
278 item = new QTreeWidgetItem( modulesTree );
280 /*if( p_obj->psz_object_name )
281 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
282 qfu( p_obj->psz_object_name ) + "\" (" +
283 QString::number((uintptr_t)p_obj) + ")" );
285 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
286 QString::number((uintptr_t)p_obj) + ")" );
288 item->setExpanded( true );
290 vlc_list_t *l = vlc_list_children( p_obj );
291 for( int i=0; i < l->i_count; i++ )
292 buildTree( item, l->p_values[i].p_object );
293 vlc_list_release( l );
296 void MessagesDialog::updateTree()
298 modulesTree->clear();
299 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
302 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
304 int canc = vlc_savecancel();
306 QApplication::postEvent( data->self, new MsgEvent( item ) );
308 vlc_restorecancel( canc );