CCOfflinePageContent.m 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. //
  2. // CCOfflinePageContent.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 "CCOfflinePageContent.h"
  24. #import "AppDelegate.h"
  25. #ifdef CUSTOM_BUILD
  26. #import "CustomSwift.h"
  27. #else
  28. #import "Nextcloud-Swift.h"
  29. #endif
  30. @interface CCOfflinePageContent () <CCActionsDeleteDelegate, CCActionsSettingFavoriteDelegate>
  31. {
  32. NSArray *dataSource;
  33. BOOL _reloadDataSource;
  34. }
  35. @end
  36. @implementation CCOfflinePageContent
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. // Custom Cell
  41. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellOffline" bundle:nil] forCellReuseIdentifier:@"OfflineCell"];
  42. // dataSource
  43. dataSource = [NSMutableArray new];
  44. // Metadata
  45. _metadata = [CCMetadata new];
  46. self.tableView.tableFooterView = [UIView new];
  47. self.tableView.separatorColor = COLOR_SEPARATOR_TABLE;
  48. self.tableView.emptyDataSetDelegate = self;
  49. self.tableView.emptyDataSetSource = self;
  50. self.tableView.allowsMultipleSelectionDuringEditing = NO;
  51. // calculate _serverUrl
  52. if ([self.pageType isEqualToString:k_pageOfflineFavorites] && !_serverUrl)
  53. _serverUrl = nil;
  54. if ([self.pageType isEqualToString:k_pageOfflineOffline] && !_serverUrl)
  55. _serverUrl = nil;
  56. if ([self.pageType isEqualToString:k_pageOfflineLocal] && !_serverUrl)
  57. _serverUrl = [CCUtility getDirectoryLocal];
  58. // Title & color
  59. self.title = _titleViewControl;
  60. }
  61. // Apparirà
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. [super viewWillAppear:animated];
  65. // Color
  66. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  67. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  68. // Plus Button
  69. [app plusButtonVisibile:true];
  70. [self reloadDatasource];
  71. }
  72. // E' arrivato
  73. - (void)viewDidAppear:(BOOL)animated
  74. {
  75. [super viewDidAppear:animated];
  76. // cancell Progress
  77. [self.navigationController cancelCCProgress];
  78. // update Badge
  79. [app updateApplicationIconBadgeNumber];
  80. }
  81. - (void)didReceiveMemoryWarning {
  82. [super didReceiveMemoryWarning];
  83. }
  84. #pragma --------------------------------------------------------------------------------------------
  85. #pragma mark ==== DZNEmptyDataSetSource ====
  86. #pragma --------------------------------------------------------------------------------------------
  87. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  88. {
  89. // only for root
  90. if (!_serverUrl || [_serverUrl isEqualToString:[CCUtility getDirectoryLocal]])
  91. return YES;
  92. else
  93. return NO;
  94. }
  95. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  96. {
  97. return 0.0f;
  98. }
  99. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  100. {
  101. return - self.navigationController.navigationBar.frame.size.height;
  102. }
  103. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  104. {
  105. return [UIColor whiteColor];
  106. }
  107. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  108. {
  109. return [UIImage imageNamed:image_brandBackgroundLite];
  110. }
  111. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  112. {
  113. NSString *text;
  114. if ([self.pageType isEqualToString:k_pageOfflineFavorites] || [self.pageType isEqualToString:k_pageOfflineOffline])
  115. text = [NSString stringWithFormat:@"%@", @""];
  116. if ([self.pageType isEqualToString:k_pageOfflineLocal])
  117. text = [NSString stringWithFormat:@"%@", @""];
  118. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:COLOR_BRAND};
  119. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  120. }
  121. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  122. {
  123. NSString *text;
  124. if ([self.pageType isEqualToString:k_pageOfflineFavorites])
  125. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  126. if ([self.pageType isEqualToString:k_pageOfflineOffline])
  127. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_offline_view_", nil)];
  128. if ([self.pageType isEqualToString:k_pageOfflineLocal])
  129. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_local_view_", nil)];
  130. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  131. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  132. paragraph.alignment = NSTextAlignmentCenter;
  133. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  134. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== UIDocumentInteractionController <delegate> =====
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  140. {
  141. // evitiamo il rimando della eventuale photo e/o video
  142. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  143. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  144. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  145. }
  146. }
  147. #pragma --------------------------------------------------------------------------------------------
  148. #pragma mark ===== Delete <delegate> =====
  149. #pragma--------------------------------------------------------------------------------------------
  150. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  151. {
  152. NSLog(@"[LOG] Delete error %@", message);
  153. }
  154. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  155. {
  156. [self reloadDatasource];
  157. }
  158. #pragma --------------------------------------------------------------------------------------------
  159. #pragma mark ===== Favorite <delegate> =====
  160. #pragma--------------------------------------------------------------------------------------------
  161. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  162. {
  163. NSLog(@"[LOG] Remove Favorite error %@", message);
  164. }
  165. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  166. {
  167. [CCCoreData setMetadataFavoriteFileID:metadataNet.fileID favorite:[metadataNet.options boolValue] activeAccount:app.activeAccount context:nil];
  168. [self reloadDatasource];
  169. }
  170. #pragma --------------------------------------------------------------------------------------------
  171. #pragma mark ==== Download Thumbnail <Delegate> ====
  172. #pragma --------------------------------------------------------------------------------------------
  173. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  174. {
  175. // i am in Favorites
  176. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline])
  177. [self reloadDatasource];
  178. }
  179. #pragma --------------------------------------------------------------------------------------------
  180. #pragma mark ==== Download <Delegate> ====
  181. #pragma --------------------------------------------------------------------------------------------
  182. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  183. {
  184. CCMetadata *metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, app.activeAccount] context:nil];
  185. // Activity
  186. [CCCoreData addActivityFile:metadata.fileNamePrint action:k_activityDebugActionDownload note:[NSString stringWithFormat:@"Selector : %@ - Error : %@", selector, message] type:k_activityTypeFailure verbose:k_activityVerboseDebug account:metadata.account];
  187. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  188. [app updateApplicationIconBadgeNumber];
  189. }
  190. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  191. {
  192. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, app.activeAccount] context:nil];
  193. // Activity
  194. [CCCoreData addActivityFile:_metadata.fileNamePrint action:k_activityDebugActionDownload note:[NSString stringWithFormat:@"Selector : %@", selector] type:k_activityTypeSuccess verbose:k_activityVerboseDebug account:_metadata.account];
  195. // File exists
  196. if ([self shouldPerformSegue])
  197. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  198. [app updateApplicationIconBadgeNumber];
  199. }
  200. #pragma --------------------------------------------------------------------------------------------
  201. #pragma mark ===== menu =====
  202. #pragma--------------------------------------------------------------------------------------------
  203. - (void)openModel:(CCMetadata *)metadata
  204. {
  205. UIViewController *viewController;
  206. BOOL isLocal = NO;
  207. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  208. if ([self.pageType isEqualToString:k_pageOfflineLocal])
  209. isLocal = YES;
  210. if ([metadata.model isEqualToString:@"cartadicredito"])
  211. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  212. if ([metadata.model isEqualToString:@"bancomat"])
  213. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  214. if ([metadata.model isEqualToString:@"contocorrente"])
  215. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  216. if ([metadata.model isEqualToString:@"accountweb"])
  217. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  218. if ([metadata.model isEqualToString:@"patenteguida"])
  219. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  220. if ([metadata.model isEqualToString:@"cartaidentita"])
  221. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  222. if ([metadata.model isEqualToString:@"passaporto"])
  223. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  224. if ([metadata.model isEqualToString:@"note"]) {
  225. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:isLocal serverUrl:serverUrl];
  226. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  227. [self presentViewController:navigationController animated:YES completion:nil];
  228. } else {
  229. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  230. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  231. [self presentViewController:navigationController animated:YES completion:nil];
  232. }
  233. }
  234. - (void)openWith:(CCMetadata *)metadata
  235. {
  236. NSString *fileNamePath;
  237. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline])
  238. fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID];
  239. if ([_pageType isEqualToString:k_pageOfflineLocal])
  240. fileNamePath = [NSString stringWithFormat:@"%@/%@", _serverUrl, metadata.fileNameData];
  241. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  242. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  243. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  244. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  245. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  246. _docController.delegate = self;
  247. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  248. }
  249. }
  250. - (void)requestDeleteMetadata:(CCMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  251. {
  252. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  253. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  254. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) {
  255. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  256. }
  257. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  258. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", _serverUrl, metadata.fileNameData];
  259. NSString *iconPath = [NSString stringWithFormat:@"%@/.%@.ico", _serverUrl, metadata.fileNameData];
  260. [[NSFileManager defaultManager] removeItemAtPath:fileNamePath error:nil];
  261. [[NSFileManager defaultManager] removeItemAtPath:iconPath error:nil];
  262. }
  263. [self reloadDatasource];
  264. }]];
  265. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  266. }]];
  267. alertController.popoverPresentationController.sourceView = self.view;
  268. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  269. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  270. [alertController.view layoutIfNeeded];
  271. [self presentViewController:alertController animated:YES completion:nil];
  272. }
  273. -(void)cellButtonDownWasTapped:(id)sender
  274. {
  275. CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView];
  276. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
  277. CCMetadata *metadata = [CCMetadata new];
  278. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  279. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  280. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  281. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  282. } else {
  283. metadata = [dataSource objectAtIndex:indexPath.row];
  284. }
  285. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  286. actionSheet.animationDuration = 0.2;
  287. actionSheet.blurRadius = 0.0f;
  288. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  289. actionSheet.buttonHeight = 50.0;
  290. actionSheet.cancelButtonHeight = 50.0f;
  291. actionSheet.separatorHeight = 5.0f;
  292. actionSheet.automaticallyTintButtonImages = @(NO);
  293. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:COLOR_CRYPTOCLOUD };
  294. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:COLOR_TEXT_ANTHRACITE };
  295. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_BRAND };
  296. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:COLOR_TEXT_ANTHRACITE };
  297. actionSheet.separatorColor = COLOR_SEPARATOR_TABLE;
  298. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  299. // NO Directory - NO Template
  300. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  301. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[UIImage imageNamed:image_actionSheetOpenIn] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  302. [self.tableView setEditing:NO animated:YES];
  303. [self openWith:metadata];
  304. }];
  305. }
  306. // ONLY Root Favorites : Remove file/folder Favorites
  307. if (_serverUrl == nil && [_pageType isEqualToString:k_pageOfflineFavorites]) {
  308. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_favorites_", nil) image:[UIImage imageNamed:image_actionSheetOffline] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  309. [self.tableView setEditing:NO animated:YES];
  310. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  311. }];
  312. }
  313. // ONLY Root Offline : Remove file/folder offline
  314. if (_serverUrl == nil && [_pageType isEqualToString:k_pageOfflineOffline]) {
  315. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_offline_", nil) image:[UIImage imageNamed:image_actionSheetOffline] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  316. if (metadata.directory) {
  317. // remove tag offline for all folder/subfolder/file
  318. NSString *relativeRoot = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  319. NSString *dirServerUrl = [CCUtility stringAppendServerUrl:relativeRoot addFileName:metadata.fileNameData];
  320. NSArray *directories = [CCCoreData getOfflineDirectoryActiveAccount:app.activeAccount];
  321. for (TableDirectory *directory in directories)
  322. if ([directory.serverUrl containsString:dirServerUrl]) {
  323. [CCCoreData setOfflineDirectoryServerUrl:directory.serverUrl offline:NO activeAccount:app.activeAccount];
  324. [CCCoreData removeOfflineAllFileFromServerUrl:directory.serverUrl activeAccount:app.activeAccount];
  325. }
  326. } else {
  327. [CCCoreData setOfflineLocalFileID:metadata.fileID offline:NO activeAccount:app.activeAccount];
  328. }
  329. [self.tableView setEditing:NO animated:YES];
  330. [self reloadDatasource];
  331. }];
  332. }
  333. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil) image:[UIImage imageNamed:image_delete] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDestructive handler:^(AHKActionSheet *as) {
  334. [self requestDeleteMetadata:metadata indexPath:indexPath];
  335. }];
  336. [actionSheet show];
  337. }
  338. #pragma --------------------------------------------------------------------------------------------
  339. #pragma mark ==== Table ====
  340. #pragma --------------------------------------------------------------------------------------------
  341. - (CCMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  342. {
  343. CCMetadata *metadata;
  344. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) {
  345. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  346. metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", [record valueForKey:@"fileID"], app.activeAccount] context:nil];
  347. }
  348. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  349. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  350. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  351. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  352. }
  353. return metadata;
  354. }
  355. - (void)readFolderWithForced:(BOOL)forced serverUrl:(NSString *)serverUrl
  356. {
  357. [self reloadDatasource];
  358. }
  359. - (void)reloadDatasource
  360. {
  361. if ([_pageType isEqualToString:k_pageOfflineFavorites]) {
  362. NSMutableArray *metadatas = [NSMutableArray new];
  363. NSArray *recordsTableMetadata ;
  364. if (!_serverUrl) {
  365. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] context:nil];
  366. } else {
  367. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  368. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  369. }
  370. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  371. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  372. for (NSString *fileID in fileIDs)
  373. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  374. dataSource = [NSArray arrayWithArray:metadatas];
  375. }
  376. if ([_pageType isEqualToString:k_pageOfflineOffline]) {
  377. NSMutableArray *metadatas = [NSMutableArray new];
  378. NSArray *recordsTableMetadata ;
  379. if (!_serverUrl) {
  380. recordsTableMetadata = [CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  381. } else {
  382. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  383. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  384. }
  385. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  386. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  387. for (NSString *fileID in fileIDs)
  388. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  389. dataSource = [NSArray arrayWithArray:metadatas];
  390. }
  391. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  392. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_serverUrl error:nil];
  393. NSMutableArray *metadatas = [NSMutableArray new];
  394. for (NSString *subpath in subpaths)
  395. if (![[subpath lastPathComponent] hasPrefix:@"."])
  396. [metadatas addObject:subpath];
  397. dataSource = [NSArray arrayWithArray:metadatas];
  398. }
  399. [self.tableView reloadData];
  400. }
  401. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  402. {
  403. return 60;
  404. }
  405. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  406. {
  407. return 1;
  408. }
  409. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  410. {
  411. return [dataSource count];
  412. }
  413. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  414. {
  415. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  416. CCMetadata *metadata;
  417. // Initialize
  418. cell.statusImageView.image = nil;
  419. cell.offlineImageView.image = nil;
  420. // change color selection
  421. UIView *selectionColor = [[UIView alloc] init];
  422. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  423. cell.selectedBackgroundView = selectionColor;
  424. // i am in Favorites OR i am in Offline
  425. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) {
  426. metadata = [dataSource objectAtIndex:indexPath.row];
  427. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  428. if (_serverUrl == nil) {
  429. if ([_pageType isEqualToString:k_pageOfflineFavorites])
  430. cell.offlineImageView.image = [UIImage imageNamed:image_favorite];
  431. if ([_pageType isEqualToString:k_pageOfflineOffline])
  432. cell.offlineImageView.image = [UIImage imageNamed:image_offline];
  433. }
  434. if (cell.fileImageView.image == nil && metadata.thumbnailExists)
  435. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  436. }
  437. // i am in local
  438. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  439. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  440. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  441. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  442. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _serverUrl, metadata.fileNamePrint]];
  443. if (!cell.fileImageView.image) {
  444. UIImage *icon = [CCGraphics createNewImageFrom:metadata.fileID directoryUser:_serverUrl fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:NO optimizedFileName:[CCUtility getOptimizedPhoto]];
  445. if (icon) {
  446. [CCGraphics saveIcoWithFileID:metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _serverUrl, metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  447. cell.fileImageView.image = icon;
  448. }
  449. }
  450. }
  451. // ButtonDown Tapped
  452. [cell.buttonDown addTarget:self action:@selector(cellButtonDownWasTapped:) forControlEvents:UIControlEventTouchUpInside];
  453. // color and font
  454. if (metadata.cryptated) {
  455. cell.labelTitle.textColor = COLOR_CRYPTOCLOUD;
  456. cell.labelInfoFile.textColor = [UIColor blackColor];
  457. } else {
  458. cell.labelTitle.textColor = COLOR_TEXT_ANTHRACITE;
  459. cell.labelInfoFile.textColor = [UIColor blackColor];
  460. }
  461. if (metadata.directory) {
  462. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  463. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  464. }
  465. // File name
  466. cell.labelTitle.text = metadata.fileNamePrint;
  467. cell.labelInfoFile.text = @"";
  468. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  469. if (cell.fileImageView.image == nil)
  470. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  471. // it's encrypted ???
  472. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  473. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  474. // it's in download mode
  475. if ([metadata.session length] > 0 && [metadata.session containsString:@"download"])
  476. cell.statusImageView.image = [UIImage imageNamed:image_attention];
  477. // text and length
  478. if (metadata.directory) {
  479. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  480. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  481. } else {
  482. NSString *date = [CCUtility dateDiff:metadata.date];
  483. NSString *length = [CCUtility transformedSize:metadata.size];
  484. if ([metadata.type isEqualToString: k_metadataType_template])
  485. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  486. if ([metadata.type isEqualToString: k_metadataType_file] || [metadata.type isEqualToString: k_metadataType_local])
  487. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", date, length];
  488. cell.accessoryType = UITableViewCellAccessoryNone;
  489. }
  490. return cell;
  491. }
  492. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  493. {
  494. // deselect row
  495. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  496. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  497. // if is in download [do not touch]
  498. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  499. return;
  500. // File
  501. if (([_metadata.type isEqualToString: k_metadataType_file] || [_metadata.type isEqualToString: k_metadataType_local]) && _metadata.directory == NO) {
  502. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  503. // File exists
  504. if ([self shouldPerformSegue])
  505. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  506. } else {
  507. // File do not exists
  508. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:_metadata.account];
  509. [[CCNetworking sharedNetworking] downloadFile:_metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  510. }
  511. }
  512. // Model
  513. if ([self.metadata.type isEqualToString: k_metadataType_template])
  514. [self openModel:self.metadata];
  515. // Directory
  516. if (_metadata.directory)
  517. [self performSegueDirectoryWithControlPasscode];
  518. }
  519. -(void)performSegueDirectoryWithControlPasscode
  520. {
  521. CCOfflinePageContent *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflinePageContent"];
  522. NSString *serverUrl;
  523. if (([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) && !_serverUrl) {
  524. serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  525. } else {
  526. serverUrl = _serverUrl;
  527. }
  528. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  529. vc.pageType = _pageType;
  530. vc.titleViewControl = _metadata.fileNamePrint;
  531. [self.navigationController pushViewController:vc animated:YES];
  532. }
  533. #pragma --------------------------------------------------------------------------------------------
  534. #pragma mark ===== Navigation ====
  535. #pragma --------------------------------------------------------------------------------------------
  536. - (BOOL)shouldPerformSegue
  537. {
  538. // if i am in background -> exit
  539. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  540. // if i am not window -> exit
  541. if (self.view.window == NO)
  542. return NO;
  543. // Collapsed but i am in detail -> exit
  544. if (self.splitViewController.isCollapsed)
  545. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  546. // Video in run -> exit
  547. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  548. return YES;
  549. }
  550. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  551. {
  552. id viewController = segue.destinationViewController;
  553. if ([viewController isKindOfClass:[UINavigationController class]]) {
  554. UINavigationController *nav = viewController;
  555. _detailViewController = (CCDetail *)nav.topViewController;
  556. } else {
  557. _detailViewController = segue.destinationViewController;
  558. }
  559. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  560. if ([self.pageType isEqualToString:k_pageOfflineFavorites] || [self.pageType isEqualToString:k_pageOfflineOffline]) {
  561. for (CCMetadata *metadata in dataSource) {
  562. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  563. [allRecordsDataSourceImagesVideos addObject:metadata];
  564. }
  565. }
  566. if ([self.pageType isEqualToString:k_pageOfflineLocal]) {
  567. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  568. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  569. for (NSString *fileName in dataSource) {
  570. CCMetadata *metadata = [CCMetadata new];
  571. metadata = [CCUtility insertFileSystemInMetadata:fileName directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  572. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  573. [allRecordsDataSourceImagesVideos addObject:metadata];
  574. }
  575. _detailViewController.sourceDirectoryLocal = YES;
  576. }
  577. _detailViewController.metadataDetail = _metadata;
  578. _detailViewController.dateFilterQuery = nil;
  579. _detailViewController.isCameraUpload = NO;
  580. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  581. [_detailViewController setTitle:_metadata.fileNamePrint];
  582. }
  583. @end