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>
41 DialogHandler::DialogHandler (intf_thread_t *p_intf, QObject *_parent)
42 : QObject( _parent ), intf (p_intf),
43 critical (VLC_OBJECT(p_intf), "dialog-critical"),
44 login (VLC_OBJECT(p_intf), "dialog-login"),
45 question (VLC_OBJECT(p_intf), "dialog-question"),
46 progressBar (VLC_OBJECT(p_intf), "dialog-progress-bar")
48 var_Create (intf, "dialog-error", VLC_VAR_ADDRESS);
49 var_AddCallback (intf, "dialog-error", error, this);
50 connect (this, SIGNAL(error(const QString &, const QString &)),
51 SLOT(displayError(const QString &, const QString &)));
53 critical.addCallback(this, SLOT(displayCritical(void *)),
54 Qt::BlockingQueuedConnection);
55 login.addCallback(this, SLOT(requestLogin(void *)),
56 Qt::BlockingQueuedConnection);
57 question.addCallback(this, SLOT(requestAnswer(void *)),
58 Qt::BlockingQueuedConnection);
59 question.addCallback(this, SLOT(requestAnswer(void *)),
60 Qt::BlockingQueuedConnection);
61 progressBar.addCallback(this, SLOT(startProgressBar(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 (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 (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 = new QDialogButtonBox;
131 QPushButton *okButton = new QPushButton( "&Ok" );
132 QPushButton *cancelButton = new QPushButton( "&Cancel" );
133 buttonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
134 buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
136 CONNECT( buttonBox, accepted(), dialog, accept() );
137 CONNECT( buttonBox, rejected(), dialog, 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 (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 if (data->cancel != NULL)
165 box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole);
170 if (box->clickedButton () == yes)
173 if (box->clickedButton () == no)
179 data->answer = answer;
183 QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
184 struct dialog_progress_bar_t *data)
185 : QProgressDialog ( ),
189 setLabelText( qfu(data->message) );
192 setWindowModality ( Qt::ApplicationModal );
195 setCancelButton( new QPushButton( "&" + qfu(data->cancel) ) );
196 if (data->title != NULL)
197 setWindowTitle (qfu(data->title));
199 setWindowRole ("vlc-progress");
202 connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
203 connect (this, SIGNAL(described(const QString&)),
204 SLOT(setLabelText(const QString&)));
205 connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
207 data->pf_update = update;
208 data->pf_check = check;
209 data->pf_destroy = destroy;
214 void QVLCProgressDialog::update (void *priv, const char *text, float value)
216 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
218 self->setRange( 0, 1000 );
220 emit self->described (qfu(text));
221 emit self->progressed ((int)(value * 1000.));
224 static QMutex cancel_mutex;
226 bool QVLCProgressDialog::check (void *priv)
228 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
229 QMutexLocker locker (&cancel_mutex);
230 return self->cancelled;
233 void QVLCProgressDialog::destroy (void *priv)
235 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
237 emit self->handler->progressBarDestroyed (self);
240 void QVLCProgressDialog::saveCancel (void)
242 QMutexLocker locker (&cancel_mutex);
246 void DialogHandler::startProgressBar (void *value)
248 dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
249 QWidget *dlg = new QVLCProgressDialog (this, data);
251 QTimer::singleShot( 1500, dlg, SLOT( show() ) );
255 void DialogHandler::stopProgressBar (QWidget *dlg)