1 /*****************************************************************************
2 * interaction.cpp : Interaction stuff
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
27 #include "dialogs/errors.hpp"
28 #include "dialogs/interaction.hpp"
29 #include "main_interface.hpp"
33 #include <QPushButton>
34 #include <QProgressBar>
35 #include <QMessageBox>
36 #include <QDialogButtonBox>
40 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
41 interaction_dialog_t *_p_dialog ) : QObject( 0 ),
42 p_intf( _p_intf), p_dialog( _p_dialog )
44 QVBoxLayout *layout = NULL;
49 if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
51 i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
52 qfu( p_dialog->psz_description ),
53 QMessageBox::Ok, 0, 0 );
55 else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
57 if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
58 ErrorsDialog::getInstance( p_intf )->addError(
59 qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
61 // QApplication::style()->standardPixmap(QStyle::SP_MessageBoxCritical)
63 else if( p_dialog->i_flags & DIALOG_WARNING )
65 if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
66 ErrorsDialog::getInstance( p_intf )->addWarning(
67 qfu( p_dialog->psz_title ),qfu( p_dialog->psz_description ) );
70 else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
72 p_dialog->i_status = SENT_DIALOG;
73 i_ret = QMessageBox::question( NULL,
74 qfu( p_dialog->psz_title), qfu( p_dialog->psz_description ),
75 p_dialog->psz_default_button ?
76 qfu( p_dialog->psz_default_button ) : QString::null,
77 p_dialog->psz_alternate_button ?
78 qfu( p_dialog->psz_alternate_button ) : QString::null,
79 p_dialog->psz_other_button ?
80 qfu( p_dialog->psz_other_button ) : QString::null, 0,
81 p_dialog->psz_other_button ? 2 : -1 );
83 else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
85 dialog = new QWidget( 0 ); layout = new QVBoxLayout( dialog );
86 layout->setMargin( 2 );
87 panel = new QWidget( 0 );
88 QGridLayout *grid = new QGridLayout;
90 description = new QLabel( qfu( p_dialog->psz_description ) );
91 grid->addWidget( description, 0, 0, 1, 2 );
93 grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
94 loginEdit = new QLineEdit;
95 grid->addWidget( loginEdit, 1, 1 );
97 grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
98 passwordEdit = new QLineEdit;
99 passwordEdit->setEchoMode( QLineEdit::Password );
100 grid->addWidget( passwordEdit, 2, 1 );
102 panel->setLayout( grid );
103 layout->addWidget( panel );
105 else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
107 dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
108 layout->setMargin( 2 );
109 description = new QLabel( qfu( p_dialog->psz_description ) );
110 layout->addWidget( description );
112 progressBar = new QProgressBar;
113 progressBar->setMaximum( 1000 );
114 progressBar->setTextVisible( true );
115 progressBar->setOrientation( Qt::Horizontal );
116 layout->addWidget( progressBar );
118 else if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
120 progressBar = p_intf->p_sys->p_mi->pgBar;
123 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
125 dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
126 layout->setMargin( 2 );
127 description = new QLabel( qfu( p_dialog->psz_description ) );
128 layout->addWidget( description );
130 inputEdit = new QLineEdit;
131 layout->addWidget( inputEdit );
134 msg_Err( p_intf, "unknown dialog type %i", p_dialog->i_flags );
136 /* We used a message box */
139 if( i_ret == 0 ) Finish( DIALOG_OK_YES );
140 else if ( i_ret == 1 ) Finish( DIALOG_NO );
141 else Finish( DIALOG_CANCELLED );
144 /* Custom box, finish it */
146 QDialogButtonBox *buttonBox = new QDialogButtonBox;
148 if( p_dialog->psz_default_button )
150 defaultButton = new QPushButton;
151 defaultButton->setFocus();
152 defaultButton->setText( qfu( p_dialog->psz_default_button ) );
153 buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
155 if( p_dialog->psz_alternate_button )
157 altButton = new QPushButton;
158 altButton->setText( qfu( p_dialog->psz_alternate_button ) );
159 buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
161 if( p_dialog->psz_other_button )
163 otherButton = new QPushButton;
164 otherButton->setText( qfu( p_dialog->psz_other_button ) );
165 buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
167 layout->addWidget( buttonBox );
168 if( p_dialog->psz_default_button )
169 BUTTONACT( defaultButton, defaultB() );
170 if( p_dialog->psz_alternate_button )
171 BUTTONACT( altButton, altB() );
172 if( p_dialog->psz_other_button )
173 BUTTONACT( otherButton, otherB() );
174 dialog->setLayout( layout );
175 dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
179 void InteractionDialog::update()
181 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
182 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
184 assert( progressBar );
185 progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
186 description->setText( qfu( p_dialog->psz_description ) );
187 msg_Dbg( p_intf, "Setting progress to %i", progressBar->value() );
190 if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
191 ( p_dialog->val.f_float >= 100.0 ) )
193 if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
194 ( p_dialog->val.f_float >= 100.0 ) )
195 altButton->setText( qtr( "close" ) );
198 InteractionDialog::~InteractionDialog()
200 // if( panel ) delete panel;
201 if( dialog ) delete dialog;
204 void InteractionDialog::defaultB()
206 Finish( DIALOG_OK_YES );
208 void InteractionDialog::altB()
212 void InteractionDialog::otherB()
214 Finish( DIALOG_CANCELLED );
217 void InteractionDialog::Finish( int i_ret )
219 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
221 if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
223 p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
224 p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
226 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
228 p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
230 p_dialog->i_status = ANSWERED_DIALOG;
231 p_dialog->i_return = i_ret;
233 /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
234 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
235 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
236 p_dialog->b_cancelled = true;
239 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
240 playlist_Signal( THEPL );