]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
46aa53b7aa5a89cdb7710d8813f872beb09602c7
[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 #include "dialogs_provider.hpp"
29
30 #include <QSpacerItem>
31 #include <QSpinBox>
32 #include <QLabel>
33 #include <QTextEdit>
34 #include <QTextCursor>
35 #include <QFileDialog>
36 #include <QTextStream>
37 #include <QMessageBox>
38 #include <QTabWidget>
39 #include <QTreeWidget>
40 #include <QTreeWidgetItem>
41 #include <QHeaderView>
42 #include <QMutex>
43
44 MessagesDialog *MessagesDialog::instance = NULL;
45
46 struct msg_cb_data_t
47 {
48     MessagesDialog *self;
49     QMutex          lock; /**< protects MessagesDialog::messages */
50 };
51
52 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
53                : QVLCFrame( _p_intf )
54 {
55     setWindowTitle( qtr( "Messages" ) );
56
57     /* General widgets */
58     QGridLayout *mainLayout = new QGridLayout( this );
59     mainTab = new QTabWidget( this );
60     mainTab->setTabPosition( QTabWidget::North );
61
62
63     /* Messages */
64     QWidget     *msgWidget = new QWidget;
65     QGridLayout *msgLayout = new QGridLayout( msgWidget );
66
67     messages = new QTextEdit();
68     messages->setReadOnly( true );
69     messages->setGeometry( 0, 0, 440, 600 );
70     messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
71
72     msgLayout->addWidget( messages, 0, 0, 1, 0 );
73     mainTab->addTab( msgWidget, qtr( "Messages" ) );
74
75
76     /* Modules tree */
77     QWidget     *treeWidget = new QWidget;
78     QGridLayout *treeLayout = new QGridLayout( treeWidget );
79
80     modulesTree = new QTreeWidget();
81     modulesTree->header()->hide();
82
83     treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
84     mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
85
86
87     /* Buttons and general layout */
88     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
89     closeButton->setDefault( true );
90     clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
91     saveLogButton = new QPushButton( qtr( "&Save as..." ) );
92     saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
93
94     verbosityBox = new QSpinBox();
95     verbosityBox->setRange( 0, 2 );
96     verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
97     verbosityBox->setWrapping( true );
98     verbosityBox->setMaximumWidth( 50 );
99
100     verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
101
102     mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
103     mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
104     mainLayout->addWidget( verbosityBox, 1, 1 );
105     mainLayout->setColumnStretch( 2, 10 );
106     mainLayout->addWidget( saveLogButton, 1, 3 );
107     mainLayout->addWidget( clearUpdateButton, 1, 4 );
108     mainLayout->addWidget( closeButton, 1, 5 );
109
110     BUTTONACT( closeButton, hide() );
111     BUTTONACT( clearUpdateButton, clearOrUpdate() );
112     BUTTONACT( saveLogButton, save() );
113     CONNECT( mainTab, currentChanged( int ),
114              this, updateTab( int ) );
115
116     /* General action */
117     readSettings( "Messages", QSize( 600, 450 ) );
118
119
120     /* Hook up to LibVLC messaging */
121     cb_data = new msg_cb_data_t;
122     cb_data->self = this;
123     sub = msg_Subscribe (_p_intf->p_libvlc, sinkMessage, cb_data);
124 }
125
126 MessagesDialog::~MessagesDialog ()
127 {
128     writeSettings( "messages" );
129     msg_Unsubscribe (sub);
130     delete cb_data;
131 };
132
133 void MessagesDialog::updateTab( int index )
134 {
135     /* Second tab : modules tree */
136     if( index == 1 )
137     {
138         verbosityLabel->hide();
139         verbosityBox->hide();
140         clearUpdateButton->setText( qtr( "&Update" ) );
141         saveLogButton->hide();
142         updateTree();
143     }
144     /* First tab : messages */
145     else
146     {
147         verbosityLabel->show();
148         verbosityBox->show();
149         clearUpdateButton->setText( qtr( "&Clear" ) );
150         saveLogButton->show();
151     }
152 }
153
154 void MessagesDialog::sinkMessage (msg_cb_data_t *data, msg_item_t *item,
155                                   unsigned overruns)
156 {
157     MessagesDialog *self = data->self;
158     QMutexLocker locker (&data->lock);
159
160     self->sinkMessage (item, overruns);
161 }
162
163 void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
164 {
165     if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
166      || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
167         return;
168
169     messages->textCursor().movePosition( QTextCursor::End );
170     messages->setFontItalic( true );
171     messages->setTextColor( "darkBlue" );
172     messages->insertPlainText( qfu( item->psz_module ) );
173
174     switch (item->i_type)
175     {
176         case VLC_MSG_INFO:
177             messages->setTextColor( "blue" );
178             messages->insertPlainText( " info: " );
179             break;
180         case VLC_MSG_ERR:
181             messages->setTextColor( "red" );
182             messages->insertPlainText( " error: " );
183             break;
184         case VLC_MSG_WARN:
185             messages->setTextColor( "green" );
186             messages->insertPlainText( " warning: " );
187             break;
188         case VLC_MSG_DBG:
189         default:
190             messages->setTextColor( "grey" );
191             messages->insertPlainText( " debug: " );
192             break;
193     }
194
195     /* Add message Regular black Font */
196     messages->setFontItalic( false );
197     messages->setTextColor( "black" );
198     messages->insertPlainText( qfu(item->psz_msg) );
199     messages->insertPlainText( "\n" );
200     messages->ensureCursorVisible();
201 }
202
203 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
204                                 vlc_object_t *p_obj )
205 {
206     QTreeWidgetItem *item;
207
208     if( parentItem )
209         item = new QTreeWidgetItem( parentItem );
210     else
211         item = new QTreeWidgetItem( modulesTree );
212
213     if( p_obj->psz_object_name )
214         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
215                        qfu( p_obj->psz_object_name ) + "\" (" +
216                        QString::number((uintptr_t)p_obj) + ")" );
217     else
218         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
219                        QString::number((uintptr_t)p_obj) + ")" );
220
221     item->setExpanded( true );
222
223     vlc_list_t *l = vlc_list_children( p_obj );
224     for( int i=0; i < l->i_count; i++ )
225         buildTree( item, l->p_values[i].p_object );
226     vlc_list_release( l );
227 }
228
229 void MessagesDialog::clearOrUpdate()
230 {
231     if( mainTab->currentIndex() )
232         updateTree();
233     else
234         clear();
235 }
236
237 void MessagesDialog::updateTree()
238 {
239     modulesTree->clear();
240     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
241 }
242
243 void MessagesDialog::clear()
244 {
245     QMutexLocker locker (&cb_data->lock);
246     messages->clear();
247 }
248
249 bool MessagesDialog::save()
250 {
251     QString saveLogFileName = QFileDialog::getSaveFileName(
252             this, qtr( "Save log file as..." ),
253             qfu( config_GetHomeDir() ),
254             qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
255
256     if( !saveLogFileName.isNull() )
257     {
258         QFile file( saveLogFileName );
259         if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
260             QMessageBox::warning( this, qtr( "Application" ),
261                     qtr( "Cannot write to file %1:\n%2." )
262                     .arg( saveLogFileName )
263                     .arg( file.errorString() ) );
264             return false;
265         }
266
267         QTextStream out( &file );
268         QMutexLocker locker (&cb_data->lock);
269         out << messages->toPlainText() << "\n";
270
271         return true;
272     }
273     return false;
274 }