VLCDropboxController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // VLCDropboxController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 23.05.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCDropboxController.h"
  9. #import "VLCAppDelegate.h"
  10. @interface VLCDropboxController ()
  11. {
  12. DBRestClient *_restClient;
  13. NSArray *_currentFileList;
  14. NSInteger _outstandingNetworkRequests;
  15. }
  16. @end
  17. @implementation VLCDropboxController
  18. #pragma mark - session handling
  19. - (void)startSession
  20. {
  21. }
  22. - (void)logout
  23. {
  24. [[DBSession sharedSession] unlinkAll];
  25. }
  26. - (BOOL)sessionIsLinked
  27. {
  28. return [[DBSession sharedSession] isLinked];
  29. }
  30. - (DBRestClient *)restClient {
  31. if (!_restClient) {
  32. _restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
  33. _restClient.delegate = self;
  34. }
  35. return _restClient;
  36. }
  37. #pragma mark - file management
  38. - (void)requestDirectoryListingAtPath:(NSString *)path
  39. {
  40. if (self.sessionIsLinked)
  41. [[self restClient] loadMetadata:path];
  42. }
  43. - (void)downloadFileToDocumentFolder:(DBMetadata *)file
  44. {
  45. if (!file.isDirectory) {
  46. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  47. NSString *filePath = [searchPaths[0] stringByAppendingString:file.filename];
  48. //FIXME: add UI hook to display activity
  49. [[self restClient] loadFile:file.path intoPath:filePath];
  50. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  51. [self.delegate operationWithProgressInformationStarted];
  52. }
  53. }
  54. #pragma mark - restClient delegate
  55. - (BOOL)_supportedFileExtension:(NSString *)filename
  56. {
  57. if ([filename rangeOfString:kSupportedFileExtensions options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].length != 0 || [filename rangeOfString:kSupportedSubtitleFileExtensions options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].length != 0)
  58. return YES;
  59. return NO;
  60. }
  61. - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
  62. NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
  63. if (metadata.isDirectory) {
  64. NSArray *contents = metadata.contents;
  65. NSUInteger metaDataCount = metadata.contents.count;
  66. for (NSUInteger x = 0; x < metaDataCount; x++) {
  67. DBMetadata *file = contents[x];
  68. if ([file isDirectory] || [self _supportedFileExtension:file.filename])
  69. [listOfGoodFilesAndFolders addObject:file];
  70. }
  71. }
  72. _currentFileList = [NSArray arrayWithArray:listOfGoodFilesAndFolders];
  73. APLog(@"found filtered metadata for %i files", _currentFileList.count);
  74. if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
  75. [self.delegate mediaListUpdated];
  76. }
  77. - (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error
  78. {
  79. APLog(@"DBMetadata download failed with error %i", error.code);
  80. }
  81. - (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
  82. {
  83. /* update library now that we got a file */
  84. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  85. [appDelegate updateMediaList];
  86. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  87. [self.delegate operationWithProgressInformationStopped];
  88. }
  89. - (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
  90. {
  91. APLog(@"DBFile download failed with error %i", error.code);
  92. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  93. [self.delegate operationWithProgressInformationStopped];
  94. }
  95. - (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
  96. {
  97. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  98. [self.delegate currentProgressInformation:progress];
  99. }
  100. #pragma mark - DBSession delegate
  101. - (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId
  102. {
  103. APLog(@"DBSession received authorization failure with user ID %@", userId);
  104. }
  105. #pragma mark - DBNetworkRequest delegate
  106. - (void)networkRequestStarted
  107. {
  108. _outstandingNetworkRequests++;
  109. if (_outstandingNetworkRequests == 1)
  110. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  111. }
  112. - (void)networkRequestStopped
  113. {
  114. _outstandingNetworkRequests--;
  115. if (_outstandingNetworkRequests == 0)
  116. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  117. }
  118. #pragma mark - VLC internal communication and delegate
  119. - (NSArray *)currentListFiles
  120. {
  121. return _currentFileList;
  122. }
  123. @end