CCFavorites.m 36 KB

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