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