]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/external.cpp
dialog_ProgressSet: add an optional string parameter
[vlc] / modules / gui / qt4 / dialogs / external.cpp
1 /*****************************************************************************
2  * external.hpp : 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
39 QVLCVariable::QVLCVariable (vlc_object_t *obj, const char *varname, int type)
40     : object (obj), name (qfu(varname))
41 {
42     var_Create (object, qtu(name), type);
43     var_AddCallback (object, qtu(name), callback, this);
44 }
45
46 QVLCVariable::~QVLCVariable (void)
47 {
48     var_DelCallback (object, qtu(name), callback, this);
49     var_Destroy (object, qtu(name));
50 }
51
52 int QVLCVariable::callback (vlc_object_t *object, const char *,
53                             vlc_value_t, vlc_value_t cur, void *data)
54 {
55     QVLCVariable *self = (QVLCVariable *)data;
56     emit self->pointerChanged (object, cur.p_address);
57 }
58
59
60 DialogHandler::DialogHandler (intf_thread_t *intf)
61     : intf (intf),
62       message (VLC_OBJECT(intf), "dialog-fatal", VLC_VAR_ADDRESS),
63       login (VLC_OBJECT(intf), "dialog-login", VLC_VAR_ADDRESS),
64       question (VLC_OBJECT(intf), "dialog-question", VLC_VAR_ADDRESS),
65       progressBar (VLC_OBJECT(intf), "dialog-progress-bar", VLC_VAR_ADDRESS)
66 {
67     connect (&message, SIGNAL(pointerChanged(vlc_object_t *, void *)),
68              SLOT(displayMessage(vlc_object_t *, void *)),
69              Qt::BlockingQueuedConnection);
70     connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
71              SLOT(requestLogin(vlc_object_t *, void *)),
72              Qt::BlockingQueuedConnection);
73     connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
74              SLOT(requestAnswer(vlc_object_t *, void *)),
75              Qt::BlockingQueuedConnection);
76     connect (&progressBar, SIGNAL(pointerChanged(vlc_object_t *, void *)),
77              SLOT(startProgressBar(vlc_object_t *, void *)),
78              Qt::BlockingQueuedConnection);
79     connect (this,
80              SIGNAL(progressBarDestroyed(QWidget *)),
81              SLOT(stopProgressBar(QWidget *)));
82
83     dialog_Register (intf);
84 }
85
86 DialogHandler::~DialogHandler (void)
87 {
88     dialog_Unregister (intf);
89 }
90
91 void DialogHandler::displayMessage (vlc_object_t *, void *value)
92 {
93      const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
94
95     if (dialog->modal)
96         QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
97                                QMessageBox::Ok);
98     else
99     if (config_GetInt (intf, "qt-error-dialogs"))
100         ErrorsDialog::getInstance (intf)->addError(qfu(dialog->title),
101                                                    qfu(dialog->message));
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     layout->setMargin (2);
112
113     /* User name and password fields */
114     QWidget *panel = new QWidget (dialog);
115     QGridLayout *grid = new QGridLayout;
116     grid->addWidget (new QLabel (qfu(data->message)), 0, 0, 1, 2);
117
118     QLineEdit *userLine = new QLineEdit;
119     grid->addWidget (new QLabel (qtr("User name")), 1, 0);
120     grid->addWidget (userLine, 1, 1);
121
122     QLineEdit *passLine = new QLineEdit;
123     passLine->setEchoMode (QLineEdit::Password);
124     grid->addWidget (new QLabel (qtr("Password")), 2, 0);
125     grid->addWidget (passLine, 2, 1);
126
127     panel->setLayout (grid);
128     layout->addWidget (panel);
129
130     /* OK, Cancel buttons */
131     QDialogButtonBox *buttonBox;
132     buttonBox = new QDialogButtonBox (QDialogButtonBox::Ok
133                                        | QDialogButtonBox::Cancel);
134     connect (buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
135     connect (buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
136     layout->addWidget (buttonBox);
137
138     /* Run the dialog */
139     dialog->setLayout (layout);
140
141     if (dialog->exec ())
142     {
143         *data->username = strdup (qtu(userLine->text ()));
144         *data->password = strdup (qtu(passLine->text ()));
145     }
146     else
147         *data->username = *data->password = NULL;
148
149     delete dialog;
150 }
151
152 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
153 {
154     dialog_question_t *data = (dialog_question_t *)value;
155
156     QMessageBox *box = new QMessageBox (QMessageBox::Question,
157                                         qfu(data->title), qfu(data->message));
158     QAbstractButton *yes = (data->yes != NULL)
159         ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
160     QAbstractButton *no = (data->no != NULL)
161         ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
162     QAbstractButton *cancel = (data->cancel != NULL)
163         ? box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole)
164         : NULL;
165
166     box->exec ();
167
168     int answer;
169     if (box->clickedButton () == yes)
170         answer = 1;
171     else
172     if (box->clickedButton () == no)
173         answer = 2;
174     else
175         answer = 3;
176
177     delete box;
178     data->answer = answer;
179 }
180
181
182 QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
183                                         struct dialog_progress_bar_t *data)
184     : QProgressDialog (qfu(data->message),
185                        data->cancel ? ("&" + qfu(data->cancel)) : 0, 0, 1000),
186       cancelled (false),
187       handler (parent)
188 {
189     if (data->title != NULL)
190         setWindowTitle (qfu(data->title));
191     setMinimumDuration (0);
192
193     connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
194     connect (this, SIGNAL(described(const QString&)),
195                    SLOT(setLabelText(const QString&)));
196     connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
197
198     data->pf_update = update;
199     data->pf_check = check;
200     data->pf_destroy = destroy;
201     data->p_sys = this;
202 }
203
204 QVLCProgressDialog::~QVLCProgressDialog (void)
205 {
206 }
207
208 void QVLCProgressDialog::update (void *priv, const char *text, float value)
209 {
210     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
211
212     if (text != NULL)
213         emit self->described (qfu(text));
214     emit self->progressed ((int)(value * 1000.));
215 }
216
217 static QMutex cancel_mutex;
218
219 bool QVLCProgressDialog::check (void *priv)
220 {
221     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
222     QMutexLocker locker (&cancel_mutex);
223     return self->cancelled;
224 }
225
226 void QVLCProgressDialog::destroy (void *priv)
227 {
228     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
229
230     emit self->handler->progressBarDestroyed (self);
231 }
232
233 void QVLCProgressDialog::saveCancel (void)
234 {
235     QMutexLocker locker (&cancel_mutex);
236     cancelled = true;
237 }
238
239 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
240 {
241     dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
242     QWidget *dlg = new QVLCProgressDialog (this, data);
243
244     dlg->show ();
245 }
246
247 void DialogHandler::stopProgressBar (QWidget *dlg)
248 {
249     delete dlg;
250 }