CCTransfers.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. //
  2. // CCTransfers.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/04/17.
  6. // Copyright (c) 2014 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 "CCTransfers.h"
  24. #import "AppDelegate.h"
  25. #import "CCMain.h"
  26. #import "CCDetail.h"
  27. #import "CCSection.h"
  28. #import "CCMetadata.h"
  29. #import "CCTransfersCell.h"
  30. #ifdef CUSTOM_BUILD
  31. #import "CustomSwift.h"
  32. #else
  33. #import "Nextcloud-Swift.h"
  34. #endif
  35. #define download 1
  36. #define downloadwwan 2
  37. #define upload 3
  38. #define uploadwwan 4
  39. @interface CCTransfers ()
  40. {
  41. // Datasource
  42. CCSectionDataSourceMetadata *_sectionDataSource;
  43. }
  44. @end
  45. @implementation CCTransfers
  46. #pragma --------------------------------------------------------------------------------------------
  47. #pragma mark ===== Init =====
  48. #pragma --------------------------------------------------------------------------------------------
  49. - (id)initWithCoder:(NSCoder *)aDecoder
  50. {
  51. if (self = [super initWithCoder:aDecoder]) {
  52. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  53. app.activeTransfers = self;
  54. }
  55. return self;
  56. }
  57. - (void)viewDidLoad {
  58. [super viewDidLoad];
  59. // Custom Cell
  60. [_tableView registerNib:[UINib nibWithNibName:@"CCTransfersCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  61. _tableView.delegate = self;
  62. _tableView.dataSource = self;
  63. _tableView.emptyDataSetDelegate = self;
  64. _tableView.emptyDataSetSource = self;
  65. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  66. _tableView.backgroundColor = [NCColorBrand sharedInstance].tableBackground;
  67. self.title = NSLocalizedString(@"_transfers_", nil);
  68. [self reloadDatasource];
  69. }
  70. // Apparirà
  71. - (void)viewWillAppear:(BOOL)animated
  72. {
  73. [super viewWillAppear:animated];
  74. // Color
  75. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  76. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  77. }
  78. // E' arrivato
  79. - (void)viewDidAppear:(BOOL)animated
  80. {
  81. [super viewDidAppear:animated];
  82. [self reloadDatasource];
  83. }
  84. - (void)didReceiveMemoryWarning {
  85. [super didReceiveMemoryWarning];
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ==== DZNEmptyDataSetSource ====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  91. {
  92. return 0.0f;
  93. }
  94. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  95. {
  96. return [UIColor whiteColor];
  97. }
  98. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  99. {
  100. return [UIImage imageNamed:image_transfersNoRecord];
  101. }
  102. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  103. {
  104. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_transfer_", nil)];
  105. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  106. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  107. }
  108. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  109. {
  110. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_transfer_sub_", nil)];
  111. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  112. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  113. paragraph.alignment = NSTextAlignmentCenter;
  114. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  115. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  116. }
  117. #pragma --------------------------------------------------------------------------------------------
  118. #pragma mark - ===== Progress & Task Button =====
  119. #pragma --------------------------------------------------------------------------------------------
  120. - (void)triggerProgressTask:(NSNotification *)notification
  121. {
  122. NSDictionary *dict = notification.userInfo;
  123. NSString *fileID = [dict valueForKey:@"fileID"];
  124. BOOL cryptated = [[dict valueForKey:@"cryptated"] boolValue];
  125. float progress = [[dict valueForKey:@"progress"] floatValue];
  126. // Check
  127. if (!fileID)
  128. return;
  129. [app.listProgressMetadata setObject:[NSNumber numberWithFloat:progress] forKey:fileID];
  130. NSIndexPath *indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:fileID];
  131. if (indexPath && indexPath.row == 0) {
  132. CCTransfersCell *cell = (CCTransfersCell *)[_tableView cellForRowAtIndexPath:indexPath];
  133. if (cryptated) cell.progressView.progressTintColor = [NCColorBrand sharedInstance].cryptocloud;
  134. else cell.progressView.progressTintColor = [UIColor blackColor];
  135. cell.progressView.hidden = NO;
  136. [cell.progressView setProgress:progress];
  137. } else {
  138. [self reloadDatasource];
  139. }
  140. }
  141. - (void)reloadTaskButton:(id)sender withEvent:(UIEvent *)event
  142. {
  143. if (app.activeMain == nil)
  144. return;
  145. UITouch * touch = [[event allTouches] anyObject];
  146. CGPoint location = [touch locationInView:_tableView];
  147. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  148. if (indexPath) {
  149. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  150. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  151. if (metadata)
  152. [app.activeMain reloadTaskButton:metadata];
  153. }
  154. }
  155. - (void)reloadAllTask
  156. {
  157. if (app.activeMain == nil)
  158. return;
  159. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  160. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  161. if ([metadata.session containsString:@"download"] && (metadata.sessionTaskIdentifierPlist != k_taskIdentifierDone))
  162. continue;
  163. if ([metadata.session containsString:@"upload"] && (metadata.sessionTaskIdentifier != k_taskIdentifierStop))
  164. continue;
  165. [app.activeMain reloadTaskButton:metadata];
  166. }
  167. }
  168. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  169. {
  170. if (app.activeMain == nil)
  171. return;
  172. UITouch * touch = [[event allTouches] anyObject];
  173. CGPoint location = [touch locationInView:_tableView];
  174. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  175. if (indexPath) {
  176. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  177. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  178. if (metadata)
  179. [app.activeMain cancelTaskButton:metadata reloadTable:YES];
  180. }
  181. }
  182. - (void)cancelAllTask
  183. {
  184. if (app.activeMain == nil)
  185. return;
  186. BOOL lastAndRefresh = NO;
  187. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  188. if ([key isEqualToString:[_sectionDataSource.allRecordsDataSource.allKeys lastObject]])
  189. lastAndRefresh = YES;
  190. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  191. if ([metadata.session containsString:@"upload"] && metadata.cryptated && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone && metadata.sessionTaskIdentifierPlist >= 0) || (metadata.sessionTaskIdentifier >= 0 && metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone)))
  192. continue;
  193. [app.activeMain cancelTaskButton:metadata reloadTable:lastAndRefresh];
  194. }
  195. }
  196. - (void)stopTaskButton:(id)sender withEvent:(UIEvent *)event
  197. {
  198. if (app.activeMain == nil)
  199. return;
  200. UITouch * touch = [[event allTouches] anyObject];
  201. CGPoint location = [touch locationInView:_tableView];
  202. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  203. if (indexPath) {
  204. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  205. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  206. if (metadata)
  207. [app.activeMain stopTaskButton:metadata];
  208. }
  209. }
  210. - (void)stopAllTask
  211. {
  212. if (app.activeMain == nil)
  213. return;
  214. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  215. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  216. if ([metadata.session containsString:@"download"]) {
  217. [app.activeMain cancelTaskButton:metadata reloadTable:YES];
  218. continue;
  219. }
  220. if ([metadata.session containsString:@"upload"] && metadata.cryptated && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone && metadata.sessionTaskIdentifierPlist >= 0) || (metadata.sessionTaskIdentifier >= 0 && metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone)))
  221. continue;
  222. [app.activeMain stopTaskButton:metadata];
  223. }
  224. }
  225. #pragma --------------------------------------------------------------------------------------------
  226. #pragma mark - ==== Datasource ====
  227. #pragma --------------------------------------------------------------------------------------------
  228. - (void)reloadDatasource
  229. {
  230. // test
  231. if (app.activeAccount.length == 0)
  232. return;
  233. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND ((session CONTAINS 'upload') OR (session CONTAINS 'download' AND (sessionSelector != 'loadPlist')))", app.activeAccount] fieldOrder:@"sessionTaskIdentifier" ascending:YES];
  234. _sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:app.listProgressMetadata groupByField:@"session" replaceDateToExifDate:NO activeAccount:app.activeAccount];
  235. [_tableView reloadData];
  236. }
  237. #pragma --------------------------------------------------------------------------------------------
  238. #pragma mark - ==== Table ====
  239. #pragma --------------------------------------------------------------------------------------------
  240. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  241. {
  242. return 50;
  243. }
  244. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  245. {
  246. return [[_sectionDataSource.sectionArrayRow allKeys] count];
  247. }
  248. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  249. {
  250. return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  251. }
  252. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  253. {
  254. return 13.0f;
  255. }
  256. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  257. {
  258. UIVisualEffectView *visualEffectView;
  259. NSString *titleSection, *numberTitle;
  260. NSInteger typeOfSession = 0;
  261. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  262. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSDate class]]) titleSection = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:section]];
  263. NSArray *metadatas = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]];
  264. NSUInteger rowsCount = [metadatas count];
  265. visualEffectView = [[UIVisualEffectView alloc] init];
  266. visualEffectView.backgroundColor = [UIColor clearColor];
  267. // title section
  268. if ([titleSection isEqualToString:@"_none_"]) {
  269. titleSection = @"";
  270. } else if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"]) {
  271. typeOfSession = download;
  272. titleSection = NSLocalizedString(@"_title_section_download_",nil);
  273. } else if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"]) {
  274. typeOfSession = downloadwwan;
  275. titleSection = [NSLocalizedString(@"_title_section_download_",nil) stringByAppendingString:@" Wi-Fi"];
  276. } else if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"]) {
  277. typeOfSession = upload;
  278. titleSection = NSLocalizedString(@"_title_section_upload_",nil);
  279. } else if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"]) {
  280. typeOfSession = uploadwwan;
  281. titleSection = [NSLocalizedString(@"_title_section_upload_",nil) stringByAppendingString:@" Wi-Fi"];
  282. } else {
  283. titleSection = NSLocalizedString(titleSection,nil);
  284. }
  285. // title label on left
  286. UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(8, 3, 0, 13)];
  287. titleLabel.textColor = [UIColor blackColor];
  288. titleLabel.font = [UIFont systemFontOfSize:9];
  289. titleLabel.textAlignment = NSTextAlignmentLeft;
  290. titleLabel.text = titleSection;
  291. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  292. [visualEffectView addSubview:titleLabel];
  293. // element (s) on right
  294. UILabel *elementLabel=[[UILabel alloc]initWithFrame:CGRectMake(-8, 3, 0, 13)];
  295. elementLabel.textColor = [UIColor blackColor];
  296. elementLabel.font = [UIFont systemFontOfSize:9];
  297. elementLabel.textAlignment = NSTextAlignmentRight;
  298. elementLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  299. if ((typeOfSession == download && app.queueNunDownload > rowsCount) || (typeOfSession == downloadwwan && app.queueNumDownloadWWan > rowsCount) ||
  300. (typeOfSession == upload && app.queueNumUpload > rowsCount) || (typeOfSession == uploadwwan && app.queueNumUploadWWan > rowsCount)) {
  301. numberTitle = [NSString stringWithFormat:@"%lu+", (unsigned long)rowsCount];
  302. } else {
  303. numberTitle = [NSString stringWithFormat:@"%lu", (unsigned long)rowsCount];
  304. }
  305. if (rowsCount > 1)
  306. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_elements_",nil)];
  307. else
  308. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_element_",nil)];
  309. // view
  310. [visualEffectView addSubview:elementLabel];
  311. return visualEffectView;
  312. }
  313. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  314. {
  315. NSString *titleSection;
  316. NSString *element_s;
  317. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  318. // Prepare view for title in footer
  319. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  320. UILabel *titleFooterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
  321. titleFooterLabel.textColor = [UIColor blackColor];
  322. titleFooterLabel.font = [UIFont systemFontOfSize:12];
  323. titleFooterLabel.textAlignment = NSTextAlignmentCenter;
  324. // Footer Download
  325. if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  326. // element or elements ?
  327. if (app.queueNunDownload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  328. else element_s = NSLocalizedString(@"_element_",nil);
  329. // Num record to upload
  330. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_", nil), app.queueNunDownload, element_s]];
  331. titleFooterLabel.attributedText = stringFooter;
  332. [view addSubview:titleFooterLabel];
  333. return view;
  334. }
  335. // Footer Download WWAN
  336. if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  337. // element or elements ?
  338. if (app.queueNumDownloadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  339. else element_s = NSLocalizedString(@"_element_",nil);
  340. // Add the symbol WiFi and Num record
  341. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  342. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  343. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  344. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_wwan_", nil), app.queueNumDownloadWWan, element_s]];
  345. [stringFooter insertAttributedString:attachmentString atIndex:0];
  346. titleFooterLabel.attributedText = stringFooter;
  347. [view addSubview:titleFooterLabel];
  348. return view;
  349. }
  350. // Footer Upload
  351. if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  352. // element or elements ?
  353. if (app.queueNumUpload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  354. else element_s = NSLocalizedString(@"_element_",nil);
  355. // Num record to upload
  356. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_", nil), app.queueNumUpload, element_s]];
  357. titleFooterLabel.attributedText = stringFooter;
  358. [view addSubview:titleFooterLabel];
  359. return view;
  360. }
  361. // Footer Upload WWAN
  362. if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  363. // element or elements ?
  364. if (app.queueNumUploadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  365. else element_s = NSLocalizedString(@"_element_",nil);
  366. // Add the symbol WiFi and Num record
  367. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  368. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  369. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  370. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_wwan_", nil), app.queueNumUploadWWan,element_s]];
  371. [stringFooter insertAttributedString:attachmentString atIndex:0];
  372. titleFooterLabel.attributedText = stringFooter;
  373. [view addSubview:titleFooterLabel];
  374. return view;
  375. }
  376. return nil;
  377. }
  378. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  379. {
  380. //NSString *titleSection;
  381. //if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]])
  382. // titleSection = [_sectionDataSource.sections objectAtIndex:section];
  383. //if ([titleSection rangeOfString:@"upload"].location != NSNotFound && [titleSection rangeOfString:@"wwan"].location != NSNotFound && titleSection != nil) return 18.0f;
  384. //else return 0.0f;
  385. return 18.0f;
  386. }
  387. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  388. {
  389. return [_sectionDataSource.sections indexOfObject:title];
  390. }
  391. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  392. {
  393. NSString *dataFile;
  394. NSString *lunghezzaFile;
  395. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  396. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  397. CCTransfersCell *cell = (CCTransfersCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  398. cell.backgroundColor = [UIColor clearColor];
  399. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  400. // ----------------------------------------------------------------------------------------------------------
  401. // DEFAULT
  402. // ----------------------------------------------------------------------------------------------------------
  403. cell.fileImageView.image = nil;
  404. cell.statusImageView.image = nil;
  405. cell.labelTitle.enabled = YES;
  406. cell.labelTitle.text = @"";
  407. cell.labelInfoFile.enabled = YES;
  408. cell.labelInfoFile.text = @"";
  409. cell.progressView.progress = 0.0;
  410. cell.progressView.hidden = YES;
  411. cell.cancelTaskButton.hidden = YES;
  412. cell.reloadTaskButton.hidden = YES;
  413. cell.stopTaskButton.hidden = YES;
  414. // colori e font
  415. if (metadata.cryptated) {
  416. cell.labelTitle.textColor = [NCColorBrand sharedInstance].cryptocloud;
  417. cell.labelInfoFile.textColor = [UIColor blackColor];
  418. } else {
  419. cell.labelTitle.textColor = [UIColor blackColor];
  420. cell.labelInfoFile.textColor = [UIColor blackColor];
  421. }
  422. // ----------------------------------------------------------------------------------------------------------
  423. // File Name & Folder
  424. // ----------------------------------------------------------------------------------------------------------
  425. // nome del file
  426. cell.labelTitle.text = metadata.fileNamePrint;
  427. // è una directory
  428. if (metadata.directory) {
  429. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  430. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  431. lunghezzaFile = @" ";
  432. } else {
  433. // è un file
  434. dataFile = [CCUtility dateDiff:metadata.date];
  435. lunghezzaFile = [CCUtility transformedSize:metadata.size];
  436. // Plist ancora da scaricare
  437. if (metadata.cryptated && [metadata.title length] == 0) {
  438. dataFile = @" ";
  439. lunghezzaFile = @" ";
  440. }
  441. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  442. [dateFormatter setDateStyle:NSDateFormatterShortStyle];
  443. [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  444. cell.accessoryType = UITableViewCellAccessoryNone;
  445. }
  446. // ----------------------------------------------------------------------------------------------------------
  447. // File Image View
  448. // ----------------------------------------------------------------------------------------------------------
  449. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  450. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  451. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  452. } else {
  453. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  454. }
  455. // ----------------------------------------------------------------------------------------------------------
  456. // Image Status cyptated & Lock Passcode
  457. // ----------------------------------------------------------------------------------------------------------
  458. // File Cyptated
  459. if (metadata.cryptated && metadata.directory == NO && [metadata.type isEqualToString: k_metadataType_template] == NO) {
  460. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  461. }
  462. // ----------------------------------------------------------------------------------------------------------
  463. // downloadFile
  464. // ----------------------------------------------------------------------------------------------------------
  465. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound) {
  466. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusdownloadcrypto];
  467. else cell.statusImageView.image = [UIImage imageNamed:image_statusdownload];
  468. // Fai comparire il RELOAD e lo STOP solo se non è un Task Plist
  469. if (metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone) {
  470. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  471. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  472. cell.cancelTaskButton.hidden = NO;
  473. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  474. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  475. cell.reloadTaskButton.hidden = NO;
  476. }
  477. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  478. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  479. if (progress > 0) {
  480. if (metadata.cryptated) cell.progressView.progressTintColor = [NCColorBrand sharedInstance].cryptocloud;
  481. else cell.progressView.progressTintColor = [UIColor blackColor];
  482. cell.progressView.progress = progress;
  483. cell.progressView.hidden = NO;
  484. }
  485. // ----------------------------------------------------------------------------------------------------------
  486. // downloadFile Error
  487. // ----------------------------------------------------------------------------------------------------------
  488. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  489. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  490. if ([metadata.sessionError length] == 0)
  491. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_downloaded_",nil)];
  492. else
  493. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  494. }
  495. }
  496. // ----------------------------------------------------------------------------------------------------------
  497. // uploadFile
  498. // ----------------------------------------------------------------------------------------------------------
  499. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"upload"].location != NSNotFound) {
  500. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusuploadcrypto];
  501. else cell.statusImageView.image = [UIImage imageNamed:image_statusupload];
  502. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetaskcrypto] forState:UIControlStateNormal];
  503. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetask] forState:UIControlStateNormal];
  504. cell.cancelTaskButton.hidden = NO;
  505. if (metadata.sessionTaskIdentifier == k_taskIdentifierStop) {
  506. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  507. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  508. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusstopcrypto];
  509. else cell.statusImageView.image = [UIImage imageNamed:image_statusstop];
  510. cell.reloadTaskButton.hidden = NO;
  511. cell.stopTaskButton.hidden = YES;
  512. } else {
  513. if (metadata.cryptated)[cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  514. else [cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  515. cell.stopTaskButton.hidden = NO;
  516. cell.reloadTaskButton.hidden = YES;
  517. }
  518. // se non c'è una preview in bianconero metti l'immagine di default
  519. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]] == NO)
  520. cell.fileImageView.image = [UIImage imageNamed:image_uploaddisable];
  521. cell.labelTitle.enabled = NO;
  522. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  523. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  524. if (progress > 0) {
  525. if (metadata.cryptated) cell.progressView.progressTintColor = [NCColorBrand sharedInstance].cryptocloud;
  526. else cell.progressView.progressTintColor = [UIColor blackColor];
  527. cell.progressView.progress = progress;
  528. cell.progressView.hidden = NO;
  529. }
  530. // ----------------------------------------------------------------------------------------------------------
  531. // uploadFileError
  532. // ----------------------------------------------------------------------------------------------------------
  533. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  534. cell.labelTitle.enabled = NO;
  535. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  536. if ([metadata.sessionError length] == 0)
  537. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_uploaded_",nil)];
  538. else
  539. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  540. }
  541. }
  542. [cell.reloadTaskButton addTarget:self action:@selector(reloadTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  543. [cell.cancelTaskButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  544. [cell.stopTaskButton addTarget:self action:@selector(stopTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  545. return cell;
  546. }
  547. @end