CCOfflinePageContent.m 36 KB

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