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
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.
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.
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 *****************************************************************************/
27 #include "external.hpp"
29 #include <vlc_dialog.h>
32 #include <QDialogButtonBox>
35 #include <QMessageBox>
36 #include <QProgressDialog>
39 QVLCVariable::QVLCVariable (vlc_object_t *obj, const char *varname, int type)
40 : object (obj), name (qfu(varname))
42 var_Create (object, qtu(name), type);
43 var_AddCallback (object, qtu(name), callback, this);
46 QVLCVariable::~QVLCVariable (void)
48 var_DelCallback (object, qtu(name), callback, this);
49 var_Destroy (object, qtu(name));
52 int QVLCVariable::callback (vlc_object_t *object, const char *,
53 vlc_value_t, vlc_value_t cur, void *data)
55 QVLCVariable *self = (QVLCVariable *)data;
56 emit self->pointerChanged (object, cur.p_address);
60 DialogHandler::DialogHandler (intf_thread_t *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)
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);
80 SIGNAL(progressBarDestroyed(QWidget *)),
81 SLOT(stopProgressBar(QWidget *)));
83 dialog_Register (intf);
86 DialogHandler::~DialogHandler (void)
88 dialog_Unregister (intf);
91 void DialogHandler::displayMessage (vlc_object_t *, void *value)
93 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
96 QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
99 if (config_GetInt (intf, "qt-error-dialogs"))
100 ErrorsDialog::getInstance (intf)->addError(qfu(dialog->title),
101 qfu(dialog->message));
104 void DialogHandler::requestLogin (vlc_object_t *, void *value)
106 dialog_login_t *data = (dialog_login_t *)value;
107 QDialog *dialog = new QDialog;
108 QLayout *layout = new QVBoxLayout (dialog);
110 dialog->setWindowTitle (qfu(data->title));
111 layout->setMargin (2);
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);
118 QLineEdit *userLine = new QLineEdit;
119 grid->addWidget (new QLabel (qtr("User name")), 1, 0);
120 grid->addWidget (userLine, 1, 1);
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);
127 panel->setLayout (grid);
128 layout->addWidget (panel);
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);
139 dialog->setLayout (layout);
143 *data->username = strdup (qtu(userLine->text ()));
144 *data->password = strdup (qtu(passLine->text ()));
147 *data->username = *data->password = NULL;
152 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
154 dialog_question_t *data = (dialog_question_t *)value;
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)
169 if (box->clickedButton () == yes)
172 if (box->clickedButton () == no)
178 data->answer = answer;
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),
189 if (data->title != NULL)
190 setWindowTitle (qfu(data->title));
191 setMinimumDuration (0);
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)));
198 data->pf_update = update;
199 data->pf_check = check;
200 data->pf_destroy = destroy;
204 QVLCProgressDialog::~QVLCProgressDialog (void)
208 void QVLCProgressDialog::update (void *priv, const char *text, float value)
210 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
213 emit self->described (qfu(text));
214 emit self->progressed ((int)(value * 1000.));
217 static QMutex cancel_mutex;
219 bool QVLCProgressDialog::check (void *priv)
221 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
222 QMutexLocker locker (&cancel_mutex);
223 return self->cancelled;
226 void QVLCProgressDialog::destroy (void *priv)
228 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
230 emit self->handler->progressBarDestroyed (self);
233 void QVLCProgressDialog::saveCancel (void)
235 QMutexLocker locker (&cancel_mutex);
239 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
241 dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
242 QWidget *dlg = new QVLCProgressDialog (this, data);
247 void DialogHandler::stopProgressBar (QWidget *dlg)