VLCDropboxController.m 4.0 KB

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