VLCiOSLegacyDialogProvider.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*****************************************************************************
  2. * VLCiOSLegacyDialogProvider.m: an implementation of the libvlc dialog API
  3. * Included for compatiblity with iOS 7
  4. *****************************************************************************
  5. * Copyright (C) 2009, 2014-2015 VLC authors and VideoLAN
  6. * Copyright (C) 2016 VideoLabs SAS
  7. * $Id$
  8. *
  9. * Authors: Felix Paul Kühne <fkuehne # videolan org>
  10. * Pierre d'Herbemont <pdherbemont # videolan org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCLibrary.h"
  27. #import "VLCiOSLegacyDialogProvider.h"
  28. @interface VLCiOSLegacyDialogProvider ()
  29. {
  30. VLCLibrary *_libraryInstance;
  31. }
  32. @end
  33. @interface VLCBlockingAlertView : UIAlertView <UIAlertViewDelegate>
  34. @property (copy, nonatomic) void (^completion)(BOOL, NSInteger);
  35. - (id)initWithTitle:(NSString *)title
  36. message:(NSString *)message
  37. cancelButtonTitle:(NSString *)cancelButtonTitle
  38. otherButtonTitles:(NSArray *)otherButtonTitles;
  39. @end
  40. static void displayErrorCallback(void *p_data,
  41. const char *psz_title,
  42. const char *psz_text)
  43. {
  44. @autoreleasepool {
  45. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  46. [dialogProvider performSelectorOnMainThread:@selector(displayError:)
  47. withObject:@[toNSStr(psz_title),
  48. toNSStr(psz_text)]
  49. waitUntilDone:NO];
  50. }
  51. }
  52. static void displayLoginCallback(void *p_data,
  53. libvlc_dialog_id *p_id,
  54. const char *psz_title,
  55. const char *psz_text,
  56. const char *psz_default_username,
  57. bool b_ask_store)
  58. {
  59. @autoreleasepool {
  60. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  61. [dialogProvider performSelectorOnMainThread:@selector(displayLoginDialog:)
  62. withObject:@[[NSValue valueWithPointer:p_id],
  63. toNSStr(psz_title),
  64. toNSStr(psz_text),
  65. toNSStr(psz_default_username),
  66. @(b_ask_store)]
  67. waitUntilDone:NO];
  68. }
  69. }
  70. static void displayQuestionCallback(void *p_data,
  71. libvlc_dialog_id *p_id,
  72. const char *psz_title,
  73. const char *psz_text,
  74. libvlc_dialog_question_type i_type,
  75. const char *psz_cancel,
  76. const char *psz_action1,
  77. const char *psz_action2)
  78. {
  79. @autoreleasepool {
  80. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  81. [dialogProvider performSelectorOnMainThread:@selector(displayQuestion:)
  82. withObject:@[[NSValue valueWithPointer:p_id],
  83. toNSStr(psz_title),
  84. toNSStr(psz_text),
  85. @(i_type),
  86. toNSStr(psz_cancel),
  87. toNSStr(psz_action1),
  88. toNSStr(psz_action2)]
  89. waitUntilDone:NO];
  90. }
  91. }
  92. static void displayProgressCallback(void *p_data,
  93. libvlc_dialog_id *p_id,
  94. const char *psz_title,
  95. const char *psz_text,
  96. bool b_indeterminate,
  97. float f_position,
  98. const char *psz_cancel)
  99. {
  100. @autoreleasepool {
  101. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  102. [dialogProvider performSelectorOnMainThread:@selector(displayProgressDialog:)
  103. withObject:@[[NSValue valueWithPointer:p_id],
  104. toNSStr(psz_title),
  105. toNSStr(psz_text),
  106. @(b_indeterminate),
  107. @(f_position),
  108. toNSStr(psz_cancel)]
  109. waitUntilDone:NO];
  110. }
  111. }
  112. static void cancelCallback(void *p_data,
  113. libvlc_dialog_id *p_id)
  114. {
  115. @autoreleasepool {
  116. // FIXME: the saddest NO-OP
  117. VKLog(@"%s: %i", __PRETTY_FUNCTION__, (int)p_id);
  118. }
  119. }
  120. static void updateProgressCallback(void *p_data,
  121. libvlc_dialog_id *p_id,
  122. float f_position,
  123. const char *psz_text)
  124. {
  125. @autoreleasepool {
  126. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  127. [dialogProvider performSelectorOnMainThread:@selector(updateDisplayedProgressDialog:)
  128. withObject:@[[NSValue valueWithPointer:p_id],
  129. @(f_position),
  130. toNSStr(psz_text)]
  131. waitUntilDone:NO];
  132. }
  133. }
  134. @implementation VLCiOSLegacyDialogProvider
  135. - (void)dealloc
  136. {
  137. libvlc_dialog_set_callbacks(_libraryInstance.instance,
  138. NULL,
  139. NULL);
  140. }
  141. - (instancetype)initWithLibrary:(VLCLibrary *)library
  142. {
  143. self = [super init];
  144. if (self != nil) {
  145. if (library == nil) {
  146. library = [VLCLibrary sharedLibrary];
  147. }
  148. _libraryInstance = library;
  149. /* callback setup */
  150. const libvlc_dialog_cbs cbs = {
  151. displayErrorCallback,
  152. displayLoginCallback,
  153. displayQuestionCallback,
  154. displayProgressCallback,
  155. cancelCallback,
  156. updateProgressCallback
  157. };
  158. libvlc_dialog_set_callbacks(_libraryInstance.instance,
  159. &cbs,
  160. (__bridge void *)self);
  161. }
  162. return self;
  163. }
  164. - (void)displayError:(NSArray * _Nonnull)dialogData
  165. {
  166. VLCBlockingAlertView *alert = [[VLCBlockingAlertView alloc] initWithTitle:dialogData[0]
  167. message:dialogData[1]
  168. delegate:nil
  169. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  170. otherButtonTitles:nil];
  171. alert.completion = nil;
  172. [alert show];
  173. }
  174. - (void)displayLoginDialog:(NSArray * _Nonnull)dialogData
  175. {
  176. VLCBlockingAlertView *alert = [[VLCBlockingAlertView alloc] initWithTitle:dialogData[1]
  177. message:dialogData[2]
  178. delegate:nil
  179. cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
  180. otherButtonTitles:NSLocalizedString(@"Login", nil), [dialogData[4] boolValue] ? NSLocalizedString(@"Store", nil) : nil, nil];
  181. alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
  182. __weak typeof(alert) weakAlert = alert;
  183. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  184. if (!cancelled) {
  185. NSString *username = [weakAlert textFieldAtIndex:0].text;
  186. NSString *password = [weakAlert textFieldAtIndex:1].text;
  187. libvlc_dialog_post_login([dialogData[0] pointerValue],
  188. username ? [username UTF8String] : "",
  189. password ? [password UTF8String] : "",
  190. buttonIndex != alert.firstOtherButtonIndex);
  191. } else {
  192. libvlc_dialog_dismiss([dialogData[0] pointerValue]);
  193. }
  194. };
  195. alert.delegate = alert;
  196. [alert show];
  197. }
  198. - (void)displayQuestion:(NSArray * _Nonnull)dialogData
  199. {
  200. VLCBlockingAlertView * alert = [[VLCBlockingAlertView alloc] initWithTitle:dialogData[1]
  201. message:dialogData[2]
  202. delegate:nil
  203. cancelButtonTitle:dialogData[4]
  204. otherButtonTitles:dialogData[5], dialogData[6], nil];
  205. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  206. if (cancelled)
  207. libvlc_dialog_post_action([dialogData[0] pointerValue], 3);
  208. else
  209. libvlc_dialog_post_action([dialogData[0] pointerValue], (int)buttonIndex);
  210. };
  211. alert.delegate = alert;
  212. [alert show];
  213. }
  214. - (void)displayProgressDialog:(NSArray * _Nonnull)dialogData
  215. {
  216. VKLog(@"%s: %@", __PRETTY_FUNCTION__, dialogData);
  217. }
  218. - (void)updateDisplayedProgressDialog:(NSArray * _Nonnull)dialogData
  219. {
  220. VKLog(@"%s: %@", __PRETTY_FUNCTION__, dialogData);
  221. }
  222. @end
  223. @implementation VLCBlockingAlertView
  224. - (id)initWithTitle:(NSString *)title
  225. message:(NSString *)message
  226. cancelButtonTitle:(NSString *)cancelButtonTitle
  227. otherButtonTitles:(NSArray *)otherButtonTitles
  228. {
  229. self = [self initWithTitle:title
  230. message:message
  231. delegate:self
  232. cancelButtonTitle:cancelButtonTitle
  233. otherButtonTitles:nil];
  234. if (self) {
  235. for (NSString *buttonTitle in otherButtonTitles)
  236. [self addButtonWithTitle:buttonTitle];
  237. }
  238. return self;
  239. }
  240. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  241. {
  242. if (self.completion) {
  243. self.completion(buttonIndex == self.cancelButtonIndex, buttonIndex);
  244. self.completion = nil;
  245. }
  246. }
  247. @end