]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
Enable verbosity setting
[vlc] / modules / gui / qt4 / dialogs / messages.cpp
1 /*****************************************************************************
2  * Messages.cpp : Information about an item
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: Messages.cpp 16024 2006-07-13 13:51:05Z xtophe $
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( _("Messages" ) );
42     resize(600, 400);
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     verbosityBox = new QSpinBox();
49     verbosityBox->setRange(0, 2);
50     verbosityBox->setValue(config_GetInt(p_intf, "verbose"));
51     verbosityBox->setWrapping(true);
52     verbosityBox->setMaximumWidth( 50 );
53     QLabel *verbosityLabel = new QLabel(qtr("Verbosity Level"));
54     messages = new QTextEdit();
55     messages->setReadOnly(true);
56     messages->setGeometry(0, 0, 440, 600);
57     messages->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58
59     layout->addWidget(messages, 0, 0, 1, 0);
60     layout->addWidget(verbosityLabel, 1, 0, 1,1 );
61     layout->addWidget(verbosityBox, 1, 1 );
62     layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 1,2 );
63     layout->addWidget(saveLogButton, 1, 3 );
64     layout->addWidget(clearButton, 1, 4 );
65     layout->addWidget(closeButton, 1, 5 );
66
67     connect( closeButton, SIGNAL( clicked() ) ,
68            this, SLOT( onCloseButton()));
69     connect( clearButton, SIGNAL( clicked() ) ,
70            this, SLOT( onClearButton()));
71     connect( saveLogButton, SIGNAL( clicked() ) ,
72            this, SLOT( onSaveButton()));
73     connect( DialogsProvider::getInstance(NULL)->fixed_timer,
74              SIGNAL( timeout() ), this, SLOT(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     messages->textCursor().movePosition( QTextCursor::End );
91     if( p_sub->i_start != i_stop )
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           // [FIXME] Does not work as the old one
98           // Outputs too much data ?
99           // if (p_sub->p_msg[i_start].i_type = VLC_MSG_ERR)
100           //          continue;
101           //  if( !b_verbose &&
102           //         VLC_MSG_ERR != p_sub->p_msg[i_start].i_type )
103           //                continue;
104
105             /* Append all messages to log window */
106
107             if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
108                 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
109                 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
110                     verbosityBox->value() >= 1 ||
111                 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
112                     verbosityBox->value() >= 2 )
113             {
114                 messages->setFontItalic(true);
115                 messages->setTextColor("darkBlue");
116                 messages->insertPlainText(p_sub->p_msg[i_start].psz_module);
117             }
118             else
119                 continue;
120
121             switch( p_sub->p_msg[i_start].i_type )
122             {
123                 case VLC_MSG_INFO:
124                     messages->setTextColor("blue");
125                     messages->insertPlainText(" info: ");
126                     break;
127                 case VLC_MSG_ERR:
128                     messages->setTextColor("red");
129                     messages->insertPlainText(" error: ");
130                     break;
131                 case VLC_MSG_WARN:
132                     messages->setTextColor("green");
133                     messages->insertPlainText(" warning: ");
134                     break;
135                 case VLC_MSG_DBG:
136                 default:
137                     messages->setTextColor("grey");
138                     messages->insertPlainText(" debug: ");
139                     break;
140             }
141
142             /* Add message Regular black Font */
143             messages->setFontItalic(false);
144             messages->setTextColor("black");
145             messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
146             messages->insertPlainText( "\n" );
147         }
148         messages->ensureCursorVisible();
149
150         vlc_mutex_lock( p_sub->p_lock );
151         p_sub->i_start = i_start;
152         vlc_mutex_unlock( p_sub->p_lock );
153     }
154 }
155
156 void MessagesDialog::onCloseButton()
157 {
158     this->toggleVisible();
159 }
160
161 void MessagesDialog::onClearButton()
162 {
163     messages->clear();
164 }
165
166 bool MessagesDialog::onSaveButton()
167 {
168     QString saveLogFileName = QFileDialog::getSaveFileName(
169             this,
170             "Choose a filename to save the logs under...",
171             p_intf->p_vlc->psz_homedir,
172             "Texts / Logs (*.log *.txt);; All (*.*) ");
173
174     if (saveLogFileName != NULL)
175     {
176         QFile file(saveLogFileName);
177         if (!file.open(QFile::WriteOnly | QFile::Text)) {
178             QMessageBox::warning(this, qtr("Application"),
179                     qtr("Cannot write file %1:\n%2.")
180                     .arg(saveLogFileName)
181                     .arg(file.errorString()));
182             return false;
183         }
184
185         QTextStream out(&file);
186         out << messages->toPlainText() << "\n";
187
188         return true;
189     }
190     return false;
191 }