]> git.sesse.net Git - vlc/blob - src/interface/dialog.c
s/informations/information/
[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 <vlc_extensions.h>
35 #include <assert.h>
36 #include "libvlc.h"
37
38 static vlc_mutex_t provider_lock = VLC_STATIC_MUTEX;
39
40 #undef dialog_Register
41 /**
42  * Registers an object as the dialog provider.
43  * It is assumed that the appropriate variable callbacks are already
44  * registered.
45  */
46 int dialog_Register (vlc_object_t *obj)
47 {
48     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
49     int ret = VLC_EGENERIC;
50
51     vlc_mutex_lock (&provider_lock);
52     if (priv->p_dialog_provider == NULL)
53     {   /* Since the object is responsible for unregistering itself before
54          * it terminates, at reference is not needed. */
55         priv->p_dialog_provider = obj;
56         ret = VLC_SUCCESS;
57     }
58     vlc_mutex_unlock (&provider_lock);
59     return ret;
60 }
61
62 #undef dialog_Unregister
63 /**
64  * Unregisters the dialog provider.
65  * Note that unless you have unregistered the callbacks already, the provider
66  * might still be in use by other threads. Also, you need to cancel all
67  * pending dialogs yourself.
68  */
69 int dialog_Unregister (vlc_object_t *obj)
70 {
71     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
72     int ret = VLC_EGENERIC;
73
74     vlc_mutex_lock (&provider_lock);
75     if (priv->p_dialog_provider == obj)
76     {
77         priv->p_dialog_provider = NULL;
78         ret = VLC_SUCCESS;
79     }
80     vlc_mutex_unlock (&provider_lock);
81     return ret;
82 }
83
84 static vlc_object_t *dialog_GetProvider (vlc_object_t *obj)
85 {
86     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
87     vlc_object_t *provider;
88
89     vlc_mutex_lock (&provider_lock);
90     if ((provider = priv->p_dialog_provider) != NULL)
91         vlc_object_hold (provider);
92     vlc_mutex_unlock (&provider_lock);
93     return provider;
94 }
95
96 /**
97  * Sends an error message through the user interface (if any).
98  * @param obj the VLC object emitting the error
99  * @param modal whether to wait for user to acknowledge the error
100  *              before returning control to the caller
101  * @param title title of the error dialog
102  * @param fmt format string for the error message
103  * @param ap parameters list for the formatted error message
104  */
105 void dialog_VFatal (vlc_object_t *obj, bool modal, const char *title,
106                     const char *fmt, va_list ap)
107 {
108     char *text;
109
110     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
111         return;
112
113     vlc_object_t *provider = dialog_GetProvider (obj);
114     if (provider == NULL)
115     {
116         msg_Err (obj, "%s", title);
117         msg_GenericVa (obj, VLC_MSG_ERR, MODULE_STRING, fmt, ap);
118         return;
119     }
120
121     if (vasprintf (&text, fmt, ap) != -1)
122     {
123         dialog_fatal_t dialog = { title, text, };
124         var_SetAddress (provider,
125                         modal ? "dialog-critical" : "dialog-error", &dialog);
126         free (text);
127     }
128     vlc_object_release (provider);
129 }
130
131 #undef dialog_Login
132 /**
133  * Requests a username and password through the user interface.
134  * @param obj the VLC object requesting credential information
135  * @param username a pointer to the specified username [OUT]
136  * @param password a pointer to the specified password [OUT]
137  * @param title title for the dialog
138  * @param text format string for the message in the dialog
139  * @return Nothing. If a user name resp. a password was specified,
140  * it will be returned as a heap-allocated character array
141  * into the username resp password pointer. Those must be freed with free().
142  * Otherwise *username resp *password will be NULL.
143  */
144 void dialog_Login (vlc_object_t *obj, char **username, char **password,
145                    const char *title, const char *fmt, ...)
146 {
147     assert ((username != NULL) && (password != NULL));
148
149     *username = *password = NULL;
150     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
151         return;
152
153     vlc_object_t *provider = dialog_GetProvider (obj);
154     if (provider == NULL)
155         return;
156
157     char *text;
158     va_list ap;
159
160     va_start (ap, fmt);
161     if (vasprintf (&text, fmt, ap) != -1)
162     {
163         dialog_login_t dialog = { title, text, username, password, };
164         var_SetAddress (provider, "dialog-login", &dialog);
165         free (text);
166     }
167     va_end (ap);
168     vlc_object_release (provider);
169 }
170
171
172 #undef dialog_Question
173 /**
174  * Asks a total (Yes/No/Cancel) question through the user interface.
175  * @param obj VLC object emitting the question
176  * @param title dialog box title
177  * @param text dialog box text
178  * @param yes first choice/button text
179  * @param no second choice/button text
180  * @param cancel third answer/button text, or NULL if no third option
181  * @return 0 if the user could not answer the question (e.g. there is no UI),
182  * 1, 2 resp. 3 if the user pressed the first, second resp. third button.
183  */
184 int dialog_Question (vlc_object_t *obj, const char *title, const char *text,
185                      const char *yes, const char *no, const char *cancel)
186 {
187     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
188         return 0;
189
190     vlc_object_t *provider = dialog_GetProvider (obj);
191     if (provider == NULL)
192         return 0;
193
194     dialog_question_t dialog = { title, text, yes, no, cancel, 0, };
195     var_SetAddress (provider, "dialog-question", &dialog);
196     vlc_object_release (provider);
197     return dialog.answer;
198 }
199
200 #undef dialog_ProgressCreate
201 /**
202  * Creates a progress bar dialog.
203  */
204 dialog_progress_bar_t *
205 dialog_ProgressCreate (vlc_object_t *obj, const char *title,
206                        const char *message, const char *cancel)
207 {
208     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
209         return NULL;
210
211     vlc_object_t *provider = dialog_GetProvider (obj);
212     if (provider == NULL)
213         return NULL;
214
215     dialog_progress_bar_t *dialog = malloc (sizeof (*dialog));
216     if (dialog != NULL)
217     {
218         dialog->title = title;
219         dialog->message = message;
220         dialog->cancel = cancel;
221         var_SetAddress (provider, "dialog-progress-bar", dialog);
222 #ifndef NDEBUG
223         dialog->title = dialog->message = dialog->cancel = NULL;
224 #endif
225         assert (dialog->pf_update);
226         assert (dialog->pf_check);
227         assert (dialog->pf_destroy);
228     }
229
230     /* FIXME: This could conceivably crash if the dialog provider is destroyed
231      * before the dialog user. Holding the provider does not help, as it only
232      * protects object variable operations. For instance, it does not prevent
233      * unloading of the interface plugin. In the short term, the only solution
234      * is to not use progress dialog after deinitialization of the interfaces.
235      */
236     vlc_object_release (provider);
237     return dialog;
238 }
239
240 void dialog_ProgressDestroy (dialog_progress_bar_t *dialog)
241 {
242     assert (dialog);
243
244     dialog->pf_destroy (dialog->p_sys);
245     free (dialog);
246 }
247
248 void dialog_ProgressSet (dialog_progress_bar_t *dialog, const char *text,
249                          float value)
250 {
251     assert (dialog);
252
253     dialog->pf_update (dialog->p_sys, text, value);
254 }
255
256 bool dialog_ProgressCancelled (dialog_progress_bar_t *dialog)
257 {
258     assert (dialog);
259
260     return dialog->pf_check (dialog->p_sys);
261 }
262
263 #undef dialog_ExtensionUpdate
264 int dialog_ExtensionUpdate (vlc_object_t *obj, extension_dialog_t *dialog)
265 {
266     assert (obj);
267     assert (dialog);
268
269     vlc_object_t *dp = dialog_GetProvider(obj);
270     if (!dp)
271     {
272         msg_Warn (obj, "Dialog provider is not set, can't update dialog '%s'",
273                   dialog->psz_title);
274         return VLC_EGENERIC;
275     }
276
277     // Signaling the dialog provider
278     int ret = var_SetAddress (dp, "dialog-extension", dialog);
279
280     vlc_object_release (dp);
281     return ret;
282 }