]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
Merge branch 'master' into lpcm_encoder
[vlc] / modules / gui / qt4 / dialogs / messages.cpp
1 /*****************************************************************************
2  * Messages.cpp : Information about an item
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "dialogs/messages.hpp"
28
29 #include <QSpinBox>
30 #include <QLabel>
31 #include <QTextEdit>
32 #include <QTextCursor>
33 #include <QFileDialog>
34 #include <QTextStream>
35 #include <QMessageBox>
36 #include <QTabWidget>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItem>
39 #include <QHeaderView>
40 #include <QMutex>
41 #include <QLineEdit>
42 #include <QPushButton>
43 #include <QScrollBar>
44
45 #include <assert.h>
46
47 enum {
48     MsgEvent_Type = QEvent::User + MsgEventType + 1,
49 };
50
51 class MsgEvent : public QEvent
52 {
53 public:
54     MsgEvent( msg_item_t *msg )
55         : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
56     {
57         msg_Hold( msg );
58     }
59     virtual ~MsgEvent()
60     {
61         msg_Release( msg );
62     }
63
64     msg_item_t *msg;
65 };
66
67 struct msg_cb_data_t
68 {
69     MessagesDialog *self;
70 };
71 static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
72
73 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
74                : QVLCFrame( _p_intf )
75 {
76     setWindowTitle( qtr( "Messages" ) );
77     setWindowRole( "vlc-messages" );
78
79     /* Build Ui */
80     ui.setupUi( this );
81     updateTree();
82
83     /* Modules tree */
84     ui.modulesTree->header()->hide();
85
86     /* Buttons and general layout */
87     ui.saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
88
89     ui.verbosityBox->setValue( var_InheritInteger( p_intf, "verbose" ) );
90
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)");
95
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 ) );
103
104     /* General action */
105     readSettings( "Messages", QSize( 600, 450 ) );
106
107     /* Hook up to LibVLC messaging */
108     cbData = new msg_cb_data_t;
109     cbData->self = this;
110     sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
111     changeVerbosity( ui.verbosityBox->value() );
112 }
113
114 MessagesDialog::~MessagesDialog()
115 {
116     writeSettings( "Messages" );
117     msg_Unsubscribe( sub );
118     delete cbData;
119 };
120
121 void MessagesDialog::changeVerbosity( int verbosity )
122 {
123     msg_SubscriptionSetVerbosity( sub , verbosity );
124 }
125
126 void MessagesDialog::updateConfig()
127 {
128     config_PutPsz(p_intf, "verbose-objects", qtu(ui.vbobjectsEdit->text()));
129     //vbobjectsEdit->setText("vbEdit changed!");
130
131     if( !ui.vbobjectsEdit->text().isEmpty() )
132     {
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, "," )) )
139         {
140             switch( psz_object[0] )
141             {
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;
147              }
148         }
149         free( psz_verbose_objects );
150     }
151     else
152     {
153         msg_EnableObjectPrinting( p_intf, "all");
154     }
155 }
156
157 void MessagesDialog::sinkMessage( msg_item_t *item )
158 {
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() );
165
166     /* Copy selected text to the clipboard */
167     if( messages->textCursor().hasSelection() )
168         messages->copy();
169
170     /* Fix selected text bug */
171     if( !messages->textCursor().atEnd() ||
172          messages->textCursor().anchor() != messages->textCursor().position() )
173          messages->moveCursor( QTextCursor::End );
174
175     messages->setFontItalic( true );
176     messages->setTextColor( "darkBlue" );
177     messages->insertPlainText( qfu( item->psz_module ) );
178
179     switch (item->i_type)
180     {
181         case VLC_MSG_INFO:
182             messages->setTextColor( "blue" );
183             messages->insertPlainText( " info: " );
184             break;
185         case VLC_MSG_ERR:
186             messages->setTextColor( "red" );
187             messages->insertPlainText( " error: " );
188             break;
189         case VLC_MSG_WARN:
190             messages->setTextColor( "green" );
191             messages->insertPlainText( " warning: " );
192             break;
193         case VLC_MSG_DBG:
194         default:
195             messages->setTextColor( "grey" );
196             messages->insertPlainText( " debug: " );
197             break;
198     }
199
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();
206 }
207
208 void MessagesDialog::customEvent( QEvent *event )
209 {
210     MsgEvent *msge = static_cast<MsgEvent *>(event);
211
212     assert( msge );
213     sinkMessage( msge->msg );
214 }
215
216 void MessagesDialog::clear()
217 {
218     ui.messages->clear();
219 }
220
221 bool MessagesDialog::save()
222 {
223     QString saveLogFileName = QFileDialog::getSaveFileName(
224             this, qtr( "Save log file as..." ),
225             QVLCUserDir( VLC_DOCUMENTS_DIR ),
226             qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
227
228     if( !saveLogFileName.isNull() )
229     {
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() ) );
236             return false;
237         }
238
239         QTextStream out( &file );
240         out << ui.messages->toPlainText() << "\n";
241
242         return true;
243     }
244     return false;
245 }
246
247 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
248                                 vlc_object_t *p_obj )
249 {
250     QTreeWidgetItem *item;
251
252     if( parentItem )
253         item = new QTreeWidgetItem( parentItem );
254     else
255         item = new QTreeWidgetItem( ui.modulesTree );
256
257     char *name = vlc_object_get_name( p_obj );
258     if( name != NULL )
259     {
260         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
261                        qfu( name ) + "\" (" +
262                        QString::number((uintptr_t)p_obj) + ")" );
263         free( name );
264     }
265     else
266         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
267                        QString::number((uintptr_t)p_obj) + ")" );
268
269     item->setExpanded( true );
270
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 );
275 }
276
277 void MessagesDialog::updateTree()
278 {
279     ui.modulesTree->clear();
280     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
281 }
282
283 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
284 {
285     int canc = vlc_savecancel();
286
287     QApplication::postEvent( data->self, new MsgEvent( item ) );
288
289     vlc_restorecancel( canc );
290 }
291