CCFavorites.m 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //
  2. // CCFavorites.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 16/01/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCFavorites.h"
  24. #import "AppDelegate.h"
  25. #import "CCSynchronize.h"
  26. #import "NCBridgeSwift.h"
  27. @interface CCFavorites ()
  28. {
  29. AppDelegate *appDelegate;
  30. NSArray *_dataSource;
  31. BOOL _reloadDataSource;
  32. // Automatic Upload Folder
  33. NSString *_autoUploadFileName;
  34. NSString *_autoUploadDirectory;
  35. }
  36. @end
  37. @implementation CCFavorites
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== Init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  47. appDelegate.activeFavorites = self;
  48. }
  49. return self;
  50. }
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMain" bundle:nil] forCellReuseIdentifier:@"CellMain"];
  55. // dataSource
  56. _dataSource = [NSMutableArray new];
  57. // Metadata
  58. _metadata = [tableMetadata new];
  59. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  60. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  61. self.tableView.emptyDataSetDelegate = self;
  62. self.tableView.emptyDataSetSource = self;
  63. self.tableView.delegate = self;
  64. // calculate _serverUrl
  65. if (!_serverUrl)
  66. _serverUrl = nil;
  67. // Title
  68. if (_titleViewControl)
  69. self.title = _titleViewControl;
  70. else
  71. self.title = NSLocalizedString(@"_favorites_", nil);
  72. }
  73. // Apparirà
  74. - (void)viewWillAppear:(BOOL)animated
  75. {
  76. [super viewWillAppear:animated];
  77. // Color
  78. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  79. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  80. // Plus Button
  81. [appDelegate plusButtonVisibile:true];
  82. [self reloadDatasource];
  83. }
  84. // E' arrivato
  85. - (void)viewDidAppear:(BOOL)animated
  86. {
  87. [super viewDidAppear:animated];
  88. // Active Main
  89. appDelegate.activeFavorites = self;
  90. }
  91. - (void)changeTheming
  92. {
  93. if (self.isViewLoaded && self.view.window)
  94. [appDelegate changeTheming:self];
  95. // Reload Table View
  96. [self.tableView reloadData];
  97. }
  98. - (void)triggerProgressTask:(NSNotification *)notification
  99. {
  100. //NSDictionary *dict = notification.userInfo;
  101. //float progress = [[dict valueForKey:@"progress"] floatValue];
  102. }
  103. #pragma --------------------------------------------------------------------------------------------
  104. #pragma mark ==== DZNEmptyDataSetSource ====
  105. #pragma --------------------------------------------------------------------------------------------
  106. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  107. {
  108. return [NCBrandColor sharedInstance].backgroundView;
  109. }
  110. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  111. {
  112. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favoriteNoFiles"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite];
  113. }
  114. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  115. {
  116. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  117. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  118. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  119. }
  120. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  121. {
  122. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  123. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  124. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  125. paragraph.alignment = NSTextAlignmentCenter;
  126. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  127. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  128. }
  129. #pragma --------------------------------------------------------------------------------------------
  130. #pragma mark ===== Delete =====
  131. #pragma--------------------------------------------------------------------------------------------
  132. - (void)deleteFile:(NSArray *)metadatas e2ee:(BOOL)e2ee
  133. {
  134. NSInteger numDelete = metadatas.count;
  135. __block NSInteger cont = 0;
  136. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  137. for (tableMetadata *metadata in metadatas) {
  138. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  139. [ocNetworking deleteFileOrFolder:metadata.fileName serverUrl:serverUrl completion:^(NSString *message, NSInteger errorCode) {
  140. if (errorCode == 0 || errorCode == 404) {
  141. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  142. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID] clearDateReadDirectoryID:metadata.directoryID];
  143. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  144. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:metadata.fileID];
  145. [[appDelegate activePhotos].fileIDHide addObject:metadata.fileID];
  146. // Directory ?
  147. if (metadata.directory) {
  148. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName]];
  149. }
  150. // E2EE (if exists the record)
  151. if (e2ee) {
  152. [[NCManageDatabase sharedInstance] deleteE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, serverUrl, metadata.fileName]];
  153. }
  154. }
  155. if (++cont == numDelete) {
  156. if (e2ee) {
  157. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  158. [[NCNetworkingEndToEnd sharedManager] rebuildAndSendEndToEndMetadataOnServerUrl:serverUrl account:appDelegate.activeAccount user:appDelegate.activeUser userID:appDelegate.activeUserID password:appDelegate.activePassword url:appDelegate.activeUrl];
  159. dispatch_async(dispatch_get_main_queue(), ^{
  160. // ONLY for this View
  161. [self reloadDatasource];
  162. });
  163. });
  164. } else {
  165. // ONLY for this View
  166. [self reloadDatasource];;
  167. }
  168. }
  169. }];
  170. }
  171. }
  172. #pragma --------------------------------------------------------------------------------------------
  173. #pragma mark ===== Favorite =====
  174. #pragma--------------------------------------------------------------------------------------------
  175. - (void)addFavoriteFolder:(NSString *)serverUrl
  176. {
  177. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  178. if (!directoryID) return;
  179. NSString *selector;
  180. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  181. metadataNet.action = actionReadFolder;
  182. metadataNet.depth = @"1";
  183. metadataNet.directoryID = directoryID;
  184. if ([CCUtility getFavoriteOffline])
  185. selector = selectorReadFolderWithDownload;
  186. else
  187. selector = selectorReadFolder;
  188. metadataNet.selector = selector;
  189. metadataNet.serverUrl = serverUrl;
  190. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  191. }
  192. - (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
  193. {
  194. NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:[[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID] activeUrl:appDelegate.activeUrl];
  195. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  196. [ocNetworking settingFavorite:fileNameServerUrl favorite:favorite completion:^(NSString *message, NSInteger errorCode) {
  197. if (errorCode == 0) {
  198. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:favorite];
  199. [self reloadDatasource];
  200. } else {
  201. if (errorCode == kOCErrorServerUnauthorized)
  202. [appDelegate openLoginView:self loginType:k_login_Modify_Password selector:k_intro_login];
  203. }
  204. }];
  205. }
  206. #pragma --------------------------------------------------------------------------------------------
  207. #pragma mark ===== listingFavorites =====
  208. #pragma--------------------------------------------------------------------------------------------
  209. - (void)listingFavorites
  210. {
  211. // test
  212. if (appDelegate.activeAccount.length == 0)
  213. return;
  214. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  215. [ocNetworking listingFavorites:@"" account:appDelegate.activeAccount success:^(NSArray *metadatas) {
  216. NSString *father = @"";
  217. NSMutableArray *filesEtag = [NSMutableArray new];
  218. for (tableMetadata *metadata in metadatas) {
  219. // insert for test NOT favorite
  220. [filesEtag addObject:metadata.fileID];
  221. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  222. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  223. if (![serverUrlSon containsString:father]) {
  224. if (metadata.directory) {
  225. if ([CCUtility getFavoriteOffline])
  226. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  227. else
  228. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolder];
  229. } else {
  230. if ([CCUtility getFavoriteOffline])
  231. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  232. else
  233. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  234. }
  235. father = serverUrlSon;
  236. }
  237. }
  238. // Verify remove favorite
  239. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  240. for (tableMetadata *metadata in allRecordFavorite)
  241. if (![filesEtag containsObject:metadata.fileID])
  242. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  243. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  244. } failure:^(NSString *message, NSInteger errorCode) {
  245. NSLog(@"[LOG] Listing Favorites failure error %d, %@", (int)errorCode, message);
  246. }];
  247. }
  248. #pragma --------------------------------------------------------------------------------------------
  249. #pragma mark ==== Download Thumbnail ====
  250. #pragma --------------------------------------------------------------------------------------------
  251. - (void)downloadThumbnail:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl indexPath:(NSIndexPath *)indexPath
  252. {
  253. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  254. [ocNetworking downloadThumbnailWithDimOfThumbnail:@"m" fileID:metadata.fileID fileNamePath:[CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:serverUrl activeUrl:appDelegate.activeUrl] fileNameView:metadata.fileNameView completion:^(NSString *message, NSInteger errorCode) {
  255. if(errorCode == 0 && [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  256. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  257. }
  258. }];
  259. }
  260. #pragma --------------------------------------------------------------------------------------------
  261. #pragma mark ==== Download <Delegate> ====
  262. #pragma --------------------------------------------------------------------------------------------
  263. - (void)downloadStart:(NSString *)fileID account:(NSString *)account task:(NSURLSessionDownloadTask *)task serverUrl:(NSString *)serverUrl
  264. {
  265. [self reloadDatasource];
  266. [appDelegate updateApplicationIconBadgeNumber];
  267. }
  268. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  269. {
  270. if (errorCode == 0) {
  271. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  272. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  273. [self openWith:_metadata];
  274. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  275. [self openWith:_metadata];
  276. } else {
  277. if ([self shouldPerformSegue])
  278. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  279. }
  280. } else {
  281. if (errorCode != k_CCErrorFileAlreadyInDownload)
  282. [appDelegate messageNotification:@"_download_file_" description:errorMessage visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  283. }
  284. }
  285. - (void)openWith:(tableMetadata *)metadata
  286. {
  287. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView]) {
  288. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView]];
  289. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  290. _docController.delegate = self;
  291. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  292. }
  293. }
  294. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  295. {
  296. CGPoint location = [tapGesture locationInView:self.tableView];
  297. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  298. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  299. if (metadata)
  300. [appDelegate.activeMain openWindowShare:metadata];
  301. }
  302. #pragma mark -
  303. #pragma --------------------------------------------------------------------------------------------
  304. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  305. #pragma --------------------------------------------------------------------------------------------
  306. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  307. {
  308. return YES;
  309. }
  310. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  311. {
  312. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  313. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  314. return [self canOpenMenuAction:metadata];
  315. }
  316. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  317. {
  318. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  319. if (direction == MGSwipeDirectionRightToLeft) {
  320. [self actionDelete:indexPath];
  321. }
  322. if (direction == MGSwipeDirectionLeftToRight) {
  323. [self settingFavorite:[_dataSource objectAtIndex:indexPath.row] favorite:NO];
  324. }
  325. return YES;
  326. }
  327. - (void)actionDelete:(NSIndexPath *)indexPath
  328. {
  329. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  330. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  331. [self deleteFile:[[NSArray alloc] initWithObjects:[_dataSource objectAtIndex:indexPath.row], nil] e2ee:false];
  332. }]];
  333. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  334. }]];
  335. alertController.popoverPresentationController.sourceView = self.view;
  336. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  337. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  338. [alertController.view layoutIfNeeded];
  339. [self presentViewController:alertController animated:YES completion:nil];
  340. }
  341. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  342. {
  343. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  344. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  345. UIImage *iconHeader;
  346. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  347. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  348. actionSheet.animationDuration = 0.2;
  349. actionSheet.buttonHeight = 50.0;
  350. actionSheet.cancelButtonHeight = 50.0f;
  351. actionSheet.separatorHeight = 5.0f;
  352. actionSheet.automaticallyTintButtonImages = @(NO);
  353. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  354. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  355. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  356. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  357. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  358. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  359. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  360. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  361. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  362. } else {
  363. if (metadata.directory)
  364. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  365. else
  366. iconHeader = [UIImage imageNamed:metadata.iconName];
  367. }
  368. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  369. ];
  370. // Share
  371. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  372. [appDelegate.activeMain openWindowShare:metadata];
  373. }];
  374. // NO Directory
  375. if (metadata.directory == NO) {
  376. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"openFile"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  377. [self.tableView setEditing:NO animated:YES];
  378. [self openWith:metadata];
  379. }];
  380. }
  381. [actionSheet show];
  382. }
  383. #pragma --------------------------------------------------------------------------------------------
  384. #pragma mark ==== Table ====
  385. #pragma --------------------------------------------------------------------------------------------
  386. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  387. {
  388. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  389. return metadata;
  390. }
  391. - (void)readFolder:(NSString *)serverUrl
  392. {
  393. [self reloadDatasource];
  394. }
  395. - (void)reloadDatasource
  396. {
  397. NSMutableArray *metadatas = [NSMutableArray new];
  398. NSArray *recordsTableMetadata ;
  399. NSString *sorted = [CCUtility getOrderSettings];
  400. if ([sorted isEqualToString:@"fileName"])
  401. sorted = @"fileName";
  402. if (!_serverUrl) {
  403. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  404. } else {
  405. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  406. if (directoryID)
  407. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@", directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  408. }
  409. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil fileIDHide:nil activeAccount:appDelegate.activeAccount];
  410. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  411. for (NSString *fileID in fileIDs)
  412. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  413. _dataSource = [NSArray arrayWithArray:metadatas];
  414. // get auto upload folder
  415. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  416. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  417. [self.tableView reloadData];
  418. }
  419. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  420. {
  421. return 60;
  422. }
  423. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  424. {
  425. return 1;
  426. }
  427. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  428. {
  429. return [_dataSource count];
  430. }
  431. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  432. {
  433. CCCellMain *cell = (CCCellMain *)[tableView dequeueReusableCellWithIdentifier:@"CellMain" forIndexPath:indexPath];
  434. tableMetadata *metadata;
  435. // variable base
  436. cell.delegate = self;
  437. // separator
  438. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  439. // Initialize
  440. cell.status.image = nil;
  441. cell.favorite.image = nil;
  442. cell.local.image = nil;
  443. cell.shared.image = nil;
  444. // change color selection
  445. UIView *selectionColor = [[UIView alloc] init];
  446. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  447. cell.selectedBackgroundView = selectionColor;
  448. metadata = [_dataSource objectAtIndex:indexPath.row];
  449. // favorite
  450. if (_serverUrl == nil)
  451. cell.favorite.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite];
  452. cell.labelTitle.textColor = [UIColor blackColor];
  453. // filename
  454. cell.labelTitle.text = metadata.fileNameView;
  455. cell.labelInfoFile.text = @"";
  456. // Shared
  457. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  458. if (!serverUrl)
  459. return cell;
  460. NSString *shareLink = [appDelegate.sharesLink objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  461. NSString *shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  462. // Immage
  463. if (metadata.directory) {
  464. if (metadata.e2eEncrypted) {
  465. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] multiplier:3 color:[NCBrandColor sharedInstance].brandElement];
  466. } else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory]) {
  467. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderPhotos"] multiplier:3 color:[NCBrandColor sharedInstance].brandElement];
  468. } else if ([shareLink length] > 0) {
  469. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_public"] multiplier:3 color:[NCBrandColor sharedInstance].brandElement];
  470. } else if ([shareUserAndGroup length] > 0) {
  471. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_shared_with_me"] multiplier:3 color:[NCBrandColor sharedInstance].brandElement];
  472. } else {
  473. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:3 color:[NCBrandColor sharedInstance].brandElement];
  474. }
  475. } else {
  476. if ([shareLink length] > 0 || [shareUserAndGroup length] > 0) {
  477. if ([shareLink length] > 0)
  478. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"sharebylink"] multiplier:2 color:[NCBrandColor sharedInstance].icon];
  479. else
  480. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] multiplier:2 color:[NCBrandColor sharedInstance].icon];
  481. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionConnectionMounted:)];
  482. [tap setNumberOfTapsRequired:1];
  483. cell.shared.userInteractionEnabled = YES;
  484. [cell.shared addGestureRecognizer:tap];
  485. }
  486. cell.file.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  487. if (cell.file.image == nil) {
  488. cell.file.image = [UIImage imageNamed:metadata.iconName];
  489. if (metadata.thumbnailExists) {
  490. [self downloadThumbnail:metadata serverUrl:serverUrl indexPath:indexPath];
  491. }
  492. }
  493. // ----------------------------------------------------------------------------------------------------------
  494. // E2EE Image Status Encrypted
  495. // ----------------------------------------------------------------------------------------------------------
  496. tableE2eEncryption *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND fileNameIdentifier == %@", appDelegate.activeAccount, metadata.fileName]];
  497. if (tableE2eEncryption)
  498. cell.status.image = [UIImage imageNamed:@"encrypted"];
  499. }
  500. // text and length
  501. if (metadata.directory) {
  502. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  503. cell.accessoryType = UITableViewCellAccessoryNone;
  504. } else {
  505. NSString *date = [CCUtility dateDiff:metadata.date];
  506. NSString *length = [CCUtility transformedSize:metadata.size];
  507. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView])
  508. cell.local.image = [UIImage imageNamed:@"local"];
  509. else
  510. cell.local.image = nil;
  511. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  512. cell.accessoryType = UITableViewCellAccessoryNone;
  513. }
  514. // ----------------------------------------------------------------------------------------------------------
  515. // swipe
  516. // ----------------------------------------------------------------------------------------------------------
  517. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  518. if (_serverUrl == nil) {
  519. cell.leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[UIColor whiteColor]] backgroundColor:[NCBrandColor sharedInstance].yellowFavorite padding:25]];
  520. cell.leftExpansion.buttonIndex = 0;
  521. cell.leftExpansion.fillOnTrigger = NO;
  522. //centerIconOverText
  523. MGSwipeButton *favoriteButton = (MGSwipeButton *)[cell.leftButtons objectAtIndex:0];
  524. [favoriteButton centerIconOverText];
  525. }
  526. // RIGHT
  527. cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] multiplier:2 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  528. cell.rightExpansion.buttonIndex = 0;
  529. cell.rightExpansion.fillOnTrigger = NO;
  530. //centerIconOverText
  531. MGSwipeButton *deleteButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:0];
  532. [deleteButton centerIconOverText];
  533. // ----------------------------------------------------------------------------------------------------------
  534. // more
  535. // ----------------------------------------------------------------------------------------------------------
  536. cell.more.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"more"] multiplier:2 color:[NCBrandColor sharedInstance].icon];
  537. if ([self canOpenMenuAction:metadata]) {
  538. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  539. [tapMore setNumberOfTapsRequired:1];
  540. cell.more.userInteractionEnabled = YES;
  541. [cell.more addGestureRecognizer:tapMore];
  542. }
  543. return cell;
  544. }
  545. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  546. {
  547. // deselect row
  548. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  549. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  550. // if is in download [do not touch]
  551. if (_metadata.status == k_metadataStatusWaitDownload || _metadata.status == k_metadataStatusInDownload || _metadata.status == k_metadataStatusDownloading)
  552. return;
  553. // File
  554. if (_metadata.directory == NO) {
  555. // File do not exists
  556. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  557. if (serverUrl) {
  558. if ([CCUtility fileProviderStorageExists:_metadata.fileID fileName:_metadata.fileNameView]) {
  559. [self downloadFileSuccessFailure:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  560. } else {
  561. _metadata.session = k_download_session;
  562. _metadata.sessionError = @"";
  563. _metadata.sessionSelector = selectorLoadFileView;
  564. _metadata.status = k_metadataStatusWaitDownload;
  565. // Add Metadata for Download
  566. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:_metadata];
  567. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume delegate:self];
  568. }
  569. }
  570. }
  571. // Directory
  572. if (_metadata.directory)
  573. [self performSegueDirectoryWithControlPasscode];
  574. }
  575. -(void)performSegueDirectoryWithControlPasscode
  576. {
  577. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  578. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  579. if (serverUrl) {
  580. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileName];
  581. vc.titleViewControl = _metadata.fileNameView;
  582. [self.navigationController pushViewController:vc animated:YES];
  583. }
  584. }
  585. #pragma --------------------------------------------------------------------------------------------
  586. #pragma mark ===== Navigation ====
  587. #pragma --------------------------------------------------------------------------------------------
  588. - (BOOL)shouldPerformSegue
  589. {
  590. // if i am in background -> exit
  591. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  592. // if i am not window -> exit
  593. if (self.view.window == NO)
  594. return NO;
  595. // Collapsed but i am in detail -> exit
  596. if (self.splitViewController.isCollapsed)
  597. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  598. return YES;
  599. }
  600. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  601. {
  602. id viewController = segue.destinationViewController;
  603. if ([viewController isKindOfClass:[UINavigationController class]]) {
  604. UINavigationController *nav = viewController;
  605. _detailViewController = (CCDetail *)nav.topViewController;
  606. } else {
  607. _detailViewController = segue.destinationViewController;
  608. }
  609. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  610. for (tableMetadata *metadata in _dataSource) {
  611. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  612. [allRecordsDataSourceImagesVideos addObject:metadata];
  613. }
  614. _detailViewController.metadataDetail = _metadata;
  615. _detailViewController.dateFilterQuery = nil;
  616. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  617. [_detailViewController setTitle:_metadata.fileNameView];
  618. }
  619. @end