CCFavorites.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. - (void)changeTheming
  82. {
  83. if (self.isViewLoaded && self.view.window)
  84. [app changeTheming:self];
  85. // Reload Table View
  86. [self.tableView reloadData];
  87. }
  88. - (void)triggerProgressTask:(NSNotification *)notification
  89. {
  90. NSDictionary *dict = notification.userInfo;
  91. float progress = [[dict valueForKey:@"progress"] floatValue];
  92. if (progress == 0)
  93. [self.navigationController cancelCCProgress];
  94. else
  95. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCBrandColor sharedInstance].navigationBarProgress];
  96. }
  97. #pragma --------------------------------------------------------------------------------------------
  98. #pragma mark ==== DZNEmptyDataSetSource ====
  99. #pragma --------------------------------------------------------------------------------------------
  100. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. return [UIColor whiteColor];
  103. }
  104. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. return [UIImage imageNamed:@"favoriteNoFiles"];
  107. }
  108. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  109. {
  110. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  111. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  112. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  113. }
  114. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  115. {
  116. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  117. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  118. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  119. paragraph.alignment = NSTextAlignmentCenter;
  120. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  121. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  122. }
  123. #pragma --------------------------------------------------------------------------------------------
  124. #pragma mark ===== UIDocumentInteractionController <delegate> =====
  125. #pragma --------------------------------------------------------------------------------------------
  126. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  127. {
  128. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  129. // evitiamo il rimando della eventuale photo e/o video
  130. if (tableAccount.autoUpload) {
  131. [[NCManageDatabase sharedInstance] setAccountAutoUploadDateAssetType:PHAssetMediaTypeImage assetDate:[NSDate date]];
  132. [[NCManageDatabase sharedInstance] setAccountAutoUploadDateAssetType:PHAssetMediaTypeVideo assetDate:[NSDate date]];
  133. }
  134. }
  135. #pragma --------------------------------------------------------------------------------------------
  136. #pragma mark ===== Delete <delegate> =====
  137. #pragma--------------------------------------------------------------------------------------------
  138. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  139. {
  140. NSLog(@"[LOG] Delete error %@", message);
  141. }
  142. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  143. {
  144. [self reloadDatasource];
  145. }
  146. #pragma --------------------------------------------------------------------------------------------
  147. #pragma mark ===== Favorite <delegate> =====
  148. #pragma--------------------------------------------------------------------------------------------
  149. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  150. {
  151. NSLog(@"[LOG] Remove Favorite error %@", message);
  152. }
  153. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  154. {
  155. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadataNet.fileID favorite:[metadataNet.options boolValue]];
  156. [self reloadDatasource];
  157. }
  158. - (void)readListingFavorites
  159. {
  160. // test
  161. if (app.activeAccount.length == 0)
  162. return;
  163. // verify is offline procedure is in progress selectorDownloadSynchronize
  164. if ([[app verifyExistsInQueuesDownloadSelector:selectorDownloadSynchronize] count] > 0)
  165. return;
  166. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  167. }
  168. - (void)addFavoriteFolder:(NSString *)serverUrl
  169. {
  170. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  171. NSString *selector;
  172. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  173. metadataNet.action = actionReadFolder;
  174. metadataNet.directoryID = directoryID;
  175. metadataNet.priority = NSOperationQueuePriorityNormal;
  176. if ([CCUtility getFavoriteOffline])
  177. selector = selectorReadFolderWithDownload;
  178. else
  179. selector = selectorReadFolder;
  180. metadataNet.selector = selector;
  181. metadataNet.serverUrl = serverUrl;
  182. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  183. }
  184. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  185. {
  186. // verify active user
  187. tableAccount *record = [[NCManageDatabase sharedInstance] getAccountActive];
  188. if (![record.account isEqualToString:metadataNet.account])
  189. return;
  190. NSString *father = @"";
  191. NSMutableArray *filesEtag = [NSMutableArray new];
  192. for (tableMetadata *metadata in metadatas) {
  193. // type of file
  194. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  195. // do not insert cryptated favorite file
  196. if (typeFilename == k_metadataTypeFilenameCrypto || typeFilename == k_metadataTypeFilenamePlist)
  197. continue;
  198. // insert for test NOT favorite
  199. [filesEtag addObject:metadata.fileID];
  200. // Get ServerUrl
  201. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  202. serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  203. if (![serverUrl containsString:father]) {
  204. if (metadata.directory) {
  205. NSString *selector;
  206. if ([CCUtility getFavoriteOffline])
  207. selector = selectorReadFolderWithDownload;
  208. else
  209. selector = selectorReadFolder;
  210. [[CCSynchronize sharedSynchronize] synchronizedFolder:serverUrl selector:selector];
  211. } else {
  212. if ([CCUtility getFavoriteOffline])
  213. [[CCSynchronize sharedSynchronize] synchronizedFile:metadata selector:selectorReadFileWithDownload];
  214. else
  215. [[CCSynchronize sharedSynchronize] synchronizedFile:metadata selector:selectorReadFile];
  216. }
  217. father = serverUrl;
  218. }
  219. }
  220. // Verify remove favorite
  221. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", app.activeAccount] sorted:nil ascending:NO];
  222. for (tableMetadata *metadata in allRecordFavorite)
  223. if (![filesEtag containsObject:metadata.fileID])
  224. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  225. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  226. }
  227. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  228. {
  229. NSLog(@"Read Favorites Failure");
  230. }
  231. #pragma --------------------------------------------------------------------------------------------
  232. #pragma mark ==== Download Thumbnail <Delegate> ====
  233. #pragma --------------------------------------------------------------------------------------------
  234. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  235. {
  236. [self reloadDatasource];
  237. }
  238. #pragma --------------------------------------------------------------------------------------------
  239. #pragma mark ==== Download <Delegate> ====
  240. #pragma --------------------------------------------------------------------------------------------
  241. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  242. {
  243. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  244. }
  245. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  246. {
  247. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  248. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  249. //[self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  250. [self openWith:_metadata];
  251. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  252. [self openWith:_metadata];
  253. } else {
  254. if ([self shouldPerformSegue])
  255. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  256. }
  257. }
  258. #pragma --------------------------------------------------------------------------------------------
  259. #pragma mark ===== menu =====
  260. #pragma--------------------------------------------------------------------------------------------
  261. - (void)openModel:(tableMetadata *)metadata
  262. {
  263. UIViewController *viewController;
  264. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  265. if ([metadata.model isEqualToString:@"cartadicredito"])
  266. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  267. if ([metadata.model isEqualToString:@"bancomat"])
  268. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  269. if ([metadata.model isEqualToString:@"contocorrente"])
  270. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  271. if ([metadata.model isEqualToString:@"accountweb"])
  272. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  273. if ([metadata.model isEqualToString:@"patenteguida"])
  274. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  275. if ([metadata.model isEqualToString:@"cartaidentita"])
  276. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  277. if ([metadata.model isEqualToString:@"passaporto"])
  278. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  279. if ([metadata.model isEqualToString:@"note"]) {
  280. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  281. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  282. [self presentViewController:navigationController animated:YES completion:nil];
  283. } else {
  284. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  285. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  286. [self presentViewController:navigationController animated:YES completion:nil];
  287. }
  288. }
  289. - (void)openWith:(tableMetadata *)metadata
  290. {
  291. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID];
  292. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  293. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  294. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  295. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  296. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  297. _docController.delegate = self;
  298. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  299. }
  300. }
  301. - (void)requestDeleteMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  302. {
  303. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  304. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  305. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  306. [self reloadDatasource];
  307. }]];
  308. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  309. }]];
  310. alertController.popoverPresentationController.sourceView = self.view;
  311. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  312. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  313. [alertController.view layoutIfNeeded];
  314. [self presentViewController:alertController animated:YES completion:nil];
  315. }
  316. /*
  317. #pragma --------------------------------------------------------------------------------------------
  318. #pragma mark ===== UnZipFile =====
  319. #pragma --------------------------------------------------------------------------------------------
  320. - (void)unZipFile:(NSString *)fileID
  321. {
  322. [_hudDeterminate visibleHudTitle:NSLocalizedString(@"_unzip_in_progress_", nil) mode:MBProgressHUDModeDeterminate color:nil];
  323. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  324. NSString *fileZip = [NSString stringWithFormat:@"%@/%@", app.directoryUser, fileID];
  325. [SSZipArchive unzipFileAtPath:fileZip toDestination:[CCUtility getDirectoryLocal] overwrite:YES password:nil progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
  326. dispatch_async(dispatch_get_main_queue(), ^{
  327. float progress = (float) entryNumber / (float)total;
  328. [_hudDeterminate progress:progress];
  329. });
  330. } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
  331. dispatch_async(dispatch_get_main_queue(), ^{
  332. [_hudDeterminate hideHud];
  333. if (succeeded) [app messageNotification:@"_info_" description:@"_file_unpacked_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode:0];
  334. else [app messageNotification:@"_error_" description:[NSString stringWithFormat:@"Error %ld", (long)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
  335. });
  336. }];
  337. });
  338. }
  339. */
  340. - (void)requestMoreMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  341. {
  342. UIImage *iconHeader;
  343. metadata = [_dataSource objectAtIndex:indexPath.row];
  344. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  345. actionSheet.animationDuration = 0.2;
  346. actionSheet.blurRadius = 0.0f;
  347. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  348. actionSheet.buttonHeight = 50.0;
  349. actionSheet.cancelButtonHeight = 50.0f;
  350. actionSheet.separatorHeight = 5.0f;
  351. actionSheet.automaticallyTintButtonImages = @(NO);
  352. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].cryptocloud };
  353. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  354. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].brand };
  355. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  356. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  357. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  358. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  359. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  360. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  361. } else {
  362. if (metadata.directory)
  363. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  364. else
  365. iconHeader = [UIImage imageNamed:metadata.iconName];
  366. }
  367. [actionSheet addButtonWithTitle: metadata.fileNamePrint image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  368. ];
  369. // ONLY Root Favorites : Remove file/folder Favorites
  370. if (_serverUrl == nil) {
  371. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_favorites_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetOffline"] color:[NCBrandColor sharedInstance].brand] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  372. [self.tableView setEditing:NO animated:YES];
  373. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  374. }];
  375. }
  376. // Share
  377. if (_metadata.cryptated == NO) {
  378. [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) {
  379. // close swipe
  380. [self setEditing:NO animated:YES];
  381. [app.activeMain openWindowShare:metadata];
  382. }];
  383. }
  384. // NO Directory - NO Template
  385. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  386. [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) {
  387. [self.tableView setEditing:NO animated:YES];
  388. [self openWith:metadata];
  389. }];
  390. }
  391. [actionSheet show];
  392. }
  393. #pragma mark -
  394. #pragma --------------------------------------------------------------------------------------------
  395. #pragma mark ===== Swipe Tablet -> menu =====
  396. #pragma --------------------------------------------------------------------------------------------
  397. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  398. {
  399. return UITableViewCellEditingStyleDelete;
  400. }
  401. - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
  402. return NSLocalizedString(@"_more_", nil);
  403. }
  404. - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
  405. {
  406. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  407. }
  408. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  409. {
  410. return NSLocalizedString(@"_delete_", nil);
  411. }
  412. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  413. {
  414. if (editingStyle == UITableViewCellEditingStyleDelete) {
  415. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  416. }
  417. }
  418. #pragma --------------------------------------------------------------------------------------------
  419. #pragma mark ==== Table ====
  420. #pragma --------------------------------------------------------------------------------------------
  421. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  422. {
  423. NSManagedObject *record = [_dataSource objectAtIndex:indexPath.row];
  424. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", [record valueForKey:@"fileID"]]];
  425. return metadata;
  426. }
  427. - (void)readFolderWithForced:(BOOL)forced serverUrl:(NSString *)serverUrl
  428. {
  429. [self reloadDatasource];
  430. }
  431. - (void)reloadDatasource
  432. {
  433. NSMutableArray *metadatas = [NSMutableArray new];
  434. NSArray *recordsTableMetadata ;
  435. if (!_serverUrl) {
  436. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", app.activeAccount] sorted:nil ascending:NO];
  437. } else {
  438. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  439. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", app.activeAccount, directoryID] sorted:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  440. }
  441. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  442. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  443. for (NSString *fileID in fileIDs)
  444. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  445. _dataSource = [NSArray arrayWithArray:metadatas];
  446. [self.tableView reloadData];
  447. }
  448. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  449. {
  450. return 60;
  451. }
  452. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  453. {
  454. return 1;
  455. }
  456. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  457. {
  458. return [_dataSource count];
  459. }
  460. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  461. {
  462. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  463. tableMetadata *metadata;
  464. // separator
  465. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  466. // Initialize
  467. cell.status.image = nil;
  468. cell.favorite.image = nil;
  469. cell.local.image = nil;
  470. // change color selection
  471. UIView *selectionColor = [[UIView alloc] init];
  472. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  473. cell.selectedBackgroundView = selectionColor;
  474. metadata = [_dataSource objectAtIndex:indexPath.row];
  475. cell.file.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  476. if (_serverUrl == nil)
  477. cell.favorite.image = [UIImage imageNamed:@"favorite"];
  478. if (cell.file.image == nil && metadata.thumbnailExists)
  479. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  480. // encrypted color
  481. if (metadata.cryptated) {
  482. cell.labelTitle.textColor = [NCBrandColor sharedInstance].cryptocloud;
  483. } else {
  484. cell.labelTitle.textColor = [UIColor blackColor];
  485. }
  486. // File name
  487. cell.labelTitle.text = metadata.fileNamePrint;
  488. cell.labelInfoFile.text = @"";
  489. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  490. if (cell.file.image == nil) {
  491. if (metadata.directory) {
  492. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  493. } else {
  494. cell.file.image = [UIImage imageNamed:metadata.iconName];
  495. }
  496. }
  497. // it's encrypted ???
  498. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  499. cell.status.image = [UIImage imageNamed:@"lock"];
  500. // text and length
  501. if (metadata.directory) {
  502. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  503. cell.accessoryType = UITableViewCellAccessoryNone;
  504. //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  505. } else {
  506. NSString *date = [CCUtility dateDiff:metadata.date];
  507. NSString *length = [CCUtility transformedSize:metadata.size];
  508. if ([metadata.type isEqualToString: k_metadataType_template])
  509. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  510. if ([metadata.type isEqualToString: k_metadataType_file] || [metadata.type isEqualToString: k_metadataType_local]) {
  511. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]];
  512. if (fileExists)
  513. cell.local.image = [UIImage imageNamed:@"local"];
  514. else
  515. cell.local.image = nil;
  516. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  517. //cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ • %@", date, length];
  518. //cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ ◦ %@", date, length];
  519. }
  520. cell.accessoryType = UITableViewCellAccessoryNone;
  521. }
  522. return cell;
  523. }
  524. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  525. {
  526. // deselect row
  527. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  528. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  529. // if is in download [do not touch]
  530. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  531. return;
  532. // File
  533. if (([_metadata.type isEqualToString: k_metadataType_file]) && _metadata.directory == NO) {
  534. // File do not exists
  535. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  536. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  537. [self downloadFileSuccess:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil];
  538. } else {
  539. [[CCNetworking sharedNetworking] downloadFile:_metadata.fileID serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  540. }
  541. }
  542. // Model
  543. if ([self.metadata.type isEqualToString: k_metadataType_template])
  544. [self openModel:self.metadata];
  545. // Directory
  546. if (_metadata.directory)
  547. [self performSegueDirectoryWithControlPasscode];
  548. }
  549. -(void)performSegueDirectoryWithControlPasscode
  550. {
  551. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  552. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  553. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  554. vc.titleViewControl = _metadata.fileNamePrint;
  555. [self.navigationController pushViewController:vc animated:YES];
  556. }
  557. #pragma --------------------------------------------------------------------------------------------
  558. #pragma mark ===== Navigation ====
  559. #pragma --------------------------------------------------------------------------------------------
  560. - (BOOL)shouldPerformSegue
  561. {
  562. // if i am in background -> exit
  563. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  564. // if i am not window -> exit
  565. if (self.view.window == NO)
  566. return NO;
  567. // Collapsed but i am in detail -> exit
  568. if (self.splitViewController.isCollapsed)
  569. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  570. // Video in run -> exit
  571. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  572. return YES;
  573. }
  574. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  575. {
  576. id viewController = segue.destinationViewController;
  577. if ([viewController isKindOfClass:[UINavigationController class]]) {
  578. UINavigationController *nav = viewController;
  579. _detailViewController = (CCDetail *)nav.topViewController;
  580. } else {
  581. _detailViewController = segue.destinationViewController;
  582. }
  583. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  584. for (tableMetadata *metadata in _dataSource) {
  585. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  586. [allRecordsDataSourceImagesVideos addObject:metadata];
  587. }
  588. _detailViewController.metadataDetail = _metadata;
  589. _detailViewController.dateFilterQuery = nil;
  590. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  591. [_detailViewController setTitle:_metadata.fileNamePrint];
  592. }
  593. @end