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