extensions.cpp 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*****************************************************************************
 * extensions.cpp: Extensions manager for Qt: dialogs manager
 ****************************************************************************
 * Copyright (C) 2009-2010 VideoLAN and authors
 * $Id$
 *
 * Authors: Jean-Philippe André < jpeg # 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.
 *****************************************************************************/

#include "extensions.hpp"
25
#include "extensions_manager.hpp" // for isUnloading()
26 27 28 29 30 31 32 33 34

#include <vlc_dialog.h>

#include <QGridLayout>
#include <QPushButton>
#include <QSignalMapper>
#include <QLabel>
#include <QPixmap>
#include <QLineEdit>
35
#include <QTextBrowser>
36 37 38 39
#include <QCheckBox>
#include <QListWidget>
#include <QComboBox>
#include <QCloseEvent>
40
#include <QCoreApplication>
41
#include <QKeyEvent>
42
#include "util/customwidgets.hpp"
43 44 45

ExtensionsDialogProvider *ExtensionsDialogProvider::instance = NULL;

46 47
static void DialogCallback( extension_dialog_t *p_ext_dialog,
                            void *p_data );
48 49 50 51 52 53


ExtensionsDialogProvider::ExtensionsDialogProvider( intf_thread_t *_p_intf,
                                                    extensions_manager_t *p_mgr )
        : QObject( NULL ), p_intf( _p_intf ), p_extensions_manager( p_mgr )
{
54
    vlc_dialog_provider_set_ext_callback( p_intf, DialogCallback, NULL );
55 56

    CONNECT( this, SignalDialog( extension_dialog_t* ),
57
             this, UpdateExtDialog( extension_dialog_t* ) );
58 59 60 61
}

ExtensionsDialogProvider::~ExtensionsDialogProvider()
{
62
    msg_Dbg( p_intf, "ExtensionsDialogProvider is quitting..." );
63
    vlc_dialog_provider_set_ext_callback( p_intf, NULL, NULL );
64 65 66 67
}

/** Create a dialog
 * Note: Lock on p_dialog->lock must be held. */
68 69
ExtensionDialog* ExtensionsDialogProvider::CreateExtDialog(
        extension_dialog_t *p_dialog )
70 71 72 73 74 75
{
    ExtensionDialog *dialog = new ExtensionDialog( p_intf,
                                                   p_extensions_manager,
                                                   p_dialog );
    p_dialog->p_sys_intf = (void*) dialog;
    CONNECT( dialog, destroyDialog( extension_dialog_t* ),
76
             this, DestroyExtDialog( extension_dialog_t* ) );
77 78 79 80 81
    return dialog;
}

/** Destroy a dialog
 * Note: Lock on p_dialog->lock must be held. */
82
int ExtensionsDialogProvider::DestroyExtDialog( extension_dialog_t *p_dialog )
83 84 85 86 87 88 89
{
    assert( p_dialog );
    ExtensionDialog *dialog = ( ExtensionDialog* ) p_dialog->p_sys_intf;
    if( !dialog )
        return VLC_EGENERIC;
    delete dialog;
    p_dialog->p_sys_intf = NULL;
90
    vlc_cond_signal( &p_dialog->cond );
91 92 93 94 95 96
    return VLC_SUCCESS;
}

/**
 * Update/Create/Destroy a dialog
 **/
97 98
ExtensionDialog* ExtensionsDialogProvider::UpdateExtDialog(
        extension_dialog_t *p_dialog )
99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
    assert( p_dialog );

    ExtensionDialog *dialog = ( ExtensionDialog* ) p_dialog->p_sys_intf;
    if( p_dialog->b_kill && !dialog )
    {
        /* This extension could not be activated properly but tried
           to create a dialog. We must ignore it. */
        return NULL;
    }

    vlc_mutex_lock( &p_dialog->lock );
    if( !p_dialog->b_kill && !dialog )
    {
113
        dialog = CreateExtDialog( p_dialog );
114
        dialog->setVisible( !p_dialog->b_hide );
115
        dialog->has_lock = false;
116 117 118 119 120
    }
    else if( !p_dialog->b_kill && dialog )
    {
        dialog->has_lock = true;
        dialog->UpdateWidgets();
121
        if( strcmp( qtu( dialog->windowTitle() ),
122 123
                    p_dialog->psz_title ) != 0 )
            dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
124
        dialog->has_lock = false;
125
        dialog->setVisible( !p_dialog->b_hide );
126 127 128
    }
    else if( p_dialog->b_kill )
    {
129
        DestroyExtDialog( p_dialog );
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    }
    vlc_cond_signal( &p_dialog->cond );
    vlc_mutex_unlock( &p_dialog->lock );
    return dialog;
}

