]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
Add the tree of the modules loaded in the messages dialog.
[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
42 MessagesDialog *MessagesDialog::instance = NULL;
43
44 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf) :  QVLCFrame( _p_intf )
45 {
46     setWindowTitle( qtr( "Messages" ) );
47     resize( 600, 450 );
48
49     /* General widgets */
50     QGridLayout *mainLayout = new QGridLayout( this );
51     QTabWidget  *mainTab = new QTabWidget( this );
52     mainTab->setTabPosition( QTabWidget::North );
53
54     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
55     closeButton->setDefault( true );
56     BUTTONACT( closeButton, close() );
57
58     mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
59     mainLayout->addWidget( closeButton, 1, 5 );
60
61
62     /* Messages */
63     QWidget     *msgWidget = new QWidget;
64     QGridLayout *msgLayout = new QGridLayout( msgWidget );
65
66     QPushButton *clearButton = new QPushButton( qtr( "&Clear" ) );
67     QPushButton *saveLogButton = new QPushButton( qtr( "&Save as..." ) );
68
69     verbosityBox = new QSpinBox();
70     verbosityBox->setRange( 0, 2 );
71     verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
72     verbosityBox->setWrapping( true );
73     verbosityBox->setMaximumWidth( 50 );
74
75     QLabel *verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
76
77     messages = new QTextEdit();
78     messages->setReadOnly( true );
79     messages->setGeometry( 0, 0, 440, 600 );
80     messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
81
82     msgLayout->addWidget( messages, 0, 0, 1, 0 );
83     msgLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
84     msgLayout->addWidget( verbosityBox, 1, 1 );
85     msgLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 1, 2 );
86     msgLayout->addWidget( saveLogButton, 1, 3 );
87     msgLayout->addWidget( clearButton, 1, 4 );
88
89     BUTTONACT( clearButton, clear() );
90     BUTTONACT( saveLogButton, save() );
91     ON_TIMEOUT( updateLog() );
92
93     mainTab->addTab( msgWidget, qtr( "Messages" ) );
94
95     /* Module tree */
96     QWidget     *treeWidget = new QWidget;
97     QGridLayout *treeLayout = new QGridLayout( treeWidget );
98
99     modulesTree = new QTreeWidget();
100     modulesTree->setGeometry( 0, 0, 440, 600 );
101
102     QPushButton *updateButton = new QPushButton( qtr( "&Update" ) );
103
104     treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
105     treeLayout->addWidget( updateButton, 1, 6 );
106
107     BUTTONACT( updateButton, updateTree() );
108
109     mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
110
111
112     readSettings( "Messages" );
113 }
114
115 void MessagesDialog::updateLog()
116 {
117     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
118     int i_start;
119
120     vlc_mutex_lock( p_sub->p_lock );
121     int i_stop = *p_sub->pi_stop;
122     vlc_mutex_unlock( p_sub->p_lock );
123
124     if( p_sub->i_start != i_stop )
125     {
126         messages->textCursor().movePosition( QTextCursor::End );
127
128         for( i_start = p_sub->i_start;
129                 i_start != i_stop;
130                 i_start = (i_start+1) % VLC_MSG_QSIZE )
131         {
132             if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
133                 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
134                 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
135                     verbosityBox->value() >= 1 ||
136                 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
137                     verbosityBox->value() >= 2 )
138             {
139                 messages->setFontItalic( true );
140                 messages->setTextColor( "darkBlue" );
141                 messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
142             }
143             else
144                 continue;
145
146             switch( p_sub->p_msg[i_start].i_type )
147             {
148                 case VLC_MSG_INFO:
149                     messages->setTextColor( "blue" );
150                     messages->insertPlainText( " info: " );
151                     break;
152                 case VLC_MSG_ERR:
153                     messages->setTextColor( "red" );
154                     messages->insertPlainText( " error: " );
155                     break;
156                 case VLC_MSG_WARN:
157                     messages->setTextColor( "green" );
158                     messages->insertPlainText( " warning: " );
159                     break;
160                 case VLC_MSG_DBG:
161                 default:
162                     messages->setTextColor( "grey" );
163                     messages->insertPlainText( " debug: " );
164                     break;
165             }
166
167             /* Add message Regular black Font */
168             messages->setFontItalic( false );
169             messages->setTextColor( "black" );
170             messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
171             messages->insertPlainText( "\n" );
172         }
173         messages->ensureCursorVisible();
174
175         vlc_mutex_lock( p_sub->p_lock );
176         p_sub->i_start = i_start;
177         vlc_mutex_unlock( p_sub->p_lock );
178     }
179 }
180
181 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem, vlc_object_t *p_obj )
182 {
183     vlc_object_yield( p_obj );
184     QTreeWidgetItem *item;
185
186     if( parentItem )
187         item = new QTreeWidgetItem( parentItem );
188     else
189         item = new QTreeWidgetItem( modulesTree );
190
191     if( p_obj->psz_object_name )
192         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" + qfu( p_obj->psz_object_name ) + "\" (" + QString::number(p_obj->i_object_id) + ")\n" );
193     else
194         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" + QString::number(p_obj->i_object_id) + ")\n" );
195
196     for( int i=0; i < p_obj->i_children; i++ )
197     {
198         buildTree( item, p_obj->pp_children[i]);
199     }
200
201     vlc_object_release( p_obj );
202 }
203
204 void MessagesDialog::updateTree()
205 {
206     modulesTree->clear();
207
208     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
209 }
210
211 void MessagesDialog::close()
212 {
213     hide();
214 }
215
216 void MessagesDialog::clear()
217 {
218     messages->clear();
219 }
220
221 bool MessagesDialog::save()
222 {
223     QString saveLogFileName = QFileDialog::getSaveFileName(
224             this, qtr( "Choose a filename to save the logs under..." ),
225             qfu( p_intf->p_libvlc->psz_homedir ),
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 file %1:\n%2." )
234                     .arg( saveLogFileName )
235                     .arg( file.errorString() ) );
236             return false;
237         }
238
239         QTextStream out( &file );
240         out << messages->toPlainText() << "\n";
241
242         return true;
243     }
244     return false;
245 }