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 *intf, QObject *_parent)
42 : QObject( _parent ), intf (intf),
43 critical (VLC_OBJECT(intf), "dialog-critical"),
44 login (VLC_OBJECT(intf), "dialog-login"),
45 question (VLC_OBJECT(intf), "dialog-question"),
46 progressBar (VLC_OBJECT(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 connect (&critical, SIGNAL(pointerChanged(vlc_object_t *, void *)),
54 SLOT(displayCritical(vlc_object_t *, void *)),
55 Qt::BlockingQueuedConnection);
56 connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
57 SLOT(requestLogin(vlc_object_t *, void *)),
58 Qt::BlockingQueuedConnection);
59 connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
60 SLOT(requestAnswer(vlc_object_t *, void *)),
61 Qt::BlockingQueuedConnection);
62 connect (&progressBar, SIGNAL(pointerChanged(vlc_object_t *, void *)),
63 SLOT(startProgressBar(vlc_object_t *, void *)),
64 Qt::BlockingQueuedConnection);
66 SIGNAL(progressBarDestroyed(QWidget *)),
67 SLOT(stopProgressBar(QWidget *)));
69 dialog_Register (intf);
72 DialogHandler::~DialogHandler (void)
74 dialog_Unregister (intf);
76 var_DelCallback (intf, "dialog-error", error, this);
77 var_Destroy (intf, "dialog-error");
80 int DialogHandler::error (vlc_object_t *obj, const char *,
81 vlc_value_t, vlc_value_t value, void *data)
83 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value.p_address;
84 DialogHandler *self = static_cast<DialogHandler *>(data);
86 if (var_InheritBool (obj, "qt-error-dialogs"))
87 emit self->error (qfu(dialog->title), qfu(dialog->message));
91 void DialogHandler::displayError (const QString& title, const QString& message)
93 ErrorsDialog::getInstance (intf)->addError(title, message);
96 void DialogHandler::displayCritical (vlc_object_t *, void *value)
98 const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
100 QMessageBox::critical (NULL, qfu(dialog->title), 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 dialog->setWindowRole ("vlc-login");
112 layout->setMargin (2);
114 /* User name and password fields */
115 QWidget *panel = new QWidget (dialog);
116 QGridLayout *grid = new QGridLayout;
117 grid->addWidget (new QLabel (qfu(data->message)), 0, 0, 1, 2);
119 QLineEdit *userLine = new QLineEdit;
120 grid->addWidget (new QLabel (qtr("User name")), 1, 0);
121 grid->addWidget (userLine, 1, 1);
123 QLineEdit *passLine = new QLineEdit;
124 passLine->setEchoMode (QLineEdit::Password);
125 grid->addWidget (new QLabel (qtr("Password")), 2, 0);
126 grid->addWidget (passLine, 2, 1);
128 panel->setLayout (grid);
129 layout->addWidget (panel);
131 /* OK, Cancel buttons */
132 QDialogButtonBox *buttonBox = new QDialogButtonBox;
133 QPushButton *okButton = new QPushButton( "&Ok" );
134 QPushButton *cancelButton = new QPushButton( "&Cancel" );
135 buttonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
136 buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
138 CONNECT( buttonBox, accepted(), dialog, accept() );
139 CONNECT( buttonBox, rejected(), dialog, reject() );
140 layout->addWidget (buttonBox);
143 dialog->setLayout (layout);
147 *data->username = strdup (qtu(userLine->text ()));
148 *data->password = strdup (qtu(passLine->text ()));
151 *data->username = *data->password = NULL;
156 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
158 dialog_question_t *data = (dialog_question_t *)value;
160 QMessageBox *box = new QMessageBox (QMessageBox::Question,
161 qfu(data->title), qfu(data->message));
162 QAbstractButton *yes = (data->yes != NULL)
163 ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
164 QAbstractButton *no = (data->no != NULL)
165 ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
166 if (data->cancel != NULL)
167 box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole);
172 if (box->clickedButton () == yes)
175 if (box->clickedButton () == no)
181 data->answer = answer;
185 QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
186 struct dialog_progress_bar_t *data)
187 : QProgressDialog ( ),
191 setLabelText( qfu(data->message) );
195 setCancelButton( new QPushButton( "&" + qfu(data->cancel) ) );
196 if (data->title != NULL)
197 setWindowTitle (qfu(data->title));
199 setWindowRole ("vlc-progress");
200 setMinimumDuration (300);
203 connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
204 connect (this, SIGNAL(described(const QString&)),
205 SLOT(setLabelText(const QString&)));
206 connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
208 data->pf_update = update;
209 data->pf_check = check;
210 data->pf_destroy = destroy;
215 void QVLCProgressDialog::update (void *priv, const char *text, float value)
217 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
219 self->setRange( 0, 1000 );
221 emit self->described (qfu(text));
222 emit self->progressed ((int)(value * 1000.));
225 static QMutex cancel_mutex;
227 bool QVLCProgressDialog::check (void *priv)
229 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
230 QMutexLocker locker (&cancel_mutex);
231 return self->cancelled;
234 void QVLCProgressDialog::destroy (void *priv)
236 QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
238 emit self->handler->progressBarDestroyed (self);
241 void QVLCProgressDialog::saveCancel (void)
243 QMutexLocker locker (&cancel_mutex);
247 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
249 dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
250 QWidget *dlg = new QVLCProgressDialog (this, data);
252 QTimer::singleShot( 300, dlg, SLOT( show() ) );
256 void DialogHandler::stopProgressBar (QWidget *dlg)