VLCDropboxController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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] stringByAppendingFormat:@"/%@", file.filename];
  48. [[self restClient] loadFile:file.path intoPath:filePath];
  49. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  50. [self.delegate operationWithProgressInformationStarted];
  51. }
  52. }
  53. #pragma mark - restClient delegate
  54. - (BOOL)_supportedFileExtension:(NSString *)filename
  55. {
  56. if ([filename rangeOfString:kSupportedFileExtensions options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].length != 0 || [filename rangeOfString:kSupportedSubtitleFileExtensions options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].length != 0)
  57. return YES;
  58. return NO;
  59. }
  60. - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
  61. NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
  62. if (metadata.isDirectory) {
  63. NSArray *contents = metadata.contents;
  64. NSUInteger metaDataCount = metadata.contents.count;
  65. for (NSUInteger x = 0; x < metaDataCount; x++) {
  66. DBMetadata *file = contents[x];
  67. if ([file isDirectory] || [self _supportedFileExtension:file.filename])
  68. [listOfGoodFilesAndFolders addObject:file];
  69. }
  70. }
  71. _currentFileList = [NSArray arrayWithArray:listOfGoodFilesAndFolders];
  72. APLog(@"found filtered metadata for %i files", _currentFileList.count);
  73. if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
  74. [self.delegate mediaListUpdated];
  75. }
  76. - (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error
  77. {
  78. APLog(@"DBMetadata download failed with error %i", error.code);
  79. }
  80. - (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
  81. {
  82. /* update library now that we got a file */
  83. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  84. [appDelegate updateMediaList];
  85. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  86. [self.delegate operationWithProgressInformationStopped];
  87. }
  88. - (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
  89. {
  90. APLog(@"DBFile download failed with error %i", error.code);
  91. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  92. [self.delegate operationWithProgressInformationStopped];
  93. }
  94. - (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
  95. {
  96. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  97. [self.delegate currentProgressInformation:progress];
  98. }
  99. #pragma mark - DBSession delegate
  100. - (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId
  101. {
  102. APLog(@"DBSession received authorization failure with user ID %@", userId);
  103. }
  104. #pragma mark - DBNetworkRequest delegate
  105. - (void)networkRequestStarted
  106. {
  107. _outstandingNetworkRequests++;
  108. if (_outstandingNetworkRequests == 1)
  109. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  110. }
  111. - (void)networkRequestStopped
  112. {
  113. _outstandingNetworkRequests--;
  114. if (_outstandingNetworkRequests == 0)
  115. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  116. }
  117. #pragma mark - VLC internal communication and delegate
  118. - (NSArray *)currentListFiles
  119. {
  120. return _currentFileList;
  121. }
  122. @end