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;
57 emit self->pointerChanged (object, cur.p_address);
62 DialogHandler::DialogHandler (intf_thread_t *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)
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);
82 SIGNAL(progressBarDestroyed(QWidget *)),
83 SLOT(stopProgressBar(QWidget *)));
85 dialog_Register (intf);
88 DialogHandler::~DialogHandler (void)
90 dialog_Unregister (intf);
93 void DialogHandler::displayMessage (vlc_object_t *, void *value)
95 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
98 QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
101 if (config_GetInt (intf, "qt-error-dialogs"))
102 ErrorsDialog::getInstance (intf)->addError(qfu(dialog->title),
103 qfu(dialog->message));
106 void DialogHandler::requestLogin (vlc_object_t *, void *value)
108 dialog_login_t *data = (dialog_login_t *)value;
109 QDialog *dialog = new QDialog;
110 QLayout *layout = new QVBoxLayout (dialog);
112 dialog->setWindowTitle (qfu(data->title));
113 layout->setMargin (2);
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);
120 QLineEdit *userLine = new QLineEdit;
121 grid->addWidget (new QLabel (qtr("User name")), 1, 0);
122 grid->addWidget (userLine, 1, 1);
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);
129 panel->setLayout (grid);
130 layout->addWidget (panel);
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);
141 dialog->setLayout (layout);
145 *data->username = strdup (qtu(userLine->text ()));
146 *data->password = strdup (qtu(passLine->text ()));
149 *data->username = *data->password = NULL;
154 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
156 dialog_question_t *data = (dialog_question_t *)value;
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)
171 if (box->clickedButton () == yes)
174 if (box->clickedButton () == no)
180 data->answer = answer;
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),
191 if (data->title != NULL)
192 setWindowTitle (qfu(data->title));
193 setMinimumDuration (0);
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)));
200 data->pf_update = update;
201 data->pf_check = check;
202 data->pf_destroy = destroy;
206 QVLCProgressDialog::~QVLCProgressDialog (void)
210 void QVLCProgressDialog::update (void *priv, const char *text, float value)
212 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
215 emit self->described (qfu(text));
216 emit self->progressed ((int)(value * 1000.));
219 static QMutex cancel_mutex;
221 bool QVLCProgressDialog::check (void *priv)
223 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
224 QMutexLocker locker (&cancel_mutex);
225 return self->cancelled;
228 void QVLCProgressDialog::destroy (void *priv)
230 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
232 emit self->handler->progressBarDestroyed (self);
235 void QVLCProgressDialog::saveCancel (void)
237 QMutexLocker locker (&cancel_mutex);
241 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
243 dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
244 QWidget *dlg = new QVLCProgressDialog (this, data);
249 void DialogHandler::stopProgressBar (QWidget *dlg)