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