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 *****************************************************************************/
28 #include "dialogs/errors.hpp"
29 #include "dialogs/interaction.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;
51 if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
53 i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
54 qfu( p_dialog->psz_description ),
55 QMessageBox::Ok, QMessageBox::Ok );
57 else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
59 if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
60 ErrorsDialog::getInstance( p_intf )->addError(
61 qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
62 i_ret = QMessageBox::AcceptRole;
64 else if( p_dialog->i_flags & DIALOG_WARNING )
66 if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
67 ErrorsDialog::getInstance( p_intf )->addWarning(
68 qfu( p_dialog->psz_title ),qfu( p_dialog->psz_description ) );
69 i_ret = QMessageBox::AcceptRole;
71 else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
73 p_dialog->i_status = SENT_DIALOG;
75 QMessageBox cancelBox;
77 cancelBox.setWindowTitle( qfu( p_dialog->psz_title) );
78 cancelBox.setText( qfu( p_dialog->psz_description ) );
80 if( p_dialog->psz_default_button )
81 cancelBox.addButton( "&" + qfu( p_dialog->psz_default_button ),
82 QMessageBox::AcceptRole );
84 if( p_dialog->psz_alternate_button )
85 cancelBox.addButton( "&" + qfu( p_dialog->psz_alternate_button ),
86 QMessageBox::RejectRole );
88 if( p_dialog->psz_other_button )
89 cancelBox.addButton( "&" + qfu( p_dialog->psz_other_button ),
90 QMessageBox::ActionRole );
92 i_ret = cancelBox.exec();
93 msg_Dbg( p_intf, "Warning %i %i", i_ret, cancelBox.result() );
95 else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
97 dialog = new QWidget; layout = new QVBoxLayout( dialog );
98 layout->setMargin( 2 );
99 panel = new QWidget( dialog );
100 QGridLayout *grid = new QGridLayout;
102 description = new QLabel( qfu( p_dialog->psz_description ) );
103 grid->addWidget( description, 0, 0, 1, 2 );
105 grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
106 loginEdit = new QLineEdit;
107 grid->addWidget( loginEdit, 1, 1 );
109 grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
110 passwordEdit = new QLineEdit;
111 passwordEdit->setEchoMode( QLineEdit::Password );
112 grid->addWidget( passwordEdit, 2, 1 );
114 panel->setLayout( grid );
115 layout->addWidget( panel );
117 else if( (p_dialog->i_flags & DIALOG_INTF_PROGRESS ) ||
118 ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) )
120 dialog = new QWidget; layout = new QVBoxLayout( dialog );
121 layout->setMargin( 2 );
122 description = new QLabel( qfu( p_dialog->psz_description ) );
123 layout->addWidget( description );
125 progressBar = new QProgressBar;
126 progressBar->setMaximum( 1000 );
127 progressBar->setTextVisible( true );
128 progressBar->setOrientation( Qt::Horizontal );
129 layout->addWidget( progressBar );
131 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
133 dialog = new QWidget; layout = new QVBoxLayout( dialog );
134 layout->setMargin( 2 );
135 description = new QLabel( qfu( p_dialog->psz_description ) );
136 layout->addWidget( description );
138 inputEdit = new QLineEdit;
139 layout->addWidget( inputEdit );
143 msg_Err( p_intf, "Unknown dialog type %i", p_dialog->i_flags );
147 msg_Dbg( p_intf, "Warning %i", i_ret );
148 /* We used a QMessageBox */
151 if( i_ret == QMessageBox::AcceptRole || i_ret == QMessageBox::Ok )
152 Finish( DIALOG_OK_YES );
153 else if ( i_ret == QMessageBox::RejectRole ) Finish( DIALOG_NO );
154 else Finish( DIALOG_CANCELLED );
157 /* Custom dialog, finish it */
160 /* Start the DialogButtonBox config */
161 QDialogButtonBox *buttonBox = new QDialogButtonBox;
163 if( p_dialog->psz_default_button )
165 defaultButton = new QPushButton;
166 defaultButton->setFocus();
167 defaultButton->setText( "&" + qfu( p_dialog->psz_default_button ) );
168 buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
170 if( p_dialog->psz_alternate_button )
172 altButton = new QPushButton;
173 altButton->setText( "&" + qfu( p_dialog->psz_alternate_button ) );
174 buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
176 if( p_dialog->psz_other_button )
178 otherButton = new QPushButton;
179 otherButton->setText( "&" + qfu( p_dialog->psz_other_button ) );
180 buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
182 layout->addWidget( buttonBox );
183 /* End the DialogButtonBox */
186 if( p_dialog->psz_default_button ) BUTTONACT( defaultButton, defaultB() );
187 if( p_dialog->psz_alternate_button ) BUTTONACT( altButton, altB() );
188 if( p_dialog->psz_other_button ) BUTTONACT( otherButton, otherB() );
190 /* set the layouts and thte title */
191 dialog->setLayout( layout );
192 dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
196 void InteractionDialog::update()
198 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
199 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
201 assert( progressBar );
202 progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
204 description->setText( qfu( p_dialog->psz_description ) );
208 if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
209 ( p_dialog->val.f_float >= 100.0 ) )
212 msg_Dbg( p_intf, "Progress Done" );
215 if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
216 ( p_dialog->val.f_float >= 100.0 ) )
219 altButton->setText( qtr( "&Close" ) );
223 InteractionDialog::~InteractionDialog()
228 void InteractionDialog::defaultB()
230 Finish( DIALOG_OK_YES );
232 void InteractionDialog::altB()
236 void InteractionDialog::otherB()
238 Finish( DIALOG_CANCELLED );
241 void InteractionDialog::Finish( int i_ret )
243 vlc_object_lock( (vlc_object_t *)(p_dialog->p_interaction) );
245 /* Special cases when we have to return psz to the core */
246 if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
248 p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
249 p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
251 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
253 p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
256 /* We finished the dialog, answer it */
257 p_dialog->i_status = ANSWERED_DIALOG;
258 p_dialog->i_return = i_ret;
260 /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
261 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
262 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
263 p_dialog->b_cancelled = true;
266 vlc_object_unlock( (vlc_object_t *)(p_dialog->p_interaction) );