CCTransfers.m 31 KB

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