dialogProvider.m 11.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*****************************************************************************
 * dialogProvider.m: Minimal Dialog Provider for Mac OS X
 *****************************************************************************
 * Copyright (C) 2009-2010 the VideoLAN team
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#import <stdlib.h>                                      /* malloc(), free() */
#import <string.h>

#ifdef HAVE_CONFIG_H
# import "config.h"
#endif

#import <vlc_common.h>
#import <vlc_plugin.h>
#import <vlc_dialog.h>
#import <vlc_interface.h>

#import <Cocoa/Cocoa.h>
#import "VLCLoginPanel.h"
41
#import "VLCProgressPanel.h"
42
43
44
45
46
47
48
49
50
51
52
53

/*****************************************************************************
 * Prototypes
 *****************************************************************************/
static int  OpenIntf(vlc_object_t *);
static void CloseIntf(vlc_object_t *);
static void Run(intf_thread_t * );

static int DisplayError(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
static int DisplayCritical(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
static int DisplayQuestion(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
static int DisplayLogin(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
54
55
56
57
58
static int DisplayProgressPanelAction(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );

static void updateProgressPanel (void *, const char *, float);
static bool checkProgressPanel (void *);
static void destroyProgressPanel (void *);
59
60
61

struct intf_sys_t
{
62
63
    VLCProgressPanel *currentProgressBarPanel;

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    vlc_mutex_t lock;
    vlc_cond_t wait;
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

vlc_module_begin()
/* Minimal interface. see intf.m */
set_shortname("Mac OS X Dialogs")
add_shortcut("macosx_dialog_provider")
add_shortcut("miosx")
set_description("Minimal Mac OS X Dialog Provider")
set_capability("interface", 50)
set_callbacks(OpenIntf, CloseIntf)
set_category(CAT_INTERFACE)
set_subcategory(SUBCAT_INTERFACE_MAIN)
vlc_module_end()

/*****************************************************************************
 * OpenIntf: initialize interface
 *****************************************************************************/
int OpenIntf(vlc_object_t *p_this)
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

    p_intf->p_sys = malloc(sizeof(intf_sys_t));
    if(!p_intf->p_sys)
        return VLC_ENOMEM;

    memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));

97
    p_intf->b_should_run_on_first_thread = true;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    p_intf->pf_run = Run;

    msg_Dbg(p_intf,"Opening Mac OS X dialog provider");
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Run: waiting for the death
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    /* subscribe to various interactive dialogues */
    var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
    var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
    var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
    var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
    var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
    var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
    var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
    var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
118
119
    var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
    var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
120
121
122
123
124
125
    dialog_Register(p_intf);

    msg_Dbg(p_intf,"Mac OS X dialog provider initialised");

    /* idle */
    while(vlc_object_alive(p_intf))
126
        msleep(INTF_IDLE_SLEEP);
127
128
    
    /* unsubscribe from the interactive dialogues */
129
    dialog_Unregister(p_intf);
130
131
132
133
    var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
    var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
    var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
    var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
134
    var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}
/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
void CloseIntf(vlc_object_t *p_this)
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

    msg_Dbg(p_intf,"Mac OS X dialog provider closed");
    free(p_intf->p_sys);
}


/*****************************************************************************
 * Callbacks triggered by the "dialog-*" variables
 *****************************************************************************/
static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
    dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
    NSRunInformationalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
                            [NSString stringWithUTF8String:p_dialog->message],
                            @"OK", nil, nil);
    [o_pool release];
159
    return VLC_SUCCESS;
160
161
162
163
164
165
166
167
168
169
}

static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
    dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
    NSRunCriticalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
                            [NSString stringWithUTF8String:p_dialog->message],
                            @"OK", nil, nil);
    [o_pool release];
170
    return VLC_SUCCESS;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
}

