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, 0, 0 );
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 ) );
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 ) );
71 else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
73 p_dialog->i_status = SENT_DIALOG;
74 i_ret = QMessageBox::question( NULL,
75 qfu( p_dialog->psz_title), qfu( p_dialog->psz_description ),
76 p_dialog->psz_default_button ?
77 qfu( p_dialog->psz_default_button ) : QString::null,
78 p_dialog->psz_alternate_button ?
79 qfu( p_dialog->psz_alternate_button ) : QString::null,
80 p_dialog->psz_other_button ?
81 qfu( p_dialog->psz_other_button ) : QString::null, 0,
82 p_dialog->psz_other_button ? 2 : -1 );
84 else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
86 dialog = new QWidget( 0 ); layout = new QVBoxLayout( dialog );
87 layout->setMargin( 2 );
88 panel = new QWidget( 0 );
89 QGridLayout *grid = new QGridLayout;
91 description = new QLabel( qfu( p_dialog->psz_description ) );
92 grid->addWidget( description, 0, 0, 1, 2 );
94 grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
95 loginEdit = new QLineEdit;
96 grid->addWidget( loginEdit, 1, 1 );
98 grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
99 passwordEdit = new QLineEdit;
100 passwordEdit->setEchoMode( QLineEdit::Password );
101 grid->addWidget( passwordEdit, 2, 1 );
103 panel->setLayout( grid );
104 layout->addWidget( panel );
106 else if( (p_dialog->i_flags & DIALOG_INTF_PROGRESS ) ||
107 ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) )
109 dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
110 layout->setMargin( 2 );
111 description = new QLabel( qfu( p_dialog->psz_description ) );
112 layout->addWidget( description );
114 progressBar = new QProgressBar;
115 progressBar->setMaximum( 1000 );
116 progressBar->setTextVisible( true );
117 progressBar->setOrientation( Qt::Horizontal );
118 layout->addWidget( progressBar );
120 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
122 dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
123 layout->setMargin( 2 );
124 description = new QLabel( qfu( p_dialog->psz_description ) );
125 layout->addWidget( description );
127 inputEdit = new QLineEdit;
128 layout->addWidget( inputEdit );
132 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 if ( i_ret == 2 ) return ;
142 else Finish( DIALOG_CANCELLED );
145 /* Custom box, finish it */
148 /* Start the DialogButtonBox config */
149 QDialogButtonBox *buttonBox = new QDialogButtonBox;
151 if( p_dialog->psz_default_button )
153 defaultButton = new QPushButton;
154 defaultButton->setFocus();
155 defaultButton->setText( qfu( p_dialog->psz_default_button ) );
156 buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
158 if( p_dialog->psz_alternate_button )
160 altButton = new QPushButton;
161 altButton->setText( qfu( p_dialog->psz_alternate_button ) );
162 buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
164 if( p_dialog->psz_other_button )
166 otherButton = new QPushButton;
167 otherButton->setText( qfu( p_dialog->psz_other_button ) );
168 buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
170 layout->addWidget( buttonBox );
171 /* End the DialogButtonBox */
174 if( p_dialog->psz_default_button )
175 BUTTONACT( defaultButton, defaultB() );
176 if( p_dialog->psz_alternate_button )
177 BUTTONACT( altButton, altB() );
178 if( p_dialog->psz_other_button )
179 BUTTONACT( otherButton, otherB() );
181 /* set the layouts and thte title */
182 dialog->setLayout( layout );
183 dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
187 void InteractionDialog::update()
189 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
190 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
192 assert( progressBar );
193 progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
195 description->setText( qfu( p_dialog->psz_description ) );
199 if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
200 ( p_dialog->val.f_float >= 100.0 ) )
203 msg_Dbg( p_intf, "Progress Done" );
206 if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
207 ( p_dialog->val.f_float >= 100.0 ) )
210 altButton->setText( qtr( "&Close" ) );
214 InteractionDialog::~InteractionDialog()
220 void InteractionDialog::defaultB()
222 Finish( DIALOG_OK_YES );
224 void InteractionDialog::altB()
228 void InteractionDialog::otherB()
230 Finish( DIALOG_CANCELLED );
233 void InteractionDialog::Finish( int i_ret )
235 vlc_object_lock( p_dialog->p_interaction );
237 if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
239 p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
240 p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
242 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
244 p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
246 p_dialog->i_status = ANSWERED_DIALOG;
247 p_dialog->i_return = i_ret;
249 /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
250 if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
251 p_dialog->i_flags & DIALOG_INTF_PROGRESS )
252 p_dialog->b_cancelled = true;
255 vlc_object_unlock( p_dialog->p_interaction );