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" );
84 ui.modulesTree->header()->hide();
86 /* Buttons and general layout */
87 ui.saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
89 ui.verbosityBox->setValue( var_InheritInteger( p_intf, "verbose" ) );
91 ui.vbobjectsEdit->setText(config_GetPsz( p_intf, "verbose-objects"));
92 ui.vbobjectsEdit->setToolTip( "verbose-objects usage: \n"
93 "--verbose-objects=+printthatobject,-dontprintthatone\n"
94 "(keyword 'all' to applies to all objects)");
96 BUTTONACT( ui.clearButton, clear() );
97 BUTTONACT( ui.updateButton, updateTree() );
98 BUTTONACT( ui.saveLogButton, save() );
99 CONNECT( ui.vbobjectsEdit, editingFinished(), this, updateConfig());
100 CONNECT( ui.bottomButtonsBox, rejected(), this, hide() );
101 CONNECT( ui.verbosityBox, valueChanged( int ),
102 this, changeVerbosity( int ) );
105 readSettings( "Messages", QSize( 600, 450 ) );
107 /* Hook up to LibVLC messaging */
108 cbData = new msg_cb_data_t;
110 sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
111 changeVerbosity( ui.verbosityBox->value() );
114 MessagesDialog::~MessagesDialog()
116 writeSettings( "Messages" );
117 msg_Unsubscribe( sub );
121 void MessagesDialog::changeVerbosity( int verbosity )
123 msg_SubscriptionSetVerbosity( sub , verbosity );
126 void MessagesDialog::updateConfig()
128 config_PutPsz(p_intf, "verbose-objects", qtu(ui.vbobjectsEdit->text()));
129 //vbobjectsEdit->setText("vbEdit changed!");
131 if( !ui.vbobjectsEdit->text().isEmpty() )
133 /* if user sets filter, go with the idea that user just wants that to be shown,
134 so disable all by default and enable those that user wants */
135 msg_DisableObjectPrinting( p_intf, "all");
136 char * psz_verbose_objects = strdup(qtu(ui.vbobjectsEdit->text()));
137 char * psz_object, * iter = psz_verbose_objects;
138 while( (psz_object = strsep( &iter, "," )) )
140 switch( psz_object[0] )
142 printf("%s\n", psz_object+1);
143 case '+': msg_EnableObjectPrinting(p_intf, psz_object+1); break;
144 case '-': msg_DisableObjectPrinting(p_intf, psz_object+1); break;
145 /* user can but just 'lua,playlist' on filter */
146 default: msg_EnableObjectPrinting(p_intf, psz_object); break;
149 free( psz_verbose_objects );
153 msg_EnableObjectPrinting( p_intf, "all");
157 void MessagesDialog::sinkMessage( msg_item_t *item )
159 QTextEdit *messages = ui.messages;
160 /* Only scroll if the viewport is at the end.
161 Don't bug user by auto-changing/loosing viewport on insert(). */
162 bool b_autoscroll = ( messages->verticalScrollBar()->value()
163 + messages->verticalScrollBar()->pageStep()
164 >= messages->verticalScrollBar()->maximum() );
166 /* Copy selected text to the clipboard */
167 if( messages->textCursor().hasSelection() )
170 /* Fix selected text bug */
171 if( !messages->textCursor().atEnd() ||
172 messages->textCursor().anchor() != messages->textCursor().position() )
173 messages->moveCursor( QTextCursor::End );
175 messages->setFontItalic( true );
176 messages->setTextColor( "darkBlue" );
177 messages->insertPlainText( qfu( item->psz_module ) );
179 switch (item->i_type)
182 messages->setTextColor( "blue" );
183 messages->insertPlainText( " info: " );
186 messages->setTextColor( "red" );
187 messages->insertPlainText( " error: " );
190 messages->setTextColor( "green" );
191 messages->insertPlainText( " warning: " );
195 messages->setTextColor( "grey" );
196 messages->insertPlainText( " debug: " );
200 /* Add message Regular black Font */
201 messages->setFontItalic( false );
202 messages->setTextColor( "black" );
203 messages->insertPlainText( qfu(item->psz_msg) );
204 messages->insertPlainText( "\n" );
205 if ( b_autoscroll ) messages->ensureCursorVisible();
208 void MessagesDialog::customEvent( QEvent *event )
210 MsgEvent *msge = static_cast<MsgEvent *>(event);
213 sinkMessage( msge->msg );
216 void MessagesDialog::clear()
218 ui.messages->clear();
221 bool MessagesDialog::save()
223 QString saveLogFileName = QFileDialog::getSaveFileName(
224 this, qtr( "Save log file as..." ),
225 QVLCUserDir( VLC_DOCUMENTS_DIR ),
226 qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
228 if( !saveLogFileName.isNull() )
230 QFile file( saveLogFileName );
231 if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
232 QMessageBox::warning( this, qtr( "Application" ),
233 qtr( "Cannot write to file %1:\n%2." )
234 .arg( saveLogFileName )
235 .arg( file.errorString() ) );
239 QTextStream out( &file );
240 out << ui.messages->toPlainText() << "\n";
247 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
248 vlc_object_t *p_obj )
250 QTreeWidgetItem *item;
253 item = new QTreeWidgetItem( parentItem );
255 item = new QTreeWidgetItem( ui.modulesTree );
257 char *name = vlc_object_get_name( p_obj );
260 item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
261 qfu( name ) + "\" (" +
262 QString::number((uintptr_t)p_obj) + ")" );
266 item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
267 QString::number((uintptr_t)p_obj) + ")" );
269 item->setExpanded( true );
271 vlc_list_t *l = vlc_list_children( p_obj );
272 for( int i=0; i < l->i_count; i++ )
273 buildTree( item, l->p_values[i].p_object );
274 vlc_list_release( l );
277 void MessagesDialog::updateTree()
279 ui.modulesTree->clear();
280 buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
283 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
285 int canc = vlc_savecancel();
287 QApplication::postEvent( data->self, new MsgEvent( item ) );
289 vlc_restorecancel( canc );