/**
 * Ask the dialog manager to create/update/kill the dialog. Thread-safe.
 **/
void ExtensionsDialogProvider::ManageDialog( extension_dialog_t *p_dialog )
{
    assert( p_dialog );
    ExtensionsManager *extMgr = ExtensionsManager::getInstance( p_intf );
    assert( extMgr != NULL );
    if( !extMgr->isUnloading() )
        emit SignalDialog( p_dialog ); // Safe because we signal Qt thread
    else
147
        UpdateExtDialog( p_dialog ); // This is safe, we're already in Qt thread
148 149 150 151 152
}

/**
 * Ask the dialogs provider to create a new dialog
 **/
153 154
static void DialogCallback( extension_dialog_t *p_ext_dialog,
                            void *p_data )
155
{
156
    (void) p_data;
157 158

    ExtensionsDialogProvider *p_edp = ExtensionsDialogProvider::getInstance();
159 160
    if( p_edp )
        p_edp->ManageDialog( p_ext_dialog );
161 162 163 164 165 166 167
}


ExtensionDialog::ExtensionDialog( intf_thread_t *_p_intf,
                                  extensions_manager_t *p_mgr,
                                  extension_dialog_t *_p_dialog )
         : QDialog( NULL ), p_intf( _p_intf ), p_extensions_manager( p_mgr )
168
         , p_dialog( _p_dialog ), has_lock(true)
169 170
{
    assert( p_dialog );
171
    CONNECT( ExtensionsDialogProvider::getInstance(), destroyed(),
172
             this, parentDestroyed() );
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    msg_Dbg( p_intf, "Creating a new dialog: '%s'", p_dialog->psz_title );
    this->setWindowFlags( Qt::WindowMinMaxButtonsHint
                        | Qt::WindowCloseButtonHint );
    this->setWindowTitle( qfu( p_dialog->psz_title ) );

    layout = new QGridLayout( this );
    clickMapper = new QSignalMapper( this );
    CONNECT( clickMapper, mapped( QObject* ), this, TriggerClick( QObject* ) );
    inputMapper = new QSignalMapper( this );
    CONNECT( inputMapper, mapped( QObject* ), this, SyncInput( QObject* ) );
    selectMapper = new QSignalMapper( this );
    CONNECT( selectMapper, mapped( QObject* ), this, SyncSelection(QObject*) );

    UpdateWidgets();
}

ExtensionDialog::~ExtensionDialog()
{
    msg_Dbg( p_intf, "Deleting extension dialog '%s'", qtu(windowTitle()) );
}

