CCOfflinePageContent.m 37 KB

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