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>
38 #include <QPushButton>
40 DialogHandler::DialogHandler (intf_thread_t *intf, QObject *_parent)
41 : QObject( _parent ), intf (intf),
42 critical (VLC_OBJECT(intf), "dialog-critical"),
43 login (VLC_OBJECT(intf), "dialog-login"),
44 question (VLC_OBJECT(intf), "dialog-question"),
45 progressBar (VLC_OBJECT(intf), "dialog-progress-bar")
47 var_Create (intf, "dialog-error", VLC_VAR_ADDRESS);
48 var_AddCallback (intf, "dialog-error", error, this);
49 connect (this, SIGNAL(error(const QString &, const QString &)),
50 SLOT(displayError(const QString &, const QString &)));
52 connect (&critical, SIGNAL(pointerChanged(vlc_object_t *, void *)),
53 SLOT(displayCritical(vlc_object_t *, void *)),
54 Qt::BlockingQueuedConnection);
55 connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
56 SLOT(requestLogin(vlc_object_t *, void *)),
57 Qt::BlockingQueuedConnection);
58 connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
59 SLOT(requestAnswer(vlc_object_t *, void *)),
60 Qt::BlockingQueuedConnection);
61 connect (&progressBar, SIGNAL(pointerChanged(vlc_object_t *, void *)),
62 SLOT(startProgressBar(vlc_object_t *, void *)),
63 Qt::BlockingQueuedConnection);
65 SIGNAL(progressBarDestroyed(QWidget *)),
66 SLOT(stopProgressBar(QWidget *)));
68 dialog_Register (intf);
71 DialogHandler::~DialogHandler (void)
73 dialog_Unregister (intf);
75 var_DelCallback (intf, "dialog-error", error, this);
76 var_Destroy (intf, "dialog-error");
79 int DialogHandler::error (vlc_object_t *obj, const char *,
80 vlc_value_t, vlc_value_t value, void *data)
82 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value.p_address;
83 DialogHandler *self = static_cast<DialogHandler *>(data);
85 if (var_InheritBool (obj, "qt-error-dialogs"))
86 emit self->error (qfu(dialog->title), qfu(dialog->message));
90 void DialogHandler::displayError (const QString& title, const QString& message)
92 ErrorsDialog::getInstance (intf)->addError(title, message);
95 void DialogHandler::displayCritical (vlc_object_t *, void *value)
97 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
99 QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
103 void DialogHandler::requestLogin (vlc_object_t *, void *value)
105 dialog_login_t *data = (dialog_login_t *)value;
106 QDialog *dialog = new QDialog;
107 QLayout *layout = new QVBoxLayout (dialog);
109 dialog->setWindowTitle (qfu(data->title));
110 dialog->setWindowRole ("vlc-login");
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 = new QDialogButtonBox;
132 QPushButton *okButton = new QPushButton( "&Ok" );
133 QPushButton *cancelButton = new QPushButton( "&Cancel" );
134 buttonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
135 buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
137 CONNECT( buttonBox, accepted(), dialog, accept() );
138 CONNECT( buttonBox, rejected(), dialog, reject() );
139 layout->addWidget (buttonBox);
142 dialog->setLayout (layout);
146 *data->username = strdup (qtu(userLine->text ()));
147 *data->password = strdup (qtu(passLine->text ()));
150 *data->username = *data->password = NULL;
155 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
157 dialog_question_t *data = (dialog_question_t *)value;
159 QMessageBox *box = new QMessageBox (QMessageBox::Question,
160 qfu(data->title), qfu(data->message));
161 QAbstractButton *yes = (data->yes != NULL)
162 ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
163 QAbstractButton *no = (data->no != NULL)
164 ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
165 if (data->cancel != NULL)
166 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 setWindowRole ("vlc-progress");
194 setMinimumDuration (300);
197 connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
198 connect (this, SIGNAL(described(const QString&)),
199 SLOT(setLabelText(const QString&)));
200 connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
202 data->pf_update = update;
203 data->pf_check = check;
204 data->pf_destroy = destroy;
208 QVLCProgressDialog::~QVLCProgressDialog (void)
212 void QVLCProgressDialog::update (void *priv, const char *text, float value)
214 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
217 emit self->described (qfu(text));
218 emit self->progressed ((int)(value * 1000.));
221 static QMutex cancel_mutex;
223 bool QVLCProgressDialog::check (void *priv)
225 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
226 QMutexLocker locker (&cancel_mutex);
227 return self->cancelled;
230 void QVLCProgressDialog::destroy (void *priv)
232 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
234 emit self->handler->progressBarDestroyed (self);
237 void QVLCProgressDialog::saveCancel (void)
239 QMutexLocker locker (&cancel_mutex);
243 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
245 dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
246 QWidget *dlg = new QVLCProgressDialog (this, data);
251 void DialogHandler::stopProgressBar (QWidget *dlg)