CCFavorites.m 30 KB

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