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