CCFavorites.m 38 KB

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