VLCDropboxController.m 4.4 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 "NSString+SupportedMedia.h"
  10. #import "VLCAppDelegate.h"
  11. @interface VLCDropboxController ()
  12. {
  13. DBRestClient *_restClient;
  14. NSArray *_currentFileList;
  15. NSInteger _outstandingNetworkRequests;
  16. }
  17. @end
  18. @implementation VLCDropboxController
  19. #pragma mark - session handling
  20. - (void)startSession
  21. {
  22. }
  23. - (void)logout
  24. {
  25. [[DBSession sharedSession] unlinkAll];
  26. }
  27. - (BOOL)sessionIsLinked
  28. {
  29. return [[DBSession sharedSession] isLinked];
  30. }
  31. - (DBRestClient *)restClient {
  32. if (!_restClient) {
  33. _restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
  34. _restClient.delegate = self;
  35. }
  36. return _restClient;
  37. }
  38. #pragma mark - file management
  39. - (void)requestDirectoryListingAtPath:(NSString *)path
  40. {
  41. if (self.sessionIsLinked)
  42. [[self restClient] loadMetadata:path];
  43. }
  44. - (void)downloadFileToDocumentFolder:(DBMetadata *)file
  45. {
  46. if (!file.isDirectory) {
  47. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  48. NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
  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 isSupportedMediaFormat] || [filename isSupportedSubtitleFormat])
  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