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