]> git.sesse.net Git - vlc/blob - modules/gui/macosx/coredialogs.m
macosx: fixes a memory leak.
[vlc] / modules / gui / macosx / coredialogs.m
1 /*****************************************************************************
2  * coredialogs.m: Mac OS X Core Dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "intf.h"
26 #import "coredialogs.h"
27 #import "misc.h"
28
29 /* for the icon in our custom error panel */
30 #import <ApplicationServices/ApplicationServices.h>
31
32 /*****************************************************************************
33  * VLCCoreDialogProvider implementation
34  *****************************************************************************/
35 @implementation VLCCoreDialogProvider
36
37 static VLCCoreDialogProvider *_o_sharedInstance = nil;
38
39 + (VLCCoreDialogProvider *)sharedInstance
40 {
41     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
42 }
43
44 -(id)init
45 {
46     if( _o_sharedInstance )
47         [self dealloc];
48     else
49     {
50         _o_sharedInstance = [super init];
51         o_error_panel = [[VLCErrorPanel alloc] init];
52         b_progress_cancelled = NO;
53     }
54     
55     return _o_sharedInstance;
56 }
57
58 -(void)awakeFromNib
59 {
60     [o_auth_login_txt setStringValue: _NS("User name")];
61     [o_auth_pw_txt setStringValue: _NS("Password")];
62     [o_auth_cancel_btn setTitle: _NS("Cancel")];
63     [o_auth_ok_btn setTitle: _NS("OK")];
64     [o_prog_cancel_btn setTitle: _NS("Cancel")];
65     [o_prog_bar setUsesThreadedAnimation: YES];
66
67 }
68
69 -(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
70 {
71     NSString *o_type = [NSString stringWithUTF8String:type];
72
73     if( [o_type isEqualToString: @"dialog-error"] )
74         [self performSelectorOnMainThread:@selector(showFatalDialog:) withObject:o_value waitUntilDone:YES];
75     else if( [o_type isEqualToString: @"dialog-critical"] )
76         [self performSelectorOnMainThread:@selector(showFatalWaitDialog:) withObject:o_value waitUntilDone:YES];
77     else if( [o_type isEqualToString: @"dialog-question"] )
78         [self performSelectorOnMainThread:@selector(showQuestionDialog:) withObject:o_value waitUntilDone:YES];
79     else if( [o_type isEqualToString: @"dialog-login"] )
80         [self performSelectorOnMainThread:@selector(showLoginDialog:) withObject:o_value waitUntilDone:YES];
81     else if( [o_type isEqualToString: @"dialog-progress-bar"] )
82         [self performSelectorOnMainThread:@selector(showProgressDialog:) withObject:o_value waitUntilDone:YES];
83     else
84         msg_Err( VLCIntf, "unhandled dialog type: '%s'", type );
85 }
86
87 -(void)showFatalDialog: (NSValue *)o_value
88 {
89     dialog_fatal_t *p_dialog = [o_value pointerValue];
90
91     [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
92     [o_error_panel showPanel];
93 }
94
95 -(void)showFatalWaitDialog: (NSValue *)o_value
96 {
97     dialog_fatal_t *p_dialog = [o_value pointerValue];
98     NSAlert *o_alert;
99
100     o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
101     [o_alert setAlertStyle: NSCriticalAlertStyle];
102     [o_alert runModal];
103 }
104
105 -(void)showQuestionDialog: (NSValue *)o_value
106 {
107     dialog_question_t *p_dialog = [o_value pointerValue];
108     NSAlert *o_alert;
109     NSString *o_yes, *o_no, *o_cancel;
110     NSInteger i_returnValue = 0;
111     
112     if( p_dialog->yes != NULL )
113         o_yes = [NSString stringWithUTF8String: p_dialog->yes];
114     if( p_dialog->no != NULL )
115         o_no = [NSString stringWithUTF8String: p_dialog->no];
116     if( p_dialog->cancel != NULL )
117         o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
118
119     o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
120     [o_alert setAlertStyle: NSInformationalAlertStyle];
121     i_returnValue = [o_alert runModal];
122
123     if( i_returnValue == NSAlertDefaultReturn )
124         p_dialog->answer = 1;
125     if( i_returnValue == NSAlertAlternateReturn )
126         p_dialog->answer = 2;
127     if( i_returnValue == NSAlertOtherReturn )
128         p_dialog->answer = 3;
129 }
130
131 -(void)showLoginDialog: (NSValue *)o_value
132 {
133     dialog_login_t *p_dialog = [o_value pointerValue];
134     NSInteger i_returnValue = 0;
135
136     [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
137     [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
138     [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
139     [o_auth_login_fld setStringValue: @""];
140     [o_auth_pw_fld setStringValue: @""];
141
142     [o_auth_win center];
143     i_returnValue = [NSApp runModalForWindow: o_auth_win];
144     [o_auth_win close];
145     if( i_returnValue )
146     {
147         *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
148         *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
149     }
150     else
151     {
152          *p_dialog->username = *p_dialog->password = NULL;
153     }
154 }
155
156 -(IBAction)loginDialogAction:(id)sender
157 {
158     if( [[sender title] isEqualToString: _NS("OK")] )
159         [NSApp stopModalWithCode: 1];
160     else
161         [NSApp stopModalWithCode: 0];
162 }
163
164 -(void)showProgressDialog: (NSValue *)o_value
165 {
166     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
167
168     if( p_dialog->title != NULL )
169     {
170         [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
171         [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
172     }
173     else
174     {
175         [o_prog_win setTitle: @""];
176         [o_prog_title_txt setStringValue: @""];
177     }
178     if( p_dialog->cancel != NULL )
179         [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
180     else
181         [o_prog_cancel_btn setTitle: _NS("Cancel")];
182     if( p_dialog->message != NULL )
183         [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
184     else
185         [o_prog_description_txt setStringValue: @""];
186     [o_prog_bar setDoubleValue: 0];
187     [o_prog_bar startAnimation: self];
188
189     [o_prog_win makeKeyAndOrderFront: self];
190 }
191
192 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
193 {
194     [o_prog_description_txt setStringValue: string];
195     [o_prog_bar setDoubleValue: d_number];
196 }
197
198 -(void)destroyProgressPanel
199 {
200     [o_prog_bar stopAnimation: self];
201     [o_prog_win close];
202 }
203
204 -(IBAction)progDialogAction:(id)sender
205 {
206     b_progress_cancelled = YES;
207 }
208
209 -(BOOL)progressCancelled
210 {
211     return b_progress_cancelled;
212 }
213
214 -(id)errorPanel
215 {
216     return o_error_panel;
217 }
218
219 @end
220
221 /*****************************************************************************
222  * VLCErrorPanel implementation
223  *****************************************************************************/
224 @implementation VLCErrorPanel
225 -(id)init
226 {
227     [super init];
228
229     if( !b_nib_loaded )
230     {
231         b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
232     
233         /* init strings */
234         [o_window setTitle: _NS("Errors and Warnings")];
235         [o_cleanup_button setTitle: _NS("Clean up")];
236         [o_messages_btn setTitle: _NS("Show Details")];
237     }
238
239     /* init data sources */
240     o_errors = [[NSMutableArray alloc] init];
241     o_icons = [[NSMutableArray alloc] init];
242
243     return self;
244 }
245
246 -(void)dealloc
247 {
248     [o_errors release];
249     [o_icons release];
250     [super dealloc];
251 }
252
253 -(void)showPanel
254 {
255     [o_window makeKeyAndOrderFront: self];
256 }
257
258 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
259 {
260     /* format our string as desired */
261     NSMutableAttributedString * ourError;
262     ourError = [[NSMutableAttributedString alloc] initWithString:
263         [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
264         attributes:
265         [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
266     [ourError
267         addAttribute: NSFontAttributeName
268         value: [NSFont boldSystemFontOfSize:11]
269         range: NSMakeRange( 0, [o_error length])];
270     [o_errors addObject: ourError];
271     [ourError release];
272
273     [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
274
275     [o_error_table reloadData];
276 }
277
278 -(IBAction)cleanupTable:(id)sender
279 {
280     [o_errors removeAllObjects];
281     [o_icons removeAllObjects];
282     [o_error_table reloadData];
283 }
284
285 -(IBAction)showMessages:(id)sender
286 {
287     [[VLCMain sharedInstance] showMessagesPanel: sender];
288 }
289
290 /*----------------------------------------------------------------------------
291  * data source methods
292  *---------------------------------------------------------------------------*/
293 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
294 {
295     return [o_errors count];
296 }
297
298 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
299     (NSTableColumn *)theTableColumn row: (NSInteger)row
300 {
301     if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
302         return [o_errors objectAtIndex: row];
303
304     if( [[theTableColumn identifier] isEqualToString: @"icon"] )
305         return [o_icons objectAtIndex: row];
306
307     return @"unknown identifier";
308 }
309
310 @end