CCFavorites.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. //
  2. // CCFavorites.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 "CCFavorites.h"
  24. #import "AppDelegate.h"
  25. #ifdef CUSTOM_BUILD
  26. #import "CustomSwift.h"
  27. #else
  28. #import "Nextcloud-Swift.h"
  29. #endif
  30. @interface CCFavorites () <CCActionsDeleteDelegate, CCActionsSettingFavoriteDelegate>
  31. {
  32. NSArray *_dataSource;
  33. BOOL _reloadDataSource;
  34. CCHud *_hudDeterminate;
  35. }
  36. @end
  37. @implementation CCFavorites
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== Init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  45. }
  46. return self;
  47. }
  48. - (void)viewDidLoad
  49. {
  50. [super viewDidLoad];
  51. // Custom Cell
  52. [self.tableView registerNib:[UINib nibWithNibName:@"CCFavoritesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  53. // dataSource
  54. _dataSource = [NSMutableArray new];
  55. // Metadata
  56. _metadata = [CCMetadata new];
  57. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  58. self.tableView.separatorColor = COLOR_SEPARATOR_TABLE;
  59. self.tableView.emptyDataSetDelegate = self;
  60. self.tableView.emptyDataSetSource = self;
  61. self.tableView.delegate = self;
  62. // calculate _serverUrl
  63. if (!_serverUrl)
  64. _serverUrl = nil;
  65. // Title
  66. if (_titleViewControl)
  67. self.title = _titleViewControl;
  68. else
  69. self.title = NSLocalizedString(@"_favorites_", nil);
  70. }
  71. // Apparirà
  72. - (void)viewWillAppear:(BOOL)animated
  73. {
  74. [super viewWillAppear:animated];
  75. // Color
  76. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  77. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  78. // Plus Button
  79. [app plusButtonVisibile:true];
  80. [self reloadDatasource];
  81. }
  82. // E' arrivato
  83. - (void)viewDidAppear:(BOOL)animated
  84. {
  85. [super viewDidAppear:animated];
  86. // update Badge
  87. [app updateApplicationIconBadgeNumber];
  88. }
  89. - (void)didReceiveMemoryWarning {
  90. [super didReceiveMemoryWarning];
  91. }
  92. - (void)triggerProgressTask:(NSNotification *)notification
  93. {
  94. NSDictionary *dict = notification.userInfo;
  95. float progress = [[dict valueForKey:@"progress"] floatValue];
  96. if (progress == 0)
  97. [self.navigationController cancelCCProgress];
  98. else
  99. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:COLOR_NAVIGATIONBAR_PROGRESS];
  100. }
  101. #pragma --------------------------------------------------------------------------------------------
  102. #pragma mark ==== DZNEmptyDataSetSource ====
  103. #pragma --------------------------------------------------------------------------------------------
  104. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. return [UIColor whiteColor];
  107. }
  108. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  109. {
  110. return [UIImage imageNamed:image_filesNoFiles];
  111. }
  112. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  113. {
  114. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_files_no_files_", nil)];
  115. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  116. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  117. }
  118. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  119. {
  120. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  121. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  122. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  123. paragraph.alignment = NSTextAlignmentCenter;
  124. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  125. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  126. }
  127. #pragma --------------------------------------------------------------------------------------------
  128. #pragma mark ===== UIDocumentInteractionController <delegate> =====
  129. #pragma --------------------------------------------------------------------------------------------
  130. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  131. {
  132. // evitiamo il rimando della eventuale photo e/o video
  133. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  134. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  135. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  136. }
  137. }
  138. #pragma --------------------------------------------------------------------------------------------
  139. #pragma mark ===== Delete <delegate> =====
  140. #pragma--------------------------------------------------------------------------------------------
  141. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  142. {
  143. NSLog(@"[LOG] Delete error %@", message);
  144. }
  145. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  146. {
  147. [self reloadDatasource];
  148. }
  149. #pragma --------------------------------------------------------------------------------------------
  150. #pragma mark ===== Favorite <delegate> =====
  151. #pragma--------------------------------------------------------------------------------------------
  152. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  153. {
  154. NSLog(@"[LOG] Remove Favorite error %@", message);
  155. }
  156. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  157. {
  158. [CCCoreData setMetadataFavoriteFileID:metadataNet.fileID favorite:[metadataNet.options boolValue] activeAccount:app.activeAccount context:nil];
  159. [self reloadDatasource];
  160. }
  161. #pragma --------------------------------------------------------------------------------------------
  162. #pragma mark ==== Download Thumbnail <Delegate> ====
  163. #pragma --------------------------------------------------------------------------------------------
  164. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  165. {
  166. [self reloadDatasource];
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ==== Download <Delegate> ====
  170. #pragma --------------------------------------------------------------------------------------------
  171. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  172. {
  173. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  174. [app updateApplicationIconBadgeNumber];
  175. }
  176. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  177. {
  178. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, app.activeAccount] context:nil];
  179. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  180. [self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  181. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  182. [self openWith:_metadata];
  183. } else {
  184. if ([self shouldPerformSegue])
  185. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  186. }
  187. [app updateApplicationIconBadgeNumber];
  188. }
  189. #pragma --------------------------------------------------------------------------------------------
  190. #pragma mark ===== menu =====
  191. #pragma--------------------------------------------------------------------------------------------
  192. - (void)openModel:(CCMetadata *)metadata
  193. {
  194. UIViewController *viewController;
  195. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  196. if ([metadata.model isEqualToString:@"cartadicredito"])
  197. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  198. if ([metadata.model isEqualToString:@"bancomat"])
  199. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  200. if ([metadata.model isEqualToString:@"contocorrente"])
  201. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  202. if ([metadata.model isEqualToString:@"accountweb"])
  203. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  204. if ([metadata.model isEqualToString:@"patenteguida"])
  205. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  206. if ([metadata.model isEqualToString:@"cartaidentita"])
  207. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  208. if ([metadata.model isEqualToString:@"passaporto"])
  209. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  210. if ([metadata.model isEqualToString:@"note"]) {
  211. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  212. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  213. [self presentViewController:navigationController animated:YES completion:nil];
  214. } else {
  215. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  216. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  217. [self presentViewController:navigationController animated:YES completion:nil];
  218. }
  219. }
  220. - (void)openWith:(CCMetadata *)metadata
  221. {
  222. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID];
  223. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  224. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  225. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  226. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  227. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  228. _docController.delegate = self;
  229. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  230. }
  231. }
  232. - (void)requestDeleteMetadata:(CCMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  233. {
  234. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  235. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  236. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  237. [self reloadDatasource];
  238. }]];
  239. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  240. }]];
  241. alertController.popoverPresentationController.sourceView = self.view;
  242. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  243. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  244. [alertController.view layoutIfNeeded];
  245. [self presentViewController:alertController animated:YES completion:nil];
  246. }
  247. #pragma --------------------------------------------------------------------------------------------
  248. #pragma mark ===== UnZipFile =====
  249. #pragma --------------------------------------------------------------------------------------------
  250. - (void)unZipFile:(NSString *)fileID
  251. {
  252. [_hudDeterminate visibleHudTitle:NSLocalizedString(@"_unzip_in_progress_", nil) mode:MBProgressHUDModeDeterminate color:nil];
  253. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  254. NSString *fileZip = [NSString stringWithFormat:@"%@/%@", app.directoryUser, fileID];
  255. [SSZipArchive unzipFileAtPath:fileZip toDestination:[CCUtility getDirectoryLocal] overwrite:YES password:nil progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
  256. dispatch_async(dispatch_get_main_queue(), ^{
  257. float progress = (float) entryNumber / (float)total;
  258. [_hudDeterminate progress:progress];
  259. });
  260. } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
  261. dispatch_async(dispatch_get_main_queue(), ^{
  262. [_hudDeterminate hideHud];
  263. if (succeeded) [app messageNotification:@"_info_" description:@"_file_unpacked_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess];
  264. else [app messageNotification:@"_error_" description:[NSString stringWithFormat:@"Error %ld", (long)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  265. });
  266. }];
  267. });
  268. }
  269. - (void)requestMoreMetadata:(CCMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  270. {
  271. UIImage *iconHeader;
  272. metadata = [_dataSource objectAtIndex:indexPath.row];
  273. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  274. actionSheet.animationDuration = 0.2;
  275. actionSheet.blurRadius = 0.0f;
  276. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  277. actionSheet.buttonHeight = 50.0;
  278. actionSheet.cancelButtonHeight = 50.0f;
  279. actionSheet.separatorHeight = 5.0f;
  280. actionSheet.automaticallyTintButtonImages = @(NO);
  281. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_CRYPTOCLOUD };
  282. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_TEXT_ANTHRACITE };
  283. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_BRAND };
  284. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:COLOR_TEXT_ANTHRACITE };
  285. actionSheet.separatorColor = COLOR_SEPARATOR_TABLE;
  286. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  287. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  288. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]])
  289. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  290. else
  291. iconHeader = [UIImage imageNamed:metadata.iconName];
  292. [actionSheet addButtonWithTitle: metadata.fileNamePrint
  293. image: iconHeader
  294. backgroundColor: COLOR_TABBAR
  295. height: 50.0
  296. type: AHKActionSheetButtonTypeDisabled
  297. handler: nil
  298. ];
  299. // ONLY Root Favorites : Remove file/folder Favorites
  300. if (_serverUrl == nil) {
  301. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_favorites_", nil) image:[UIImage imageNamed:image_actionSheetOffline] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  302. [self.tableView setEditing:NO animated:YES];
  303. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  304. }];
  305. }
  306. // Share
  307. if (_metadata.cryptated == NO && app.hasServerShareSupport) {
  308. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil)
  309. image:[UIImage imageNamed:image_actionSheetShare]
  310. backgroundColor:[UIColor whiteColor]
  311. height: 50.0
  312. type:AHKActionSheetButtonTypeDefault
  313. handler:^(AHKActionSheet *as) {
  314. // close swipe
  315. [self setEditing:NO animated:YES];
  316. [app.activeMain openWindowShare:metadata];
  317. }];
  318. }
  319. // NO Directory - NO Template
  320. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  321. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[UIImage imageNamed:image_actionSheetOpenIn] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  322. [self.tableView setEditing:NO animated:YES];
  323. [self openWith:metadata];
  324. }];
  325. }
  326. [actionSheet show];
  327. }
  328. #pragma mark -
  329. #pragma --------------------------------------------------------------------------------------------
  330. #pragma mark ===== Swipe Tablet -> menu =====
  331. #pragma --------------------------------------------------------------------------------------------
  332. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  333. {
  334. return UITableViewCellEditingStyleDelete;
  335. }
  336. - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
  337. return NSLocalizedString(@"_more_", nil);
  338. }
  339. - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
  340. {
  341. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  342. }
  343. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  344. {
  345. return NSLocalizedString(@"_delete_", nil);
  346. }
  347. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  348. {
  349. if (editingStyle == UITableViewCellEditingStyleDelete) {
  350. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  351. }
  352. }
  353. #pragma --------------------------------------------------------------------------------------------
  354. #pragma mark ==== Table ====
  355. #pragma --------------------------------------------------------------------------------------------
  356. - (CCMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  357. {
  358. CCMetadata *metadata;
  359. NSManagedObject *record = [_dataSource objectAtIndex:indexPath.row];
  360. metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", [record valueForKey:@"fileID"], app.activeAccount] context:nil];
  361. return metadata;
  362. }
  363. - (void)readFolderWithForced:(BOOL)forced serverUrl:(NSString *)serverUrl
  364. {
  365. [self reloadDatasource];
  366. }
  367. - (void)reloadDatasource
  368. {
  369. NSMutableArray *metadatas = [NSMutableArray new];
  370. NSArray *recordsTableMetadata ;
  371. if (!_serverUrl) {
  372. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] context:nil];
  373. } else {
  374. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  375. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  376. }
  377. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  378. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  379. for (NSString *fileID in fileIDs)
  380. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  381. _dataSource = [NSArray arrayWithArray:metadatas];
  382. [self.tableView reloadData];
  383. }
  384. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  385. {
  386. return 60;
  387. }
  388. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  389. {
  390. return 1;
  391. }
  392. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  393. {
  394. return [_dataSource count];
  395. }
  396. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  397. {
  398. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  399. CCMetadata *metadata;
  400. // separator
  401. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  402. // Initialize
  403. cell.statusImageView.image = nil;
  404. cell.offlineImageView.image = nil;
  405. // change color selection
  406. UIView *selectionColor = [[UIView alloc] init];
  407. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  408. cell.selectedBackgroundView = selectionColor;
  409. metadata = [_dataSource objectAtIndex:indexPath.row];
  410. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  411. if (_serverUrl == nil)
  412. cell.offlineImageView.image = [UIImage imageNamed:image_favorite];
  413. if (cell.fileImageView.image == nil && metadata.thumbnailExists)
  414. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  415. // encrypted color
  416. if (metadata.cryptated) {
  417. cell.labelTitle.textColor = COLOR_CRYPTOCLOUD;
  418. } else {
  419. cell.labelTitle.textColor = [UIColor blackColor];
  420. }
  421. // File name
  422. cell.labelTitle.text = metadata.fileNamePrint;
  423. cell.labelInfoFile.text = @"";
  424. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  425. if (cell.fileImageView.image == nil)
  426. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  427. // it's encrypted ???
  428. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  429. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  430. // text and length
  431. if (metadata.directory) {
  432. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  433. cell.accessoryType = UITableViewCellAccessoryNone;
  434. //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  435. } else {
  436. NSString *date = [CCUtility dateDiff:metadata.date];
  437. NSString *length = [CCUtility transformedSize:metadata.size];
  438. if ([metadata.type isEqualToString: k_metadataType_template])
  439. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  440. if ([metadata.type isEqualToString: k_metadataType_file] || [metadata.type isEqualToString: k_metadataType_local]) {
  441. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]];
  442. if (fileExists)
  443. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ • %@", date, length];
  444. else
  445. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ ◦ %@", date, length];
  446. }
  447. cell.accessoryType = UITableViewCellAccessoryNone;
  448. }
  449. return cell;
  450. }
  451. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  452. {
  453. // deselect row
  454. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  455. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  456. // if is in download [do not touch]
  457. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  458. return;
  459. // File
  460. if (([_metadata.type isEqualToString: k_metadataType_file]) && _metadata.directory == NO) {
  461. // File do not exists
  462. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:_metadata.account];
  463. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  464. [self downloadFileSuccess:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil];
  465. } else {
  466. [[CCNetworking sharedNetworking] downloadFile:_metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  467. }
  468. }
  469. // Model
  470. if ([self.metadata.type isEqualToString: k_metadataType_template])
  471. [self openModel:self.metadata];
  472. // Directory
  473. if (_metadata.directory)
  474. [self performSegueDirectoryWithControlPasscode];
  475. }
  476. -(void)performSegueDirectoryWithControlPasscode
  477. {
  478. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  479. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  480. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  481. vc.titleViewControl = _metadata.fileNamePrint;
  482. [self.navigationController pushViewController:vc animated:YES];
  483. }
  484. #pragma --------------------------------------------------------------------------------------------
  485. #pragma mark ===== Navigation ====
  486. #pragma --------------------------------------------------------------------------------------------
  487. - (BOOL)shouldPerformSegue
  488. {
  489. // if i am in background -> exit
  490. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  491. // if i am not window -> exit
  492. if (self.view.window == NO)
  493. return NO;
  494. // Collapsed but i am in detail -> exit
  495. if (self.splitViewController.isCollapsed)
  496. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  497. // Video in run -> exit
  498. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  499. return YES;
  500. }
  501. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  502. {
  503. id viewController = segue.destinationViewController;
  504. if ([viewController isKindOfClass:[UINavigationController class]]) {
  505. UINavigationController *nav = viewController;
  506. _detailViewController = (CCDetail *)nav.topViewController;
  507. } else {
  508. _detailViewController = segue.destinationViewController;
  509. }
  510. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  511. for (CCMetadata *metadata in _dataSource) {
  512. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  513. [allRecordsDataSourceImagesVideos addObject:metadata];
  514. }
  515. _detailViewController.metadataDetail = _metadata;
  516. _detailViewController.dateFilterQuery = nil;
  517. _detailViewController.isCameraUpload = NO;
  518. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  519. [_detailViewController setTitle:_metadata.fileNamePrint];
  520. }
  521. @end