]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
a056ab7bea42a7eb838e86ac3e88794c51a519d4
[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
39 MessagesDialog *MessagesDialog::instance = NULL;
40
41 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf) :  QVLCFrame( _p_intf )
42 {
43     setWindowTitle( qtr( "Messages" ) );
44     resize( 600, 450 );
45
46     QGridLayout *layout = new QGridLayout( this );
47     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
48     QPushButton *clearButton = new QPushButton( qtr( "&Clear" ) );
49     QPushButton *saveLogButton = new QPushButton( qtr( "&Save as..." ) );
50     closeButton->setDefault( true );
51
52     verbosityBox = new QSpinBox();
53     verbosityBox->setRange( 0, 2 );
54     verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
55     verbosityBox->setWrapping( true );
56     verbosityBox->setMaximumWidth( 50 );
57
58     QLabel *verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
59
60     messages = new QTextEdit();
61     messages->setReadOnly( true );
62     messages->setGeometry( 0, 0, 440, 600 );
63     messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
64
65     layout->addWidget( messages, 0, 0, 1, 0 );
66     layout->addWidget( verbosityLabel, 1, 0, 1, 1 );
67     layout->addWidget( verbosityBox, 1, 1 );
68     layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 1, 2 );
69     layout->addWidget( saveLogButton, 1, 3 );
70     layout->addWidget( clearButton, 1, 4 );
71     layout->addWidget( closeButton, 1, 5 );
72
73     BUTTONACT( closeButton, close() );
74     BUTTONACT( clearButton, clear() );
75     BUTTONACT( saveLogButton, save() );
76     ON_TIMEOUT( updateLog() );
77 }
78
79 MessagesDialog::~MessagesDialog()
80 {
81 }
82
83 void MessagesDialog::updateLog()
84 {
85     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
86     int i_start;
87
88     vlc_mutex_lock( p_sub->p_lock );
89     int i_stop = *p_sub->pi_stop;
90     vlc_mutex_unlock( p_sub->p_lock );
91
92     if( p_sub->i_start != i_stop )
93     {
94         messages->textCursor().movePosition( QTextCursor::End );
95
96         for( i_start = p_sub->i_start;
97                 i_start != i_stop;
98                 i_start = (i_start+1) % VLC_MSG_QSIZE )
99         {
100             if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
101                 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
102                 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
103                     verbosityBox->value() >= 1 ||
104                 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
105                     verbosityBox->value() >= 2 )
106             {
107                 messages->setFontItalic( true );
108                 messages->setTextColor( "darkBlue" );
109                 messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
110             }
111             else
112                 continue;
113
114             switch( p_sub->p_msg[i_start].i_type )
115             {
116                 case VLC_MSG_INFO:
117                     messages->setTextColor( "blue" );
118                     messages->insertPlainText( " info: " );
119                     break;
120                 case VLC_MSG_ERR:
121                     messages->setTextColor( "red" );
122                     messages->insertPlainText( " error: " );
123                     break;
124                 case VLC_MSG_WARN:
125                     messages->setTextColor( "green" );
126                     messages->insertPlainText( " warning: " );
127                     break;
128                 case VLC_MSG_DBG:
129                 default:
130                     messages->setTextColor( "grey" );
131                     messages->insertPlainText( " debug: " );
132                     break;
133             }
134
135             /* Add message Regular black Font */
136             messages->setFontItalic( false );
137             messages->setTextColor( "black" );
138             messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
139             messages->insertPlainText( "\n" );
140         }
141         messages->ensureCursorVisible();
142
143         vlc_mutex_lock( p_sub->p_lock );
144         p_sub->i_start = i_start;
145         vlc_mutex_unlock( p_sub->p_lock );
146     }
147 }
148
149 void MessagesDialog::close()
150 {
151     hide();
152 }
153
154 void MessagesDialog::clear()
155 {
156     messages->clear();
157 }
158
159 bool MessagesDialog::save()
160 {
161     QString saveLogFileName = QFileDialog::getSaveFileName(
162             this, qtr( "Choose a filename to save the logs under..." ),
163             qfu( p_intf->p_libvlc->psz_homedir ),
164             "Texts / Logs (*.log *.txt);; All (*.*) " );
165
166     if( !saveLogFileName.isNull() )
167     {
168         QFile file( saveLogFileName );
169         if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
170             QMessageBox::warning( this, qtr( "Application" ),
171                     qtr( "Cannot write file %1:\n%2." )
172                     .arg( saveLogFileName )
173                     .arg( file.errorString() ) );
174             return false;
175         }
176
177         QTextStream out( &file );
178         out << messages->toPlainText() << "\n";
179
180         return true;
181     }
182     return false;
183 }