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