CCFavorites.m 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. // Automatic Upload Folder
  31. NSString *autoUploadFileName;
  32. NSString *autoUploadDirectory;
  33. // Datasource
  34. CCSectionDataSourceMetadata *sectionDataSource;
  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. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  56. // Metadata
  57. self.metadata = [tableMetadata new];
  58. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  59. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  60. self.tableView.emptyDataSetDelegate = self;
  61. self.tableView.emptyDataSetSource = self;
  62. self.tableView.delegate = self;
  63. // calculate _serverUrl
  64. if (!_serverUrl)
  65. _serverUrl = nil;
  66. // Title
  67. if (_titleViewControl)
  68. self.title = _titleViewControl;
  69. else
  70. self.title = NSLocalizedString(@"_favorites_", nil);
  71. }
  72. // Apparirà
  73. - (void)viewWillAppear:(BOOL)animated
  74. {
  75. [super viewWillAppear:animated];
  76. // Color
  77. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  78. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  79. // Plus Button
  80. [appDelegate plusButtonVisibile:true];
  81. [self reloadDatasource:nil action:k_action_NULL];
  82. }
  83. // E' arrivato
  84. - (void)viewDidAppear:(BOOL)animated
  85. {
  86. [super viewDidAppear:animated];
  87. // Active Main
  88. appDelegate.activeFavorites = self;
  89. }
  90. - (void)changeTheming
  91. {
  92. if (self.isViewLoaded && self.view.window)
  93. [appDelegate changeTheming:self];
  94. // Reload Table View
  95. [self.tableView reloadData];
  96. }
  97. #pragma --------------------------------------------------------------------------------------------
  98. #pragma mark ==== DZNEmptyDataSetSource ====
  99. #pragma --------------------------------------------------------------------------------------------
  100. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. return [NCBrandColor sharedInstance].backgroundView;
  103. }
  104. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favoriteNoFiles"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite];
  107. }
  108. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  109. {
  110. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  111. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  112. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  113. }
  114. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  115. {
  116. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  117. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  118. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  119. paragraph.alignment = NSTextAlignmentCenter;
  120. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  121. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  122. }
  123. #pragma --------------------------------------------------------------------------------------------
  124. #pragma mark ===== Favorite =====
  125. #pragma--------------------------------------------------------------------------------------------
  126. - (void)addFavoriteFolder:(NSString *)serverUrl
  127. {
  128. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  129. if (!directoryID) return;
  130. NSString *selector;
  131. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  132. metadataNet.action = actionReadFolder;
  133. metadataNet.depth = @"1";
  134. metadataNet.directoryID = directoryID;
  135. if ([CCUtility getFavoriteOffline])
  136. selector = selectorReadFolderWithDownload;
  137. else
  138. selector = selectorReadFolder;
  139. metadataNet.selector = selector;
  140. metadataNet.serverUrl = serverUrl;
  141. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  142. }
  143. - (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
  144. {
  145. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  146. if(!serverUrl)
  147. return;
  148. NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:serverUrl activeUrl:appDelegate.activeUrl];
  149. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  150. [ocNetworking settingFavorite:fileNameServerUrl favorite:favorite completion:^(NSString *message, NSInteger errorCode) {
  151. if (errorCode == 0) {
  152. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:favorite];
  153. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:serverUrl fileID:metadata.fileID action:k_action_MOD];
  154. } else {
  155. if (errorCode == kOCErrorServerUnauthorized)
  156. [appDelegate openLoginView:self loginType:k_login_Modify_Password selector:k_intro_login];
  157. }
  158. }];
  159. }
  160. #pragma --------------------------------------------------------------------------------------------
  161. #pragma mark ===== listingFavorites =====
  162. #pragma--------------------------------------------------------------------------------------------
  163. - (void)listingFavorites
  164. {
  165. // test
  166. if (appDelegate.activeAccount.length == 0)
  167. return;
  168. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  169. [ocNetworking listingFavorites:@"" account:appDelegate.activeAccount success:^(NSArray *metadatas) {
  170. NSString *father = @"";
  171. NSMutableArray *filesEtag = [NSMutableArray new];
  172. for (tableMetadata *metadata in metadatas) {
  173. // insert for test NOT favorite
  174. [filesEtag addObject:metadata.fileID];
  175. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  176. if (!serverUrl)
  177. continue;
  178. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  179. if (![serverUrlSon containsString:father]) {
  180. if (metadata.directory) {
  181. // use : readFileForFolder less secure but more optimized old
  182. // use : readFolder more secure but less optimed V 2.22.0
  183. if ([CCUtility getFavoriteOffline])
  184. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload];
  185. //[[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  186. else
  187. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder];
  188. //[[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolder];
  189. } else {
  190. if ([CCUtility getFavoriteOffline])
  191. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  192. else
  193. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  194. }
  195. father = serverUrlSon;
  196. }
  197. }
  198. // Verify remove favorite
  199. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  200. for (tableMetadata *metadata in allRecordFavorite)
  201. if (![filesEtag containsObject:metadata.fileID])
  202. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  203. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  204. } failure:^(NSString *message, NSInteger errorCode) {
  205. NSLog(@"[LOG] Listing Favorites failure error %d, %@", (int)errorCode, message);
  206. }];
  207. }
  208. #pragma --------------------------------------------------------------------------------------------
  209. #pragma mark ==== Download Thumbnail ====
  210. #pragma --------------------------------------------------------------------------------------------
  211. - (void)downloadThumbnail:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl indexPath:(NSIndexPath *)indexPath
  212. {
  213. CGFloat width = [[NCUtility sharedInstance] getScreenWidthForPreview];
  214. CGFloat height = [[NCUtility sharedInstance] getScreenHeightForPreview];
  215. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  216. [ocNetworking downloadPreviewWithMetadata:metadata serverUrl:serverUrl withWidth:width andHeight:height completion:^(NSString *message, NSInteger errorCode) {
  217. if (errorCode == 0 && [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] && [[NCMainCommon sharedInstance] isValidIndexPath:indexPath tableView:self.tableView]) {
  218. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  219. }
  220. }];
  221. }
  222. #pragma --------------------------------------------------------------------------------------------
  223. #pragma mark ==== Download <Delegate> ====
  224. #pragma --------------------------------------------------------------------------------------------
  225. - (void)downloadStart:(NSString *)fileID account:(NSString *)account task:(NSURLSessionDownloadTask *)task serverUrl:(NSString *)serverUrl
  226. {
  227. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:serverUrl fileID:fileID action:k_action_MOD];
  228. [appDelegate updateApplicationIconBadgeNumber];
  229. }
  230. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  231. {
  232. if (errorCode == 0) {
  233. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  234. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_compress] || [metadata.typeFile isEqualToString: k_metadataTypeFile_unknown] || [selector isEqualToString:selectorOpenIn]) {
  235. [self openIn:metadata];
  236. } else {
  237. if ([self shouldPerformSegue])
  238. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  239. }
  240. } else {
  241. // File do not exists on server, remove in local
  242. if (errorCode == kOCErrorServerPathNotFound || errorCode == kCFURLErrorBadServerResponse) {
  243. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:fileID] error:nil];
  244. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID] clearDateReadDirectoryID:nil];
  245. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  246. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:fileID];
  247. }
  248. }
  249. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:serverUrl fileID:fileID action:k_action_MOD];
  250. }
  251. - (void)openIn:(tableMetadata *)metadata
  252. {
  253. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  254. UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  255. docController.delegate = self;
  256. NSIndexPath *indexPath = [sectionDataSource.fileIDIndexPath objectForKey:metadata.fileID];
  257. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  258. if (cell) {
  259. [docController presentOptionsMenuFromRect:cell.frame inView:self.tableView animated:YES];
  260. } else {
  261. [docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  262. }
  263. }
  264. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  265. {
  266. CGPoint location = [tapGesture locationInView:self.tableView];
  267. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  268. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  269. if (metadata)
  270. [appDelegate.activeMain openWindowShare:metadata];
  271. }
  272. #pragma --------------------------------------------------------------------------------------------
  273. #pragma mark ===== Progress & Task Button =====
  274. #pragma --------------------------------------------------------------------------------------------
  275. - (void)triggerProgressTask:(NSNotification *)notification
  276. {
  277. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceFileIDIndexPath:sectionDataSource.fileIDIndexPath tableView:self.tableView];
  278. }
  279. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  280. {
  281. UITouch *touch = [[event allTouches] anyObject];
  282. CGPoint location = [touch locationInView:self.tableView];
  283. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  284. if ([[NCMainCommon sharedInstance] isValidIndexPath:indexPath tableView:self.tableView]) {
  285. tableMetadata *metadataSection = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  286. if (metadataSection) {
  287. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadataSection.fileID]];
  288. if (metadata)
  289. [[NCMainCommon sharedInstance] cancelTransferMetadata:metadata reloadDatasource:true];
  290. }
  291. }
  292. }
  293. - (void)cancelAllTask:(id)sender
  294. {
  295. CGPoint location = [sender locationInView:self.tableView];
  296. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  297. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  298. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  299. [[NCMainCommon sharedInstance] cancelAllTransfer];
  300. }]];
  301. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  302. alertController.popoverPresentationController.sourceView = self.tableView;
  303. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  304. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  305. [alertController.view layoutIfNeeded];
  306. [self presentViewController:alertController animated:YES completion:nil];
  307. }
  308. #pragma mark -
  309. #pragma --------------------------------------------------------------------------------------------
  310. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  311. #pragma --------------------------------------------------------------------------------------------
  312. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  313. {
  314. return YES;
  315. }
  316. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  317. {
  318. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  319. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  320. return [self canOpenMenuAction:metadata];
  321. }
  322. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  323. {
  324. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  325. if (direction == MGSwipeDirectionRightToLeft) {
  326. [self actionDelete:indexPath];
  327. }
  328. if (direction == MGSwipeDirectionLeftToRight) {
  329. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  330. [self settingFavorite:metadata favorite:NO];
  331. }
  332. return YES;
  333. }
  334. - (void)actionDelete:(NSIndexPath *)indexPath
  335. {
  336. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  337. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  338. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  339. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  340. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  341. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, serverUrl]];
  342. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:serverUrl folderFileID:tableDirectory.fileID completion:^(NSInteger errorCode, NSString *message) {
  343. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:serverUrl fileID:metadata.fileID action:k_action_DEL];
  344. }];
  345. }]];
  346. if (localFile) {
  347. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  348. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  349. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  350. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:serverUrl fileID:metadata.fileID action:k_action_MOD];
  351. }]];
  352. }
  353. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  354. }]];
  355. alertController.popoverPresentationController.sourceView = self.tableView;
  356. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  357. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  358. [alertController.view layoutIfNeeded];
  359. [self presentViewController:alertController animated:YES completion:nil];
  360. }
  361. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  362. {
  363. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  364. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  365. UIImage *iconHeader;
  366. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  367. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  368. actionSheet.animationDuration = 0.2;
  369. actionSheet.buttonHeight = 50.0;
  370. actionSheet.cancelButtonHeight = 50.0f;
  371. actionSheet.separatorHeight = 5.0f;
  372. actionSheet.automaticallyTintButtonImages = @(NO);
  373. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  374. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  375. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  376. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  377. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  378. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  379. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  380. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  381. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  382. } else {
  383. if (metadata.directory)
  384. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  385. else
  386. iconHeader = [UIImage imageNamed:metadata.iconName];
  387. }
  388. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  389. ];
  390. // Favorite : ONLY root
  391. if (_serverUrl == nil) {
  392. [actionSheet addButtonWithTitle: NSLocalizedString(@"_remove_favorites_", nil)
  393. image: [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite]
  394. backgroundColor: [NCBrandColor sharedInstance].backgroundView
  395. height: 50.0
  396. type: AHKActionSheetButtonTypeDefault
  397. handler: ^(AHKActionSheet *as) {
  398. [self settingFavorite:metadata favorite:NO];
  399. }];
  400. }
  401. // Share
  402. [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) {
  403. [appDelegate.activeMain openWindowShare:metadata];
  404. }];
  405. // NO Directory
  406. if (metadata.directory == NO) {
  407. [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) {
  408. [self.tableView setEditing:NO animated:YES];
  409. if ([CCUtility fileProviderStorageExists:metadata.fileID fileNameView:metadata.fileNameView]) {
  410. [self openIn:metadata];
  411. } else {
  412. metadata.session = k_download_session;
  413. metadata.sessionError = @"";
  414. metadata.sessionSelector = selectorOpenIn;
  415. metadata.status = k_metadataStatusWaitDownload;
  416. // Add Metadata for Download
  417. tableMetadata *metadataForDownload = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  418. [[CCNetworking sharedNetworking] downloadFile:metadataForDownload taskStatus:k_taskStatusResume delegate:self];
  419. }
  420. }];
  421. }
  422. // Delete
  423. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil)
  424. image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] multiplier:2 color:[UIColor redColor]]
  425. backgroundColor:[NCBrandColor sharedInstance].backgroundView
  426. height:50.0
  427. type:AHKActionSheetButtonTypeDestructive
  428. handler:^(AHKActionSheet *as) {
  429. [self actionDelete:indexPath];
  430. }];
  431. [actionSheet show];
  432. }
  433. #pragma --------------------------------------------------------------------------------------------
  434. #pragma mark ==== Table ====
  435. #pragma --------------------------------------------------------------------------------------------
  436. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  437. {
  438. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  439. return metadata;
  440. }
  441. - (void)reloadDatasource:(NSString *)fileID action:(NSInteger)action
  442. {
  443. NSArray *recordsTableMetadata ;
  444. NSString *sorted = [CCUtility getOrderSettings];
  445. if ([sorted isEqualToString:@"fileName"])
  446. sorted = @"fileName";
  447. if (!_serverUrl) {
  448. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  449. } else {
  450. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  451. if (directoryID)
  452. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@", directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  453. }
  454. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil filterFileID:appDelegate.filterFileID filterTypeFileImage:NO filterTypeFileVideo:NO activeAccount:appDelegate.activeAccount];
  455. // get auto upload folder
  456. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  457. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  458. [self.tableView reloadData];
  459. }
  460. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  461. {
  462. return 60;
  463. }
  464. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  465. {
  466. return [[sectionDataSource.sectionArrayRow allKeys] count];
  467. }
  468. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  469. {
  470. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  471. }
  472. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  473. {
  474. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  475. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  476. return [CCCellMain new];
  477. }
  478. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  479. if (serverUrl == nil) {
  480. return [CCCellMain new];
  481. }
  482. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, serverUrl]];
  483. if (directory == nil) {
  484. return [CCCellMain new];
  485. }
  486. tableMetadata *metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", directory.fileID]];
  487. // Download thumbnail
  488. if (metadata.thumbnailExists && !metadataFolder.e2eEncrypted && ![CCUtility fileProviderStorageIconExists:metadata.fileID fileNameView:metadata.fileNameView]) {
  489. [self downloadThumbnail:metadata serverUrl:serverUrl indexPath:indexPath];
  490. }
  491. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory];
  492. // NORMAL - > MAIN
  493. if ([cell isKindOfClass:[CCCellMain class]]) {
  494. // More
  495. if ([self canOpenMenuAction:metadata]) {
  496. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  497. [tapMore setNumberOfTapsRequired:1];
  498. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  499. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  500. }
  501. // MGSwipeButton
  502. ((CCCellMain *)cell).delegate = self;
  503. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  504. if (_serverUrl == nil) {
  505. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[UIColor whiteColor]] backgroundColor:[NCBrandColor sharedInstance].yellowFavorite padding:25]];
  506. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  507. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  508. //centerIconOverText
  509. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  510. [favoriteButton centerIconOverText];
  511. }
  512. // RIGHT
  513. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] multiplier:2 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  514. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  515. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  516. //centerIconOverText
  517. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  518. [deleteButton centerIconOverText];
  519. }
  520. // TRANSFER
  521. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  522. // gesture Transfer
  523. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  524. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  525. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  526. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  527. }
  528. return cell;
  529. }
  530. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  531. {
  532. // deselect row
  533. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  534. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  535. // if is in download [do not touch]
  536. if (self.metadata.status == k_metadataStatusWaitDownload || self.metadata.status == k_metadataStatusInDownload || self.metadata.status == k_metadataStatusDownloading)
  537. return;
  538. // File
  539. if (self.metadata.directory == NO) {
  540. // File do not exists
  541. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:self.metadata.directoryID];
  542. if (serverUrl) {
  543. if ([CCUtility fileProviderStorageExists:self.metadata.fileID fileNameView:self.metadata.fileNameView]) {
  544. [self downloadFileSuccessFailure:self.metadata.fileName fileID:self.metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  545. } else {
  546. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, serverUrl]];
  547. if (tableDirectory.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  548. [appDelegate messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  549. } else {
  550. if (([self.metadata.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadata.typeFile isEqualToString: k_metadataTypeFile_audio] || [_metadata.typeFile isEqualToString: k_metadataTypeFile_image]) && self.metadata.e2eEncrypted == NO) {
  551. if ([self shouldPerformSegue])
  552. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  553. } else {
  554. self.metadata.session = k_download_session;
  555. self.metadata.sessionError = @"";
  556. self.metadata.sessionSelector = selectorLoadFileView;
  557. self.metadata.status = k_metadataStatusWaitDownload;
  558. // Add Metadata for Download
  559. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:self.metadata];
  560. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume delegate:self];
  561. [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
  562. }
  563. }
  564. }
  565. }
  566. }
  567. // Directory
  568. if (self.metadata.directory)
  569. [self performSegueDirectoryWithControlPasscode];
  570. }
  571. -(void)performSegueDirectoryWithControlPasscode
  572. {
  573. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  574. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:self.metadata.directoryID];
  575. if (serverUrl) {
  576. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:self.metadata.fileName];
  577. vc.titleViewControl = self.metadata.fileNameView;
  578. [self.navigationController pushViewController:vc animated:YES];
  579. }
  580. }
  581. #pragma --------------------------------------------------------------------------------------------
  582. #pragma mark ===== Navigation ====
  583. #pragma --------------------------------------------------------------------------------------------
  584. - (BOOL)shouldPerformSegue
  585. {
  586. // if i am in background -> exit
  587. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  588. // if i am not window -> exit
  589. if (self.view.window == NO)
  590. return NO;
  591. // Collapsed but i am in detail -> exit
  592. if (self.splitViewController.isCollapsed)
  593. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  594. return YES;
  595. }
  596. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  597. {
  598. id viewController = segue.destinationViewController;
  599. if ([viewController isKindOfClass:[UINavigationController class]]) {
  600. UINavigationController *nav = viewController;
  601. _detailViewController = (CCDetail *)nav.topViewController;
  602. } else {
  603. _detailViewController = segue.destinationViewController;
  604. }
  605. NSMutableArray *photoDataSource = [NSMutableArray new];
  606. for (NSString *fileID in sectionDataSource.allFileID) {
  607. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  608. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  609. [photoDataSource addObject:metadata];
  610. }
  611. _detailViewController.metadataDetail = self.metadata;
  612. _detailViewController.dateFilterQuery = nil;
  613. _detailViewController.photoDataSource = photoDataSource;
  614. [_detailViewController setTitle:self.metadata.fileNameView];
  615. }
  616. @end