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
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 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")
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 &)));
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);
64 SIGNAL(progressBarDestroyed(QWidget *)),
65 SLOT(stopProgressBar(QWidget *)));
67 dialog_Register (intf);
70 DialogHandler::~DialogHandler (void)
72 dialog_Unregister (intf);
74 var_DelCallback (intf, "dialog-error", error, this);
75 var_Destroy (intf, "dialog-error");
78 int DialogHandler::error (vlc_object_t *obj, const char *,
79 vlc_value_t, vlc_value_t value, void *data)
81 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value.p_address;
82 DialogHandler *self = static_cast<DialogHandler *>(data);
84 if (var_InheritBool (obj, "qt-error-dialogs"))
85 emit self->error (qfu(dialog->title), qfu(dialog->message));
89 void DialogHandler::displayError (const QString& title, const QString& message)
91 ErrorsDialog::getInstance (intf)->addError(title, message);
94 void DialogHandler::displayCritical (vlc_object_t *, void *value)
96 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
98 QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
102 void DialogHandler::requestLogin (vlc_object_t *, void *value)
104 dialog_login_t *data = (dialog_login_t *)value;
105 QDialog *dialog = new QDialog;
106 QLayout *layout = new QVBoxLayout (dialog);
108 dialog->setWindowTitle (qfu(data->title));
109 dialog->setWindowRole ("vlc-login");
110 layout->setMargin (2);
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);
117 QLineEdit *userLine = new QLineEdit;
118 grid->addWidget (new QLabel (qtr("User name")), 1, 0);
119 grid->addWidget (userLine, 1, 1);
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);
126 panel->setLayout (grid);
127 layout->addWidget (panel);
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);
138 dialog->setLayout (layout);
142 *data->username = strdup (qtu(userLine->text ()));
143 *data->password = strdup (qtu(passLine->text ()));
146 *data->username = *data->password = NULL;
151 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
153 dialog_question_t *data = (dialog_question_t *)value;
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);
167 if (box->clickedButton () == yes)
170 if (box->clickedButton () == no)
176 data->answer = answer;
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),
187 if (data->title != NULL)
188 setWindowTitle (qfu(data->title));
189 setWindowRole ("vlc-progress");
190 setMinimumDuration (0);
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)));
197 data->pf_update = update;
198 data->pf_check = check;
199 data->pf_destroy = destroy;
203 QVLCProgressDialog::~QVLCProgressDialog (void)
207 void QVLCProgressDialog::update (void *priv, const char *text, float value)
209 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
212 emit self->described (qfu(text));
213 emit self->progressed ((int)(value * 1000.));
216 static QMutex cancel_mutex;
218 bool QVLCProgressDialog::check (void *priv)
220 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
221 QMutexLocker locker (&cancel_mutex);
222 return self->cancelled;
225 void QVLCProgressDialog::destroy (void *priv)
227 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
229 emit self->handler->progressBarDestroyed (self);
232 void QVLCProgressDialog::saveCancel (void)
234 QMutexLocker locker (&cancel_mutex);
238 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
240 dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
241 QWidget *dlg = new QVLCProgressDialog (this, data);
246 void DialogHandler::stopProgressBar (QWidget *dlg)