]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/external.cpp
Qt: forgotten include
[vlc] / modules / gui / qt4 / dialogs / external.cpp
1 /*****************************************************************************
2  * external.cpp : Dialogs from other LibVLC core and other plugins
3  ****************************************************************************
4  * Copyright (C) 2009 RĂ©mi Denis-Courmont
5  * Copyright (C) 2006 the VideoLAN team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 //#include "qt4.hpp"
27 #include "external.hpp"
28 #include "errors.hpp"
29 #include <vlc_dialog.h>
30
31 #include <QDialog>
32 #include <QDialogButtonBox>
33 #include <QLabel>
34 #include <QLineEdit>
35 #include <QMessageBox>
36 #include <QProgressDialog>
37 #include <QMutex>
38 #include <QPushButton>
39 #include <QTimer>
40
41 DialogHandler::DialogHandler (intf_thread_t *intf, QObject *_parent)
42     : QObject( _parent ), intf (intf),
43       critical (VLC_OBJECT(intf), "dialog-critical"),
44       login (VLC_OBJECT(intf), "dialog-login"),
45       question (VLC_OBJECT(intf), "dialog-question"),
46       progressBar (VLC_OBJECT(intf), "dialog-progress-bar")
47 {
48     var_Create (intf, "dialog-error", VLC_VAR_ADDRESS);
49     var_AddCallback (intf, "dialog-error", error, this);
50     connect (this, SIGNAL(error(const QString &, const QString &)),
51              SLOT(displayError(const QString &, const QString &)));
52
53     connect (&critical, SIGNAL(pointerChanged(vlc_object_t *, void *)),
54              SLOT(displayCritical(vlc_object_t *, void *)),
55              Qt::BlockingQueuedConnection);
56     connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
57              SLOT(requestLogin(vlc_object_t *, void *)),
58              Qt::BlockingQueuedConnection);
59     connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
60              SLOT(requestAnswer(vlc_object_t *, void *)),
61              Qt::BlockingQueuedConnection);
62     connect (&progressBar, SIGNAL(pointerChanged(vlc_object_t *, void *)),
63              SLOT(startProgressBar(vlc_object_t *, void *)),
64              Qt::BlockingQueuedConnection);
65     connect (this,
66              SIGNAL(progressBarDestroyed(QWidget *)),
67              SLOT(stopProgressBar(QWidget *)));
68
69     dialog_Register (intf);
70 }
71
72 DialogHandler::~DialogHandler (void)
73 {
74     dialog_Unregister (intf);
75
76     var_DelCallback (intf, "dialog-error", error, this);
77     var_Destroy (intf, "dialog-error");
78 }
79
80 int DialogHandler::error (vlc_object_t *obj, const char *,
81                           vlc_value_t, vlc_value_t value, void *data)
82 {
83     const dialog_fatal_t *dialog = (const dialog_fatal_t *)value.p_address;
84     DialogHandler *self = static_cast<DialogHandler *>(data);
85
86     if (var_InheritBool (obj, "qt-error-dialogs"))
87         emit self->error (qfu(dialog->title), qfu(dialog->message));
88     return VLC_SUCCESS;
89 }
90
91 void DialogHandler::displayError (const QString& title, const QString& message)
92 {
93     ErrorsDialog::getInstance (intf)->addError(title, message);
94 }
95
96 void DialogHandler::displayCritical (vlc_object_t *, void *value)
97 {
98     const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
99
100     QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
101                            QMessageBox::Ok);
102 }
103
104 void DialogHandler::requestLogin (vlc_object_t *, void *value)
105 {
106     dialog_login_t *data = (dialog_login_t *)value;
107     QDialog *dialog = new QDialog;
108     QLayout *layout = new QVBoxLayout (dialog);
109
110     dialog->setWindowTitle (qfu(data->title));
111     dialog->setWindowRole ("vlc-login");
112     layout->setMargin (2);
113
114     /* User name and password fields */
115     QWidget *panel = new QWidget (dialog);
116     QGridLayout *grid = new QGridLayout;
117     grid->addWidget (new QLabel (qfu(data->message)), 0, 0, 1, 2);
118
119     QLineEdit *userLine = new QLineEdit;
120     grid->addWidget (new QLabel (qtr("User name")), 1, 0);
121     grid->addWidget (userLine, 1, 1);
122
123     QLineEdit *passLine = new QLineEdit;
124     passLine->setEchoMode (QLineEdit::Password);
125     grid->addWidget (new QLabel (qtr("Password")), 2, 0);
126     grid->addWidget (passLine, 2, 1);
127
128     panel->setLayout (grid);
129     layout->addWidget (panel);
130
131     /* OK, Cancel buttons */
132     QDialogButtonBox *buttonBox = new QDialogButtonBox;
133     QPushButton *okButton = new QPushButton( "&Ok" );
134     QPushButton *cancelButton = new QPushButton( "&Cancel" );
135     buttonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
136     buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
137
138     CONNECT( buttonBox, accepted(), dialog, accept() );
139     CONNECT( buttonBox, rejected(), dialog, reject() );
140     layout->addWidget (buttonBox);
141
142     /* Run the dialog */
143     dialog->setLayout (layout);
144
145     if (dialog->exec ())
146     {
147         *data->username = strdup (qtu(userLine->text ()));
148         *data->password = strdup (qtu(passLine->text ()));
149     }
150     else
151         *data->username = *data->password = NULL;
152
153     delete dialog;
154 }
155
156 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
157 {
158     dialog_question_t *data = (dialog_question_t *)value;
159
160     QMessageBox *box = new QMessageBox (QMessageBox::Question,
161                                         qfu(data->title), qfu(data->message));
162     QAbstractButton *yes = (data->yes != NULL)
163         ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
164     QAbstractButton *no = (data->no != NULL)
165         ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
166     if (data->cancel != NULL)
167         box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole);
168
169     box->exec ();
170
171     int answer;
172     if (box->clickedButton () == yes)
173         answer = 1;
174     else
175     if (box->clickedButton () == no)
176         answer = 2;
177     else
178         answer = 3;
179
180     delete box;
181     data->answer = answer;
182 }
183
184
185 QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
186                                         struct dialog_progress_bar_t *data)
187     : QProgressDialog ( ),
188       handler (parent),
189       cancelled (false)
190 {
191     setLabelText( qfu(data->message) );
192     setRange( 0, 0 );
193
194     if( data->cancel )
195         setCancelButton( new QPushButton( "&" + qfu(data->cancel) ) );
196     if (data->title != NULL)
197         setWindowTitle (qfu(data->title));
198
199     setWindowRole ("vlc-progress");
200     setMinimumDuration (300);
201     setValue( 0 );
202
203     connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
204     connect (this, SIGNAL(described(const QString&)),
205                    SLOT(setLabelText(const QString&)));
206     connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
207
208     data->pf_update = update;
209     data->pf_check = check;
210     data->pf_destroy = destroy;
211     data->p_sys = this;
212 }
213
214
215 void QVLCProgressDialog::update (void *priv, const char *text, float value)
216 {
217     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
218     if( value > 0 )
219         self->setRange( 0, 1000 );
220     if (text != NULL)
221         emit self->described (qfu(text));
222     emit self->progressed ((int)(value * 1000.));
223 }
224
225 static QMutex cancel_mutex;
226
227 bool QVLCProgressDialog::check (void *priv)
228 {
229     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
230     QMutexLocker locker (&cancel_mutex);
231     return self->cancelled;
232 }
233
234 void QVLCProgressDialog::destroy (void *priv)
235 {
236     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
237
238     emit self->handler->progressBarDestroyed (self);
239 }
240
241 void QVLCProgressDialog::saveCancel (void)
242 {
243     QMutexLocker locker (&cancel_mutex);
244     cancelled = true;
245 }
246
247 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
248 {
249     dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
250     QWidget *dlg = new QVLCProgressDialog (this, data);
251
252     QTimer::singleShot( 300, dlg, SLOT( show() ) );
253 //    dlg->show ();
254 }
255
256 void DialogHandler::stopProgressBar (QWidget *dlg)
257 {
258     delete dlg;
259 }