CCOfflinePageContent.m 36 KB

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