CCFavorites.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. //
  2. // CCFavorites.m
  3. // Crypto Cloud Technology Nextcloud
  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 () <CCActionsDeleteDelegate, CCActionsSettingFavoriteDelegate>
  28. {
  29. NSArray *_dataSource;
  30. BOOL _reloadDataSource;
  31. CCHud *_hudDeterminate;
  32. }
  33. @end
  34. @implementation CCFavorites
  35. #pragma --------------------------------------------------------------------------------------------
  36. #pragma mark ===== Init =====
  37. #pragma --------------------------------------------------------------------------------------------
  38. - (id)initWithCoder:(NSCoder *)aDecoder
  39. {
  40. if (self = [super initWithCoder:aDecoder]) {
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  43. app.activeFavorites = self;
  44. }
  45. return self;
  46. }
  47. - (void)viewDidLoad
  48. {
  49. [super viewDidLoad];
  50. // Custom Cell
  51. [self.tableView registerNib:[UINib nibWithNibName:@"CCFavoritesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  52. // dataSource
  53. _dataSource = [NSMutableArray new];
  54. // Metadata
  55. _metadata = [tableMetadata new];
  56. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  57. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  58. self.tableView.emptyDataSetDelegate = self;
  59. self.tableView.emptyDataSetSource = self;
  60. self.tableView.delegate = self;
  61. // calculate _serverUrl
  62. if (!_serverUrl)
  63. _serverUrl = nil;
  64. // Title
  65. if (_titleViewControl)
  66. self.title = _titleViewControl;
  67. else
  68. self.title = NSLocalizedString(@"_favorites_", nil);
  69. }
  70. // Apparirà
  71. - (void)viewWillAppear:(BOOL)animated
  72. {
  73. [super viewWillAppear:animated];
  74. // Color
  75. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  76. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  77. // Plus Button
  78. [app plusButtonVisibile:true];
  79. [self reloadDatasource];
  80. }
  81. // E' arrivato
  82. - (void)viewDidAppear:(BOOL)animated
  83. {
  84. [super viewDidAppear:animated];
  85. // Active Main
  86. app.activeFavorites = self;
  87. }
  88. - (void)changeTheming
  89. {
  90. if (self.isViewLoaded && self.view.window)
  91. [app changeTheming:self];
  92. // Reload Table View
  93. [self.tableView reloadData];
  94. }
  95. - (void)triggerProgressTask:(NSNotification *)notification
  96. {
  97. NSDictionary *dict = notification.userInfo;
  98. float progress = [[dict valueForKey:@"progress"] floatValue];
  99. if (progress == 0)
  100. [self.navigationController cancelCCProgress];
  101. else
  102. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCBrandColor sharedInstance].navigationBarProgress];
  103. }
  104. #pragma --------------------------------------------------------------------------------------------
  105. #pragma mark ==== DZNEmptyDataSetSource ====
  106. #pragma --------------------------------------------------------------------------------------------
  107. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  108. {
  109. return [UIColor whiteColor];
  110. }
  111. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  112. {
  113. return [UIImage imageNamed:@"favoriteNoFiles"];
  114. }
  115. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  116. {
  117. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  118. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  119. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  120. }
  121. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  122. {
  123. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  124. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  125. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  126. paragraph.alignment = NSTextAlignmentCenter;
  127. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  128. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  129. }
  130. #pragma --------------------------------------------------------------------------------------------
  131. #pragma mark ===== Delete <delegate> =====
  132. #pragma--------------------------------------------------------------------------------------------
  133. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  134. {
  135. NSLog(@"[LOG] DeleteFileOrFolder failure error %lu, %@", (long)errorCode, message);
  136. }
  137. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  138. {
  139. [self reloadDatasource];
  140. }
  141. #pragma --------------------------------------------------------------------------------------------
  142. #pragma mark ===== Favorite <delegate> =====
  143. #pragma--------------------------------------------------------------------------------------------
  144. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  145. {
  146. NSLog(@"[LOG] Setting Favorite failure error %lu, %@", (long)errorCode, message);
  147. }
  148. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  149. {
  150. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadataNet.fileID favorite:[metadataNet.options boolValue]];
  151. [self reloadDatasource];
  152. }
  153. - (void)readListingFavorites
  154. {
  155. // test
  156. if (app.activeAccount.length == 0)
  157. return;
  158. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  159. }
  160. - (void)addFavoriteFolder:(NSString *)serverUrl
  161. {
  162. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  163. if (!directoryID) return;
  164. NSString *selector;
  165. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  166. metadataNet.action = actionReadFolder;
  167. metadataNet.depth = @"1";
  168. metadataNet.directoryID = directoryID;
  169. if ([CCUtility getFavoriteOffline])
  170. selector = selectorReadFolderWithDownload;
  171. else
  172. selector = selectorReadFolder;
  173. metadataNet.selector = selector;
  174. metadataNet.serverUrl = serverUrl;
  175. [app addNetworkingOperationQueue:app.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  176. }
  177. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  178. {
  179. // verify active user
  180. tableAccount *record = [[NCManageDatabase sharedInstance] getAccountActive];
  181. if (![record.account isEqualToString:metadataNet.account])
  182. return;
  183. NSString *father = @"";
  184. NSMutableArray *filesEtag = [NSMutableArray new];
  185. for (tableMetadata *metadata in metadatas) {
  186. // type of file
  187. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  188. // do not insert cryptated favorite file
  189. if (typeFilename == k_metadataTypeFilenameCrypto || typeFilename == k_metadataTypeFilenamePlist)
  190. continue;
  191. // insert for test NOT favorite
  192. [filesEtag addObject:metadata.fileID];
  193. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  194. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  195. if (![serverUrlSon containsString:father]) {
  196. if (metadata.directory) {
  197. if ([CCUtility getFavoriteOffline])
  198. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileNameData serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  199. else
  200. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileNameData serverUrl:serverUrl selector:selectorReadFileFolder];
  201. } else {
  202. if ([CCUtility getFavoriteOffline])
  203. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  204. else
  205. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  206. }
  207. father = serverUrlSon;
  208. }
  209. }
  210. // Verify remove favorite
  211. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", app.activeAccount] sorted:nil ascending:NO];
  212. for (tableMetadata *metadata in allRecordFavorite)
  213. if (![filesEtag containsObject:metadata.fileID])
  214. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  215. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  216. }
  217. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  218. {
  219. NSLog(@"[LOG] Listing Favorites failure error %lu, %@", (long)errorCode, message);
  220. }
  221. #pragma --------------------------------------------------------------------------------------------
  222. #pragma mark ==== Download Thumbnail <Delegate> ====
  223. #pragma --------------------------------------------------------------------------------------------
  224. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  225. {
  226. [self reloadDatasource];
  227. }
  228. #pragma --------------------------------------------------------------------------------------------
  229. #pragma mark ==== Download <Delegate> ====
  230. #pragma --------------------------------------------------------------------------------------------
  231. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  232. {
  233. if (errorCode != k_CCErrorFileAlreadyInDownload)
  234. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  235. }
  236. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  237. {
  238. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  239. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  240. //[self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  241. [self openWith:_metadata];
  242. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  243. [self openWith:_metadata];
  244. } else {
  245. if ([self shouldPerformSegue])
  246. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  247. }
  248. }
  249. #pragma --------------------------------------------------------------------------------------------
  250. #pragma mark ===== menu =====
  251. #pragma--------------------------------------------------------------------------------------------
  252. - (void)openModel:(tableMetadata *)metadata
  253. {
  254. UIViewController *viewController;
  255. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  256. if (serverUrl) {
  257. if ([metadata.model isEqualToString:@"cartadicredito"])
  258. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  259. if ([metadata.model isEqualToString:@"bancomat"])
  260. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  261. if ([metadata.model isEqualToString:@"contocorrente"])
  262. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  263. if ([metadata.model isEqualToString:@"accountweb"])
  264. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  265. if ([metadata.model isEqualToString:@"patenteguida"])
  266. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  267. if ([metadata.model isEqualToString:@"cartaidentita"])
  268. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  269. if ([metadata.model isEqualToString:@"passaporto"])
  270. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  271. if ([metadata.model isEqualToString:@"note"]) {
  272. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  273. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  274. [self presentViewController:navigationController animated:YES completion:nil];
  275. } else {
  276. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  277. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  278. [self presentViewController:navigationController animated:YES completion:nil];
  279. }
  280. }
  281. }
  282. - (void)openWith:(tableMetadata *)metadata
  283. {
  284. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID];
  285. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  286. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  287. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  288. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  289. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  290. _docController.delegate = self;
  291. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  292. }
  293. }
  294. - (void)requestDeleteMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  295. {
  296. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  297. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  298. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  299. [self reloadDatasource];
  300. }]];
  301. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  302. }]];
  303. alertController.popoverPresentationController.sourceView = self.view;
  304. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  305. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  306. [alertController.view layoutIfNeeded];
  307. [self presentViewController:alertController animated:YES completion:nil];
  308. }
  309. - (void)requestMoreMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  310. {
  311. UIImage *iconHeader;
  312. metadata = [_dataSource objectAtIndex:indexPath.row];
  313. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  314. actionSheet.animationDuration = 0.2;
  315. actionSheet.buttonHeight = 50.0;
  316. actionSheet.cancelButtonHeight = 50.0f;
  317. actionSheet.separatorHeight = 5.0f;
  318. actionSheet.automaticallyTintButtonImages = @(NO);
  319. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].cryptocloud };
  320. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  321. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  322. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  323. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  324. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  325. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  326. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  327. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  328. } else {
  329. if (metadata.directory)
  330. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  331. else
  332. iconHeader = [UIImage imageNamed:metadata.iconName];
  333. }
  334. [actionSheet addButtonWithTitle: metadata.fileNamePrint image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  335. ];
  336. // Share
  337. if (_metadata.cryptated == NO) {
  338. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brand] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  339. [app.activeMain openWindowShare:metadata];
  340. }];
  341. }
  342. // NO Directory - NO Template
  343. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  344. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetOpenIn"] color:[NCBrandColor sharedInstance].brand] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  345. [self.tableView setEditing:NO animated:YES];
  346. [self openWith:metadata];
  347. }];
  348. }
  349. [actionSheet show];
  350. }
  351. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  352. {
  353. CGPoint location = [tapGesture locationInView:self.tableView];
  354. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  355. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  356. if (metadata)
  357. [app.activeMain openWindowShare:metadata];
  358. }
  359. #pragma mark -
  360. #pragma --------------------------------------------------------------------------------------------
  361. #pragma mark ===== Swipe Tablet -> menu =====
  362. #pragma --------------------------------------------------------------------------------------------
  363. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  364. {
  365. return YES;
  366. }
  367. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  368. {
  369. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  370. if (direction == MGSwipeDirectionRightToLeft) {
  371. // Delete
  372. if (index == 0)
  373. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  374. // More
  375. if (index == 1)
  376. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  377. }
  378. if (direction == MGSwipeDirectionLeftToRight) {
  379. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  380. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  381. }
  382. return YES;
  383. }
  384. #pragma --------------------------------------------------------------------------------------------
  385. #pragma mark ==== Table ====
  386. #pragma --------------------------------------------------------------------------------------------
  387. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  388. {
  389. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  390. return metadata;
  391. }
  392. - (void)readFolder:(NSString *)serverUrl
  393. {
  394. [self reloadDatasource];
  395. }
  396. - (void)reloadDatasource
  397. {
  398. NSMutableArray *metadatas = [NSMutableArray new];
  399. NSArray *recordsTableMetadata ;
  400. NSString *sorted = [CCUtility getOrderSettings];
  401. if ([sorted isEqualToString:@"fileName"])
  402. sorted = @"fileNamePrint";
  403. if (!_serverUrl) {
  404. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", app.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  405. } else {
  406. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  407. if (directoryID)
  408. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", app.activeAccount, directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  409. }
  410. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil activeAccount:app.activeAccount];
  411. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  412. for (NSString *fileID in fileIDs)
  413. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  414. _dataSource = [NSArray arrayWithArray:metadatas];
  415. [self.tableView reloadData];
  416. }
  417. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  418. {
  419. return 60;
  420. }
  421. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  422. {
  423. return 1;
  424. }
  425. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  426. {
  427. return [_dataSource count];
  428. }
  429. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  430. {
  431. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  432. tableMetadata *metadata;
  433. // variable base
  434. cell.delegate = self;
  435. cell.indexPath = indexPath;
  436. // separator
  437. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  438. // Initialize
  439. cell.status.image = nil;
  440. cell.favorite.image = nil;
  441. cell.local.image = nil;
  442. cell.shared.image = nil;
  443. // change color selection
  444. UIView *selectionColor = [[UIView alloc] init];
  445. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  446. cell.selectedBackgroundView = selectionColor;
  447. metadata = [_dataSource objectAtIndex:indexPath.row];
  448. // favorite
  449. if (_serverUrl == nil)
  450. cell.favorite.image = [UIImage imageNamed:@"favorite"];
  451. // encrypted color
  452. if (metadata.cryptated) {
  453. cell.labelTitle.textColor = [NCBrandColor sharedInstance].cryptocloud;
  454. } else {
  455. cell.labelTitle.textColor = [UIColor blackColor];
  456. }
  457. // filename
  458. cell.labelTitle.text = metadata.fileNamePrint;
  459. cell.labelInfoFile.text = @"";
  460. // Shared
  461. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  462. if (!serverUrl)
  463. return cell;
  464. NSString *shareLink = [app.sharesLink objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  465. NSString *shareUserAndGroup = [app.sharesUserAndGroup objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  466. // Immage
  467. if (metadata.directory) {
  468. if ([shareLink length] > 0) {
  469. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_public"] color:[NCBrandColor sharedInstance].brand];
  470. } else if ([shareUserAndGroup length] > 0) {
  471. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_shared_with_me"] color:[NCBrandColor sharedInstance].brand];
  472. } else {
  473. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  474. }
  475. } else {
  476. if ([shareLink length] > 0 || [shareUserAndGroup length] > 0) {
  477. if ([shareLink length] > 0)
  478. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"shareLink"] color:[NCBrandColor sharedInstance].brand];
  479. else
  480. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brand];
  481. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionConnectionMounted:)];
  482. [tap setNumberOfTapsRequired:1];
  483. cell.shared.userInteractionEnabled = YES;
  484. [cell.shared addGestureRecognizer:tap];
  485. }
  486. cell.file.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  487. if (cell.file.image == nil) {
  488. cell.file.image = [UIImage imageNamed:metadata.iconName];
  489. if (metadata.thumbnailExists)
  490. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  491. }
  492. }
  493. // it's encrypted ???
  494. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  495. cell.status.image = [UIImage imageNamed:@"lock"];
  496. // text and length
  497. if (metadata.directory) {
  498. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  499. //cell.accessoryType = UITableViewCellAccessoryNone;
  500. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  501. } else {
  502. NSString *date = [CCUtility dateDiff:metadata.date];
  503. NSString *length = [CCUtility transformedSize:metadata.size];
  504. if ([metadata.type isEqualToString: k_metadataType_template])
  505. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  506. if ([metadata.type isEqualToString: k_metadataType_file]) {
  507. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]];
  508. if (fileExists)
  509. cell.local.image = [UIImage imageNamed:@"local"];
  510. else
  511. cell.local.image = nil;
  512. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  513. }
  514. cell.accessoryType = UITableViewCellAccessoryNone;
  515. }
  516. // ======== MGSwipe ========
  517. //configure left buttons : ONLY Root Favorites : Remove file/folder Favorites
  518. if (_serverUrl == nil) {
  519. cell.leftButtons = @[[MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_unfavorite_", nil)] icon:[UIImage imageNamed:@"swipeUnfavorite"] backgroundColor:[UIColor colorWithRed:242.0/255.0 green:220.0/255.0 blue:132.0/255.0 alpha:1.000]]];
  520. cell.leftExpansion.buttonIndex = 0;
  521. cell.leftExpansion.fillOnTrigger = NO;
  522. //centerIconOverText
  523. MGSwipeButton *favoriteButton = (MGSwipeButton *)[cell.leftButtons objectAtIndex:0];
  524. [favoriteButton centerIconOverText];
  525. }
  526. //configure right buttons
  527. cell.rightButtons = @[[MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_delete_", nil)] icon:[UIImage imageNamed:@"swipeDelete"] backgroundColor:[UIColor redColor]], [MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_more_", nil)] icon:[UIImage imageNamed:@"swipeMore"] backgroundColor:[UIColor lightGrayColor]]];
  528. cell.rightSwipeSettings.transition = MGSwipeTransitionBorder;
  529. //centerIconOverText
  530. MGSwipeButton *deleteButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:0];
  531. MGSwipeButton *moreButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:1];
  532. [deleteButton centerIconOverText];
  533. [moreButton centerIconOverText];
  534. return cell;
  535. }
  536. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  537. {
  538. // deselect row
  539. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  540. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  541. // if is in download [do not touch]
  542. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  543. return;
  544. // File
  545. if (([_metadata.type isEqualToString: k_metadataType_file]) && _metadata.directory == NO) {
  546. // File do not exists
  547. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  548. if (serverUrl) {
  549. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  550. [self downloadFileSuccess:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil];
  551. } else {
  552. [[CCNetworking sharedNetworking] downloadFile:_metadata.fileID serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  553. }
  554. }
  555. }
  556. // Model
  557. if ([self.metadata.type isEqualToString: k_metadataType_template])
  558. [self openModel:self.metadata];
  559. // Directory
  560. if (_metadata.directory)
  561. [self performSegueDirectoryWithControlPasscode];
  562. }
  563. -(void)performSegueDirectoryWithControlPasscode
  564. {
  565. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  566. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  567. if (serverUrl) {
  568. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  569. vc.titleViewControl = _metadata.fileNamePrint;
  570. [self.navigationController pushViewController:vc animated:YES];
  571. }
  572. }
  573. #pragma --------------------------------------------------------------------------------------------
  574. #pragma mark ===== Navigation ====
  575. #pragma --------------------------------------------------------------------------------------------
  576. - (BOOL)shouldPerformSegue
  577. {
  578. // if i am in background -> exit
  579. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  580. // if i am not window -> exit
  581. if (self.view.window == NO)
  582. return NO;
  583. // Collapsed but i am in detail -> exit
  584. if (self.splitViewController.isCollapsed)
  585. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  586. // Video in run -> exit
  587. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  588. return YES;
  589. }
  590. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  591. {
  592. id viewController = segue.destinationViewController;
  593. if ([viewController isKindOfClass:[UINavigationController class]]) {
  594. UINavigationController *nav = viewController;
  595. _detailViewController = (CCDetail *)nav.topViewController;
  596. } else {
  597. _detailViewController = segue.destinationViewController;
  598. }
  599. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  600. for (tableMetadata *metadata in _dataSource) {
  601. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  602. [allRecordsDataSourceImagesVideos addObject:metadata];
  603. }
  604. _detailViewController.metadataDetail = _metadata;
  605. _detailViewController.dateFilterQuery = nil;
  606. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  607. [_detailViewController setTitle:_metadata.fileNamePrint];
  608. }
  609. @end