messages.cpp 6.01 KB
Newer Older
1
2
3
/*****************************************************************************
 * Messages.cpp : Information about an item
 ****************************************************************************
4
 * Copyright (C) 2006-2007 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
23
24
25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26
27
28

#include "dialogs/messages.hpp"
#include "dialogs_provider.hpp"
29

zorglub's avatar
zorglub committed
30
31
32
33
34
35
36
37
#include <QSpacerItem>
#include <QSpinBox>
#include <QLabel>
#include <QTextEdit>
#include <QTextCursor>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
38
39
40

MessagesDialog *MessagesDialog::instance = NULL;

zorglub's avatar
zorglub committed
41
MessagesDialog::MessagesDialog( intf_thread_t *_p_intf) :  QVLCFrame( _p_intf )
42
{
43
    setWindowTitle( qtr( "Messages" ) );
44
    resize( 600, 450 );
45

46
47
48
49
    QGridLayout *layout = new QGridLayout( this );
    QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
    QPushButton *clearButton = new QPushButton( qtr( "&Clear" ) );
    QPushButton *saveLogButton = new QPushButton( qtr( "&Save as..." ) );
50
51
    closeButton->setDefault( true );

zorglub's avatar
zorglub committed
52
    verbosityBox = new QSpinBox();
53
54
55
    verbosityBox->setRange( 0, 2 );
    verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
    verbosityBox->setWrapping( true );
zorglub's avatar
zorglub committed
56
    verbosityBox->setMaximumWidth( 50 );
57

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
58
    QLabel *verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
59

60
    messages = new QTextEdit();
61
62
63
    messages->setReadOnly( true );
    messages->setGeometry( 0, 0, 440, 600 );
    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
70
71
    layout->addWidget( saveLogButton, 1, 3 );
    layout->addWidget( clearButton, 1, 4 );
    layout->addWidget( closeButton, 1, 5 );
72

zorglub's avatar
zorglub committed
73
74
75
76
    BUTTONACT( closeButton, close() );
    BUTTONACT( clearButton, clear() );
    BUTTONACT( saveLogButton, save() );
    ON_TIMEOUT( updateLog() );
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}

MessagesDialog::~MessagesDialog()
{
}

void MessagesDialog::updateLog()
{
    msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
    int i_start;

    vlc_mutex_lock( p_sub->p_lock );
    int i_stop = *p_sub->pi_stop;
    vlc_mutex_unlock( p_sub->p_lock );

    if( p_sub->i_start != i_stop )
    {
94
95
        messages->textCursor().movePosition( QTextCursor::End );

96
97
98
99
        for( i_start = p_sub->i_start;
                i_start != i_stop;
                i_start = (i_start+1) % VLC_MSG_QSIZE )
        {
zorglub's avatar
zorglub committed
100
101
102
103
104
105
106
            if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
                p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
                p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
                    verbosityBox->value() >= 1 ||
                p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
                    verbosityBox->value() >= 2 )
            {
107
108
                messages->setFontItalic( true );
                messages->setTextColor( "darkBlue" );
109
                messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
zorglub's avatar
zorglub committed
110
111
112
            }
            else
                continue;
113
114
115
116

            switch( p_sub->p_msg[i_start].i_type )
            {
                case VLC_MSG_INFO:
117
118
                    messages->setTextColor( "blue" );
                    messages->insertPlainText( " info: " );
119
120
                    break;
                case VLC_MSG_ERR:
121
122
                    messages->setTextColor( "red" );
                    messages->insertPlainText( " error: " );
123
124
                    break;
                case VLC_MSG_WARN:
125
126
                    messages->setTextColor( "green" );
                    messages->insertPlainText( " warning: " );
127
128
129
                    break;
                case VLC_MSG_DBG:
                default:
130
131
                    messages->setTextColor( "grey" );
                    messages->insertPlainText( " debug: " );
132
133
134
135
                    break;
            }

            /* Add message Regular black Font */
136
137
            messages->setFontItalic( false );
            messages->setTextColor( "black" );
zorglub's avatar
zorglub committed
138
            messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
zorglub's avatar
zorglub committed
139
            messages->insertPlainText( "\n" );
140
        }
zorglub's avatar
zorglub committed
141
        messages->ensureCursorVisible();
142
143
144
145
146
147
148

        vlc_mutex_lock( p_sub->p_lock );
        p_sub->i_start = i_start;
        vlc_mutex_unlock( p_sub->p_lock );
    }
}

zorglub's avatar
zorglub committed
149
void MessagesDialog::close()
150
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
151
    hide();
152
153
}

zorglub's avatar
zorglub committed
154
void MessagesDialog::clear()
155
156
157
158
{
    messages->clear();
}

zorglub's avatar
zorglub committed
159
bool MessagesDialog::save()
160
161
{
    QString saveLogFileName = QFileDialog::getSaveFileName(
162
163
            this, qtr( "Choose a filename to save the logs under..." ),
            qfu( p_intf->p_libvlc->psz_homedir ),
164
            qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
165

zorglub's avatar
zorglub committed
166
    if( !saveLogFileName.isNull() )
167
    {
168
169
170
171
172
173
        QFile file( saveLogFileName );
        if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
            QMessageBox::warning( this, qtr( "Application" ),
                    qtr( "Cannot write file %1:\n%2." )
                    .arg( saveLogFileName )
                    .arg( file.errorString() ) );
174
175
176
            return false;
        }

177
        QTextStream out( &file );
178
179
180
181
182
183
        out << messages->toPlainText() << "\n";

        return true;
    }
    return false;
}