CCOfflinePageContent.m 35 KB

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