1 /*****************************************************************************
2 * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2009-2010 the VideoLAN team
7 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
8 * Pierre d'Herbemont <pdherbemont # videolan dot>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #import <stdlib.h> /* malloc(), free() */
35 #import <vlc_common.h>
36 #import <vlc_plugin.h>
37 #import <vlc_dialog.h>
38 #import <vlc_extensions.h>
40 #import "dialogProvider.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int OpenIntf(vlc_object_t *);
46 static void CloseIntf(vlc_object_t *);
47 static void Run(intf_thread_t * );
49 static int DisplayError(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
50 static int DisplayCritical(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
51 static int DisplayQuestion(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
52 static int DisplayLogin(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
53 static int DisplayProgressPanelAction(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
54 static int DisplayExtension(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
56 static void updateProgressPanel (void *, const char *, float);
57 static bool checkProgressPanel (void *);
58 static void destroyProgressPanel (void *);
60 @interface VLCDialogButton : NSButton
62 extension_widget_t *widget;
64 @property (readwrite) extension_widget_t *widget;
67 @implementation VLCDialogButton
71 @interface VLCDialogPopUpButton : NSPopUpButton
73 extension_widget_t *widget;
75 @property (readwrite) extension_widget_t *widget;
78 @implementation VLCDialogPopUpButton
83 @interface VLCDialogTextField : NSTextField
85 extension_widget_t *widget;
87 @property (readwrite) extension_widget_t *widget;
90 @implementation VLCDialogTextField
94 @interface VLCDialogWindow : NSWindow
96 extension_dialog_t *dialog;
98 @property (readwrite) extension_dialog_t *dialog;
101 @implementation VLCDialogWindow
106 @interface VLCDialogList : NSTableView
108 extension_widget_t *widget;
109 NSMutableArray *contentArray;
111 @property (readwrite) extension_widget_t *widget;
112 @property (readwrite, retain) NSMutableArray *contentArray;
115 @implementation VLCDialogList
117 @synthesize contentArray;
119 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
121 return [contentArray count];
124 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
126 return [[contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
130 @interface VLCDialogGridView : NSView {
131 NSUInteger _rowCount, _colCount;
132 NSMutableArray *_gridedViews;
135 - (NSSize)flexSize:(NSSize)size;
136 - (void)removeSubview:(NSView *)view;
140 // Move this to separate file
141 @implementation VLCDialogGridView
145 [_gridedViews release];
149 - (void)recomputeCount
153 for (NSDictionary *obj in _gridedViews)
155 NSUInteger row = [[obj objectForKey:@"row"] intValue];
156 NSUInteger col = [[obj objectForKey:@"col"] intValue];
157 if (col + 1 > _colCount)
159 if (row + 1 > _rowCount)
164 - (void)recomputeWindowSize
166 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
168 NSWindow *window = [self window];
169 NSRect frame = [window frame];
170 NSRect contentRect = [window contentRectForFrameRect:frame];
171 contentRect.size = [self flexSize:frame.size];
172 NSRect newFrame = [window frameRectForContentRect:contentRect];
173 newFrame.origin.y -= newFrame.size.height - frame.size.height;
174 newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
175 [window setFrame:newFrame display:YES animate:YES];
178 - (NSSize)objectSizeToFit:(NSView *)view
180 if ([view isKindOfClass:[NSControl class]]) {
181 NSControl *control = (NSControl *)view;
182 return [[control cell] cellSize];
184 return [view frame].size;
196 - (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
199 for(NSDictionary *obj in _gridedViews) {
200 NSUInteger row = [[obj objectForKey:@"row"] intValue];
201 if (row != targetRow)
203 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
206 NSView *view = [obj objectForKey:@"view"];
207 if ([view autoresizingMask] & NSViewHeightSizable)
209 NSSize sizeToFit = [self objectSizeToFit:view];
210 if (height < sizeToFit.height)
211 height = sizeToFit.height;
216 - (CGFloat)remainingRowsHeight
218 NSUInteger height = [self marginY];
221 NSUInteger autosizedRows = 0;
222 for (NSUInteger i = 0; i < _rowCount; i++) {
223 CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
224 if (!constrainedHeight)
226 height += constrainedHeight + [self marginY];
228 CGFloat remaining = 0;
229 if (height < self.bounds.size.height && autosizedRows)
230 remaining = (self.bounds.size.height - height) / autosizedRows;
237 - (CGFloat)heightOfRow:(NSUInteger)targetRow
239 NSAssert(targetRow < _rowCount, @"accessing a non existing row");
240 CGFloat height = [self constrainedHeightOfRow:targetRow];
242 height = [self remainingRowsHeight];
247 - (CGFloat)topOfRow:(NSUInteger)targetRow
249 CGFloat top = [self marginY];
250 for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
252 top += [self heightOfRow:_rowCount - i] + [self marginY];
257 - (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
260 for(NSDictionary *obj in _gridedViews) {
261 NSUInteger col = [[obj objectForKey:@"col"] intValue];
262 if (col != targetColumn)
264 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
267 NSView *view = [obj objectForKey:@"view"];
268 if ([view autoresizingMask] & NSViewWidthSizable)
270 NSSize sizeToFit = [self objectSizeToFit:view];
271 if (width < sizeToFit.width)
272 width = sizeToFit.width;
277 - (CGFloat)remainingColumnWidth
279 NSUInteger width = [self marginX];
282 NSUInteger autosizedCol = 0;
283 for (NSUInteger i = 0; i < _colCount; i++) {
284 CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
285 if (!constrainedWidth)
287 width += constrainedWidth + [self marginX];
290 CGFloat remaining = 0;
291 if (width < self.bounds.size.width && autosizedCol)
292 remaining = (self.bounds.size.width - width) / autosizedCol;
298 - (CGFloat)widthOfColumn:(NSUInteger)targetColumn
300 CGFloat width = [self constrainedWidthOfColumn:targetColumn];
302 width = [self remainingColumnWidth];
307 - (CGFloat)leftOfColumn:(NSUInteger)targetColumn
309 CGFloat left = [self marginX];
310 for (NSUInteger i = 0; i < targetColumn; i++)
312 left += [self widthOfColumn:i] + [self marginX];
320 for(NSDictionary *obj in _gridedViews) {
321 NSUInteger row = [[obj objectForKey:@"row"] intValue];
322 NSUInteger col = [[obj objectForKey:@"col"] intValue];
323 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
324 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
325 NSView *view = [obj objectForKey:@"view"];
329 if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
331 for (NSUInteger r = 0; r < rowSpan; r++) {
332 if (row + r >= _rowCount)
334 height += [self heightOfRow:row + r] + [self marginY];
336 rect.size.height = height - [self marginY];
339 rect.size.height = [self objectSizeToFit:view].height;
342 if ([view autoresizingMask] & NSViewWidthSizable) {
344 for (NSUInteger c = 0; c < colSpan; c++)
345 width += [self widthOfColumn:col + c] + [self marginX];
346 rect.size.width = width - [self marginX];
349 rect.size.width = [self objectSizeToFit:view].width;
352 rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
353 rect.origin.x = [self leftOfColumn:col];
355 [view setFrame:rect];
356 [view setNeedsDisplay:YES];
360 - (NSMutableDictionary *)objectForView:(NSView *)view
362 for (NSMutableDictionary *dict in _gridedViews)
364 if ([dict objectForKey:@"view"] == view)
370 - (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column rowSpan:(NSUInteger)rowSpan colSpan:(NSUInteger)colSpan
372 if (row + 1 > _rowCount)
374 if (column + 1 > _colCount)
375 _colCount = column + 1;
378 _gridedViews = [[NSMutableArray alloc] init];
380 NSMutableDictionary *dict = [self objectForView:view];
382 dict = [NSMutableDictionary dictionary];
383 [dict setObject:view forKey:@"view"];
384 [_gridedViews addObject:dict];
386 [dict setObject:[NSNumber numberWithInt:rowSpan] forKey:@"rowSpan"];
387 [dict setObject:[NSNumber numberWithInt:colSpan] forKey:@"colSpan"];
388 [dict setObject:[NSNumber numberWithInt:row] forKey:@"row"];
389 [dict setObject:[NSNumber numberWithInt:column] forKey:@"col"];
392 [self addSubview:view];
395 // Recompute the size of the window after making sure we won't see anymore update
396 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
397 [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
400 - (void)removeSubview:(NSView *)view
402 NSDictionary *dict = [self objectForView:view];
404 [_gridedViews removeObject:dict];
405 [view removeFromSuperview];
407 [self recomputeCount];
408 [self recomputeWindowSize];
411 [self setNeedsDisplay:YES];
414 - (void)setFrame:(NSRect)frameRect
416 [super setFrame:frameRect];
420 - (NSSize)flexSize:(NSSize)size
422 if (!_rowCount || !_colCount)
425 CGFloat minHeight = [self marginY];
426 BOOL canFlexHeight = NO;
427 for (NSUInteger i = 0; i < _rowCount; i++) {
428 CGFloat constrained = [self constrainedHeightOfRow:i];
433 minHeight += constrained + [self marginY];
436 CGFloat minWidth = [self marginX];
437 BOOL canFlexWidth = NO;
438 for (NSUInteger i = 0; i < _colCount; i++) {
439 CGFloat constrained = [self constrainedWidthOfColumn:i];
444 minWidth += constrained + [self marginX];
446 if (size.width < minWidth)
447 size.width = minWidth;
448 if (size.height < minHeight)
449 size.height = minHeight;
451 size.height = minHeight;
453 size.width = minWidth;
460 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
461 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
463 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
464 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
466 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
467 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
469 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
470 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
475 VLCDialogDisplayer *displayer;
479 bool is_hidding_noaction_dialogs;
483 #define T_HIDE_NOACTION N_("Hide no user action dialogs")
484 #define LT_HIDE_NOACTION N_("Don't display dialogs that don't require user action (Critical and error panel).")
486 #define prefix "macosx-dialog-provider"
487 /*****************************************************************************
489 *****************************************************************************/
492 /* Minimal interface. see intf.m */
493 set_shortname("Mac OS X Dialogs")
494 add_shortcut("macosx_dialog_provider")
495 add_shortcut("miosx")
496 set_description("Minimal Mac OS X Dialog Provider")
497 set_capability("interface", 0)
499 /* This setting is interesting, because when used with a libvlc app
500 * it's almost certain that the client program will display error by
501 * itself. Moreover certain action might end up in an error, but
502 * the client wants to ignored them completely. */
503 add_bool(prefix "hide-no-user-action-dialogs", true, NULL, T_HIDE_NOACTION, LT_HIDE_NOACTION, false)
505 set_callbacks(OpenIntf, CloseIntf)
506 set_category(CAT_INTERFACE)
507 set_subcategory(SUBCAT_INTERFACE_MAIN)
510 /*****************************************************************************
511 * OpenIntf: initialize interface
512 *****************************************************************************/
513 int OpenIntf(vlc_object_t *p_this)
515 intf_thread_t *p_intf = (intf_thread_t*) p_this;
517 p_intf->p_sys = malloc(sizeof(intf_sys_t));
521 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
523 p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
524 [p_intf->p_sys->displayer setIntf:p_intf];
526 bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
527 p_intf->p_sys->is_hidding_noaction_dialogs = hide;
529 /* subscribe to various interactive dialogues */
533 var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
534 var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
535 var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
536 var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
538 var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
539 var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
540 var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
541 var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
542 var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
543 var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
544 var_Create(p_intf,"dialog-extension",VLC_VAR_ADDRESS);
545 var_AddCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
546 dialog_Register(p_intf);
548 /* subscribe to our last.fm announcements */
549 [[NSDistributedNotificationCenter defaultCenter] addObserver:p_intf->p_sys->displayer
550 selector:@selector(globalNotificationReceived:)
552 object:@"VLCLastFMSupport"
553 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
555 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
560 /*****************************************************************************
561 * CloseIntf: destroy interface
562 *****************************************************************************/
563 void CloseIntf(vlc_object_t *p_this)
565 intf_thread_t *p_intf = (intf_thread_t*) p_this;
567 /* unsubscribe from the interactive dialogues */
568 dialog_Unregister(p_intf);
570 if (!p_intf->p_sys->is_hidding_noaction_dialogs)
572 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
573 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
575 var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
576 var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
577 var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
578 var_DelCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
580 [p_intf->p_sys->displayer release];
582 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
587 /*****************************************************************************
588 * Callbacks triggered by the "dialog-*" variables
589 *****************************************************************************/
590 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
592 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
593 dialog_fatal_t *dialog = value.p_address;
594 intf_thread_t *p_intf = (intf_thread_t*) p_this;
595 intf_sys_t *sys = p_intf->p_sys;
596 [sys->displayer performSelectorOnMainThread:@selector(displayError:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
601 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
603 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
604 dialog_fatal_t *dialog = value.p_address;
605 intf_thread_t *p_intf = (intf_thread_t*) p_this;
606 intf_sys_t *sys = p_intf->p_sys;
607 [sys->displayer performSelectorOnMainThread:@selector(displayCritical:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
612 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
614 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
615 dialog_question_t *dialog = value.p_address;
616 intf_thread_t *p_intf = (intf_thread_t*) p_this;
617 intf_sys_t *sys = p_intf->p_sys;
618 dialog->answer = [[sys->displayer resultFromSelectorOnMainThread:@selector(displayQuestion:) withObject:DictFromDialogQuestion(dialog)] intValue];
623 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
625 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
626 dialog_login_t *dialog = value.p_address;
627 intf_thread_t *p_intf = (intf_thread_t*) p_this;
628 intf_sys_t *sys = p_intf->p_sys;
629 NSDictionary *dict = [sys->displayer resultFromSelectorOnMainThread:@selector(displayLogin:) withObject:DictFromDialogLogin(dialog)];
631 *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
632 *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
638 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
640 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
641 dialog_progress_bar_t *dialog = value.p_address;
642 intf_thread_t *p_intf = (intf_thread_t*) p_this;
643 intf_sys_t *sys = p_intf->p_sys;
645 [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
647 dialog->pf_update = updateProgressPanel;
648 dialog->pf_check = checkProgressPanel;
649 dialog->pf_destroy = destroyProgressPanel;
650 dialog->p_sys = p_intf->p_sys;
656 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
658 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
659 intf_thread_t *p_intf = (intf_thread_t*) p_this;
660 intf_sys_t *sys = p_intf->p_sys;
661 extension_dialog_t *dialog = value.p_address;
663 // -updateExtensionDialog: Open its own runloop, so be sure to run on DefaultRunLoop.
664 [sys->displayer performSelectorOnMainThread:@selector(updateExtensionDialog:) withObject:[NSValue valueWithPointer:dialog] waitUntilDone:YES];
670 void updateProgressPanel (void *priv, const char *text, float value)
672 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
673 intf_sys_t *sys = (intf_sys_t *)priv;
675 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
676 [NSNumber numberWithFloat:value], @"value",
677 text ? [NSString stringWithUTF8String:text] : nil, @"text",
680 [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
685 void destroyProgressPanel (void *priv)
687 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
688 intf_sys_t *sys = (intf_sys_t *)priv;
690 [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
695 bool checkProgressPanel (void *priv)
697 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
698 intf_sys_t *sys = (intf_sys_t *)priv;
701 ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
708 @implementation VLCDialogDisplayer
711 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
712 assert(!_currentProgressBarPanel); // This has to be closed on main thread.
716 - (void)setIntf: (intf_thread_t *)p_mainintf
721 - (intf_thread_t *)intf
726 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
728 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
730 [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
732 [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
734 [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
736 [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
738 [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
742 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
744 - (void)displayError:(NSDictionary *)dialog
746 VLCAssertIsMainThread();
748 NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
749 [dialog objectForKey:@"message"],
753 - (void)displayCritical:(NSDictionary *)dialog
755 VLCAssertIsMainThread();
757 NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
758 [dialog objectForKey:@"message"],
762 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
764 VLCAssertIsMainThread();
766 NSInteger alertRet = 0;
768 NSAlert *alert = [NSAlert alertWithMessageText:[dialog objectForKey:@"title"]
769 defaultButton:[dialog objectForKey:@"yes"]
770 alternateButton:[dialog objectForKey:@"no"]
771 otherButton:[dialog objectForKey:@"cancel"]
772 informativeTextWithFormat:[dialog objectForKey:@"message"]];
773 [alert setAlertStyle:NSInformationalAlertStyle];
774 alertRet = [alert runModal];
778 case NSAlertDefaultReturn:
781 case NSAlertAlternateReturn:
784 case NSAlertOtherReturn:
793 return [NSNumber numberWithInt:ret];
796 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
798 VLCAssertIsMainThread();
800 VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
801 [panel createContentView];
802 [panel setDialogTitle:[dialog objectForKey:@"title"]];
803 [panel setDialogMessage:[dialog objectForKey:@"message"]];
805 NSInteger ret = [NSApp runModalForWindow:panel];
811 return [NSDictionary dictionaryWithObjectsAndKeys:
812 [panel userName], @"username",
813 [panel password], @"password",
817 - (void)displayProgressBar:(NSDictionary *)dialog
819 VLCAssertIsMainThread();
821 if(_currentProgressBarPanel)
822 [self destroyProgressPanel];
824 assert(!_currentProgressBarPanel);
825 _currentProgressBarPanel = [[VLCProgressPanel alloc] init];
826 [_currentProgressBarPanel createContentView];
827 [_currentProgressBarPanel setDialogTitle:[dialog objectForKey:@"title"]];
828 [_currentProgressBarPanel setDialogMessage:[dialog objectForKey:@"message"] ?: @""];
829 [_currentProgressBarPanel setCancelButtonLabel:[dialog objectForKey:@"cancel"]];
831 [_currentProgressBarPanel center];
832 [_currentProgressBarPanel makeKeyAndOrderFront:nil];
835 - (void)updateProgressPanel:(NSDictionary *)dict
837 VLCAssertIsMainThread();
839 assert(_currentProgressBarPanel);
840 [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
841 [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
844 - (void)destroyProgressPanel
846 VLCAssertIsMainThread();
848 [_currentProgressBarPanel close];
849 [_currentProgressBarPanel release];
850 _currentProgressBarPanel = nil;
853 - (NSNumber *)checkProgressPanel
855 VLCAssertIsMainThread();
857 return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
861 #pragma mark Last.FM support
862 - (void)globalNotificationReceived: (NSNotification *)theNotification
864 NSLog(@"globalNotificationReceived");
865 NSDictionary *userData = [theNotification userInfo];
866 BOOL lastFMEnabled = [[userData objectForKey:@"enabled"] intValue];
867 NSString *lastFMUsername = [userData objectForKey:@"username"];
868 NSString *lastFMPassword = [userData objectForKey:@"password"];
870 if (module_exists("audioscrobbler")) {
872 config_AddIntf(p_intf, "audioscrobbler");
874 config_RemoveIntf(p_intf, "audioscrobbler");
876 config_PutPsz(p_intf, "lastfm-username", [lastFMUsername UTF8String]);
877 config_PutPsz(p_intf, "lastfm-password", [lastFMPassword UTF8String]);
878 config_SaveConfigFile( p_intf, "main" );
879 config_SaveConfigFile(p_intf, "audioscrobbler");
882 msg_Err(p_intf,"Last.FM module not found, no action");
886 #pragma mark Extensions Dialog
888 - (void)triggerClick:(id)sender
890 assert([sender isKindOfClass:[VLCDialogButton class]]);
891 VLCDialogButton *button = sender;
892 extension_widget_t *widget = [button widget];
894 NSLog(@"(triggerClick)");
895 vlc_mutex_lock(&widget->p_dialog->lock);
896 extension_WidgetClicked(widget->p_dialog, widget);
897 vlc_mutex_unlock(&widget->p_dialog->lock);
900 - (void)syncTextField:(NSNotification *)notifcation
902 id sender = [notifcation object];
903 assert([sender isKindOfClass:[VLCDialogTextField class]]);
904 VLCDialogTextField *field = sender;
905 extension_widget_t *widget = [field widget];
907 vlc_mutex_lock(&widget->p_dialog->lock);
908 free(widget->psz_text);
909 widget->psz_text = strdup([[field stringValue] UTF8String]);
910 vlc_mutex_unlock(&widget->p_dialog->lock);
913 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
915 id sender = [notifcation object];
916 assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
917 VLCDialogList *list = sender;
919 struct extension_widget_value_t *value;
921 for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
922 value->b_selected = (i == [list selectedRow]);
925 - (void)popUpSelectionChanged:(id)sender
927 assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
928 VLCDialogPopUpButton *popup = sender;
929 struct extension_widget_value_t *value;
931 for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
932 value->b_selected = (i == [popup indexOfSelectedItem]);
936 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
938 NSView *contentView = [sender contentView];
939 assert([contentView isKindOfClass:[VLCDialogGridView class]]);
940 VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
942 NSRect rect = NSMakeRect(0, 0, 0, 0);
943 rect.size = frameSize;
944 rect = [sender contentRectForFrameRect:rect];
945 rect.size = [gridView flexSize:rect.size];
946 rect = [sender frameRectForContentRect:rect];
950 - (BOOL)windowShouldClose:(id)sender
952 assert([sender isKindOfClass:[VLCDialogWindow class]]);
953 VLCDialogWindow *window = sender;
954 extension_dialog_t *dialog = [window dialog];
955 extension_DialogClosed(dialog);
956 dialog->p_sys_intf = NULL;
960 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
962 assert(!widget->p_sys_intf);
964 switch (widget->type)
966 case EXTENSION_WIDGET_HTML:
968 // NSScrollView *scrollView = [[NSScrollView alloc] init];
969 // [scrollView setHasVerticalScroller:YES];
970 // NSTextView *field = [[NSTextView alloc] init];
971 // [scrollView setDocumentView:field];
972 // [scrollView setAutoresizesSubviews:YES];
973 // [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
975 // return scrollView;
976 NSTextView *field = [[NSTextView alloc] init];
977 [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
978 [field setDrawsBackground:NO];
981 case EXTENSION_WIDGET_LABEL:
983 NSTextField *field = [[NSTextField alloc] init];
984 [field setEditable:NO];
985 [field setBordered:NO];
986 [field setDrawsBackground:NO];
987 [field setFont:[NSFont systemFontOfSize:0]];
988 [[field cell] setControlSize:NSRegularControlSize];
989 [field setAutoresizingMask:NSViewNotSizable];
992 case EXTENSION_WIDGET_TEXT_FIELD:
994 VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
995 [field setWidget:widget];
996 [field setAutoresizingMask:NSViewWidthSizable];
997 [field setFont:[NSFont systemFontOfSize:0]];
998 [[field cell] setControlSize:NSRegularControlSize];
999 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:) name:NSControlTextDidChangeNotification object:field];
1002 case EXTENSION_WIDGET_BUTTON:
1004 VLCDialogButton *button = [[VLCDialogButton alloc] init];
1005 [button setBezelStyle:NSRoundedBezelStyle];
1006 [button setWidget:widget];
1007 [button setAction:@selector(triggerClick:)];
1008 [button setTarget:self];
1009 [[button cell] setControlSize:NSRegularControlSize];
1010 [button setAutoresizingMask:NSViewNotSizable];
1013 case EXTENSION_WIDGET_DROPDOWN:
1015 VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
1016 [popup setAction:@selector(popUpSelectionChanged:)];
1017 [popup setTarget:self];
1018 [popup setWidget:widget];
1021 case EXTENSION_WIDGET_LIST:
1023 NSScrollView *scrollView = [[NSScrollView alloc] init];
1024 [scrollView setHasVerticalScroller:YES];
1025 VLCDialogList *list = [[VLCDialogList alloc] init];
1026 [list setUsesAlternatingRowBackgroundColors:YES];
1027 [list setHeaderView:nil];
1028 [scrollView setDocumentView:list];
1029 [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1031 NSTableColumn *column = [[NSTableColumn alloc] init];
1032 [list addTableColumn:column];
1034 [list setDataSource:list];
1035 [list setDelegate:self];
1036 [list setWidget:widget];
1040 case EXTENSION_WIDGET_IMAGE:
1042 NSImageView *imageView = [[NSImageView alloc] init];
1043 [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1044 [imageView setImageFrameStyle:NSImageFramePhoto];
1045 [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
1055 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
1057 switch (widget->type)
1059 case EXTENSION_WIDGET_HTML:
1061 // // Get the scroll view
1062 // assert([control isKindOfClass:[NSScrollView class]]);
1063 // NSScrollView *scrollView = (NSScrollView *)control;
1064 // control = [scrollView documentView];
1066 // assert([control isKindOfClass:[NSTextView class]]);
1067 // NSTextView *textView = (NSTextView *)control;
1068 // NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1069 // NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1070 // [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
1071 // NSLog(@"%@", string);
1072 // [textView setNeedsDisplay:YES];
1073 // [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1074 // [attrString release];
1079 assert([control isKindOfClass:[NSTextView class]]);
1080 NSTextView *textView = (NSTextView *)control;
1081 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1082 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1083 [[textView textStorage] setAttributedString:attrString];
1084 [textView setNeedsDisplay:YES];
1085 [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1086 [attrString release];
1090 case EXTENSION_WIDGET_LABEL:
1091 case EXTENSION_WIDGET_PASSWORD:
1092 case EXTENSION_WIDGET_TEXT_FIELD:
1094 if (!widget->psz_text)
1096 assert([control isKindOfClass:[NSControl class]]);
1097 NSControl *field = (NSControl *)control;
1098 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1099 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1100 [field setAttributedStringValue:attrString];
1101 [attrString release];
1104 case EXTENSION_WIDGET_BUTTON:
1106 assert([control isKindOfClass:[NSButton class]]);
1107 NSButton *button = (NSButton *)control;
1108 if (!widget->psz_text)
1110 [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
1113 case EXTENSION_WIDGET_DROPDOWN:
1115 assert([control isKindOfClass:[NSPopUpButton class]]);
1116 NSPopUpButton *popup = (NSPopUpButton *)control;
1117 [popup removeAllItems];
1118 struct extension_widget_value_t *value;
1119 for(value = widget->p_values; value != NULL; value = value->p_next)
1121 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
1123 [popup synchronizeTitleAndSelectedItem];
1124 [self popUpSelectionChanged:popup];
1128 case EXTENSION_WIDGET_LIST:
1130 assert([control isKindOfClass:[NSScrollView class]]);
1131 NSScrollView *scrollView = (NSScrollView *)control;
1132 assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
1133 VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
1135 NSMutableArray *contentArray = [NSMutableArray array];
1136 struct extension_widget_value_t *value;
1137 for(value = widget->p_values; value != NULL; value = value->p_next)
1139 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
1140 [NSNumber numberWithInt:value->i_id], @"id",
1141 [NSString stringWithUTF8String:value->psz_text], @"text",
1143 [contentArray addObject:entry];
1145 list.contentArray = contentArray;
1149 case EXTENSION_WIDGET_IMAGE:
1151 assert([control isKindOfClass:[NSImageView class]]);
1152 NSImageView *imageView = (NSImageView *)control;
1153 NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
1154 NSImage *image = nil;
1155 NSLog(@"Setting image to %@", string);
1157 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
1158 [imageView setImage:image];
1166 - (void)updateWidgets:(extension_dialog_t *)dialog
1168 extension_widget_t *widget;
1169 NSWindow *window = dialog->p_sys_intf;
1170 FOREACH_ARRAY(widget, dialog->widgets)
1173 continue; /* Some widgets may be NULL at this point */
1175 BOOL shouldDestroy = widget->b_kill;
1176 NSView *control = widget->p_sys_intf;
1177 BOOL update = widget->b_update;
1180 if (!control && !shouldDestroy)
1182 control = createControlFromWidget(widget, self);
1183 updateControlFromWidget(control, widget, self);
1184 widget->p_sys_intf = control;
1185 update = YES; // Force update and repositionning
1186 [control setHidden:widget->b_hide];
1189 if (update && !shouldDestroy)
1191 updateControlFromWidget(control, widget, self);
1192 [control setHidden:widget->b_hide];
1194 int row = widget->i_row - 1;
1195 int col = widget->i_column - 1;
1196 int hsp = __MAX( 1, widget->i_horiz_span );
1197 int vsp = __MAX( 1, widget->i_vert_span );
1204 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1205 [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
1207 //this->resize( sizeHint() );
1208 widget->b_update = false;
1213 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1214 [gridView removeSubview:control];
1216 widget->p_sys_intf = NULL;
1222 - (void)updateExtensionDialog:(NSValue *)extensionDialog
1224 extension_dialog_t *dialog = [extensionDialog pointerValue];
1226 vlc_mutex_lock(&dialog->lock);
1228 NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
1230 BOOL shouldDestroy = dialog->b_kill;
1232 if (!dialog->i_width || !dialog->i_height)
1233 size = NSMakeSize(640, 480);
1235 VLCDialogWindow *window = dialog->p_sys_intf;
1236 if (!window && !shouldDestroy)
1238 NSRect content = NSMakeRect(0, 0, 1, 1);
1239 window = [[VLCDialogWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
1240 [window setDelegate:self];
1241 [window setDialog:dialog];
1242 [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
1243 VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
1244 [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1245 [window setContentView:gridView];
1247 dialog->p_sys_intf = window;
1250 [self updateWidgets:dialog];
1254 [window setDelegate:nil];
1256 dialog->p_sys_intf = NULL;
1260 if (![window isVisible]) {
1262 [window makeKeyAndOrderFront:self];
1265 vlc_cond_signal(&dialog->cond);
1266 vlc_mutex_unlock(&dialog->lock);
1271 * Helper to execute a function on main thread and get its return value.
1273 - (void)execute:(NSDictionary *)dict
1275 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
1276 id *result = [[dict objectForKey:@"result"] pointerValue];
1277 id object = [dict objectForKey:@"object"];
1279 NSAssert(sel, @"Try to execute a NULL selector");
1281 *result = [self performSelector:sel withObject:object];
1282 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
1285 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
1288 NSAssert(sel, @"Try to execute a NULL selector");
1289 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
1290 [NSValue valueWithPointer:sel], @"sel",
1291 [NSValue valueWithPointer:&result], @"result",
1292 object, @"object", nil];
1293 [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
1294 return [result autorelease];