CCFavorites.m 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. //
  2. // CCFavorites.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/01/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. UIDocumentInteractionController *docController;
  34. // Datasource
  35. CCSectionDataSourceMetadata *sectionDataSource;
  36. }
  37. @end
  38. @implementation CCFavorites
  39. #pragma --------------------------------------------------------------------------------------------
  40. #pragma mark ===== Init =====
  41. #pragma --------------------------------------------------------------------------------------------
  42. - (id)initWithCoder:(NSCoder *)aDecoder
  43. {
  44. if (self = [super initWithCoder:aDecoder]) {
  45. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  46. appDelegate.activeFavorites = self;
  47. }
  48. return self;
  49. }
  50. - (void)viewDidLoad
  51. {
  52. [super viewDidLoad];
  53. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMain" bundle:nil] forCellReuseIdentifier:@"CellMain"];
  54. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  55. // Metadata
  56. self.metadata = [tableMetadata new];
  57. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  58. self.tableView.emptyDataSetDelegate = self;
  59. self.tableView.emptyDataSetSource = self;
  60. self.tableView.delegate = self;
  61. // Register for 3D Touch Previewing if available
  62. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
  63. {
  64. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  65. }
  66. // calculate _serverUrl
  67. if (!_serverUrl)
  68. _serverUrl = nil;
  69. // Title
  70. if (_titleViewControl)
  71. self.title = _titleViewControl;
  72. else
  73. self.title = NSLocalizedString(@"_favorites_", nil);
  74. // Notification
  75. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  76. // changeTheming
  77. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  78. [self changeTheming];
  79. // Query data source
  80. [self queryDatasource];
  81. }
  82. - (void)viewDidAppear:(BOOL)animated
  83. {
  84. [super viewDidAppear:animated];
  85. // Active Main
  86. appDelegate.activeFavorites = self;
  87. [self reloadDatasource:nil action:k_action_NULL];
  88. }
  89. - (void)changeTheming
  90. {
  91. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:false];
  92. }
  93. #pragma --------------------------------------------------------------------------------------------
  94. #pragma mark ==== DZNEmptyDataSetSource ====
  95. #pragma --------------------------------------------------------------------------------------------
  96. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  97. {
  98. return NCBrandColor.sharedInstance.backgroundView;
  99. }
  100. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:300 height:300 color:NCBrandColor.sharedInstance.yellowFavorite];
  103. }
  104. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  107. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  108. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  109. }
  110. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  111. {
  112. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  113. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  114. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  115. paragraph.alignment = NSTextAlignmentCenter;
  116. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  117. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  118. }
  119. #pragma --------------------------------------------------------------------------------------------
  120. #pragma mark ===== Favorite =====
  121. #pragma--------------------------------------------------------------------------------------------
  122. - (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
  123. {
  124. NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:appDelegate.activeUrl];
  125. [[NCCommunication sharedInstance] setFavoriteWithUrlString:appDelegate.activeUrl fileName:fileNameServerUrl favorite:favorite account:appDelegate.activeAccount completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDecription) {
  126. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  127. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:favorite];
  128. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  129. } else if (errorCode != 0) {
  130. [appDelegate messageNotification:@"_error_" description:errorDecription visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  131. } else {
  132. NSLog(@"[LOG] It has been changed user during networking process, error.");
  133. }
  134. }];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== listingFavorites =====
  138. #pragma--------------------------------------------------------------------------------------------
  139. - (void)listingFavorites
  140. {
  141. // test
  142. if (appDelegate.activeAccount.length == 0)
  143. return;
  144. [[NCCommunication sharedInstance] listingFavoritesWithUrlString:appDelegate.activeUrl account:appDelegate.activeAccount completionHandler:^(NSString *account, NSArray *files, NSInteger errorCode, NSString *errorDescription) {
  145. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  146. NSString *father = @"";
  147. NSMutableArray *filesOcId = [NSMutableArray new];
  148. NSArray *metadatas = [[NCNetworking sharedInstance] convertFiles:files urlString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] serverUrl:nil user:appDelegate.activeUserID];
  149. for (tableMetadata *metadata in metadatas) {
  150. // insert for test NOT favorite
  151. [filesOcId addObject:metadata.ocId];
  152. NSString *serverUrl = metadata.serverUrl;
  153. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  154. if (![serverUrlSon containsString:father]) {
  155. if (metadata.directory) {
  156. if ([CCUtility getFavoriteOffline])
  157. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload account:account];
  158. else
  159. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder account:account];
  160. } else {
  161. if ([CCUtility getFavoriteOffline])
  162. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFileWithDownload account:account];
  163. else
  164. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFile account:account];
  165. }
  166. father = serverUrlSon;
  167. }
  168. }
  169. // Verify remove favorite
  170. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", account] sorted:nil ascending:NO];
  171. for (tableMetadata *metadata in allRecordFavorite) {
  172. if (![filesOcId containsObject:metadata.ocId])
  173. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:NO];
  174. }
  175. } else if (errorCode != 0) {
  176. [appDelegate messageNotification:@"_error_" description:errorDescription visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  177. } else {
  178. NSLog(@"[LOG] It has been changed user during networking process, error.");
  179. }
  180. }];
  181. [[OCNetworking sharedManager] listingFavoritesWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode) {
  182. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  183. NSString *father = @"";
  184. NSMutableArray *filesEtag = [NSMutableArray new];
  185. for (tableMetadata *metadata in metadatas) {
  186. // insert for test NOT favorite
  187. [filesEtag addObject:metadata.ocId];
  188. NSString *serverUrl = metadata.serverUrl;
  189. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  190. if (![serverUrlSon containsString:father]) {
  191. if (metadata.directory) {
  192. if ([CCUtility getFavoriteOffline])
  193. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload account:account];
  194. else
  195. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder account:account];
  196. } else {
  197. if ([CCUtility getFavoriteOffline])
  198. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFileWithDownload account:account];
  199. else
  200. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFile account:account];
  201. }
  202. father = serverUrlSon;
  203. }
  204. }
  205. // Verify remove favorite
  206. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", account] sorted:nil ascending:NO];
  207. for (tableMetadata *metadata in allRecordFavorite)
  208. if (![filesEtag containsObject:metadata.ocId])
  209. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:NO];
  210. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  211. } else if (errorCode != 0) {
  212. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  213. } else {
  214. NSLog(@"[LOG] It has been changed user during networking process, error.");
  215. }
  216. }];
  217. }
  218. - (void)tapActionComment:(UITapGestureRecognizer *)tapGesture
  219. {
  220. CGPoint location = [tapGesture locationInView:self.tableView];
  221. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  222. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  223. if (metadata) {
  224. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:1];
  225. }
  226. }
  227. - (void)tapActionShared:(UITapGestureRecognizer *)tapGesture
  228. {
  229. CGPoint location = [tapGesture locationInView:self.tableView];
  230. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  231. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  232. if (metadata) {
  233. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:2];
  234. }
  235. }
  236. #pragma --------------------------------------------------------------------------------------------
  237. #pragma mark ===== Progress & Task Button =====
  238. #pragma --------------------------------------------------------------------------------------------
  239. - (void)triggerProgressTask:(NSNotification *)notification
  240. {
  241. if (sectionDataSource.ocIdIndexPath != nil) {
  242. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceocIdIndexPath:sectionDataSource.ocIdIndexPath tableView:self.tableView viewController:self serverUrlViewController:self.serverUrl];
  243. }
  244. }
  245. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  246. {
  247. UITouch *touch = [[event allTouches] anyObject];
  248. CGPoint location = [touch locationInView:self.tableView];
  249. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  250. if ([[NCMainCommon sharedInstance] isValidIndexPath:indexPath view:self.tableView]) {
  251. tableMetadata *metadataSection = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  252. if (metadataSection) {
  253. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadataSection.ocId]];
  254. if (metadata)
  255. [[NCMainCommon sharedInstance] cancelTransferMetadata:metadata reloadDatasource:true uploadStatusForcedStart:false];
  256. }
  257. }
  258. }
  259. - (void)cancelAllTask:(id)sender
  260. {
  261. CGPoint location = [sender locationInView:self.tableView];
  262. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  263. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  264. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  265. [NCUtility.sharedInstance startActivityIndicatorWithView:self.view bottom:0];
  266. [[NCMainCommon sharedInstance] cancelAllTransfer];
  267. [NCUtility.sharedInstance stopActivityIndicator];
  268. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:nil ocId:nil action:k_action_NULL];
  269. }]];
  270. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  271. alertController.popoverPresentationController.sourceView = self.tableView;
  272. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  273. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  274. [alertController.view layoutIfNeeded];
  275. [self presentViewController:alertController animated:YES completion:nil];
  276. }
  277. #pragma mark -
  278. #pragma --------------------------------------------------------------------------------------------
  279. #pragma mark ===== Peek & Pop =====
  280. #pragma --------------------------------------------------------------------------------------------
  281. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  282. {
  283. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  284. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  285. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  286. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  287. if (cell) {
  288. previewingContext.sourceRect = cell.frame;
  289. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  290. viewController.metadata = metadata;
  291. viewController.imageFile = cell.file.image;
  292. viewController.showOpenIn = true;
  293. viewController.showShare = false;
  294. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_document]) {
  295. viewController.showOpenInternalViewer = true;
  296. }
  297. return viewController;
  298. }
  299. return nil;
  300. }
  301. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  302. {
  303. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  304. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  305. }
  306. #pragma mark -
  307. #pragma --------------------------------------------------------------------------------------------
  308. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  309. #pragma --------------------------------------------------------------------------------------------
  310. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  311. {
  312. return YES;
  313. }
  314. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  315. {
  316. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  317. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  318. return [self canOpenMenuAction:metadata];
  319. }
  320. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  321. {
  322. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  323. if (direction == MGSwipeDirectionRightToLeft) {
  324. [self actionDelete:indexPath];
  325. }
  326. if (direction == MGSwipeDirectionLeftToRight) {
  327. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  328. [self settingFavorite:metadata favorite:NO];
  329. }
  330. return YES;
  331. }
  332. - (void)actionDelete:(NSIndexPath *)indexPath
  333. {
  334. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  335. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  336. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  337. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  338. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  339. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:metadata.serverUrl folderocId:tableDirectory.ocId completion:^(NSInteger errorCode, NSString *message) {
  340. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_DEL];
  341. }];
  342. }]];
  343. if (localFile) {
  344. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  345. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  346. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] error:nil];
  347. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  348. }]];
  349. }
  350. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  351. }]];
  352. alertController.popoverPresentationController.sourceView = self.tableView;
  353. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  354. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  355. [alertController.view layoutIfNeeded];
  356. [self presentViewController:alertController animated:YES completion:nil];
  357. }
  358. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  359. {
  360. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  361. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  362. UIImage *iconHeader;
  363. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  364. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  365. actionSheet.animationDuration = 0.2;
  366. actionSheet.buttonHeight = 50.0;
  367. actionSheet.cancelButtonHeight = 50.0f;
  368. actionSheet.separatorHeight = 5.0f;
  369. actionSheet.automaticallyTintButtonImages = @(NO);
  370. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.encrypted };
  371. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.textView };
  372. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.textView };
  373. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.textView };
  374. actionSheet.separatorColor = NCBrandColor.sharedInstance.separator;
  375. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  376. actionSheet.cancelButtonBackgroudColor = NCBrandColor.sharedInstance.backgroundForm;
  377. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  378. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]]) {
  379. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  380. } else {
  381. if (metadata.directory)
  382. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.icon];
  383. else
  384. iconHeader = [UIImage imageNamed:metadata.iconName];
  385. }
  386. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: NCBrandColor.sharedInstance.backgroundForm height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  387. ];
  388. // Favorite : ONLY root
  389. if (_serverUrl == nil) {
  390. [actionSheet addButtonWithTitle: NSLocalizedString(@"_remove_favorites_", nil)
  391. image: [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:NCBrandColor.sharedInstance.yellowFavorite]
  392. backgroundColor: NCBrandColor.sharedInstance.backgroundForm
  393. height: 50.0
  394. type: AHKActionSheetButtonTypeDefault
  395. handler: ^(AHKActionSheet *as) {
  396. [self settingFavorite:metadata favorite:NO];
  397. }];
  398. }
  399. // Share
  400. [actionSheet addButtonWithTitle:NSLocalizedString(@"_details_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"details"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] backgroundColor:NCBrandColor.sharedInstance.backgroundForm height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  401. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:0];
  402. }];
  403. // NO Directory
  404. if (metadata.directory == NO && [NCBrandOptions sharedInstance].disable_openin_file == NO) {
  405. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"openFile"] multiplier:2 color:NCBrandColor.sharedInstance.icon] backgroundColor:NCBrandColor.sharedInstance.backgroundForm height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  406. [self.tableView setEditing:NO animated:YES];
  407. [[NCMainCommon sharedInstance] downloadOpenWithMetadata:metadata selector:selectorOpenIn];
  408. }];
  409. }
  410. // Delete
  411. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil)
  412. image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor redColor]]
  413. backgroundColor:NCBrandColor.sharedInstance.backgroundForm
  414. height:50.0
  415. type:AHKActionSheetButtonTypeDestructive
  416. handler:^(AHKActionSheet *as) {
  417. [self actionDelete:indexPath];
  418. }];
  419. [actionSheet show];
  420. }
  421. #pragma --------------------------------------------------------------------------------------------
  422. #pragma mark ==== Table ====
  423. #pragma --------------------------------------------------------------------------------------------
  424. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  425. {
  426. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  427. return metadata;
  428. }
  429. - (void)reloadDatasource:(NSString *)ocId action:(NSInteger)action
  430. {
  431. // test
  432. if (appDelegate.activeAccount.length == 0 || self.view.window == nil) {
  433. return;
  434. }
  435. [self queryDatasource];
  436. }
  437. - (void)queryDatasource
  438. {
  439. // test
  440. if (appDelegate.activeAccount.length == 0) {
  441. return;
  442. }
  443. NSArray *recordsTableMetadata;
  444. NSString *sorted = [CCUtility getOrderSettings];
  445. if ([sorted isEqualToString:@"fileName"]) sorted = @"fileName";
  446. // get auto upload folder
  447. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  448. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  449. if (!_serverUrl) {
  450. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  451. } else {
  452. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, _serverUrl] sorted:nil ascending:NO];
  453. }
  454. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil filterocId:appDelegate.filterocId filterTypeFileImage:NO filterTypeFileVideo:NO sorted:sorted ascending:[CCUtility getAscendingSettings] activeAccount:appDelegate.activeAccount];
  455. [self.tableView reloadData];
  456. }
  457. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  458. {
  459. return 60;
  460. }
  461. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  462. {
  463. return [[sectionDataSource.sectionArrayRow allKeys] count];
  464. }
  465. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  466. {
  467. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  468. }
  469. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  470. {
  471. tableShare *shareCell;
  472. tableMetadata *metadataFolder;
  473. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  474. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  475. return [CCCellMain new];
  476. }
  477. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  478. if (directory != nil) {
  479. metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", directory.ocId]];
  480. }
  481. for (tableShare *share in appDelegate.shares) {
  482. if ([share.serverUrl isEqualToString:metadata.serverUrl] && [share.fileName isEqualToString:metadata.fileName]) {
  483. shareCell = share;
  484. break;
  485. }
  486. }
  487. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory tableShare:shareCell];
  488. // NORMAL - > MAIN
  489. if ([cell isKindOfClass:[CCCellMain class]]) {
  490. // Comment tap
  491. if (metadata.commentsUnread) {
  492. UITapGestureRecognizer *tapComment = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionComment:)];
  493. [tapComment setNumberOfTapsRequired:1];
  494. ((CCCellMain *)cell).comment.userInteractionEnabled = YES;
  495. [((CCCellMain *)cell).comment addGestureRecognizer:tapComment];
  496. }
  497. // Share add Tap
  498. UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionShared:)];
  499. [tapShare setNumberOfTapsRequired:1];
  500. ((CCCellMain *)cell).viewShared.userInteractionEnabled = YES;
  501. [((CCCellMain *)cell).viewShared addGestureRecognizer:tapShare];
  502. // More
  503. if ([self canOpenMenuAction:metadata]) {
  504. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  505. [tapMore setNumberOfTapsRequired:1];
  506. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  507. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  508. }
  509. // MGSwipeButton
  510. ((CCCellMain *)cell).delegate = self;
  511. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  512. if (_serverUrl == nil) {
  513. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:NCBrandColor.sharedInstance.yellowFavorite padding:25]];
  514. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  515. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  516. //centerIconOverText
  517. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  518. [favoriteButton centerIconOverText];
  519. }
  520. // RIGHT
  521. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  522. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  523. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  524. //centerIconOverText
  525. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  526. [deleteButton centerIconOverText];
  527. }
  528. // TRANSFER
  529. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  530. // gesture Transfer
  531. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  532. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  533. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  534. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  535. }
  536. return cell;
  537. }
  538. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  539. {
  540. // deselect row
  541. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  542. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  543. // if is in download [do not touch]
  544. if (self.metadata.status == k_metadataStatusWaitDownload || self.metadata.status == k_metadataStatusInDownload || self.metadata.status == k_metadataStatusDownloading)
  545. return;
  546. // File
  547. if (self.metadata.directory == NO) {
  548. // File do not exists
  549. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  550. [[NCNetworkingMain sharedInstance] downloadFileSuccessFailure:self.metadata.fileName ocId:self.metadata.ocId serverUrl:self.metadata.serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  551. } else {
  552. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, self.metadata.serverUrl]];
  553. if (tableDirectory.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  554. [appDelegate messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  555. } else {
  556. 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) {
  557. [self shouldPerformSegue:self.metadata selector:@""];
  558. } else {
  559. self.metadata.session = k_download_session;
  560. self.metadata.sessionError = @"";
  561. self.metadata.sessionSelector = selectorLoadFileView;
  562. self.metadata.status = k_metadataStatusWaitDownload;
  563. // Add Metadata for Download
  564. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:self.metadata];
  565. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume];
  566. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.metadata.serverUrl ocId:self.metadata.ocId action:k_action_MOD];
  567. }
  568. }
  569. }
  570. }
  571. // Directory
  572. if (self.metadata.directory)
  573. [self performSegueDirectoryWithControlPasscode];
  574. }
  575. -(void)performSegueDirectoryWithControlPasscode
  576. {
  577. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  578. vc.serverUrl = [CCUtility stringAppendServerUrl:self.metadata.serverUrl addFileName:self.metadata.fileName];
  579. vc.titleViewControl = self.metadata.fileNameView;
  580. [self.navigationController pushViewController:vc animated:YES];
  581. }
  582. #pragma --------------------------------------------------------------------------------------------
  583. #pragma mark ===== Navigation ====
  584. #pragma --------------------------------------------------------------------------------------------
  585. - (void)shouldPerformSegue:(tableMetadata *)metadata selector:(NSString *)selector
  586. {
  587. // if i am in background -> exit
  588. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return;
  589. // if i am not window -> exit
  590. if (self.view.window == NO)
  591. return;
  592. // Collapsed but i am in detail -> exit
  593. if (self.splitViewController.isCollapsed)
  594. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return;
  595. // Metadata for push detail
  596. self.metadataForPushDetail = metadata;
  597. self.selectorForPushDetail = selector;
  598. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  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 *photoDataSource = [NSMutableArray new];
  610. for (NSString *ocId in sectionDataSource.allOcId) {
  611. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  612. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  613. [photoDataSource addObject:metadata];
  614. }
  615. _detailViewController.metadataDetail = self.metadataForPushDetail;
  616. _detailViewController.selectorDetail = self.selectorForPushDetail;
  617. _detailViewController.dateFilterQuery = nil;
  618. _detailViewController.photoDataSource = photoDataSource;
  619. [_detailViewController setTitle:self.metadata.fileNameView];
  620. }
  621. @end