Qt4 - Make GNOME/KDE fanboys happy.
[vlc.git] / 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 #include "qt4.hpp"
25 #include "dialogs/errors.hpp"
26 #include "dialogs/interaction.hpp"
27 #include "util/qvlcframe.hpp"
28
29 #include <QLabel>
30 #include <QLineEdit>
31 #include <QPushButton>
32 #include <QProgressBar>
33 #include <QMessageBox>
34 #include <QDialogButtonBox>
35
36 #include <assert.h>
37
38 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
39                          interaction_dialog_t *_p_dialog ) : QObject( 0 ),
40                           p_intf( _p_intf), p_dialog( _p_dialog )
41 {
42     QVBoxLayout *layout = NULL;
43     int i_ret = -1;
44     panel = NULL;
45     dialog = NULL;
46
47     if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
48     {
49         i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
50                                        qfu( p_dialog->psz_description ),
51                                        QMessageBox::Ok, 0, 0 );
52     }
53     else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
54     {
55         if( config_GetInt( p_intf, "errors-dialog" ) != 0 )
56             ErrorsDialog::getInstance( p_intf )->addError(
57                  qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
58         i_ret = 0;
59         //  QApplication::style()->standardPixmap(QStyle::SP_MessageBoxCritical)
60     }
61     else if( p_dialog->i_flags & DIALOG_WARNING )
62     {
63         if( config_GetInt( p_intf, "errors-dialog" ) != 0 )
64             ErrorsDialog::getInstance( p_intf )->addWarning(
65                 qfu( p_dialog->psz_title ),qfu( p_dialog->psz_description ) );
66         i_ret = 0;
67     }
68     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
69     {
70         p_dialog->i_status = SENT_DIALOG;
71         i_ret = QMessageBox::question( NULL,
72               qfu( p_dialog->psz_title), qfu( p_dialog->psz_description ),
73               p_dialog->psz_default_button ?
74                     qfu( p_dialog->psz_default_button ) : QString::null,
75               p_dialog->psz_alternate_button ?
76                     qfu( p_dialog->psz_alternate_button ) : QString::null,
77               p_dialog->psz_other_button ?
78                     qfu( p_dialog->psz_other_button ) : QString::null, 0,
79               p_dialog->psz_other_button ? 2 : -1 );
80     }
81     else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
82     {
83         dialog = new QWidget( 0 ); layout = new QVBoxLayout( dialog );
84         layout->setMargin( 2 );
85         panel = new QWidget( 0 );
86         QGridLayout *grid = new QGridLayout;
87
88         description = new QLabel( qfu( p_dialog->psz_description ) );
89         grid->addWidget( description, 0, 0, 1, 2 );
90
91         grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
92         loginEdit = new QLineEdit;
93         grid->addWidget( loginEdit, 1, 1 );
94
95         grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
96         passwordEdit = new QLineEdit;
97         passwordEdit->setEchoMode( QLineEdit::Password );
98         grid->addWidget( passwordEdit, 2, 1 );
99
100         panel->setLayout( grid );
101         layout->addWidget( panel );
102     }
103     else if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
104              /* TEMPORARY ! */ p_dialog->i_flags & DIALOG_INTF_PROGRESS )
105     {
106         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
107         layout->setMargin( 2 );
108         description = new QLabel( qfu( p_dialog->psz_description ) );
109         layout->addWidget( description );
110
111         progressBar = new QProgressBar;
112         progressBar->setMaximum( 1000 );
113         progressBar->setTextVisible( true );
114         progressBar->setOrientation(Qt::Horizontal);
115         layout->addWidget( progressBar );
116     }
117     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
118     {
119         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
120         layout->setMargin( 2 );
121         description = new QLabel( qfu( p_dialog->psz_description ) );
122         layout->addWidget( description );
123
124         inputEdit = new QLineEdit;
125         layout->addWidget( inputEdit );
126     }
127     else
128         msg_Err( p_intf, "unknown dialog type %i", p_dialog->i_flags );
129
130     /* We used a message box */
131     if( i_ret != -1 )
132     {
133         if( i_ret == 0 ) Finish( DIALOG_OK_YES );
134         else if ( i_ret == 1 ) Finish( DIALOG_NO );
135         else Finish( DIALOG_CANCELLED );
136     }
137     else
138     /* Custom box, finish it */
139     {
140         QDialogButtonBox *buttonBox = new QDialogButtonBox;
141
142         if( p_dialog->psz_default_button )
143         {
144             defaultButton = new QPushButton;
145             defaultButton->setFocus();
146             defaultButton->setText( qfu( p_dialog->psz_default_button ) );
147             buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
148         }
149         if( p_dialog->psz_alternate_button )
150         {
151             altButton = new QPushButton;
152             altButton->setText( qfu( p_dialog->psz_alternate_button ) );
153             buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
154         }
155         if( p_dialog->psz_other_button )
156         {
157             otherButton = new QPushButton;
158             otherButton->setText( qfu( p_dialog->psz_other_button ) );
159             buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
160         }
161         if( p_dialog->psz_default_button )
162             BUTTONACT( defaultButton, defaultB() );
163         if( p_dialog->psz_alternate_button )
164             BUTTONACT( altButton, altB() );
165         if( p_dialog->psz_other_button )
166             BUTTONACT( otherButton, otherB() );
167         dialog->setLayout( layout );
168         dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
169     }
170 }
171
172 void InteractionDialog::update()
173 {
174     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
175     {
176         assert( progressBar );
177         progressBar->setValue( (int)(p_dialog->val.f_float*1000) );
178         fprintf (stderr, "Setting progress to %i\n", progressBar->value() );
179     }
180 }
181
182 InteractionDialog::~InteractionDialog()
183 {
184 //    if( panel ) delete panel;
185     if( dialog ) delete dialog;
186 }
187
188 void InteractionDialog::defaultB()
189 {
190     Finish( DIALOG_OK_YES );
191 }
192 void InteractionDialog::altB()
193 {
194     Finish( DIALOG_NO );
195 }
196 void InteractionDialog::otherB()
197 {
198     Finish( DIALOG_CANCELLED );
199 }
200
201 void InteractionDialog::Finish( int i_ret )
202 {
203     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
204
205     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
206     {
207         p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
208         p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
209     }
210     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
211     {
212         p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
213     }
214     p_dialog->i_status = ANSWERED_DIALOG;
215     p_dialog->i_return = i_ret;
216     hide();
217     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
218     playlist_Signal( THEPL );
219 }