CCOfflinePageContent.m 36 KB

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