CCOfflinePageContent.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  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. UIImage *iconHeader;
  272. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  273. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  274. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  275. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  276. } else {
  277. metadata = [dataSource objectAtIndex:indexPath.row];
  278. }
  279. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  280. actionSheet.animationDuration = 0.2;
  281. actionSheet.blurRadius = 0.0f;
  282. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  283. actionSheet.buttonHeight = 50.0;
  284. actionSheet.cancelButtonHeight = 50.0f;
  285. actionSheet.separatorHeight = 5.0f;
  286. actionSheet.automaticallyTintButtonImages = @(NO);
  287. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_CRYPTOCLOUD };
  288. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_TEXT_ANTHRACITE };
  289. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_BRAND };
  290. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_TEXT_ANTHRACITE };
  291. actionSheet.separatorColor = COLOR_SEPARATOR_TABLE;
  292. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  293. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  294. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]])
  295. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  296. else
  297. iconHeader = [UIImage imageNamed:metadata.iconName];
  298. [actionSheet addButtonWithTitle: metadata.fileNamePrint
  299. image: iconHeader
  300. backgroundColor: COLOR_TABBAR
  301. height: 50.0
  302. type: AHKActionSheetButtonTypeDisabled
  303. handler: nil
  304. ];
  305. // NO Directory - NO Template
  306. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  307. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[UIImage imageNamed:image_actionSheetOpenIn] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  308. [self.tableView setEditing:NO animated:YES];
  309. [self openWith:metadata];
  310. }];
  311. }
  312. // ONLY Root Favorites : Remove file/folder Favorites
  313. if (_serverUrl == nil && [_pageType isEqualToString:k_pageOfflineFavorites]) {
  314. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_favorites_", nil) image:[UIImage imageNamed:image_actionSheetOffline] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  315. [self.tableView setEditing:NO animated:YES];
  316. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  317. }];
  318. }
  319. // ONLY Root Offline : Remove file/folder offline
  320. if (_serverUrl == nil && [_pageType isEqualToString:k_pageOfflineOffline]) {
  321. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_offline_", nil) image:[UIImage imageNamed:image_actionSheetOffline] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  322. if (metadata.directory) {
  323. // remove tag offline for all folder/subfolder/file
  324. NSString *relativeRoot = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  325. NSString *dirServerUrl = [CCUtility stringAppendServerUrl:relativeRoot addFileName:metadata.fileNameData];
  326. NSArray *directories = [CCCoreData getOfflineDirectoryActiveAccount:app.activeAccount];
  327. for (TableDirectory *directory in directories)
  328. if ([directory.serverUrl containsString:dirServerUrl]) {
  329. [CCCoreData setOfflineDirectoryServerUrl:directory.serverUrl offline:NO activeAccount:app.activeAccount];
  330. [CCCoreData removeOfflineAllFileFromServerUrl:directory.serverUrl activeAccount:app.activeAccount];
  331. }
  332. } else {
  333. [CCCoreData setOfflineLocalFileID:metadata.fileID offline:NO activeAccount:app.activeAccount];
  334. }
  335. [self.tableView setEditing:NO animated:YES];
  336. [self reloadDatasource];
  337. }];
  338. }
  339. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil) image:[UIImage imageNamed:image_delete] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDestructive handler:^(AHKActionSheet *as) {
  340. [self requestDeleteMetadata:metadata indexPath:indexPath];
  341. }];
  342. [actionSheet show];
  343. }
  344. #pragma --------------------------------------------------------------------------------------------
  345. #pragma mark ==== Table ====
  346. #pragma --------------------------------------------------------------------------------------------
  347. - (CCMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  348. {
  349. CCMetadata *metadata;
  350. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) {
  351. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  352. metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", [record valueForKey:@"fileID"], app.activeAccount] context:nil];
  353. }
  354. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  355. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  356. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  357. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  358. }
  359. return metadata;
  360. }
  361. - (void)readFolderWithForced:(BOOL)forced serverUrl:(NSString *)serverUrl
  362. {
  363. [self reloadDatasource];
  364. }
  365. - (void)reloadDatasource
  366. {
  367. if ([_pageType isEqualToString:k_pageOfflineFavorites]) {
  368. NSMutableArray *metadatas = [NSMutableArray new];
  369. NSArray *recordsTableMetadata ;
  370. if (!_serverUrl) {
  371. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] context:nil];
  372. } else {
  373. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  374. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  375. }
  376. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  377. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  378. for (NSString *fileID in fileIDs)
  379. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  380. dataSource = [NSArray arrayWithArray:metadatas];
  381. }
  382. if ([_pageType isEqualToString:k_pageOfflineOffline]) {
  383. NSMutableArray *metadatas = [NSMutableArray new];
  384. NSArray *recordsTableMetadata ;
  385. if (!_serverUrl) {
  386. recordsTableMetadata = [CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  387. } else {
  388. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  389. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  390. }
  391. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  392. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  393. for (NSString *fileID in fileIDs)
  394. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  395. dataSource = [NSArray arrayWithArray:metadatas];
  396. }
  397. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  398. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_serverUrl error:nil];
  399. NSMutableArray *metadatas = [NSMutableArray new];
  400. for (NSString *subpath in subpaths)
  401. if (![[subpath lastPathComponent] hasPrefix:@"."])
  402. [metadatas addObject:subpath];
  403. dataSource = [NSArray arrayWithArray:metadatas];
  404. }
  405. [self.tableView reloadData];
  406. }
  407. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  408. {
  409. return 60;
  410. }
  411. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  412. {
  413. return 1;
  414. }
  415. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  416. {
  417. return [dataSource count];
  418. }
  419. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  420. {
  421. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  422. CCMetadata *metadata;
  423. // separator
  424. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  425. // Initialize
  426. cell.statusImageView.image = nil;
  427. cell.offlineImageView.image = nil;
  428. // change color selection
  429. UIView *selectionColor = [[UIView alloc] init];
  430. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  431. cell.selectedBackgroundView = selectionColor;
  432. // i am in Favorites OR i am in Offline
  433. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) {
  434. metadata = [dataSource objectAtIndex:indexPath.row];
  435. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  436. if (_serverUrl == nil) {
  437. if ([_pageType isEqualToString:k_pageOfflineFavorites])
  438. cell.offlineImageView.image = [UIImage imageNamed:image_favorite];
  439. if ([_pageType isEqualToString:k_pageOfflineOffline])
  440. cell.offlineImageView.image = [UIImage imageNamed:image_offline];
  441. }
  442. if (cell.fileImageView.image == nil && metadata.thumbnailExists)
  443. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  444. }
  445. // i am in local
  446. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  447. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  448. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  449. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  450. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _serverUrl, metadata.fileNamePrint]];
  451. if (!cell.fileImageView.image) {
  452. 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]];
  453. if (icon) {
  454. [CCGraphics saveIcoWithFileID:metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _serverUrl, metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  455. cell.fileImageView.image = icon;
  456. }
  457. }
  458. }
  459. // ButtonDown Tapped
  460. [cell.buttonDown addTarget:self action:@selector(cellButtonDownWasTapped:) forControlEvents:UIControlEventTouchUpInside];
  461. // encrypted color
  462. if (metadata.cryptated) {
  463. cell.labelTitle.textColor = COLOR_CRYPTOCLOUD;
  464. } else {
  465. cell.labelTitle.textColor = [UIColor blackColor];
  466. }
  467. // File name
  468. cell.labelTitle.text = metadata.fileNamePrint;
  469. cell.labelInfoFile.text = @"";
  470. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  471. if (cell.fileImageView.image == nil)
  472. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  473. // it's encrypted ???
  474. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  475. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  476. // text and length
  477. if (metadata.directory) {
  478. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  479. cell.accessoryType = UITableViewCellAccessoryNone;
  480. //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  481. } else {
  482. NSString *date = [CCUtility dateDiff:metadata.date];
  483. NSString *length = [CCUtility transformedSize:metadata.size];
  484. if ([metadata.type isEqualToString: k_metadataType_template])
  485. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  486. if ([metadata.type isEqualToString: k_metadataType_file] || [metadata.type isEqualToString: k_metadataType_local]) {
  487. BOOL fileExists = NO;
  488. if ([_pageType isEqualToString:k_pageOfflineLocal])
  489. fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", _serverUrl, metadata.fileName]];
  490. if ([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline])
  491. fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]];
  492. if (fileExists)
  493. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ • %@", date, length];
  494. else
  495. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ ◦ %@", date, length];
  496. }
  497. cell.accessoryType = UITableViewCellAccessoryNone;
  498. }
  499. return cell;
  500. }
  501. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  502. {
  503. // deselect row
  504. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  505. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  506. // if is in download [do not touch]
  507. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  508. return;
  509. // File
  510. if (([_metadata.type isEqualToString: k_metadataType_file] || [_metadata.type isEqualToString: k_metadataType_local]) && _metadata.directory == NO) {
  511. // i am in local
  512. if ([_pageType isEqualToString:k_pageOfflineLocal]) {
  513. if ([self shouldPerformSegue])
  514. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  515. } else {
  516. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  517. // File exists
  518. if ([self shouldPerformSegue])
  519. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  520. } else {
  521. // File do not exists
  522. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:_metadata.account];
  523. [[CCNetworking sharedNetworking] downloadFile:_metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  524. }
  525. }
  526. }
  527. // Model
  528. if ([self.metadata.type isEqualToString: k_metadataType_template])
  529. [self openModel:self.metadata];
  530. // Directory
  531. if (_metadata.directory)
  532. [self performSegueDirectoryWithControlPasscode];
  533. }
  534. -(void)performSegueDirectoryWithControlPasscode
  535. {
  536. CCOfflinePageContent *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflinePageContent"];
  537. NSString *serverUrl;
  538. if (([_pageType isEqualToString:k_pageOfflineFavorites] || [_pageType isEqualToString:k_pageOfflineOffline]) && !_serverUrl) {
  539. serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  540. } else {
  541. serverUrl = _serverUrl;
  542. }
  543. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  544. vc.pageType = _pageType;
  545. vc.titleViewControl = _metadata.fileNamePrint;
  546. [self.navigationController pushViewController:vc animated:YES];
  547. }
  548. #pragma --------------------------------------------------------------------------------------------
  549. #pragma mark ===== Navigation ====
  550. #pragma --------------------------------------------------------------------------------------------
  551. - (BOOL)shouldPerformSegue
  552. {
  553. // if i am in background -> exit
  554. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  555. // if i am not window -> exit
  556. if (self.view.window == NO)
  557. return NO;
  558. // Collapsed but i am in detail -> exit
  559. if (self.splitViewController.isCollapsed)
  560. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  561. // Video in run -> exit
  562. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  563. return YES;
  564. }
  565. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  566. {
  567. id viewController = segue.destinationViewController;
  568. if ([viewController isKindOfClass:[UINavigationController class]]) {
  569. UINavigationController *nav = viewController;
  570. _detailViewController = (CCDetail *)nav.topViewController;
  571. } else {
  572. _detailViewController = segue.destinationViewController;
  573. }
  574. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  575. if ([self.pageType isEqualToString:k_pageOfflineFavorites] || [self.pageType isEqualToString:k_pageOfflineOffline]) {
  576. for (CCMetadata *metadata in dataSource) {
  577. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  578. [allRecordsDataSourceImagesVideos addObject:metadata];
  579. }
  580. }
  581. if ([self.pageType isEqualToString:k_pageOfflineLocal]) {
  582. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  583. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
  584. for (NSString *fileName in dataSource) {
  585. CCMetadata *metadata = [CCMetadata new];
  586. metadata = [CCUtility insertFileSystemInMetadata:fileName directory:_serverUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  587. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  588. [allRecordsDataSourceImagesVideos addObject:metadata];
  589. }
  590. _detailViewController.sourceDirectoryLocal = YES;
  591. }
  592. _detailViewController.metadataDetail = _metadata;
  593. _detailViewController.dateFilterQuery = nil;
  594. _detailViewController.isCameraUpload = NO;
  595. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  596. [_detailViewController setTitle:_metadata.fileNamePrint];
  597. }
  598. @end