QWidget* ExtensionDialog::CreateWidget( extension_widget_t *p_widget )
{
    QLabel *label = NULL;
    QPushButton *button = NULL;
199
    QTextBrowser *textArea = NULL;
200 201 202 203
    QLineEdit *textInput = NULL;
    QCheckBox *checkBox = NULL;
    QComboBox *comboBox = NULL;
    QListWidget *list = NULL;
204
    SpinningIcon *spinIcon = NULL;
205 206 207 208 209 210 211 212 213 214
    struct extension_widget_t::extension_widget_value_t *p_value = NULL;

    assert( p_widget->p_sys_intf == NULL );

    switch( p_widget->type )
    {
        case EXTENSION_WIDGET_LABEL:
            label = new QLabel( qfu( p_widget->psz_text ), this );
            p_widget->p_sys_intf = label;
            label->setTextFormat( Qt::RichText );
215
            label->setOpenExternalLinks( true );
216 217 218 219
            return label;

        case EXTENSION_WIDGET_BUTTON:
            button = new QPushButton( qfu( p_widget->psz_text ), this );
220
            clickMapper->setMapping( button, new WidgetMapper( button, p_widget ) );
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            CONNECT( button, clicked(), clickMapper, map() );
            p_widget->p_sys_intf = button;
            return button;

        case EXTENSION_WIDGET_IMAGE:
            label = new QLabel( this );
            label->setPixmap( QPixmap( qfu( p_widget->psz_text ) ) );
            if( p_widget->i_width > 0 )
                label->setMaximumWidth( p_widget->i_width );
            if( p_widget->i_height > 0 )
                label->setMaximumHeight( p_widget->i_height );
            label->setScaledContents( true );
            p_widget->p_sys_intf = label;
            return label;

        case EXTENSION_WIDGET_HTML:
237 238
            textArea = new QTextBrowser( this );
            textArea->setOpenExternalLinks( true );
239 240 241 242 243 244 245 246 247
            textArea->setHtml( qfu( p_widget->psz_text ) );
            p_widget->p_sys_intf = textArea;
            return textArea;

        case EXTENSION_WIDGET_TEXT_FIELD:
            textInput = new QLineEdit( this );
            textInput->setText( qfu( p_widget->psz_text ) );
            textInput->setReadOnly( false );
            textInput->setEchoMode( QLineEdit::Normal );
248
            inputMapper->setMapping( textInput, new WidgetMapper( textInput, p_widget ) );
249 250 251 252 253 254 255 256 257 258 259
            /// @note: maybe it would be wiser to use textEdited here?
            CONNECT( textInput, textChanged(const QString &),
                     inputMapper, map() );
            p_widget->p_sys_intf = textInput;
            return textInput;

        case EXTENSION_WIDGET_PASSWORD:
            textInput = new QLineEdit( this );
            textInput->setText( qfu( p_widget->psz_text ) );
            textInput->setReadOnly( false );
            textInput->setEchoMode( QLineEdit::Password );
260
            inputMapper->setMapping( textInput, new WidgetMapper( textInput, p_widget ) );
261 262 263 264 265 266 267 268 269 270
            /// @note: maybe it would be wiser to use textEdited here?
            CONNECT( textInput, textChanged(const QString &),
                     inputMapper, map() );
            p_widget->p_sys_intf = textInput;
            return textInput;

        case EXTENSION_WIDGET_CHECK_BOX:
            checkBox = new QCheckBox( this );
            checkBox->setText( qfu( p_widget->psz_text ) );
            checkBox->setChecked( p_widget->b_checked );
271
            clickMapper->setMapping( checkBox, new WidgetMapper( checkBox, p_widget ) );
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            CONNECT( checkBox, stateChanged( int ), clickMapper, map() );
            p_widget->p_sys_intf = checkBox;
            return checkBox;

        case EXTENSION_WIDGET_DROPDOWN:
            comboBox = new QComboBox( this );
            comboBox->setEditable( false );
            for( p_value = p_widget->p_values;
                 p_value != NULL;
                 p_value = p_value->p_next )
            {
                comboBox->addItem( qfu( p_value->psz_text ), p_value->i_id );
            }
            /* Set current item */
            if( p_widget->psz_text )
            {
                int idx = comboBox->findText( qfu( p_widget->psz_text ) );
                if( idx >= 0 )
                    comboBox->setCurrentIndex( idx );
            }
292
            selectMapper->setMapping( comboBox, new WidgetMapper( comboBox, p_widget ) );
293 294 295 296 297 298
            CONNECT( comboBox, currentIndexChanged( const QString& ),
                     selectMapper, map() );
            return comboBox;

        case EXTENSION_WIDGET_LIST:
            list = new QListWidget( this );
299
            list->setSelectionMode( QAbstractItemView::ExtendedSelection );
300 301 302 303 304 305 306 307 308
            for( p_value = p_widget->p_values;
                 p_value != NULL;
                 p_value = p_value->p_next )
            {
                QListWidgetItem *item =
                    new QListWidgetItem( qfu( p_value->psz_text ) );
                item->setData( Qt::UserRole, p_value->i_id );
                list->addItem( item );
            }
309
            selectMapper->setMapping( list, new WidgetMapper( list, p_widget ) );
310 311 312 313
            CONNECT( list, itemSelectionChanged(),
                     selectMapper, map() );
            return list;

314 315 316 317 318 319
        case EXTENSION_WIDGET_SPIN_ICON:
            spinIcon = new SpinningIcon( this );
            spinIcon->play( p_widget->i_spin_loops );
            p_widget->p_sys_intf = spinIcon;
            return spinIcon;

320
        default:
321
            msg_Err( p_intf, "Widget type %d unknown", p_widget->type );
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
            return NULL;
    }
}

