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