VLCiOSLegacyDialogProvider.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. - (instancetype)initWithLibrary:(VLCLibrary *)library;
  33. - (void)displayError:(NSArray * _Nonnull)dialogData;
  34. - (void)displayLoginDialog:(NSArray * _Nonnull)dialogData;
  35. - (void)displayQuestion:(NSArray * _Nonnull)dialogData;
  36. - (void)displayProgressDialog:(NSArray * _Nonnull)dialogData;
  37. - (void)updateDisplayedProgressDialog:(NSArray * _Nonnull)dialogData;
  38. @end
  39. @interface VLCBlockingAlertView : UIAlertView <UIAlertViewDelegate>
  40. @property (copy, nonatomic) void (^completion)(BOOL, NSInteger);
  41. - (id)initWithTitle:(NSString *)title
  42. message:(NSString *)message
  43. cancelButtonTitle:(NSString *)cancelButtonTitle
  44. otherButtonTitles:(NSArray *)otherButtonTitles;
  45. @end
  46. static void displayErrorCallback(void *p_data,
  47. const char *psz_title,
  48. const char *psz_text)
  49. {
  50. @autoreleasepool {
  51. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  52. [dialogProvider performSelectorOnMainThread:@selector(displayError:)
  53. withObject:@[toNSStr(psz_title),
  54. toNSStr(psz_text)]
  55. waitUntilDone:NO];
  56. }
  57. }
  58. static void displayLoginCallback(void *p_data,
  59. libvlc_dialog_id *p_id,
  60. const char *psz_title,
  61. const char *psz_text,
  62. const char *psz_default_username,
  63. bool b_ask_store)
  64. {
  65. @autoreleasepool {
  66. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  67. [dialogProvider performSelectorOnMainThread:@selector(displayLoginDialog:)
  68. withObject:@[[NSValue valueWithPointer:p_id],
  69. toNSStr(psz_title),
  70. toNSStr(psz_text),
  71. toNSStr(psz_default_username),
  72. @(b_ask_store)]
  73. waitUntilDone:NO];
  74. }
  75. }
  76. static void displayQuestionCallback(void *p_data,
  77. libvlc_dialog_id *p_id,
  78. const char *psz_title,
  79. const char *psz_text,
  80. libvlc_dialog_question_type i_type,
  81. const char *psz_cancel,
  82. const char *psz_action1,
  83. const char *psz_action2)
  84. {
  85. @autoreleasepool {
  86. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  87. [dialogProvider performSelectorOnMainThread:@selector(displayQuestion:)
  88. withObject:@[[NSValue valueWithPointer:p_id],
  89. toNSStr(psz_title),
  90. toNSStr(psz_text),
  91. @(i_type),
  92. toNSStr(psz_cancel),
  93. toNSStr(psz_action1),
  94. toNSStr(psz_action2)]
  95. waitUntilDone:NO];
  96. }
  97. }
  98. static void displayProgressCallback(void *p_data,
  99. libvlc_dialog_id *p_id,
  100. const char *psz_title,
  101. const char *psz_text,
  102. bool b_indeterminate,
  103. float f_position,
  104. const char *psz_cancel)
  105. {
  106. @autoreleasepool {
  107. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  108. [dialogProvider performSelectorOnMainThread:@selector(displayProgressDialog:)
  109. withObject:@[[NSValue valueWithPointer:p_id],
  110. toNSStr(psz_title),
  111. toNSStr(psz_text),
  112. @(b_indeterminate),
  113. @(f_position),
  114. toNSStr(psz_cancel)]
  115. waitUntilDone:NO];
  116. }
  117. }
  118. static void cancelCallback(void *p_data,
  119. libvlc_dialog_id *p_id)
  120. {
  121. @autoreleasepool {
  122. // FIXME: the saddest NO-OP
  123. VKLog(@"%s: %i", __PRETTY_FUNCTION__, (int)p_id);
  124. }
  125. }
  126. static void updateProgressCallback(void *p_data,
  127. libvlc_dialog_id *p_id,
  128. float f_position,
  129. const char *psz_text)
  130. {
  131. @autoreleasepool {
  132. VLCiOSLegacyDialogProvider *dialogProvider = (__bridge VLCiOSLegacyDialogProvider *)p_data;
  133. [dialogProvider performSelectorOnMainThread:@selector(updateDisplayedProgressDialog:)
  134. withObject:@[[NSValue valueWithPointer:p_id],
  135. @(f_position),
  136. toNSStr(psz_text)]
  137. waitUntilDone:NO];
  138. }
  139. }
  140. @implementation VLCiOSLegacyDialogProvider
  141. - (void)dealloc
  142. {
  143. libvlc_dialog_set_callbacks(_libraryInstance.instance,
  144. NULL,
  145. NULL);
  146. }
  147. - (instancetype)initWithLibrary:(VLCLibrary *)library
  148. {
  149. self = [super init];
  150. if (self != nil) {
  151. if (library == nil) {
  152. library = [VLCLibrary sharedLibrary];
  153. }
  154. _libraryInstance = library;
  155. /* callback setup */
  156. const libvlc_dialog_cbs cbs = {
  157. displayErrorCallback,
  158. displayLoginCallback,
  159. displayQuestionCallback,
  160. displayProgressCallback,
  161. cancelCallback,
  162. updateProgressCallback
  163. };
  164. libvlc_dialog_set_callbacks(_libraryInstance.instance,
  165. &cbs,
  166. (__bridge void *)self);
  167. }
  168. return self;
  169. }
  170. - (void)displayError:(NSArray * _Nonnull)dialogData
  171. {
  172. VLCBlockingAlertView *alert = [[VLCBlockingAlertView alloc] initWithTitle:dialogData[0]
  173. message:dialogData[1]
  174. delegate:nil
  175. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  176. otherButtonTitles:nil];
  177. alert.completion = nil;
  178. [alert show];
  179. }
  180. - (void)displayLoginDialog:(NSArray * _Nonnull)dialogData
  181. {
  182. VLCBlockingAlertView *alert = [[VLCBlockingAlertView alloc] initWithTitle:dialogData[1]
  183. message:dialogData[2]
  184. delegate:nil
  185. cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
  186. otherButtonTitles:NSLocalizedString(@"Login", nil), [dialogData[4] boolValue] ? NSLocalizedString(@"Store", nil) : nil, nil];
  187. alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
  188. __weak typeof(alert) weakAlert = alert;
  189. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  190. if (!cancelled) {
  191. NSString *username = [weakAlert textFieldAtIndex:0].text;
  192. NSString *password = [weakAlert textFieldAtIndex:1].text;
  193. libvlc_dialog_post_login([dialogData[0] pointerValue],
  194. username ? [username UTF8String] : "",
  195. password ? [password UTF8String] : "",
  196. buttonIndex != alert.firstOtherButtonIndex);
  197. } else {
  198. libvlc_dialog_dismiss([dialogData[0] pointerValue]);
  199. }
  200. };
  201. alert.delegate = alert;
  202. [alert show];
  203. }
  204. - (void)displayQuestion:(NSArray * _Nonnull)dialogData
  205. {
  206. VLCBlockingAlertView * alert = [[VLCBlockingAlertView alloc] initWithTitle:dialogData[1]
  207. message:dialogData[2]
  208. delegate:nil
  209. cancelButtonTitle:dialogData[4]
  210. otherButtonTitles:dialogData[5], dialogData[6], nil];
  211. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  212. if (cancelled)
  213. libvlc_dialog_post_action([dialogData[0] pointerValue], 3);
  214. else
  215. libvlc_dialog_post_action([dialogData[0] pointerValue], (int)buttonIndex);
  216. };
  217. alert.delegate = alert;
  218. [alert show];
  219. }
  220. - (void)displayProgressDialog:(NSArray * _Nonnull)dialogData
  221. {
  222. VKLog(@"%s: %@", __PRETTY_FUNCTION__, dialogData);
  223. }
  224. - (void)updateDisplayedProgressDialog:(NSArray * _Nonnull)dialogData
  225. {
  226. VKLog(@"%s: %@", __PRETTY_FUNCTION__, dialogData);
  227. }
  228. @end
  229. @implementation VLCBlockingAlertView
  230. - (id)initWithTitle:(NSString *)title
  231. message:(NSString *)message
  232. cancelButtonTitle:(NSString *)cancelButtonTitle
  233. otherButtonTitles:(NSArray *)otherButtonTitles
  234. {
  235. self = [self initWithTitle:title
  236. message:message
  237. delegate:self
  238. cancelButtonTitle:cancelButtonTitle
  239. otherButtonTitles:nil];
  240. if (self) {
  241. for (NSString *buttonTitle in otherButtonTitles)
  242. [self addButtonWithTitle:buttonTitle];
  243. }
  244. return self;
  245. }
  246. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  247. {
  248. if (self.completion) {
  249. self.completion(buttonIndex == self.cancelButtonIndex, buttonIndex);
  250. self.completion = nil;
  251. }
  252. }
  253. @end