/**
 * Forward click event to the extension
 * @param object A WidgetMapper, whose data() is the p_widget
 **/
int ExtensionDialog::TriggerClick( QObject *object )
{
    assert( object != NULL );
    WidgetMapper *mapping = static_cast< WidgetMapper* >( object );
    extension_widget_t *p_widget = mapping->getWidget();

    QCheckBox *checkBox = NULL;
    int i_ret = VLC_EGENERIC;

    bool lockedHere = false;
    if( !has_lock )
    {
        vlc_mutex_lock( &p_dialog->lock );
        has_lock = true;
        lockedHere = true;
    }

    switch( p_widget->type )
    {
        case EXTENSION_WIDGET_BUTTON:
            i_ret = extension_WidgetClicked( p_dialog, p_widget );
            break;

        case EXTENSION_WIDGET_CHECK_BOX:
            checkBox = static_cast< QCheckBox* >( p_widget->p_sys_intf );
            p_widget->b_checked = checkBox->isChecked();
            i_ret = VLC_SUCCESS;
            break;

        default:
            msg_Dbg( p_intf, "A click event was triggered by a wrong widget" );
            break;
    }

    if( lockedHere )
    {
        vlc_mutex_unlock( &p_dialog->lock );
        has_lock = false;
    }

    return i_ret;
}

/**
 * Synchronize psz_text with the widget's text() value on update
 * @param object A WidgetMapper
 **/
void ExtensionDialog::SyncInput( QObject *object )
{
    assert( object != NULL );

    bool lockedHere = false;
    if( !has_lock )
    {
        vlc_mutex_lock( &p_dialog->lock );
        has_lock = true;
        lockedHere = true;
    }

    WidgetMapper *mapping = static_cast< WidgetMapper* >( object );
    extension_widget_t *p_widget = mapping->getWidget();
    assert( p_widget->type == EXTENSION_WIDGET_TEXT_FIELD
            || p_widget->type == EXTENSION_WIDGET_PASSWORD );
    /* Synchronize psz_text with the new value */
    QLineEdit *widget = static_cast< QLineEdit* >( p_widget->p_sys_intf );
395
    char *psz_text = widget->text().isNull() ? NULL : strdup( qtu( widget->text() ) );
396
    free( p_widget->psz_text );
397
    p_widget->psz_text =  psz_text;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

    if( lockedHere )
    {
        vlc_mutex_unlock( &p_dialog->lock );
        has_lock = false;
    }
}

/**
 * Synchronize parameter b_selected in the values list
 * @param object A WidgetMapper
 **/
void ExtensionDialog::SyncSelection( QObject *object )
{
    assert( object != NULL );
    struct extension_widget_t::extension_widget_value_t *p_value;

    bool lockedHere = false;
    if( !has_lock )
    {
        vlc_mutex_lock( &p_dialog->lock );
        has_lock = true;
        lockedHere = true;
    }

    WidgetMapper *mapping = static_cast< WidgetMapper* >( object );
    extension_widget_t *p_widget = mapping->getWidget();
    assert( p_widget->type == EXTENSION_WIDGET_DROPDOWN
            || p_widget->type == EXTENSION_WIDGET_LIST );

    if( p_widget->type == EXTENSION_WIDGET_DROPDOWN )
    {
        QComboBox *combo = static_cast< QComboBox* >( p_widget->p_sys_intf );
        for( p_value = p_widget->p_values;
             p_value != NULL;
             p_value = p_value->p_next )
        {
//             if( !qstrcmp( p_value->psz_text, qtu( combo->currentText() ) ) )
            if( combo->itemData( combo->currentIndex(), Qt::UserRole ).toInt()
                == p_value->i_id )
            {
                p_value->b_selected = true;
            }
            else
            {
                p_value->b_selected = false;
            }
        }
        free( p_widget->psz_text );
        p_widget->psz_text = strdup( qtu( combo->currentText() ) );
    }
    else if( p_widget->type == EXTENSION_WIDGET_LIST )
    {
        QListWidget *list = static_cast<QListWidget*>( p_widget->p_sys_intf );
        QList<QListWidgetItem *> selection = list->selectedItems();
        for( p_value = p_widget->p_values;
             p_value != NULL;
             p_value = p_value->p_next )
        {
            bool b_selected = false;
            foreach( const QListWidgetItem *item, selection )
            {
//                 if( !qstrcmp( qtu( item->text() ), p_value->psz_text ) )
                if( item->data( Qt::UserRole ).toInt() == p_value->i_id )
                {
                    b_selected = true;
                    break;
                }
            }
            p_value->b_selected = b_selected;
        }
    }

    if( lockedHere )
    {
        vlc_mutex_unlock( &p_dialog->lock );
        has_lock = false;
    }
}

