]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/external.cpp
Merge branch 1.0-bugfix
[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
39 DialogHandler::DialogHandler (intf_thread_t *intf)
40     : intf (intf),
41       message (VLC_OBJECT(intf), "dialog-fatal", VLC_VAR_ADDRESS),
42       login (VLC_OBJECT(intf), "dialog-login", VLC_VAR_ADDRESS),
43       question (VLC_OBJECT(intf), "dialog-question", VLC_VAR_ADDRESS),
44       progressBar (VLC_OBJECT(intf), "dialog-progress-bar", VLC_VAR_ADDRESS)
45 {
46     connect (&message, SIGNAL(pointerChanged(vlc_object_t *, void *)),
47              SLOT(displayMessage(vlc_object_t *, void *)),
48              Qt::BlockingQueuedConnection);
49     connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
50              SLOT(requestLogin(vlc_object_t *, void *)),
51              Qt::BlockingQueuedConnection);
52     connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
53              SLOT(requestAnswer(vlc_object_t *, void *)),
54              Qt::BlockingQueuedConnection);
55     connect (&progressBar, SIGNAL(pointerChanged(vlc_object_t *, void *)),
56              SLOT(startProgressBar(vlc_object_t *, void *)),
57              Qt::BlockingQueuedConnection);
58     connect (this,
59              SIGNAL(progressBarDestroyed(QWidget *)),
60              SLOT(stopProgressBar(QWidget *)));
61
62     dialog_Register (intf);
63 }
64
65 DialogHandler::~DialogHandler (void)
66 {
67     dialog_Unregister (intf);
68 }
69
70 void DialogHandler::displayMessage (vlc_object_t *, void *value)
71 {
72      const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
73
74     if (dialog->modal)
75         QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
76                                QMessageBox::Ok);
77     else
78     if (config_GetInt (intf, "qt-error-dialogs"))
79         ErrorsDialog::getInstance (intf)->addError(qfu(dialog->title),
80                                                    qfu(dialog->message));
81 }
82
83 void DialogHandler::requestLogin (vlc_object_t *, void *value)
84 {
85     dialog_login_t *data = (dialog_login_t *)value;
86     QDialog *dialog = new QDialog;
87     QLayout *layout = new QVBoxLayout (dialog);
88
89     dialog->setWindowTitle (qfu(data->title));
90     layout->setMargin (2);
91
92     /* User name and password fields */
93     QWidget *panel = new QWidget (dialog);
94     QGridLayout *grid = new QGridLayout;
95     grid->addWidget (new QLabel (qfu(data->message)), 0, 0, 1, 2);
96
97     QLineEdit *userLine = new QLineEdit;
98     grid->addWidget (new QLabel (qtr("User name")), 1, 0);
99     grid->addWidget (userLine, 1, 1);
100
101     QLineEdit *passLine = new QLineEdit;
102     passLine->setEchoMode (QLineEdit::Password);
103     grid->addWidget (new QLabel (qtr("Password")), 2, 0);
104     grid->addWidget (passLine, 2, 1);
105
106     panel->setLayout (grid);
107     layout->addWidget (panel);
108
109     /* OK, Cancel buttons */
110     QDialogButtonBox *buttonBox;
111     buttonBox = new QDialogButtonBox (QDialogButtonBox::Ok
112                                        | QDialogButtonBox::Cancel);
113     connect (buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
114     connect (buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
115     layout->addWidget (buttonBox);
116
117     /* Run the dialog */
118     dialog->setLayout (layout);
119
120     if (dialog->exec ())
121     {
122         *data->username = strdup (qtu(userLine->text ()));
123         *data->password = strdup (qtu(passLine->text ()));
124     }
125     else
126         *data->username = *data->password = NULL;
127
128     delete dialog;
129 }
130
131 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
132 {
133     dialog_question_t *data = (dialog_question_t *)value;
134
135     QMessageBox *box = new QMessageBox (QMessageBox::Question,
136                                         qfu(data->title), qfu(data->message));
137     QAbstractButton *yes = (data->yes != NULL)
138         ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
139     QAbstractButton *no = (data->no != NULL)
140         ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
141     QAbstractButton *cancel = (data->cancel != NULL)
142         ? box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole)
143         : NULL;
144
145     box->exec ();
146
147     int answer;
148     if (box->clickedButton () == yes)
149         answer = 1;
150     else
151     if (box->clickedButton () == no)
152         answer = 2;
153     else
154         answer = 3;
155
156     delete box;
157     data->answer = answer;
158 }
159
160
161 QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
162                                         struct dialog_progress_bar_t *data)
163     : QProgressDialog (qfu(data->message),
164                        data->cancel ? ("&" + qfu(data->cancel)) : 0, 0, 1000),
165       cancelled (false),
166       handler (parent)
167 {
168     if (data->title != NULL)
169         setWindowTitle (qfu(data->title));
170     setMinimumDuration (0);
171
172     connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
173     connect (this, SIGNAL(described(const QString&)),
174                    SLOT(setLabelText(const QString&)));
175     connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
176
177     data->pf_update = update;
178     data->pf_check = check;
179     data->pf_destroy = destroy;
180     data->p_sys = this;
181 }
182
183 QVLCProgressDialog::~QVLCProgressDialog (void)
184 {
185 }
186
187 void QVLCProgressDialog::update (void *priv, const char *text, float value)
188 {
189     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
190
191     if (text != NULL)
192         emit self->described (qfu(text));
193     emit self->progressed ((int)(value * 1000.));
194 }
195
196 static QMutex cancel_mutex;
197
198 bool QVLCProgressDialog::check (void *priv)
199 {
200     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
201     QMutexLocker locker (&cancel_mutex);
202     return self->cancelled;
203 }
204
205 void QVLCProgressDialog::destroy (void *priv)
206 {
207     QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
208
209     emit self->handler->progressBarDestroyed (self);
210 }
211
212 void QVLCProgressDialog::saveCancel (void)
213 {
214     QMutexLocker locker (&cancel_mutex);
215     cancelled = true;
216 }
217
218 void DialogHandler::startProgressBar (vlc_object_t *, void *value)
219 {
220     dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
221     QWidget *dlg = new QVLCProgressDialog (this, data);
222
223     dlg->show ();
224 }
225
226 void DialogHandler::stopProgressBar (QWidget *dlg)
227 {
228     delete dlg;
229 }