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"
30 #include "main_interface.hpp"
34 #include <QPushButton>
35 #include <QProgressBar>
36 #include <QMessageBox>
37 #include <QDialogButtonBox>
41 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
42 interaction_dialog_t *_p_dialog ) : QObject( 0 ),
43 p_intf( _p_intf), p_dialog( _p_dialog )
45 QVBoxLayout *layout = NULL;
52 if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
54 i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
55 qfu( p_dialog->psz_description ),
56 QMessageBox::Ok, 0, 0 );
58 else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
60 if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
61 ErrorsDialog::getInstance( p_intf )->addError(
62 qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
65 else if( p_dialog->i_flags & DIALOG_WARNING )
67 if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
68 ErrorsDialog::getInstance( p_intf )->addWarning(
69 qfu( p_dialog->psz_title ),qfu( p_dialog->psz_description ) );
72 else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
74 p_dialog->i_status = SENT_DIALOG;
75 i_ret = QMessageBox::question( NULL,
76 qfu( p_dialog->psz_title), qfu( p_dialog->psz_description ),
77 p_dialog->psz_default_button ?
78 qfu( p_dialog->psz_default_button ) : QString::null,
79 p_dialog->psz_alternate_button ?
80 qfu( p_dialog->psz_alternate_button ) : QString::null,
81 p_dialog->psz_other_button ?
82 qfu( p_dialog->psz_other_button ) : QString::null, 0,
83 p_dialog->psz_other_button ? 2 : -1 );
85 else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
87 dialog = new QWidget( 0 ); layout = new QVBoxLayout( dialog );
88 layout->setMargin( 2 );
89 panel = new QWidget( 0 );
90 QGridLayout *grid = new QGridLayout;
92 description = new QLabel( qfu( p_dialog->psz_description ) );
93 grid->addWidget( description, 0, 0, 1, 2 );
95 grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
96 loginEdit = new QLineEdit;
97 grid->addWidget( loginEdit, 1, 1 );
99 grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
100 passwordEdit = new QLineEdit;
101 passwordEdit->setEchoMode( QLineEdit::Password );
102 grid->addWidget( passwordEdit, 2, 1 );
104 panel->setLayout( grid );
105 layout->addWidget( panel );
107 else if( (p_dialog->i_flags & DIALOG_INTF_PROGRESS ) ||
108 ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) )
110 dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
111 layout->setMargin( 2 );
112 description = new QLabel( qfu( p_dialog->psz_description ) );
113 layout->addWidget( description );
115 progressBar = new QProgressBar;
116 progressBar->setMaximum( 1000 );
117 progressBar->setTextVisible( true );
118 progressBar->setOrientation( Qt::Horizontal );
119 layout->addWidget( progressBar );
121 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
123 dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
124 layout->setMargin( 2 );
125 description = new QLabel( qfu( p_dialog->psz_description ) );
126 layout->addWidget( description );
128 inputEdit = new QLineEdit;
129 layout->addWidget( inputEdit );
133 msg_Err( p_intf, "Unknown dialog type %i", p_dialog->i_flags );
137 /* We used a message box */
140 if( i_ret == 0 ) Finish( DIALOG_OK_YES );
141 else if ( i_ret == 1 ) Finish( DIALOG_NO );
142 else if ( i_ret == 2 ) return ;
143 else Finish( DIALOG_CANCELLED );
146 /* Custom box, finish it */
149 /* Start the DialogButtonBox config */
150 QDialogButtonBox *buttonBox = new QDialogButtonBox;
152 if( p_dialog->psz_default_button )
154 defaultButton = new QPushButton;
155 defaultButton->setFocus();
156 defaultButton->setText( qfu( p_dialog->psz_default_button ) );
157 buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
159 if( p_dialog->psz_alternate_button )
161 altButton = new QPushButton;
162 altButton->setText( qfu( p_dialog->psz_alternate_button ) );
163 buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
165 if( p_dialog->psz_other_button )
167 otherButton = new QPushButton;
168 otherButton->setText( qfu( p_dialog->psz_other_button ) );
169 buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
171 layout->addWidget( buttonBox );
172 /* End the DialogButtonBox */
175 if( p_dialog->psz_default_button )
176 BUTTONACT( defaultButton, defaultB() );
177 if( p_dialog->psz_alternate_button )
178 BUTTONACT( altButton, altB() );
179 if( p_dialog->psz_other_button )
180 BUTTONACT( otherButton, otherB() );
182 /* set the layouts and thte title */
183 dialog->setLayout( layout );
184 dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
188 void InteractionDialog::update()
190 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
191 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
193 assert( progressBar );
194 progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
196 description->setText( qfu( p_dialog->psz_description ) );
200 if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
201 ( p_dialog->val.f_float >= 100.0 ) )
204 msg_Dbg( p_intf, "Progress Done" );
207 if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
208 ( p_dialog->val.f_float >= 100.0 ) )
211 altButton->setText( qtr( "&Close" ) );
215 InteractionDialog::~InteractionDialog()
221 void InteractionDialog::defaultB()
223 Finish( DIALOG_OK_YES );
225 void InteractionDialog::altB()
229 void InteractionDialog::otherB()
231 Finish( DIALOG_CANCELLED );
234 void InteractionDialog::Finish( int i_ret )
236 vlc_object_lock( p_dialog->p_interaction );
238 if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
240 p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
241 p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
243 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
245 p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
247 p_dialog->i_status = ANSWERED_DIALOG;
248 p_dialog->i_return = i_ret;
250 /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
251 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
252 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
253 p_dialog->b_cancelled = true;
256 vlc_object_unlock( p_dialog->p_interaction );
257 playlist_Signal( THEPL );