]> git.sesse.net Git - vlc/blob - src/interface/dialog.c
50500405adad7f948af61129ad033187744f6011
[vlc] / src / interface / dialog.c
1 /*****************************************************************************
2  * dialog.c: User dialog functions
3  *****************************************************************************
4  * Copyright © 2009 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 /**
22  * \file dialog.c
23  * User dialogs core
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdarg.h>
31
32 #include <vlc_common.h>
33 #include <vlc_dialog.h>
34 #include <assert.h>
35 #include "libvlc.h"
36
37 static vlc_mutex_t provider_lock = VLC_STATIC_MUTEX;
38
39 #undef dialog_Register
40 /**
41  * Registers an object as the dialog provider.
42  * It is assumed that the appropriate variable callbacks are already
43  * registered.
44  */
45 int dialog_Register (vlc_object_t *obj)
46 {
47     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
48     int ret = VLC_EGENERIC;
49
50     vlc_mutex_lock (&provider_lock);
51     if (priv->p_dialog_provider == NULL)
52     {   /* Since the object is responsible for unregistering itself before
53          * it terminates, at reference is not needed. */
54         priv->p_dialog_provider = obj;
55         ret = VLC_SUCCESS;
56     }
57     vlc_mutex_unlock (&provider_lock);
58     return ret;
59 }
60
61 #undef dialog_Unregister
62 /**
63  * Unregisters the dialog provider.
64  * Note that unless you have unregistered the callbacks already, the provider
65  * might still be in use by other threads. Also, you need to cancel all
66  * pending dialogs yourself.
67  */
68 int dialog_Unregister (vlc_object_t *obj)
69 {
70     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
71     int ret = VLC_EGENERIC;
72
73     vlc_mutex_lock (&provider_lock);
74     if (priv->p_dialog_provider == obj)
75     {
76         priv->p_dialog_provider = NULL;
77         ret = VLC_SUCCESS;
78     }
79     vlc_mutex_unlock (&provider_lock);
80     return ret;
81 }
82
83 static vlc_object_t *dialog_GetProvider (vlc_object_t *obj)
84 {
85     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
86     vlc_object_t *provider;
87
88     vlc_mutex_lock (&provider_lock);
89     if ((provider = priv->p_dialog_provider) != NULL)
90         vlc_object_hold (provider);
91     vlc_mutex_unlock (&provider_lock);
92     return provider;
93 }
94
95 /**
96  * Sends an error message through the user interface (if any).
97  * @param obj the VLC object emitting the error
98  * @param modal whether to wait for user to acknowledge the error
99  *              before returning control to the caller
100  * @param title title of the error dialog
101  * @param fmt format string for the error message
102  * @param ap parameters list for the formatted error message
103  */
104 void dialog_VFatal (vlc_object_t *obj, bool modal, const char *title,
105                     const char *fmt, va_list ap)
106 {
107     char *text;
108
109     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
110         return;
111
112     vlc_object_t *provider = dialog_GetProvider (obj);
113     if (provider == NULL)
114     {
115         msg_Err (obj, "%s", title);
116         msg_GenericVa (obj, VLC_MSG_ERR, MODULE_STRING, fmt, ap);
117         return;
118     }
119
120     if (vasprintf (&text, fmt, ap) != -1)
121     {
122         dialog_fatal_t dialog = { title, text, };
123         var_SetAddress (provider,
124                         modal ? "dialog-critical" : "dialog-error", &dialog);
125         free (text);
126     }
127     vlc_object_release (provider);
128 }
129
130 #undef dialog_Login
131 /**
132  * Requests a username and password through the user interface.
133  * @param obj the VLC object requesting credential informations
134  * @param username a pointer to the specified username [OUT]
135  * @param password a pointer to the specified password [OUT]
136  * @param title title for the dialog
137  * @param text format string for the message in the dialog
138  * @return Nothing. If a user name resp. a password was specified,
139  * it will be returned as a heap-allocated character array
140  * into the username resp password pointer. Those must be freed with free().
141  * Otherwise *username resp *password will be NULL.
142  */
143 void dialog_Login (vlc_object_t *obj, char **username, char **password,
144                    const char *title, const char *fmt, ...)
145 {
146     assert ((username != NULL) && (password != NULL));
147
148     *username = *password = NULL;
149     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
150         return;
151
152     vlc_object_t *provider = dialog_GetProvider (obj);
153     if (provider == NULL)
154         return;
155
156     char *text;
157     va_list ap;
158
159     va_start (ap, fmt);
160     if (vasprintf (&text, fmt, ap) != -1)
161     {
162         dialog_login_t dialog = { title, text, username, password, };
163         var_SetAddress (provider, "dialog-login", &dialog);
164         free (text);
165     }
166     va_end (ap);
167     vlc_object_release (provider);
168 }
169
170
171 #undef dialog_Question
172 /**
173  * Asks a total (Yes/No/Cancel) question through the user interface.
174  * @param obj VLC object emitting the question
175  * @param title dialog box title
176  * @param text dialog box text
177  * @param yes first choice/button text
178  * @param no second choice/button text
179  * @param cancel third answer/button text, or NULL if no third option
180  * @return 0 if the user could not answer the question (e.g. there is no UI),
181  * 1, 2 resp. 3 if the user pressed the first, second resp. third button.
182  */
183 int dialog_Question (vlc_object_t *obj, const char *title, const char *text,
184                      const char *yes, const char *no, const char *cancel)
185 {
186     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
187         return 0;
188
189     vlc_object_t *provider = dialog_GetProvider (obj);
190     if (provider == NULL)
191         return 0;
192
193     dialog_question_t dialog = { title, text, yes, no, cancel, 0, };
194     var_SetAddress (provider, "dialog-question", &dialog);
195     vlc_object_release (provider);
196     return dialog.answer;
197 }
198
199 #undef dialog_ProgressCreate
200 /**
201  * Creates a progress bar dialog.
202  */
203 dialog_progress_bar_t *
204 dialog_ProgressCreate (vlc_object_t *obj, const char *title,
205                        const char *message, const char *cancel)
206 {
207     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
208         return NULL;
209
210     vlc_object_t *provider = dialog_GetProvider (obj);
211     if (provider == NULL)
212         return NULL;
213
214     dialog_progress_bar_t *dialog = malloc (sizeof (*dialog));
215     if (dialog != NULL)
216     {
217         dialog->title = title;
218         dialog->message = message;
219         dialog->cancel = cancel;
220         var_SetAddress (provider, "dialog-progress-bar", dialog);
221 #ifndef NDEBUG
222         dialog->title = dialog->message = dialog->cancel = NULL;
223 #endif
224         assert (dialog->pf_update);
225         assert (dialog->pf_check);
226         assert (dialog->pf_destroy);
227     }
228
229     /* FIXME: This could conceivably crash if the dialog provider is destroyed
230      * before the dialog user. Holding the provider does not help, as it only
231      * protects object variable operations. For instance, it does not prevent
232      * unloading of the interface plugin. In the short term, the only solution
233      * is to not use progress dialog after deinitialization of the interfaces.
234      */
235     vlc_object_release (provider);
236     return dialog;
237 }
238
239 void dialog_ProgressDestroy (dialog_progress_bar_t *dialog)
240 {
241     assert (dialog);
242
243     dialog->pf_destroy (dialog->p_sys);
244     free (dialog);
245 }
246
247 void dialog_ProgressSet (dialog_progress_bar_t *dialog, const char *text,
248                          float value)
249 {
250     assert (dialog);
251
252     dialog->pf_update (dialog->p_sys, text, value);
253 }
254
255 bool dialog_ProgressCancelled (dialog_progress_bar_t *dialog)
256 {
257     assert (dialog);
258
259     return dialog->pf_check (dialog->p_sys);
260 }
261