void ExtensionDialog::UpdateWidgets()
{
    assert( p_dialog );
    extension_widget_t *p_widget;
    FOREACH_ARRAY( p_widget, p_dialog->widgets )
    {
        if( !p_widget ) continue; /* Some widgets may be NULL at this point */
        QWidget *widget;
        int row = p_widget->i_row - 1;
        int col = p_widget->i_column - 1;
        if( row < 0 )
        {
            row = layout->rowCount();
            col = 0;
        }
        else if( col < 0 )
            col = layout->columnCount();
        int hsp = __MAX( 1, p_widget->i_horiz_span );
        int vsp = __MAX( 1, p_widget->i_vert_span );
        if( !p_widget->p_sys_intf && !p_widget->b_kill )
        {
            widget = CreateWidget( p_widget );
            if( !widget )
            {
                msg_Warn( p_intf, "Could not create a widget for dialog %s",
                          p_dialog->psz_title );
                continue;
            }
            widget->setVisible( !p_widget->b_hide );
            layout->addWidget( widget, row, col, vsp, hsp );
            if( ( p_widget->i_width > 0 ) && ( p_widget->i_height > 0 ) )
                widget->resize( p_widget->i_width, p_widget->i_height );
            p_widget->p_sys_intf = widget;
            this->resize( sizeHint() );
512 513
            /* If an update was required, cancel it as we just created the widget */
            p_widget->b_update = false;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
        }
        else if( p_widget->p_sys_intf && !p_widget->b_kill
                 && p_widget->b_update )
        {
            widget = UpdateWidget( p_widget );
            if( !widget )
            {
                msg_Warn( p_intf, "Could not update a widget for dialog %s",
                          p_dialog->psz_title );
                return;
            }
            widget->setVisible( !p_widget->b_hide );
            layout->addWidget( widget, row, col, vsp, hsp );
            if( ( p_widget->i_width > 0 ) && ( p_widget->i_height > 0 ) )
                widget->resize( p_widget->i_width, p_widget->i_height );
            p_widget->p_sys_intf = widget;
            this->resize( sizeHint() );

            /* Do not update again */
            p_widget->b_update = false;
        }
        else if( p_widget->p_sys_intf && p_widget->b_kill )
        {
            DestroyWidget( p_widget );
            p_widget->p_sys_intf = NULL;
            this->resize( sizeHint() );
        }
    }
    FOREACH_END()
}

