]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/external.cpp
e9e20eb037a73eabd2e1b248474d0ba80fbd38c8
[vlc] / modules / gui / qt4 / dialogs / external.cpp
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
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 //#include "qt4.hpp"
27 #include "external.hpp"
28 #include "errors.hpp"
29 #include <vlc_dialog.h>
30
31 #include <QDialog>
32 #include <QDialogButtonBox>
33 #include <QLabel>
34 #include <QLineEdit>
35 #include <QMessageBox>
36 #include <QProgressDialog>
37 #include <QMutex>
38 #include <QPushButton>
39
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")
46 {
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 &)));
51
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);
64     connect (this,
65              SIGNAL(progressBarDestroyed(QWidget *)),
66              SLOT(stopProgressBar(QWidget *)));
67
68     dialog_Register (intf);
69 }
70
71 DialogHandler::~DialogHandler (void)
72 {
73     dialog_Unregister (intf);
74
75     var_DelCallback (intf, "dialog-error", error, this);
76     var_Destroy (intf, "dialog-error");
77 }
78
79 int DialogHandler::error (vlc_object_t *obj, const char *,
80                           vlc_value_t, vlc_value_t value, void *data)
81 {
82     const dialog_fatal_t *dialog = (const dialog_fatal_t *)value.p_address;
83     DialogHandler *self = static_cast<DialogHandler *>(data);
84
85     if (var_InheritBool (obj, "qt-error-dialogs"))
86         emit self->error (qfu(dialog->title), qfu(dialog->message));
87     return VLC_SUCCESS;
88 }
89
90 void DialogHandler::displayError (const QString& title, const QString& message)
91 {
92     ErrorsDialog::getInstance (intf)->addError(title, message);
93 }
94
95 void DialogHandler::displayCritical (vlc_object_t *, void *value)
96 {
97     const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
98
99     QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
100                            QMessageBox::Ok);
101 }
102
103 void DialogHandler::requestLogin (vlc_object_t *, void *value)
104 {
105     dialog_login_t *data = (dialog_login_t *)value;
106     QDialog *dialog = new QDialog;
107     QLayout *layout = new QVBoxLayout (dialog);
108
109     dialog->setWindowTitle (qfu(data->title));
110     dialog->setWindowRole ("vlc-login");
111     layout->setMargin (2);
112
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);
117
118     QLineEdit *userLine = new QLineEdit;
119     grid->addWidget (new QLabel (qtr("User name")), 1, 0);
120     grid->addWidget (userLine, 1, 1);
121
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);
126
127     panel->setLayout (grid);
128     layout->addWidget (panel);
129
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 );
136
137     CONNECT( buttonBox, accepted(), dialog, accept() );
138     CONNECT( buttonBox, rejected(), dialog, reject() );
139     layout->addWidget (buttonBox);
140
141     /* Run the dialog */
142     dialog->setLayout (layout);
143
144     if (dialog->exec ())
145     {
146         *data->username = strdup (qtu(userLine->text ()));
147         *data->password = strdup (qtu(passLine->text ()));
148     }
149     else
150         *data->username = *data->password = NULL;
151
152     delete dialog;
153 }
154
155 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
156 {
157     dialog_question_t *data = (dialog_question_t *)value;
158
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);
167
168     box->exec ();
169
170     int answer;
171     if (box->clickedButton () == yes)
172         answer = 1;
173     else
174     if (box->clickedButton () == no)
175         answer = 2;
176     else
177         answer = 3;
178
179     delete box;
180     data->answer = answer;
181 }
182
183
184 QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
185                                         struct dialog_progress_bar_t *data)
186     : QProgressDialog ( ),
187       handler (parent),
188       cancelled (false)
189 {
190     setLabelText( qfu(data->message) );
191     setRange( 0, 0 );
192
193     if( data->cancel )
194         setCancelButton( new QPushButton( "&" + qfu(data->cancel) ) );
195     if (data->title != NULL)
196         setWindowTitle (qfu(data->title));
197
198     setWindowRole ("vlc-progress");
199     setMinimumDuration (300);
200     setValue( 0 );
201
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)));
206
207     data->pf_update = update;
208     data->pf_check = check;
209     data->pf_destroy = destroy;
210     data->p_sys = this;
211 }
212
213
214 void QVLCProgressDialog::update (void *priv, const char *text, float value)
215 {
216     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
217     if( value > 0 )
218         self->setRange( 0, 1000 );
219     if (text != NULL)
220         emit self->described (qfu(text));
221     emit self->progressed ((int)(value * 1000.));
222 }
223
224 static QMutex cancel_mutex;
225
226 bool QVLCProgressDialog::check (void *priv)
227 {
228     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
229     QMutexLocker locker (&cancel_mutex);
230     return self->cancelled;
231 }
232
233 void QVLCProgressDialog::destroy (void *priv)
234 {
235     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
236
237     emit self->handler->progressBarDestroyed (self);
238 }
239
240 void QVLCProgressDialog::saveCancel (void)
241 {
242     QMutexLocker locker (&cancel_mutex);
243     cancelled = true;
244 }
245
246 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
247 {
248     dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
249     QWidget *dlg = new QVLCProgressDialog (this, data);
250
251     QTimer::singleShot( 300, dlg, SLOT( show() ) );
252 //    dlg->show ();
253 }
254
255 void DialogHandler::stopProgressBar (QWidget *dlg)
256 {
257     delete dlg;
258 }