]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/interaction.cpp
Custom lock for interaction
[vlc] / modules / gui / qt4 / dialogs / interaction.cpp
1 /*****************************************************************************
2  * interaction.cpp : Interaction stuff
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "dialogs/errors.hpp"
29 #include "dialogs/interaction.hpp"
30
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QPushButton>
34 #include <QProgressBar>
35 #include <QMessageBox>
36 #include <QDialogButtonBox>
37
38 #include <assert.h>
39
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 )
43 {
44     QVBoxLayout *layout = NULL;
45     description = NULL;
46     int i_ret = -1;
47     panel = NULL;
48     dialog = NULL;
49     altButton = NULL;
50
51     if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
52     {
53         i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
54                                        qfu( p_dialog->psz_description ),
55                                        QMessageBox::Ok, QMessageBox::Ok );
56     }
57     else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
58     {
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;
63     }
64     else if( p_dialog->i_flags & DIALOG_WARNING )
65     {
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;
70     }
71     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
72     {
73         p_dialog->i_status = SENT_DIALOG;
74
75         QMessageBox cancelBox;
76
77         cancelBox.setWindowTitle( qfu( p_dialog->psz_title) );
78         cancelBox.setText( qfu( p_dialog->psz_description ) );
79
80         if( p_dialog->psz_default_button )
81             cancelBox.addButton( "&" + qfu( p_dialog->psz_default_button ),
82                                  QMessageBox::AcceptRole );
83
84         if( p_dialog->psz_alternate_button )
85             cancelBox.addButton( "&" + qfu( p_dialog->psz_alternate_button ),
86                                  QMessageBox::RejectRole );
87
88         if( p_dialog->psz_other_button )
89             cancelBox.addButton( "&" + qfu( p_dialog->psz_other_button ),
90                                  QMessageBox::ActionRole );
91
92         i_ret = cancelBox.exec();
93         msg_Dbg( p_intf, "Warning %i %i", i_ret, cancelBox.result() );
94     }
95     else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
96     {
97         dialog = new QWidget; layout = new QVBoxLayout( dialog );
98         layout->setMargin( 2 );
99         panel = new QWidget( dialog );
100         QGridLayout *grid = new QGridLayout;
101
102         description = new QLabel( qfu( p_dialog->psz_description ) );
103         grid->addWidget( description, 0, 0, 1, 2 );
104
105         grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
106         loginEdit = new QLineEdit;
107         grid->addWidget( loginEdit, 1, 1 );
108
109         grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
110         passwordEdit = new QLineEdit;
111         passwordEdit->setEchoMode( QLineEdit::Password );
112         grid->addWidget( passwordEdit, 2, 1 );
113
114         panel->setLayout( grid );
115         layout->addWidget( panel );
116     }
117     else if( (p_dialog->i_flags & DIALOG_INTF_PROGRESS ) ||
118              ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) )
119     {
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 );
124
125         progressBar = new QProgressBar;
126         progressBar->setMaximum( 1000 );
127         progressBar->setTextVisible( true );
128         progressBar->setOrientation( Qt::Horizontal );
129         layout->addWidget( progressBar );
130     }
131     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
132     {
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 );
137
138         inputEdit = new QLineEdit;
139         layout->addWidget( inputEdit );
140     }
141     else
142     {
143         msg_Err( p_intf, "Unknown dialog type %i", p_dialog->i_flags );
144         return;
145     }
146
147     msg_Dbg( p_intf, "Warning %i", i_ret );
148     /* We used a QMessageBox */
149     if( i_ret != -1 )
150     {
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 );
155     }
156     else
157     /* Custom dialog, finish it */
158     {
159         assert( dialog );
160         /* Start the DialogButtonBox config */
161         QDialogButtonBox *buttonBox = new QDialogButtonBox;
162
163         if( p_dialog->psz_default_button )
164         {
165             defaultButton = new QPushButton;
166             defaultButton->setFocus();
167             defaultButton->setText( "&" + qfu( p_dialog->psz_default_button ) );
168             buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
169         }
170         if( p_dialog->psz_alternate_button )
171         {
172             altButton = new QPushButton;
173             altButton->setText( "&" + qfu( p_dialog->psz_alternate_button ) );
174             buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
175         }
176         if( p_dialog->psz_other_button )
177         {
178             otherButton = new QPushButton;
179             otherButton->setText( "&" + qfu( p_dialog->psz_other_button ) );
180             buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
181         }
182         layout->addWidget( buttonBox );
183         /* End the DialogButtonBox */
184
185         /* CONNECTs */
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() );
189
190         /* set the layouts and thte title */
191         dialog->setLayout( layout );
192         dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
193     }
194 }
195
196 void InteractionDialog::update()
197 {
198     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
199         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
200     {
201         assert( progressBar );
202         progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
203         if( description )
204             description->setText( qfu( p_dialog->psz_description ) );
205     }
206     else return;
207
208     if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
209         ( p_dialog->val.f_float >= 100.0 ) )
210     {
211         progressBar->hide();
212         msg_Dbg( p_intf, "Progress Done" );
213     }
214
215     if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
216         ( p_dialog->val.f_float >= 100.0 ) )
217     {
218         assert( altButton );
219         altButton->setText( qtr( "&Close" ) );
220     }
221 }
222
223 InteractionDialog::~InteractionDialog()
224 {
225     delete dialog;
226 }
227
228 void InteractionDialog::defaultB()
229 {
230     Finish( DIALOG_OK_YES );
231 }
232 void InteractionDialog::altB()
233 {
234     Finish( DIALOG_NO );
235 }
236 void InteractionDialog::otherB()
237 {
238     Finish( DIALOG_CANCELLED );
239 }
240
241 void InteractionDialog::Finish( int i_ret )
242 {
243     vlc_mutex_lock( p_dialog->p_lock );
244
245     /* Special cases when we have to return psz to the core */
246     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
247     {
248         p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
249         p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
250     }
251     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
252     {
253         p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
254     }
255
256     /* We finished the dialog, answer it */
257     p_dialog->i_status = ANSWERED_DIALOG;
258     p_dialog->i_return = i_ret;
259
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;
264
265     vlc_mutex_unlock( p_dialog->p_lock );
266
267     hide();
268 }
269