VLCDropboxController.m 4.5 KB

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