static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
    dialog_question_t *p_dialog = (dialog_question_t *)value.p_address;
    NSAlert *o_alert;
    NSString *o_yes, *o_no, *o_cancel;
    NSInteger i_returnValue = 0;

    if (p_dialog->yes != NULL)
        o_yes = [NSString stringWithUTF8String:p_dialog->yes];
    if (p_dialog->no != NULL)
        o_no = [NSString stringWithUTF8String:p_dialog->no];
    if (p_dialog->cancel != NULL)
        o_cancel = [NSString stringWithUTF8String:p_dialog->cancel];

    o_alert = [NSAlert alertWithMessageText:[NSString stringWithUTF8String:p_dialog->title]
                              defaultButton:o_yes
                            alternateButton:o_no 
                                otherButton:o_cancel
                  informativeTextWithFormat:[NSString stringWithUTF8String:p_dialog->message]];
    [o_alert setAlertStyle:NSInformationalAlertStyle];
    i_returnValue = [o_alert runModal];

    if (i_returnValue == NSAlertDefaultReturn)
        p_dialog->answer = 1;
    if (i_returnValue == NSAlertAlternateReturn)
        p_dialog->answer = 2;
    if (i_returnValue == NSAlertOtherReturn)
        p_dialog->answer = 3;
    [o_pool release];
203
    return VLC_SUCCESS;
204
205
206
207
208
209
210
211
212
213
214
215
}

static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
    dialog_login_t *p_dialog = (dialog_login_t *)value.p_address;
    NSInteger i_returnValue = 0;
    VLCLoginPanel *thePanel = [[VLCLoginPanel alloc] init];
    [thePanel createContentView];
    [thePanel setDialogTitle:[NSString stringWithUTF8String:p_dialog->title]];
    [thePanel setDialogMessage:[NSString stringWithUTF8String:p_dialog->message]];
    [thePanel center];
216
    i_returnValue = [NSApp runModalForWindow:thePanel];
217
    [thePanel close];
218
    if (i_returnValue) {
219
220
        *p_dialog->username = strdup( [[thePanel userName] UTF8String] );
        *p_dialog->password = strdup( [[thePanel password] UTF8String] );
221
    } else
222
223
        *p_dialog->username = *p_dialog->password = NULL;
    [o_pool release];
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    return VLC_SUCCESS;
}

static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
    dialog_progress_bar_t * p_dialog = (dialog_progress_bar_t *)value.p_address;
    intf_thread_t *p_intf = (intf_thread_t*) p_this;
    intf_sys_t *p_sys = p_intf->p_sys;

    if(p_sys->currentProgressBarPanel)
        [p_sys->currentProgressBarPanel release];

    p_sys->currentProgressBarPanel = [[VLCProgressPanel alloc] init];
    [p_sys->currentProgressBarPanel createContentView];
    if (p_dialog->title)
        [p_sys->currentProgressBarPanel setDialogTitle:[NSString stringWithUTF8String:p_dialog->title]];
    if (p_dialog->message)
        [p_sys->currentProgressBarPanel setDialogMessage:[NSString stringWithUTF8String:p_dialog->message]];
    if (p_dialog->cancel)
        [p_sys->currentProgressBarPanel setCancelButtonLabel:[NSString stringWithUTF8String:p_dialog->cancel]];
    [p_sys->currentProgressBarPanel center];
    [p_sys->currentProgressBarPanel makeKeyAndOrderFront:nil];

    p_dialog->pf_update = updateProgressPanel;
    p_dialog->pf_check = checkProgressPanel;
    p_dialog->pf_destroy = destroyProgressPanel;
    p_dialog->p_sys = p_intf->p_sys;

    [o_pool release];
    return VLC_SUCCESS;
255
}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    intf_sys_t *p_sys = (intf_sys_t *)priv;

    if (text)
        [p_sys->currentProgressBarPanel setDialogMessage:[NSString stringWithUTF8String:text]];
    else
        [p_sys->currentProgressBarPanel setDialogMessage:@""];
    [p_sys->currentProgressBarPanel setProgressAsDouble:(double)(value * 1000.)];

    [o_pool release];
}

void destroyProgressPanel (void *priv)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    intf_sys_t *p_sys = (intf_sys_t *)priv;

    [p_sys->currentProgressBarPanel close];
    [p_sys->currentProgressBarPanel release];

    [o_pool release];
}

bool checkProgressPanel (void *priv)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    intf_sys_t *p_sys = (intf_sys_t *)priv;
    BOOL b_returned;

    b_returned = [p_sys->currentProgressBarPanel isCancelled];

    [o_pool release];
    return b_returned;
}