QWidget* ExtensionDialog::UpdateWidget( extension_widget_t *p_widget )
{
    QLabel *label = NULL;
    QPushButton *button = NULL;
549
    QTextBrowser *textArea = NULL;
550 551 552 553
    QLineEdit *textInput = NULL;
    QCheckBox *checkBox = NULL;
    QComboBox *comboBox = NULL;
    QListWidget *list = NULL;
554
    SpinningIcon *spinIcon = NULL;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    struct extension_widget_t::extension_widget_value_t *p_value = NULL;

    assert( p_widget->p_sys_intf != NULL );

    switch( p_widget->type )
    {
        case EXTENSION_WIDGET_LABEL:
            label = static_cast< QLabel* >( p_widget->p_sys_intf );
            label->setText( qfu( p_widget->psz_text ) );
            return label;

        case EXTENSION_WIDGET_BUTTON:
            // FIXME: looks like removeMappings does not work
            button = static_cast< QPushButton* >( p_widget->p_sys_intf );
            button->setText( qfu( p_widget->psz_text ) );
            clickMapper->removeMappings( button );
571
            clickMapper->setMapping( button, new WidgetMapper( button, p_widget ) );
572 573 574 575 576 577 578 579 580
            CONNECT( button, clicked(), clickMapper, map() );
            return button;

        case EXTENSION_WIDGET_IMAGE:
            label = static_cast< QLabel* >( p_widget->p_sys_intf );
            label->setPixmap( QPixmap( qfu( p_widget->psz_text ) ) );
            return label;

        case EXTENSION_WIDGET_HTML:
581
            textArea = static_cast< QTextBrowser* >( p_widget->p_sys_intf );
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
            textArea->setHtml( qfu( p_widget->psz_text ) );
            return textArea;

        case EXTENSION_WIDGET_TEXT_FIELD:
            textInput = static_cast< QLineEdit* >( p_widget->p_sys_intf );
            textInput->setText( qfu( p_widget->psz_text ) );
            return textInput;

        case EXTENSION_WIDGET_PASSWORD:
            textInput = static_cast< QLineEdit* >( p_widget->p_sys_intf );
            textInput->setText( qfu( p_widget->psz_text ) );
            return textInput;

        case EXTENSION_WIDGET_CHECK_BOX:
            checkBox = static_cast< QCheckBox* >( p_widget->p_sys_intf );
            checkBox->setText( qfu( p_widget->psz_text ) );
            checkBox->setChecked( p_widget->b_checked );
            return checkBox;

        case EXTENSION_WIDGET_DROPDOWN:
            comboBox = static_cast< QComboBox* >( p_widget->p_sys_intf );
603 604 605 606 607 608 609
            // method widget:clear()
            if ( p_widget->p_values == NULL )
            {
                comboBox->clear();
                return comboBox;
            }
            // method widget:addvalue()
610 611 612 613
            for( p_value = p_widget->p_values;
                 p_value != NULL;
                 p_value = p_value->p_next )
            {
614 615
                if ( comboBox->findText( qfu( p_value->psz_text ) ) < 0 )
                    comboBox->addItem( qfu( p_value->psz_text ), p_value->i_id );
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
            }
            return comboBox;

        case EXTENSION_WIDGET_LIST:
            list = static_cast< QListWidget* >( p_widget->p_sys_intf );
            list->clear();
            for( p_value = p_widget->p_values;
                 p_value != NULL;
                 p_value = p_value->p_next )
            {
                QListWidgetItem *item =
                        new QListWidgetItem( qfu( p_value->psz_text ) );
                item->setData( Qt::UserRole, p_value->i_id );
                list->addItem( item );
            }
            return list;

633 634 635 636 637 638 639 640 641
        case EXTENSION_WIDGET_SPIN_ICON:
            spinIcon = static_cast< SpinningIcon* >( p_widget->p_sys_intf );
            if( !spinIcon->isPlaying() && p_widget->i_spin_loops != 0 )
                spinIcon->play( p_widget->i_spin_loops );
            else if( spinIcon->isPlaying() && p_widget->i_spin_loops == 0 )
                spinIcon->stop();
            p_widget->i_height = p_widget->i_width = 16;
            return spinIcon;

642
        default:
643
            msg_Err( p_intf, "Widget type %d unknown", p_widget->type );
644 645 646 647
            return NULL;
    }
}

648 649
void ExtensionDialog::DestroyWidget( extension_widget_t *p_widget,
                                     bool b_cond )
650 651 652 653 654
{
    assert( p_widget && p_widget->b_kill );
    QWidget *widget = static_cast< QWidget* >( p_widget->p_sys_intf );
    delete widget;
    p_widget->p_sys_intf = NULL;
655 656
    if( b_cond )
        vlc_cond_signal( &p_dialog->cond );
657 658 659
}

/** Implement closeEvent() in order to intercept the event */
660
void ExtensionDialog::closeEvent( QCloseEvent * )
661 662
{
    assert( p_dialog != NULL );
663 664
    msg_Dbg( p_intf, "Dialog '%s' received a closeEvent",
             p_dialog->psz_title );
665 666 667
    extension_DialogClosed( p_dialog );
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
/** Grab some keyboard input (ESC, ...) and handle actions manually */
void ExtensionDialog::keyPressEvent( QKeyEvent *event )
{
    assert( p_dialog != NULL );
    switch( event->key() )
    {
    case Qt::Key_Escape:
        close();
        return;
    default:
        QDialog::keyPressEvent( event );
        return;
    }
}

683 684 685 686 687 688 689
void ExtensionDialog::parentDestroyed()
{
    msg_Dbg( p_intf, "About to destroy dialog '%s'", p_dialog->psz_title );
    deleteLater(); // May not work at this point (event loop can be ended)
    p_dialog->p_sys_intf = NULL;
    vlc_cond_signal( &p_dialog->cond );
}