]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/external.cpp
4baeda6b683278cb17c2b67271dcf7b96b4a160f
[vlc] / modules / gui / qt4 / dialogs / external.cpp
1 /*****************************************************************************
2  * external.hpp : 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
37 QVLCVariable::QVLCVariable (vlc_object_t *obj, const char *varname, int type)
38     : object (obj), name (qfu(varname))
39 {
40     var_Create (object, qtu(name), type);
41     var_AddCallback (object, qtu(name), callback, this);
42 }
43
44 QVLCVariable::~QVLCVariable (void)
45 {
46     var_DelCallback (object, qtu(name), callback, this);
47     var_Destroy (object, qtu(name));
48 }
49
50 int QVLCVariable::callback (vlc_object_t *object, const char *,
51                             vlc_value_t, vlc_value_t cur, void *data)
52 {
53     QVLCVariable *self = (QVLCVariable *)data;
54     emit self->pointerChanged (object, cur.p_address);
55 }
56
57
58 DialogHandler::DialogHandler (intf_thread_t *intf)
59     : intf (intf),
60       message (VLC_OBJECT(intf), "dialog-fatal", VLC_VAR_ADDRESS),
61       login (VLC_OBJECT(intf), "dialog-login", VLC_VAR_ADDRESS),
62       question (VLC_OBJECT(intf), "dialog-question", VLC_VAR_ADDRESS)
63 {
64     connect (&message, SIGNAL(pointerChanged(vlc_object_t *, void *)),
65              SLOT(displayMessage(vlc_object_t *, void *)),
66              Qt::BlockingQueuedConnection);
67     connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
68              SLOT(requestLogin(vlc_object_t *, void *)),
69              Qt::BlockingQueuedConnection);
70     connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
71              SLOT(requestAnswer(vlc_object_t *, void *)),
72              Qt::BlockingQueuedConnection);
73
74     dialog_Register (intf);
75 }
76
77 DialogHandler::~DialogHandler (void)
78 {
79     dialog_Unregister (intf);
80 }
81
82 void DialogHandler::displayMessage (vlc_object_t *, void *value)
83 {
84      const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
85
86     if (dialog->modal)
87         QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
88                                QMessageBox::Ok);
89     else
90     if (config_GetInt (intf, "qt-error-dialogs"))
91         ErrorsDialog::getInstance (intf)->addError(qfu(dialog->title),
92                                                    qfu(dialog->message));
93 }
94
95 void DialogHandler::requestLogin (vlc_object_t *, void *value)
96 {
97     dialog_login_t *data = (dialog_login_t *)value;
98     QDialog *dialog = new QDialog;
99     QLayout *layout = new QVBoxLayout (dialog);
100
101     dialog->setWindowTitle (qfu(data->title));
102     layout->setMargin (2);
103
104     /* User name and password fields */
105     QWidget *panel = new QWidget (dialog);
106     QGridLayout *grid = new QGridLayout;
107     grid->addWidget (new QLabel (qfu(data->message)), 0, 0, 1, 2);
108
109     QLineEdit *userLine = new QLineEdit;
110     grid->addWidget (new QLabel (qtr("User name")), 1, 0);
111     grid->addWidget (userLine, 1, 1);
112
113     QLineEdit *passLine = new QLineEdit;
114     passLine->setEchoMode (QLineEdit::Password);
115     grid->addWidget (new QLabel (qtr("Password")), 2, 0);
116     grid->addWidget (passLine, 2, 1);
117
118     panel->setLayout (grid);
119     layout->addWidget (panel);
120
121     /* OK, Cancel buttons */
122     QDialogButtonBox *buttonBox;
123     buttonBox = new QDialogButtonBox (QDialogButtonBox::Ok
124                                        | QDialogButtonBox::Cancel);
125     connect (buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
126     connect (buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
127     layout->addWidget (buttonBox);
128
129     /* Run the dialog */
130     dialog->setLayout (layout);
131
132     if (dialog->exec ())
133     {
134         *data->username = strdup (qtu(userLine->text ()));
135         *data->password = strdup (qtu(passLine->text ()));
136     }
137     else
138         *data->username = *data->password = NULL;
139
140     delete dialog;
141 }
142
143 void DialogHandler::requestAnswer (vlc_object_t *, void *value)
144 {
145     dialog_question_t *data = (dialog_question_t *)value;
146
147     QMessageBox *box = new QMessageBox (QMessageBox::Question,
148                                         qfu(data->title), qfu(data->message));
149     QAbstractButton *yes = (data->yes != NULL)
150         ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
151     QAbstractButton *no = (data->no != NULL)
152         ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
153     QAbstractButton *cancel = (data->cancel != NULL)
154         ? box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole)
155         : NULL;
156
157     box->exec ();
158
159     int answer;
160     if (box->clickedButton () == yes)
161         answer = 1;
162     else
163     if (box->clickedButton () == no)
164         answer = 2;
165     else
166         answer = 3;
167
168     delete box;
169     data->answer = answer;
170 }