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