CCOfflinePageContent.m 36 KB

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