messages.cpp 7.82 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
#include <QTabWidget>
#include <QTreeWidget>
#include <QTreeWidgetItem>
ivoire's avatar
ivoire committed
41
#include <QHeaderView>
42
43
44

MessagesDialog *MessagesDialog::instance = NULL;

zorglub's avatar
zorglub committed
45
MessagesDialog::MessagesDialog( intf_thread_t *_p_intf) :  QVLCFrame( _p_intf )
46
{
47
    setWindowTitle( qtr( "Messages" ) );
48
    resize( 600, 450 );
49

50
51
52
53
54
    /* General widgets */
    QGridLayout *mainLayout = new QGridLayout( this );
    QTabWidget  *mainTab = new QTabWidget( this );
    mainTab->setTabPosition( QTabWidget::North );

55
    QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
56
57
58
59
60
61
62
63
64
65
66
    closeButton->setDefault( true );
    BUTTONACT( closeButton, close() );

    mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
    mainLayout->addWidget( closeButton, 1, 5 );


    /* Messages */
    QWidget     *msgWidget = new QWidget;
    QGridLayout *msgLayout = new QGridLayout( msgWidget );

67
68
    QPushButton *clearButton = new QPushButton( qtr( "&Clear" ) );
    QPushButton *saveLogButton = new QPushButton( qtr( "&Save as..." ) );
69

zorglub's avatar
zorglub committed
70
    verbosityBox = new QSpinBox();
71
72
73
    verbosityBox->setRange( 0, 2 );
    verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
    verbosityBox->setWrapping( true );
zorglub's avatar
zorglub committed
74
    verbosityBox->setMaximumWidth( 50 );
75

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

78
    messages = new QTextEdit();
79
80
81
    messages->setReadOnly( true );
    messages->setGeometry( 0, 0, 440, 600 );
    messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
82

83
84
85
86
87
88
    msgLayout->addWidget( messages, 0, 0, 1, 0 );
    msgLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
    msgLayout->addWidget( verbosityBox, 1, 1 );
    msgLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 1, 2 );
    msgLayout->addWidget( saveLogButton, 1, 3 );
    msgLayout->addWidget( clearButton, 1, 4 );
89

zorglub's avatar
zorglub committed
90
91
92
    BUTTONACT( clearButton, clear() );
    BUTTONACT( saveLogButton, save() );
    ON_TIMEOUT( updateLog() );
93

94
95
96
97
98
99
100
101
    mainTab->addTab( msgWidget, qtr( "Messages" ) );

    /* Module tree */
    QWidget     *treeWidget = new QWidget;
    QGridLayout *treeLayout = new QGridLayout( treeWidget );

    modulesTree = new QTreeWidget();
    modulesTree->setGeometry( 0, 0, 440, 600 );
ivoire's avatar
ivoire committed
102
    modulesTree->header()->hide();
103
104
105
106
107
108
109
110
111
112
113

    QPushButton *updateButton = new QPushButton( qtr( "&Update" ) );

    treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
    treeLayout->addWidget( updateButton, 1, 6 );

    BUTTONACT( updateButton, updateTree() );

    mainTab->addTab( treeWidget, qtr( "Modules tree" ) );


114
    readSettings( "Messages" );
115
116
117
118
119
120
121
122
123
124
125
126
127
}

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 )
    {
128
129
        messages->textCursor().movePosition( QTextCursor::End );

130
131
132
133
        for( i_start = p_sub->i_start;
                i_start != i_stop;
                i_start = (i_start+1) % VLC_MSG_QSIZE )
        {
zorglub's avatar
zorglub committed
134
135
136
137
138
139
140
            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 )
            {
141
142
                messages->setFontItalic( true );
                messages->setTextColor( "darkBlue" );
143
                messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
zorglub's avatar
zorglub committed
144
145
146
            }
            else
                continue;
147
148
149
150

            switch( p_sub->p_msg[i_start].i_type )
            {
                case VLC_MSG_INFO:
151
152
                    messages->setTextColor( "blue" );
                    messages->insertPlainText( " info: " );
153
154
                    break;
                case VLC_MSG_ERR:
155
156
                    messages->setTextColor( "red" );
                    messages->insertPlainText( " error: " );
157
158
                    break;
                case VLC_MSG_WARN:
159
160
                    messages->setTextColor( "green" );
                    messages->insertPlainText( " warning: " );
161
162
163
                    break;
                case VLC_MSG_DBG:
                default:
164
165
                    messages->setTextColor( "grey" );
                    messages->insertPlainText( " debug: " );
166
167
168
169
                    break;
            }

            /* Add message Regular black Font */
170
171
            messages->setFontItalic( false );
            messages->setTextColor( "black" );
zorglub's avatar
zorglub committed
172
            messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
zorglub's avatar
zorglub committed
173
            messages->insertPlainText( "\n" );
174
        }
zorglub's avatar
zorglub committed
175
        messages->ensureCursorVisible();
176
177
178
179
180
181
182

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

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
void MessagesDialog::buildTree( QTreeWidgetItem *parentItem, vlc_object_t *p_obj )
{
    vlc_object_yield( p_obj );
    QTreeWidgetItem *item;

    if( parentItem )
        item = new QTreeWidgetItem( parentItem );
    else
        item = new QTreeWidgetItem( modulesTree );

    if( p_obj->psz_object_name )
        item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" + qfu( p_obj->psz_object_name ) + "\" (" + QString::number(p_obj->i_object_id) + ")\n" );
    else
        item->setText( 0, qfu( p_obj->psz_object_type ) + " (" + QString::number(p_obj->i_object_id) + ")\n" );

    for( int i=0; i < p_obj->i_children; i++ )
    {
        buildTree( item, p_obj->pp_children[i]);
    }

    vlc_object_release( p_obj );
}

void MessagesDialog::updateTree()
{
    modulesTree->clear();

    buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
}

zorglub's avatar
zorglub committed
213
void MessagesDialog::close()
214
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
215
    hide();
216
217
}

zorglub's avatar
zorglub committed
218
void MessagesDialog::clear()
219
220
221
222
{
    messages->clear();
}

zorglub's avatar
zorglub committed
223
bool MessagesDialog::save()
224
225
{
    QString saveLogFileName = QFileDialog::getSaveFileName(
226
227
            this, qtr( "Choose a filename to save the logs under..." ),
            qfu( p_intf->p_libvlc->psz_homedir ),
228
            qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
229

zorglub's avatar
zorglub committed
230
    if( !saveLogFileName.isNull() )
231
    {
232
233
234
235
236
237
        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() ) );
238
239
240
            return false;
        }

241
        QTextStream out( &file );
242
243
244
245
246
247
        out << messages->toPlainText() << "\n";

        return true;
    }
    return false;
}