messages.cpp 8.44 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;

45 46
MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
               : QVLCFrame( _p_intf )
47
{
48
    setWindowTitle( qtr( "Messages" ) );
49

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

55

56 57 58 59
    /* Messages */
    QWidget     *msgWidget = new QWidget;
    QGridLayout *msgLayout = new QGridLayout( msgWidget );

60
    messages = new QTextEdit();
61 62 63
    messages->setReadOnly( true );
    messages->setGeometry( 0, 0, 440, 600 );
    messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
64

65
    msgLayout->addWidget( messages, 0, 0, 1, 0 );
66
    mainTab->addTab( msgWidget, qtr( "Messages" ) );
zorglub's avatar
zorglub committed
67
    ON_TIMEOUT( updateLog() );
68

69

70
    /* Modules tree */
71 72 73 74
    QWidget     *treeWidget = new QWidget;
    QGridLayout *treeLayout = new QGridLayout( treeWidget );

    modulesTree = new QTreeWidget();
ivoire's avatar
ivoire committed
75
    modulesTree->header()->hide();
76 77 78 79

    treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
    mainTab->addTab( treeWidget, qtr( "Modules tree" ) );

80

81 82 83
    /* Buttons and general layout */
    QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
    closeButton->setDefault( true );
84
    clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
85
    saveLogButton = new QPushButton( qtr( "&Save as..." ) );
86
    saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
87 88 89 90 91 92 93 94 95 96 97 98

    verbosityBox = new QSpinBox();
    verbosityBox->setRange( 0, 2 );
    verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
    verbosityBox->setWrapping( true );
    verbosityBox->setMaximumWidth( 50 );

    verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );

    mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
    mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
    mainLayout->addWidget( verbosityBox, 1, 1 );
99
    mainLayout->setColumnStretch( 2, 10 );
100
    mainLayout->addWidget( saveLogButton, 1, 3 );
101
    mainLayout->addWidget( clearUpdateButton, 1, 4 );
102 103 104
    mainLayout->addWidget( closeButton, 1, 5 );

    BUTTONACT( closeButton, hide() );
105
    BUTTONACT( clearUpdateButton, clearOrUpdate() );
106 107 108
    BUTTONACT( saveLogButton, save() );
    CONNECT( mainTab, currentChanged( int ),
             this, updateTab( int ) );
109

110
    /* General action */
111
    readSettings( "Messages", QSize( 600, 450 ) );
112 113
}

114 115
void MessagesDialog::updateTab( int index )
{
116
    /* Second tab : modules tree */
117 118 119 120
    if( index == 1 )
    {
        verbosityLabel->hide();
        verbosityBox->hide();
121
        clearUpdateButton->setText( qtr( "&Update" ) );
122 123 124
        saveLogButton->hide();
        updateTree();
    }
125
    /* First tab : messages */
126 127 128 129
    else
    {
        verbosityLabel->show();
        verbosityBox->show();
130
        clearUpdateButton->setText( qtr( "&Clear" ) );
131 132 133 134
        saveLogButton->show();
    }
}

135 136
void MessagesDialog::updateLog()
{
137
#if 0
138 139 140 141 142 143 144 145 146
    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 )
    {
147 148
        messages->textCursor().movePosition( QTextCursor::End );

149 150 151 152
        for( i_start = p_sub->i_start;
                i_start != i_stop;
                i_start = (i_start+1) % VLC_MSG_QSIZE )
        {
zorglub's avatar
zorglub committed
153 154 155 156 157 158 159
            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 )
            {
160 161
                messages->setFontItalic( true );
                messages->setTextColor( "darkBlue" );
162
                messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
zorglub's avatar
zorglub committed
163 164 165
            }
            else
                continue;
166 167 168 169

            switch( p_sub->p_msg[i_start].i_type )
            {
                case VLC_MSG_INFO:
170 171
                    messages->setTextColor( "blue" );
                    messages->insertPlainText( " info: " );
172 173
                    break;
                case VLC_MSG_ERR:
174 175
                    messages->setTextColor( "red" );
                    messages->insertPlainText( " error: " );
176 177
                    break;
                case VLC_MSG_WARN:
178 179
                    messages->setTextColor( "green" );
                    messages->insertPlainText( " warning: " );
180 181 182
                    break;
                case VLC_MSG_DBG:
                default:
183 184
                    messages->setTextColor( "grey" );
                    messages->insertPlainText( " debug: " );
185 186 187 188
                    break;
            }

            /* Add message Regular black Font */
189 190
            messages->setFontItalic( false );
            messages->setTextColor( "black" );
zorglub's avatar
zorglub committed
191
            messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
zorglub's avatar
zorglub committed
192
            messages->insertPlainText( "\n" );
193
        }
zorglub's avatar
zorglub committed
194
        messages->ensureCursorVisible();
195 196 197 198 199

        vlc_mutex_lock( p_sub->p_lock );
        p_sub->i_start = i_start;
        vlc_mutex_unlock( p_sub->p_lock );
    }
200
#endif
201 202
}

203 204
void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
                                vlc_object_t *p_obj )
205 206 207 208 209 210 211 212 213
{
    QTreeWidgetItem *item;

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

    if( p_obj->psz_object_name )
214 215
        item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
                       qfu( p_obj->psz_object_name ) + "\" (" +
216
                       QString::number((uintptr_t)p_obj) + ")" );
217
    else
218
        item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
219
                       QString::number((uintptr_t)p_obj) + ")" );
220

221 222
    item->setExpanded( true );

223 224 225
    vlc_list_t *l = vlc_list_children( p_obj );
    for( int i=0; i < l->i_count; i++ )
        buildTree( item, l->p_values[i].p_object );
226
    vlc_list_release( l );
227 228
}

229 230 231 232 233 234 235 236
void MessagesDialog::clearOrUpdate()
{
    if( mainTab->currentIndex() )
        updateTree();
    else
        clear();
}

237 238 239 240 241 242
void MessagesDialog::updateTree()
{
    modulesTree->clear();
    buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
}

zorglub's avatar
zorglub committed
243
void MessagesDialog::clear()
244 245 246 247
{
    messages->clear();
}

zorglub's avatar
zorglub committed
248
bool MessagesDialog::save()
249 250
{
    QString saveLogFileName = QFileDialog::getSaveFileName(
251
            this, qtr( "Save log file as..." ),
252
            qfu( config_GetHomeDir() ),
253
            qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
254

zorglub's avatar
zorglub committed
255
    if( !saveLogFileName.isNull() )
256
    {
257 258 259
        QFile file( saveLogFileName );
        if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
            QMessageBox::warning( this, qtr( "Application" ),
260
                    qtr( "Cannot write to file %1:\n%2." )
261 262
                    .arg( saveLogFileName )
                    .arg( file.errorString() ) );
263 264 265
            return false;
        }

266
        QTextStream out( &file );
267 268 269 270 271 272
        out << messages->toPlainText() << "\n";

        return true;
    }
    return false;
}