CCOfflinePageContent.m 34 KB

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