CCMain.m 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. //
  2. // CCMain.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/09/14.
  6. // Copyright (c) 2014 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "CCMain.h"
  24. #import "AppDelegate.h"
  25. #import "NCAutoUpload.h"
  26. #import "NCBridgeSwift.h"
  27. #import "PKDownloadButton.h"
  28. @interface CCMain () <UITextViewDelegate, MGSwipeTableCellDelegate, NCSelectDelegate, UITextFieldDelegate, UIAdaptivePresentationControllerDelegate>
  29. {
  30. AppDelegate *appDelegate;
  31. BOOL _isRoot;
  32. BOOL _isViewDidLoad;
  33. UIImageView *_imageTitleHome;
  34. NSUInteger _failedAttempts;
  35. NSDate *_lockUntilDate;
  36. UIRefreshControl *refreshControl;
  37. CCHud *_hud;
  38. // Datasource
  39. CCSectionDataSourceMetadata *sectionDataSource;
  40. // Search
  41. NSString *_searchFileName;
  42. NSMutableArray *_searchResultMetadatas;
  43. NSString *_noFilesSearchTitle;
  44. NSString *_noFilesSearchDescription;
  45. NSTimer *_timerWaitInput;
  46. // Automatic Upload Folder
  47. NSString *_autoUploadFileName;
  48. NSString *_autoUploadDirectory;
  49. // Folder
  50. BOOL _loadingFolder;
  51. tableMetadata *_metadataFolder;
  52. CGFloat heightRichWorkspace;
  53. CGFloat heightSearchBar;
  54. //
  55. NSMutableArray *selectOcId;
  56. BOOL livePhoto;
  57. }
  58. @end
  59. @implementation CCMain
  60. #pragma --------------------------------------------------------------------------------------------
  61. #pragma mark ===== Init =====
  62. #pragma --------------------------------------------------------------------------------------------
  63. - (id)initWithCoder:(NSCoder *)aDecoder
  64. {
  65. if (self = [super initWithCoder:aDecoder]) {
  66. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  67. appDelegate.activeMain = self;
  68. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initializeMain:) name:k_notificationCenter_initializeMain object:nil];
  69. }
  70. return self;
  71. }
  72. #pragma --------------------------------------------------------------------------------------------
  73. #pragma mark ===== View =====
  74. #pragma --------------------------------------------------------------------------------------------
  75. - (void)viewDidLoad
  76. {
  77. [super viewDidLoad];
  78. // init object
  79. self.metadata = [tableMetadata new];
  80. _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  81. _isViewDidLoad = YES;
  82. _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  83. _searchResultMetadatas = [NSMutableArray new];
  84. _searchFileName = @"";
  85. _noFilesSearchTitle = @"";
  86. _noFilesSearchDescription = @"";
  87. _cellFavouriteImage = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:[UIColor whiteColor]];
  88. _cellTrashImage = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor whiteColor]];
  89. selectOcId = [NSMutableArray new];
  90. // delegate
  91. self.tableView.tableFooterView = [UIView new];
  92. self.tableView.emptyDataSetDelegate = self;
  93. self.tableView.emptyDataSetSource = self;
  94. self.searchController.delegate = self;
  95. self.searchController.searchBar.delegate = self;
  96. // Notification
  97. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasource:) name:k_notificationCenter_reloadDataSource object:nil];
  98. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setTitle) name:k_notificationCenter_setTitleMain object:nil];
  99. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  100. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteFile:) name:k_notificationCenter_deleteFile object:nil];
  101. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(favoriteFile:) name:k_notificationCenter_favoriteFile object:nil];
  102. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moveFile:) name:k_notificationCenter_moveFile object:nil];
  103. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(copyFile:) name:k_notificationCenter_copyFile object:nil];
  104. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createFolder:) name:k_notificationCenter_createFolder object:nil];
  105. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_renameFile object:nil];
  106. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_downloadStartFile object:nil];
  107. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_downloadedFile object:nil];
  108. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_downloadCancelFile object:nil];
  109. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_uploadStartFile object:nil];
  110. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_uploadedFile object:nil];
  111. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_uploadCancelFile object:nil];
  112. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:k_notificationCenter_progressTask object:nil];
  113. // Search
  114. self.definesPresentationContext = YES;
  115. self.searchController.searchResultsUpdater = self;
  116. self.searchController.dimsBackgroundDuringPresentation = NO;
  117. UIButton *searchButton = self.searchController.searchBar.subviews.firstObject.subviews.lastObject;
  118. if (searchButton && [searchButton isKindOfClass:[UIButton class]]) {
  119. [searchButton setTitleColor:NCBrandColor.sharedInstance.brandElement forState:UIControlStateNormal];
  120. }
  121. UITextField *searchTextField = [self.searchController.searchBar valueForKey:@"searchField"];
  122. if (searchTextField && [searchTextField isKindOfClass:[UITextField class]]) {
  123. searchTextField.textColor = NCBrandColor.sharedInstance.textView;
  124. }
  125. // Load Rich Workspace
  126. self.viewRichWorkspace = [[[NSBundle mainBundle] loadNibNamed:@"NCRichWorkspace" owner:self options:nil] firstObject];
  127. UITapGestureRecognizer *viewRichWorkspaceTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewRichWorkspaceTapAction:)];
  128. viewRichWorkspaceTapped.numberOfTapsRequired = 1;
  129. viewRichWorkspaceTapped.delegate = self;
  130. [self.viewRichWorkspace.richView addGestureRecognizer:viewRichWorkspaceTapped];
  131. self.sortButton = self.viewRichWorkspace.sortButton;
  132. heightSearchBar = self.viewRichWorkspace.topView.frame.size.height;
  133. [self.sortButton setTitleColor:NCBrandColor.sharedInstance.brandElement forState:UIControlStateNormal];
  134. [self.sortButton addTarget:self action:@selector(toggleSortMenu) forControlEvents:UIControlEventTouchUpInside];
  135. heightRichWorkspace = UIScreen.mainScreen.bounds.size.height / 4 + heightSearchBar;
  136. [self.viewRichWorkspace setFrame:CGRectMake(0, 0, self.tableView.frame.size.width, heightRichWorkspace)];
  137. self.navigationItem.searchController = self.searchController;
  138. self.searchController.hidesNavigationBarDuringPresentation = true;
  139. self.navigationController.navigationBar.prefersLargeTitles = true;
  140. self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
  141. // Table Header View
  142. [self.tableView setTableHeaderView:self.viewRichWorkspace];
  143. // Register cell
  144. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMain" bundle:nil] forCellReuseIdentifier:@"CellMain"];
  145. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  146. self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 35, 0);
  147. // long press recognizer TableView
  148. UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressTableView:)];
  149. [self.tableView addGestureRecognizer:longPressRecognizer];
  150. // Pull-to-Refresh
  151. [self createRefreshControl];
  152. // Register for 3D Touch Previewing if available
  153. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) {
  154. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  155. }
  156. // if this is not Main (the Main uses inizializeMain)
  157. if (_isRoot == NO && appDelegate.account.length > 0) {
  158. // Read (File) Folder
  159. [self readFileReloadFolder];
  160. }
  161. // Title
  162. [self setTitle];
  163. // changeTheming
  164. [self changeTheming];
  165. }
  166. - (void)willDismissSearchController:(UISearchController *)searchController
  167. {
  168. [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
  169. }
  170. - (void)willPresentSearchController:(UISearchController *)searchController
  171. {
  172. [self updateNavBarShadow:self.tableView force:true];
  173. }
  174. - (void)viewWillAppear:(BOOL)animated
  175. {
  176. [super viewWillAppear:animated];
  177. [self updateNavBarShadow:self.tableView force:false];
  178. if(_isViewDidLoad && _isRoot) {
  179. self.navigationItem.hidesSearchBarWhenScrolling = false;
  180. [self.navigationController.navigationBar sizeToFit];
  181. }
  182. // test
  183. if (appDelegate.account.length == 0)
  184. return;
  185. if (_isSelectedMode)
  186. [self setUINavigationBarSelected];
  187. else
  188. [self setUINavigationBarDefault];
  189. // If not editing mode remove _selectedocIds
  190. if (!self.tableView.editing)
  191. [selectOcId removeAllObjects];
  192. // Check server URL "/"
  193. if (self.navigationController.viewControllers.firstObject == self && self.serverUrl == nil) {
  194. self.serverUrl = [[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account];
  195. }
  196. // RichWorkspace
  197. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.account, self.serverUrl]];
  198. if (![self.richWorkspaceText isEqualToString:directory.richWorkspace]) {
  199. self.richWorkspaceText = directory.richWorkspace;
  200. [self setTableViewHeader];
  201. }
  202. // Query data source
  203. if (self.searchController.isActive == false) {
  204. [self reloadDatasource:self.serverUrl ocId:nil];
  205. }
  206. appDelegate.activeServerUrl = self.serverUrl;
  207. appDelegate.activeViewController = self;
  208. }
  209. - (void)viewDidAppear:(BOOL)animated
  210. {
  211. [super viewDidAppear:animated];
  212. if(_isViewDidLoad && _isRoot) {
  213. self.navigationItem.hidesSearchBarWhenScrolling = true;
  214. }
  215. // Active Main
  216. appDelegate.activeMain = self;
  217. // Test viewDidLoad
  218. if (_isViewDidLoad) {
  219. _isViewDidLoad = NO;
  220. } else {
  221. if (appDelegate.account.length > 0 && [selectOcId count] == 0) {
  222. // Read (file) Folder
  223. [self readFileReloadFolder];
  224. }
  225. }
  226. // Title
  227. [self setTitle];
  228. }
  229. - (void)viewWillDisappear:(BOOL)animated
  230. {
  231. [super viewWillDisappear:animated];
  232. }
  233. - (void) viewDidLayoutSubviews
  234. {
  235. [super viewDidLayoutSubviews];
  236. [self setTableViewHeader];
  237. }
  238. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  239. {
  240. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  241. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  242. [self.tableView beginUpdates];
  243. [self.tableView endUpdates];
  244. [self setTableViewHeader];
  245. } completion:^(id<UIViewControllerTransitionCoordinatorContext> context){
  246. [self updateNavBarShadow:self.tableView force:false];
  247. }];
  248. }
  249. - (void)presentationControllerWillDismiss:(UIPresentationController *)presentationController
  250. {
  251. [self viewDidAppear:true];
  252. }
  253. - (BOOL)prefersStatusBarHidden
  254. {
  255. return NO;
  256. }
  257. // detect scroll for remove keyboard in search mode
  258. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  259. {
  260. if (self.searchController.isActive && scrollView == self.tableView) {
  261. [self.searchController.searchBar endEditing:YES];
  262. }
  263. }
  264. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  265. [self updateNavBarShadow:self.tableView force:false];
  266. }
  267. - (void)changeTheming
  268. {
  269. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:false];
  270. // Refresh control
  271. refreshControl.tintColor = UIColor.lightGrayColor;
  272. refreshControl.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  273. [self.sortButton setTitleColor:NCBrandColor.sharedInstance.brandElement forState:UIControlStateNormal];
  274. // color searchbar
  275. self.searchController.searchBar.tintColor = NCBrandColor.sharedInstance.brandElement;
  276. // color searchbbar button text (cancel)
  277. UIButton *searchButton = self.searchController.searchBar.subviews.firstObject.subviews.lastObject;
  278. if (searchButton && [searchButton isKindOfClass:[UIButton class]]) {
  279. [searchButton setTitleColor:NCBrandColor.sharedInstance.brandElement forState:UIControlStateNormal];
  280. }
  281. // color textview searchbbar
  282. UITextField *searchTextView = [self.searchController.searchBar valueForKey:@"searchField"];
  283. if (searchTextView && [searchTextView isKindOfClass:[UITextField class]]) {
  284. searchTextView.textColor = NCBrandColor.sharedInstance.textView;
  285. }
  286. // Rich Workspace
  287. [self.viewRichWorkspace loadWithRichWorkspaceText:self.richWorkspaceText];
  288. // Title
  289. [self setTitle];
  290. // Reload Table View
  291. [self tableViewReloadData];
  292. }
  293. #pragma --------------------------------------------------------------------------------------------
  294. #pragma mark ===== Initialization =====
  295. #pragma --------------------------------------------------------------------------------------------
  296. - (void)initializeMain:(NSNotification *)notification
  297. {
  298. // test
  299. if (appDelegate.account.length == 0)
  300. return;
  301. if ([appDelegate.listMainVC count] == 0 || _isRoot) {
  302. // This is Root home main add list
  303. _isRoot = YES;
  304. _serverUrl = [[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account];
  305. [appDelegate.listMainVC setObject:self forKey:_serverUrl];
  306. // go Home
  307. [self.navigationController popToRootViewControllerAnimated:NO];
  308. // Remove search mode
  309. [self cancelSearchBar];
  310. // Read this folder
  311. [self readFileReloadFolder];
  312. } else {
  313. // reload datasource
  314. [self reloadDatasource:_serverUrl ocId:nil];
  315. }
  316. }
  317. #pragma --------------------------------------------------------------------------------------------
  318. #pragma mark ==== NotificationCenter ====
  319. #pragma --------------------------------------------------------------------------------------------
  320. - (void)createFolder:(NSNotification *)notification
  321. {
  322. if (self.view.window == nil) { return; }
  323. NSDictionary *userInfo = notification.userInfo;
  324. tableMetadata *metadata = userInfo[@"metadata"];
  325. if (metadata) {
  326. if (![metadata.serverUrl isEqualToString:self.serverUrl]) { return; }
  327. [self reloadDatasource:self.serverUrl ocId:nil];
  328. } else {
  329. [self readFolder:self.serverUrl];
  330. }
  331. }
  332. - (void)deleteFile:(NSNotification *)notification
  333. {
  334. if (self.view.window == nil) { return; }
  335. NSDictionary *userInfo = notification.userInfo;
  336. tableMetadata *metadata = userInfo[@"metadata"];
  337. if (![metadata.serverUrl isEqualToString:self.serverUrl]) { return; }
  338. [self reloadDatasource:self.serverUrl ocId:nil];
  339. }
  340. - (void)moveFile:(NSNotification *)notification
  341. {
  342. if (self.view.window == nil) { return; }
  343. NSDictionary *userInfo = notification.userInfo;
  344. tableMetadata *metadata = userInfo[@"metadata"];
  345. tableMetadata *metadataNew = userInfo[@"metadataNew"];
  346. if (![metadata.serverUrl isEqualToString:self.serverUrl] && ![metadataNew.serverUrl isEqualToString:self.serverUrl]) { return; }
  347. [self reloadDatasource:self.serverUrl ocId:nil];
  348. }
  349. - (void)copyFile:(NSNotification *)notification
  350. {
  351. if (self.view.window == nil) { return; }
  352. NSDictionary *userInfo = notification.userInfo;
  353. tableMetadata *metadata = userInfo[@"metadata"];
  354. NSString *serverUrlTo = userInfo[@"serverUrlTo"];
  355. if (![metadata.serverUrl isEqualToString:self.serverUrl] && ![serverUrlTo isEqualToString:self.serverUrl]) { return; }
  356. [self reloadDatasource:self.serverUrl ocId:nil];
  357. }
  358. - (void)favoriteFile:(NSNotification *)notification
  359. {
  360. if (self.view.window == nil) { return; }
  361. NSDictionary *userInfo = notification.userInfo;
  362. tableMetadata *metadata = userInfo[@"metadata"];
  363. if (![metadata.serverUrl isEqualToString:self.serverUrl]) { return; }
  364. if (self.searchController.isActive) {
  365. [self readFolder:self.serverUrl];
  366. } else {
  367. [self reloadDatasource:self.serverUrl ocId:nil];
  368. }
  369. }
  370. #pragma --------------------------------------------------------------------------------------------
  371. #pragma mark ==== DZNEmptyDataSetSource ====
  372. #pragma --------------------------------------------------------------------------------------------
  373. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
  374. {
  375. if (_loadingFolder)
  376. return NO;
  377. else
  378. return YES;
  379. }
  380. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  381. {
  382. CGFloat height = self.tabBarController.tabBar.frame.size.height;
  383. return -height;
  384. }
  385. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  386. {
  387. return NCBrandColor.sharedInstance.backgroundView;
  388. }
  389. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  390. {
  391. if (self.searchController.isActive)
  392. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"search"] width:300 height:300 color:NCBrandColor.sharedInstance.brandElement];
  393. else
  394. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] width:300 height:300 color:NCBrandColor.sharedInstance.brandElement];
  395. }
  396. - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
  397. {
  398. if (_loadingFolder && refreshControl.isRefreshing == NO) {
  399. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  400. activityView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
  401. activityView.color = NCBrandColor.sharedInstance.brandElement;
  402. [activityView startAnimating];
  403. return activityView;
  404. }
  405. return nil;
  406. }
  407. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  408. {
  409. NSString *text;
  410. if (self.searchController.isActive) {
  411. text = _noFilesSearchTitle;
  412. } else {
  413. text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_files_no_files_", nil)];
  414. }
  415. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  416. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  417. }
  418. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  419. {
  420. NSString *text;
  421. if (self.searchController.isActive) {
  422. text = _noFilesSearchDescription;
  423. } else {
  424. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_no_file_pull_down_", nil)];
  425. }
  426. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  427. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  428. paragraph.alignment = NSTextAlignmentCenter;
  429. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  430. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  431. }
  432. #pragma --------------------------------------------------------------------------------------------
  433. #pragma mark ===== Graphic Window =====
  434. #pragma --------------------------------------------------------------------------------------------
  435. - (void)createRefreshControl
  436. {
  437. refreshControl = [NCMainRefreshControl new];
  438. self.tableView.refreshControl = refreshControl;
  439. refreshControl.tintColor = UIColor.lightGrayColor;
  440. refreshControl.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  441. [refreshControl addTarget:self action:@selector(refreshControlTarget) forControlEvents:UIControlEventValueChanged];
  442. }
  443. - (void)deleteRefreshControl
  444. {
  445. [refreshControl endRefreshing];
  446. [refreshControl removeFromSuperview];
  447. refreshControl = nil;
  448. }
  449. - (void)refreshControlTarget
  450. {
  451. [self readFolder:_serverUrl];
  452. // Actuate `Peek` feedback (weak boom)
  453. AudioServicesPlaySystemSound(1519);
  454. }
  455. - (void)setTitle
  456. {
  457. if (_isSelectedMode) {
  458. NSUInteger totali = [sectionDataSource.allRecordsDataSource count];
  459. NSUInteger selezionati = [[self.tableView indexPathsForSelectedRows] count];
  460. self.navigationItem.titleView = nil;
  461. self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)selezionati, (unsigned long)totali];
  462. } else {
  463. if (_isRoot) {
  464. self.navigationItem.title = NCBrandOptions.sharedInstance.brand;
  465. } else {
  466. self.navigationItem.title = _titleMain;
  467. }
  468. }
  469. NSString *titleButtonMenuSort = [[NCUtility shared] getTitleButtonForViewWithKey:k_layout_view_main];
  470. [self.sortButton setTitle:NSLocalizedString(titleButtonMenuSort, nil) forState:UIControlStateNormal];
  471. }
  472. - (void)setUINavigationBarDefault
  473. {
  474. UIBarButtonItem *buttonSelect = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_select_", @"") style:UIBarButtonItemStylePlain target:self action:@selector(tableViewToggle)];
  475. self.navigationController.navigationBar.hidden = NO;
  476. self.navigationItem.rightBarButtonItem = buttonSelect;
  477. self.navigationItem.leftBarButtonItem = nil;
  478. }
  479. - (void)setUINavigationBarSelected
  480. {
  481. UIBarButtonItem *buttonMore = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationMore"] style:UIBarButtonItemStylePlain target:self action:@selector(toggleSelectMenu)];
  482. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelSelect)];
  483. self.navigationItem.leftBarButtonItem = leftButton;
  484. self.navigationItem.rightBarButtonItem = buttonMore; //[[NSArray alloc] initWithObjects:buttonMore, nil];
  485. }
  486. - (void)cancelSelect
  487. {
  488. [self tableViewSelect:false];
  489. }
  490. #pragma --------------------------------------------------------------------------------------------
  491. #pragma mark ===== Save selected File =====
  492. #pragma --------------------------------------------------------------------------------------------
  493. - (void)saveToPhotoAlbum:(tableMetadata *)metadata
  494. {
  495. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView];
  496. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
  497. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] && status == PHAuthorizationStatusAuthorized) {
  498. UIImage *image = [UIImage imageWithContentsOfFile:fileNamePath];
  499. if (image)
  500. UIImageWriteToSavedPhotosAlbum(image, self, @selector(saveSelectedFilesSelector: didFinishSavingWithError: contextInfo:), nil);
  501. else
  502. [[NCContentPresenter shared] messageNotification:@"_save_selected_files_" description:@"_file_not_saved_cameraroll_" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorFileNotSaved forced:false];
  503. }
  504. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video] && status == PHAuthorizationStatusAuthorized) {
  505. if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(fileNamePath)) {
  506. UISaveVideoAtPathToSavedPhotosAlbum(fileNamePath, self, @selector(saveSelectedFilesSelector: didFinishSavingWithError: contextInfo:), nil);
  507. } else {
  508. [[NCContentPresenter shared] messageNotification:@"_save_selected_files_" description:@"_file_not_saved_cameraroll_" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorFileNotSaved forced:false];
  509. }
  510. }
  511. if (status != PHAuthorizationStatusAuthorized) {
  512. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_access_photo_not_enabled_", nil) message:NSLocalizedString(@"_access_photo_not_enabled_msg_", nil) preferredStyle:UIAlertControllerStyleAlert];
  513. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  514. [alertController addAction:okAction];
  515. [self presentViewController:alertController animated:YES completion:nil];
  516. }
  517. }
  518. - (void)saveSelectedFilesSelector:(NSString *)path didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  519. {
  520. if (error) {
  521. [[NCContentPresenter shared] messageNotification:@"_save_selected_files_" description:@"_file_not_saved_cameraroll_" delay:k_dismissAfterSecond type:messageTypeError errorCode:error.code forced:false];
  522. }
  523. }
  524. - (void)saveSelectedFiles
  525. {
  526. if (_isSelectedMode && [selectOcId count] == 0)
  527. return;
  528. [_hud visibleHudTitle:@"" mode:MBProgressHUDModeIndeterminate color:nil];
  529. NSArray *metadatas = [self getMetadatasFromSelectedRows:[self.tableView indexPathsForSelectedRows]];
  530. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  531. for (tableMetadata *metadata in metadatas) {
  532. if (metadata.directory == NO && ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])) {
  533. [[NCOperationQueue shared] downloadWithMetadata:metadata selector:selectorSaveAlbum setFavorite:false];
  534. }
  535. }
  536. [_hud hideHud];
  537. });
  538. [self tableViewSelect:false];
  539. }
  540. #pragma mark -
  541. #pragma --------------------------------------------------------------------------------------------
  542. #pragma mark ===== Peek & Pop =====
  543. #pragma --------------------------------------------------------------------------------------------
  544. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  545. {
  546. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  547. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  548. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  549. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  550. if (cell) {
  551. previewingContext.sourceRect = cell.frame;
  552. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  553. viewController.metadata = metadata;
  554. viewController.imageFile = cell.file.image;
  555. viewController.showOpenIn = true;
  556. viewController.showShare = true;
  557. viewController.showOpenQuickLook = [[NCUtility shared] isQuickLookDisplayableWithMetadata:metadata];
  558. return viewController;
  559. }
  560. return nil;
  561. }
  562. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  563. {
  564. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  565. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  566. }
  567. #pragma --------------------------------------------------------------------------------------------
  568. #pragma mark ==== Read Folder ====
  569. #pragma --------------------------------------------------------------------------------------------
  570. - (void)readFolder:(NSString *)serverUrl
  571. {
  572. // init control
  573. if (!serverUrl || !appDelegate.account || appDelegate.maintenanceMode) {
  574. [refreshControl endRefreshing];
  575. return;
  576. }
  577. // Search Mode
  578. if (self.searchController.isActive) {
  579. _searchFileName = @""; // forced reload searchg
  580. [self updateSearchResultsForSearchController:self.searchController];
  581. return;
  582. }
  583. _loadingFolder = YES;
  584. [refreshControl endRefreshing];
  585. [self tableViewReloadData];
  586. [[NCNetworking shared] readFolderWithServerUrl:serverUrl account:appDelegate.account completion:^(NSString *account, tableMetadata *metadataFolder, NSArray *metadatas, NSArray *metadatasUpdate, NSArray *metadatasLocalUpdate, NSInteger errorCode, NSString *errorDescription) {
  587. if (errorCode == 0 ) {
  588. _metadataFolder = metadataFolder;
  589. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl e2eEncrypted:_metadataFolder.e2eEncrypted account:appDelegate.account urlBase:_metadataFolder.urlBase];
  590. [self setTitle];
  591. for (tableMetadata *metadata in metadatasLocalUpdate) {
  592. if (!metadata.directory) {
  593. [[NCNetworking shared] downloadWithMetadata:metadata selector:selectorDownloadFile setFavorite:false completion:^(NSInteger errorCode) { }];
  594. }
  595. }
  596. // E2EE Is encrypted folder get metadata
  597. if (isFolderEncrypted) {
  598. if ([CCUtility isEndToEndEnabled:account]) {
  599. [[NCCommunication shared] getE2EEMetadataWithFileId:metadataFolder.fileId e2eToken:nil customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSString *e2eMetadata, NSInteger errorCode, NSString *errorDescription) {
  600. if (errorCode == 0 && e2eMetadata != nil) {
  601. BOOL result = [[NCEndToEndMetadata sharedInstance] decoderMetadata:e2eMetadata privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:self.serverUrl account:account urlBase:appDelegate.urlBase];
  602. if (result == false) {
  603. [[NCContentPresenter shared] messageNotification:@"_error_e2ee_" description:@"_e2e_error_decode_metadata_" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorDecodeMetadata forced:true];
  604. }
  605. } else if (errorCode != 404) {
  606. [[NCContentPresenter shared] messageNotification:@"_e2e_error_get_metadata_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:true];
  607. }
  608. [self reloadDatasource:_serverUrl ocId:nil];
  609. }];
  610. } else {
  611. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorE2EENotEnabled forced:true];
  612. }
  613. }
  614. } else {
  615. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:true];
  616. }
  617. _loadingFolder = NO;
  618. [self reloadDatasource:serverUrl ocId:nil];
  619. }];
  620. }
  621. - (void)readFileReloadFolder
  622. {
  623. if (!_serverUrl || !appDelegate.account || appDelegate.maintenanceMode)
  624. return;
  625. [[NCNetworking shared] readFileWithServerUrlFileName:self.serverUrl account:appDelegate.account completion:^(NSString *account, tableMetadata *metadata, NSInteger errorCode, NSString *errorDescription) {
  626. if (errorCode == 0 && [account isEqualToString:appDelegate.account]) {
  627. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, self.serverUrl]];
  628. // Rich Workspace
  629. if (![self.richWorkspaceText isEqualToString:directory.richWorkspace]) {
  630. self.richWorkspaceText = directory.richWorkspace;
  631. }
  632. [self setTableViewHeader];
  633. // Read folder: No record, Change etag or BLINK
  634. if ([sectionDataSource.allRecordsDataSource count] == 0 || [metadata.etag isEqualToString:directory.etag] == NO || self.blinkFileNamePath != nil) {
  635. [self readFolder:self.serverUrl];
  636. }
  637. } else if (errorCode != 0) {
  638. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:false];
  639. }
  640. }];
  641. }
  642. #pragma mark -
  643. #pragma --------------------------------------------------------------------------------------------
  644. #pragma mark ===== Search =====
  645. #pragma --------------------------------------------------------------------------------------------
  646. - (void)searchStartTimer
  647. {
  648. if (self.searchController.isActive == false) {
  649. return;
  650. }
  651. [[NCCommunication shared] searchLiteralWithServerUrl:appDelegate.urlBase depth:@"infinity" literal:_searchFileName showHiddenFiles:[CCUtility getShowHiddenFiles] customUserAgent:nil addCustomHeaders:nil timeout:120 completionHandler:^(NSString *account, NSArray *files, NSInteger errorCode, NSString *errorDescription) {
  652. if (errorCode == 0 && [account isEqualToString:appDelegate.account] && files != nil) {
  653. [[NCManageDatabase sharedInstance] convertNCCommunicationFilesToMetadatas:files useMetadataFolder:false account:account completion:^(tableMetadata *metadataFolder, NSArray<tableMetadata *> *metadatasFolder, NSArray<tableMetadata *> *metadatas) {
  654. [[NCManageDatabase sharedInstance] addMetadatas:metadatas];
  655. _searchResultMetadatas = [[NSMutableArray alloc] initWithArray:metadatas];
  656. _metadataFolder = nil;
  657. [self reloadDatasource:_serverUrl ocId:nil];
  658. [self tableViewReloadData];
  659. [self setTitle];
  660. }];
  661. } else {
  662. if (errorCode != 0) {
  663. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:true];
  664. }
  665. _searchFileName = @"";
  666. [self cancelSearchBar];
  667. }
  668. }];
  669. _noFilesSearchTitle = @"";
  670. _noFilesSearchDescription = NSLocalizedString(@"_search_in_progress_", nil);
  671. [self.tableView reloadEmptyDataSet];
  672. }
  673. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  674. {
  675. // Color text "Cancel"
  676. [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:NCBrandColor.sharedInstance.brandElement];
  677. if (searchController.isActive) {
  678. [self deleteRefreshControl];
  679. NSString *fileName = [CCUtility removeForbiddenCharactersServer:searchController.searchBar.text];
  680. if (fileName.length >= k_minCharsSearch && [fileName isEqualToString:_searchFileName] == NO) {
  681. _searchFileName = fileName;
  682. _metadataFolder = nil;
  683. // First : filter
  684. NSArray *records = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileNameView CONTAINS[cd] %@", appDelegate.account, _serverUrl, fileName] page:0 limit:0 sorted:@"fileName" ascending:NO];
  685. [_searchResultMetadatas removeAllObjects];
  686. for (tableMetadata *record in records) {
  687. [_searchResultMetadatas addObject:record];
  688. }
  689. // Version >= 12
  690. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:appDelegate.account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  691. if (serverVersionMajor >= 12) {
  692. [_timerWaitInput invalidate];
  693. _timerWaitInput = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(searchStartTimer) userInfo:nil repeats:NO];
  694. }
  695. [self setTitle];
  696. }
  697. if (_searchResultMetadatas.count == 0 && fileName.length == 0) {
  698. [self reloadDatasource:_serverUrl ocId:nil];
  699. }
  700. } else {
  701. [self createRefreshControl];
  702. [self reloadDatasource:_serverUrl ocId:nil];
  703. }
  704. }
  705. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
  706. {
  707. [self cancelSearchBar];
  708. }
  709. - (void)cancelSearchBar
  710. {
  711. if (self.searchController.active) {
  712. [self.searchController setActive:NO];
  713. _searchFileName = @"";
  714. _searchResultMetadatas = [NSMutableArray new];
  715. [self reloadDatasource:_serverUrl ocId:nil];
  716. }
  717. }
  718. #pragma mark -
  719. #pragma --------------------------------------------------------------------------------------------
  720. #pragma mark ===== Delete File or Folder =====
  721. #pragma --------------------------------------------------------------------------------------------
  722. - (void)deleteMetadatas
  723. {
  724. if (_isSelectedMode) {
  725. for (NSString *ocId in selectOcId) {
  726. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataFromOcId:ocId];
  727. if (metadata) {
  728. [[NCOperationQueue shared] deleteWithMetadata:metadata onlyLocal:false];
  729. }
  730. }
  731. [selectOcId removeAllObjects];
  732. [self tableViewSelect:false];
  733. } else {
  734. [[NCOperationQueue shared] deleteWithMetadata:self.metadata onlyLocal:false];
  735. }
  736. }
  737. // DELEGATE : Select
  738. - (void)dismissSelectWithServerUrl:(NSString *)serverUrl metadata:(tableMetadata *)metadata type:(NSString *)type array:(NSArray *)array buttonType:(NSString *)buttonType overwrite:(BOOL)overwrite
  739. {
  740. if (serverUrl != nil) {
  741. // E2EE DENIED
  742. if ([CCUtility isFolderEncrypted:serverUrl e2eEncrypted:metadata.e2eEncrypted account:appDelegate.account urlBase:appDelegate.urlBase]) {
  743. [[NCContentPresenter shared] messageNotification:@"_move_" description:@"_e2e_error_not_move_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorE2EENotMove forced:true];
  744. return;
  745. }
  746. BOOL move = true;
  747. if ([buttonType isEqualToString:@"done1"]) { move = false; }
  748. if (_isSelectedMode) {
  749. for (NSString *ocId in selectOcId) {
  750. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataFromOcId:ocId];
  751. if (metadata) {
  752. [[NCOperationQueue shared] copyMoveWithMetadata:metadata serverUrl:serverUrl overwrite:overwrite move:move];
  753. }
  754. }
  755. [selectOcId removeAllObjects];
  756. [self tableViewSelect:false];
  757. } else {
  758. [[NCOperationQueue shared] copyMoveWithMetadata:self.metadata serverUrl:serverUrl overwrite:overwrite move:move];
  759. }
  760. }
  761. }
  762. - (void)moveOpenWindow:(NSArray *)indexPaths
  763. {
  764. if (_isSelectedMode && [selectOcId count] == 0)
  765. return;
  766. UINavigationController *navigationController = [[UIStoryboard storyboardWithName:@"NCSelect" bundle:nil] instantiateInitialViewController];
  767. NCSelect *viewController = (NCSelect *)navigationController.topViewController;
  768. viewController.delegate = self;
  769. viewController.hideButtonCreateFolder = false;
  770. viewController.selectFile = false;
  771. viewController.includeDirectoryE2EEncryption = false;
  772. viewController.includeImages = false;
  773. viewController.type = @"";
  774. viewController.titleButtonDone = NSLocalizedString(@"_move_", nil);
  775. viewController.titleButtonDone1 = NSLocalizedString(@"_copy_", nil);
  776. viewController.isButtonDone1Hide = false;
  777. viewController.isOverwriteHide = false;
  778. viewController.keyLayout = k_layout_view_move;
  779. viewController.array = selectOcId;
  780. [navigationController setModalPresentationStyle:UIModalPresentationFullScreen];
  781. [self presentViewController:navigationController animated:YES completion:nil];
  782. }
  783. #pragma --------------------------------------------------------------------------------------------
  784. #pragma mark ===== Progress & Task Button =====
  785. #pragma --------------------------------------------------------------------------------------------
  786. - (void)triggerProgressTask:(NSNotification *)notification
  787. {
  788. if (sectionDataSource.ocIdIndexPath != nil) {
  789. [[NCMainCommon shared] triggerProgressTask:notification sectionDataSourceocIdIndexPath:sectionDataSource.ocIdIndexPath tableView:self.tableView viewController:self serverUrlViewController:self.serverUrl];
  790. }
  791. }
  792. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  793. {
  794. UITouch *touch = [[event allTouches] anyObject];
  795. CGPoint location = [touch locationInView:self.tableView];
  796. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  797. if ([self indexPathIsValid:indexPath]) {
  798. tableMetadata *metadataSection = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  799. if (metadataSection) {
  800. [[NCNetworking shared] cancelTransferMetadata:metadataSection completion:^{ }];
  801. }
  802. }
  803. }
  804. - (void)cancelAllTask:(id)sender
  805. {
  806. CGPoint location = [sender locationInView:self.tableView];
  807. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  808. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  809. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  810. [NCUtility.shared startActivityIndicatorWithView:self.view bottom:0];
  811. [[NCNetworking shared] cancelAllTransferWithAccount:appDelegate.account completion:^{
  812. [self reloadDatasource:self.serverUrl ocId:nil];
  813. }];
  814. [NCUtility.shared stopActivityIndicator];
  815. }]];
  816. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  817. alertController.popoverPresentationController.sourceView = self.tableView;
  818. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  819. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  820. [alertController.view layoutIfNeeded];
  821. [self presentViewController:alertController animated:YES completion:nil];
  822. }
  823. #pragma --------------------------------------------------------------------------------------------
  824. #pragma mark ===== Tap =====
  825. #pragma --------------------------------------------------------------------------------------------
  826. - (void)tapActionComment:(UITapGestureRecognizer *)tapGesture
  827. {
  828. CGPoint location = [tapGesture locationInView:self.tableView];
  829. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  830. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  831. if (metadata && ![CCUtility isFolderEncrypted:self.serverUrl e2eEncrypted:metadata.e2eEncrypted account:appDelegate.account urlBase: appDelegate.urlBase]) {
  832. [[NCNetworkingNotificationCenter shared] openShareWithViewController:self metadata:metadata indexPage:1];
  833. }
  834. }
  835. - (void)tapActionShared:(UITapGestureRecognizer *)tapGesture
  836. {
  837. CGPoint location = [tapGesture locationInView:self.tableView];
  838. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  839. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  840. if (metadata && ![CCUtility isFolderEncrypted:self.serverUrl e2eEncrypted:metadata.e2eEncrypted account:appDelegate.account urlBase:appDelegate.urlBase]) {
  841. [[NCNetworkingNotificationCenter shared] openShareWithViewController:self metadata:metadata indexPage:2];
  842. }
  843. }
  844. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  845. {
  846. CGPoint location = [tapGesture locationInView:self.tableView];
  847. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  848. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  849. if (metadata) {
  850. }
  851. }
  852. #pragma --------------------------------------------------------------------------------------------
  853. #pragma mark ===== Rich Workspace =====
  854. #pragma --------------------------------------------------------------------------------------------
  855. - (void)viewRichWorkspaceTapAction:(UITapGestureRecognizer *)tapGesture
  856. {
  857. UINavigationController *navigationController = [[UIStoryboard storyboardWithName:@"NCViewerRichWorkspace" bundle:nil] instantiateInitialViewController];
  858. NCViewerRichWorkspace *viewerRichWorkspace = (NCViewerRichWorkspace *)[navigationController topViewController];
  859. viewerRichWorkspace.richWorkspaceText = self.richWorkspaceText;
  860. viewerRichWorkspace.serverUrl = self.serverUrl;
  861. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  862. [self presentViewController:navigationController animated:NO completion:NULL];
  863. }
  864. - (void)toggleSortMenu
  865. {
  866. NCSortMenu *sortMenu = [NCSortMenu new];
  867. [sortMenu toggleMenuWithViewController:self key:k_layout_view_main sortButton:self.sortButton serverUrl:self.serverUrl hideDirectoryOnTop:false];
  868. }
  869. - (void)toggleSelectMenu
  870. {
  871. [self toggleSelectMenuWithViewController:self.navigationController];
  872. }
  873. #pragma --------------------------------------------------------------------------------------------
  874. #pragma mark ===== Long Press Recognized Table View / Menu Controller =====
  875. #pragma --------------------------------------------------------------------------------------------
  876. - (void)onLongPressTableView:(UILongPressGestureRecognizer*)recognizer
  877. {
  878. if (recognizer.state == UIGestureRecognizerStateBegan) {
  879. CGPoint touchPoint = [recognizer locationInView:self.tableView];
  880. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
  881. NSMutableArray *items = [NSMutableArray new];
  882. if ([self indexPathIsValid:indexPath])
  883. self.metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  884. [self becomeFirstResponder];
  885. UIMenuController *menuController = [UIMenuController sharedMenuController];
  886. [items addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"_copy_file_", nil) action:@selector(copyTouchFile:)]];
  887. [items addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"_copy_files_", nil) action:@selector(copyTouchFiles:)]];
  888. if ([NCBrandOptions sharedInstance].disable_openin_file == false) {
  889. [items addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"_open_in_", nil) action:@selector(openinTouchFile:)]];
  890. }
  891. if ([[NCUtility shared] isQuickLookDisplayableWithMetadata:self.metadata]) {
  892. [items addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"_open_quicklook_", nil) action:@selector(openQuickLookTouch:)]];
  893. }
  894. [items addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"_paste_file_", nil) action:@selector(pasteTouchFile:)]];
  895. [items addObject:[[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"_paste_files_", nil) action:@selector(pasteTouchFiles:)]];
  896. [menuController setMenuItems:items];
  897. [menuController setTargetRect:CGRectMake(touchPoint.x, touchPoint.y, 0.0f, 0.0f) inView:self.tableView];
  898. [menuController setMenuVisible:YES animated:YES];
  899. }
  900. }
  901. - (BOOL)canBecomeFirstResponder
  902. {
  903. return YES;
  904. }
  905. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  906. {
  907. // For copy file, copy files, Open in ... :
  908. //
  909. // NO Directory
  910. // NO Error Passcode
  911. // NO In Session mode (download/upload)
  912. // NO Template
  913. if (@selector(copyTouchFile:) == action || @selector(openinTouchFile:) == action || @selector(openQuickLookTouch:) == action) {
  914. if (_isSelectedMode == NO && self.metadata && !self.metadata.directory && self.metadata.status == k_metadataStatusNormal) return YES;
  915. else return NO;
  916. }
  917. if (@selector(copyTouchFiles:) == action) {
  918. if (_isSelectedMode) {
  919. NSArray *selectedMetadatas = [self getMetadatasFromSelectedRows:[self.tableView indexPathsForSelectedRows]];
  920. for (tableMetadata *metadata in selectedMetadatas) {
  921. if (!metadata.directory && metadata.status == k_metadataStatusNormal)
  922. return YES;
  923. }
  924. }
  925. return NO;
  926. }
  927. if (@selector(pasteTouchFile:) == action) {
  928. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  929. NSArray *items = [pasteboard items];
  930. if ([items count] == 1) {
  931. // Value : (NSData) ocId
  932. NSDictionary *dic = [items objectAtIndex:0];
  933. NSData *dataocId = [dic objectForKey: k_metadataKeyedUnarchiver];
  934. NSString *ocId = [NSKeyedUnarchiver unarchiveObjectWithData:dataocId];
  935. if (ocId) {
  936. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", ocId]];
  937. if (metadata) {
  938. return [CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView];
  939. } else {
  940. return NO;
  941. }
  942. }
  943. }
  944. return NO;
  945. }
  946. if (@selector(pasteTouchFiles:) == action) {
  947. BOOL isValid = NO;
  948. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  949. NSArray *items = [pasteboard items];
  950. if ([items count] <= 1) return NO;
  951. for (NSDictionary *dic in items) {
  952. // Value : (NSData) ocId
  953. NSData *dataocId = [dic objectForKey: k_metadataKeyedUnarchiver];
  954. NSString *ocId = [NSKeyedUnarchiver unarchiveObjectWithData:dataocId];
  955. if (ocId) {
  956. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataFromOcId:ocId];
  957. if (metadata) {
  958. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView]) {
  959. isValid = YES;
  960. } else {
  961. isValid = NO;
  962. break;
  963. }
  964. } else {
  965. isValid = NO;
  966. break;
  967. }
  968. } else {
  969. isValid = NO;
  970. break;
  971. }
  972. }
  973. return isValid;
  974. }
  975. return NO;
  976. }
  977. /************************************ COPY ************************************/
  978. - (void)copyTouchFile:(id)sender
  979. {
  980. // Remove all item
  981. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  982. pasteboard.items = [[NSArray alloc] init];
  983. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  984. [self copyFileToPasteboard:self.metadata];
  985. } else {
  986. [[NCNetworking shared] downloadWithMetadata:self.metadata selector:selectorLoadCopy setFavorite:false completion:^(NSInteger errorCode) { }];
  987. }
  988. }
  989. - (void)copyTouchFiles:(id)sender
  990. {
  991. // Remove all item
  992. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  993. pasteboard.items = [[NSArray alloc] init];
  994. NSArray *selectedMetadatas = [self getMetadatasFromSelectedRows:[self.tableView indexPathsForSelectedRows]];
  995. for (tableMetadata *metadata in selectedMetadatas) {
  996. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView]) {
  997. [self copyFileToPasteboard:metadata];
  998. } else {
  999. [[NCNetworking shared] downloadWithMetadata:metadata selector:selectorLoadCopy setFavorite:false completion:^(NSInteger errorCode) { }];
  1000. }
  1001. }
  1002. [self tableViewSelect:false];
  1003. }
  1004. - (void)copyFileToPasteboard:(tableMetadata *)metadata
  1005. {
  1006. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  1007. NSMutableArray *items = [[NSMutableArray alloc] initWithArray:pasteboard.items];
  1008. // Value : (NSData) ocId
  1009. NSDictionary *item = [NSDictionary dictionaryWithObjectsAndKeys:[NSKeyedArchiver archivedDataWithRootObject:metadata.ocId], k_metadataKeyedUnarchiver,nil];
  1010. [items addObject:item];
  1011. [pasteboard setItems:items];
  1012. }
  1013. /************************************ OPEN IN ... ******************************/
  1014. - (void)openinTouchFile:(id)sender
  1015. {
  1016. [[NCNetworkingNotificationCenter shared] downloadOpenWithMetadata:self.metadata selector:selectorOpenIn];
  1017. }
  1018. /************************************ OPEN QUICK LOOK ******************************/
  1019. - (void)openQuickLookTouch:(id)sender
  1020. {
  1021. [[NCNetworkingNotificationCenter shared] downloadOpenWithMetadata:self.metadata selector:selectorLoadFileQuickLook];
  1022. }
  1023. /************************************ PASTE ************************************/
  1024. - (void)pasteTouchFile:(id)sender
  1025. {
  1026. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  1027. [self uploadFilePasteArray:[pasteboard items]];
  1028. }
  1029. - (void)pasteTouchFiles:(id)sender
  1030. {
  1031. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  1032. [self uploadFilePasteArray:[pasteboard items]];
  1033. }
  1034. - (void)uploadFilePasteArray:(NSArray *)items
  1035. {
  1036. for (NSDictionary *dic in items) {
  1037. // Value : (NSData) ocId
  1038. NSData *dataocId = [dic objectForKey: k_metadataKeyedUnarchiver];
  1039. NSString *ocId = [NSKeyedUnarchiver unarchiveObjectWithData:dataocId];
  1040. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataFromOcId:ocId];
  1041. if (metadata) {
  1042. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView]) {
  1043. NSString *fileName = [[NCUtility shared] createFileName:metadata.fileNameView serverUrl:self.serverUrl account:appDelegate.account];
  1044. NSString *ocId = [[NSUUID UUID] UUIDString];
  1045. [CCUtility copyFileAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView] toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileName]];
  1046. // Prepare record metadata
  1047. tableMetadata *metadataForUpload = [[NCManageDatabase sharedInstance] createMetadataWithAccount:appDelegate.account fileName:fileName ocId:ocId serverUrl:self.serverUrl urlBase:appDelegate.urlBase url:@"" contentType:@"" livePhoto:false];
  1048. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground;
  1049. metadataForUpload.sessionSelector = selectorUploadFile;
  1050. metadataForUpload.size = metadata.size;
  1051. metadataForUpload.status = k_metadataStatusWaitUpload;
  1052. // Add Medtadata for upload
  1053. [[NCManageDatabase sharedInstance] addMetadata:metadataForUpload];
  1054. }
  1055. }
  1056. }
  1057. [[appDelegate networkingAutoUpload] startProcess];
  1058. }
  1059. #pragma mark -
  1060. #pragma --------------------------------------------------------------------------------------------
  1061. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  1062. #pragma --------------------------------------------------------------------------------------------
  1063. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  1064. {
  1065. if (metadata == nil)
  1066. return NO;
  1067. // E2EE
  1068. if (_metadataFolder.e2eEncrypted && [CCUtility isEndToEndEnabled:appDelegate.account] == NO)
  1069. return NO;
  1070. return YES;
  1071. }
  1072. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  1073. {
  1074. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  1075. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1076. return [self canOpenMenuAction:metadata];
  1077. }
  1078. -(void)swipeTableCell:(nonnull MGSwipeTableCell *)cell didChangeSwipeState:(MGSwipeState)state gestureIsActive:(BOOL)gestureIsActive
  1079. {
  1080. }
  1081. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  1082. {
  1083. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  1084. self.metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1085. if (direction == MGSwipeDirectionRightToLeft) {
  1086. [self actionDelete:indexPath];
  1087. }
  1088. if (direction == MGSwipeDirectionLeftToRight) {
  1089. [[NCNetworking shared] favoriteMetadata:self.metadata urlBase:appDelegate.urlBase completion:^(NSInteger errorCode, NSString *errorDescription) {
  1090. if (errorCode != 0) {
  1091. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:false];
  1092. }
  1093. }];
  1094. }
  1095. return YES;
  1096. }
  1097. - (void)actionDelete:(NSIndexPath *)indexPath
  1098. {
  1099. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1100. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  1101. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  1102. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1103. [self performSelector:@selector(deleteMetadatas) withObject:nil];
  1104. }]];
  1105. if (localFile) {
  1106. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1107. tableMetadata *metadataLivePhoto = [[NCManageDatabase sharedInstance] isLivePhotoWithMetadata:metadata];
  1108. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  1109. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] error:nil];
  1110. if (metadataLivePhoto) {
  1111. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadataLivePhoto.ocId]];
  1112. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadataLivePhoto.ocId] error:nil];
  1113. }
  1114. [self reloadDatasource:metadata.serverUrl ocId:metadata.ocId];
  1115. }]];
  1116. }
  1117. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1118. }]];
  1119. alertController.popoverPresentationController.sourceView = self.tableView;
  1120. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  1121. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  1122. [alertController.view layoutIfNeeded];
  1123. [self presentViewController:alertController animated:YES completion:nil];
  1124. }
  1125. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  1126. {
  1127. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  1128. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  1129. self.metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1130. [self toggleMoreMenuWithViewController:self.tabBarController indexPath:indexPath metadata:self.metadata metadataFolder:_metadataFolder];
  1131. }
  1132. #pragma --------------------------------------------------------------------------------------------
  1133. #pragma mark - ==== Datasource ====
  1134. #pragma --------------------------------------------------------------------------------------------
  1135. - (void)reloadDatasource:(NSNotification *)notification
  1136. {
  1137. NSDictionary *userInfo = notification.userInfo;
  1138. NSString *ocId = userInfo[@"ocId"];
  1139. NSString *serverUrl = userInfo[@"serverUrl"];
  1140. [self reloadDatasource:serverUrl ocId:ocId];
  1141. }
  1142. - (void)reloadDatasourceTransfer:(NSNotification *)notification
  1143. {
  1144. if (self.view.window == nil) { return; }
  1145. NSDictionary *userInfo = notification.userInfo;
  1146. tableMetadata *metadata = userInfo[@"metadata"];
  1147. NSInteger errorCode = [userInfo[@"errorCode"] integerValue];
  1148. NSString *errorDescription = userInfo[@"errorDescription"];
  1149. if (errorCode != 0 && self.view.window != nil) {
  1150. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:false];
  1151. }
  1152. [self reloadDatasource:metadata.serverUrl ocId:metadata.ocId];
  1153. }
  1154. - (void)reloadDatasource:(NSString *)serverUrl ocId:(NSString *)ocId
  1155. {
  1156. // test
  1157. if (appDelegate.account.length == 0 || serverUrl.length == 0 || serverUrl == nil) // || self.view.window == nil)
  1158. return;
  1159. // Se non siamo nella dir appropriata esci
  1160. if ([serverUrl isEqualToString:self.serverUrl] == NO || self.serverUrl == nil)
  1161. return;
  1162. // live photo
  1163. livePhoto = [CCUtility getLivePhoto];
  1164. // load share
  1165. appDelegate.shares = [[NCManageDatabase sharedInstance] getTableSharesWithAccount:appDelegate.account];
  1166. // Search Mode
  1167. if (self.searchController.isActive) {
  1168. // Create metadatas
  1169. NSMutableArray *metadatas = [NSMutableArray new];
  1170. for (tableMetadata *resultMetadata in _searchResultMetadatas) {
  1171. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataFromOcId:resultMetadata.ocId];
  1172. if (metadata) {
  1173. [metadatas addObject:metadata];
  1174. }
  1175. }
  1176. // [CCUtility getGroupBySettings]
  1177. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:metadatas listProgressMetadata:nil groupBy:nil filterTypeFileImage:NO filterTypeFileVideo:NO filterLivePhoto:YES sort:@"fileName" ascending:NO directoryOnTop:NO account:appDelegate.account];
  1178. [self tableViewReloadData];
  1179. if ([sectionDataSource.allRecordsDataSource count] == 0 && [_searchFileName length] >= k_minCharsSearch) {
  1180. _noFilesSearchTitle = NSLocalizedString(@"_search_no_record_found_", nil);
  1181. _noFilesSearchDescription = @"";
  1182. }
  1183. if ([sectionDataSource.allRecordsDataSource count] == 0 && [_searchFileName length] < k_minCharsSearch) {
  1184. _noFilesSearchTitle = @"";
  1185. _noFilesSearchDescription = NSLocalizedString(@"_search_instruction_", nil);
  1186. }
  1187. [self.tableView reloadEmptyDataSet];
  1188. return;
  1189. }
  1190. // Get MetadataFolder
  1191. if ([serverUrl isEqualToString:[[NCUtility shared] getHomeServerWithUrlBase:appDelegate.urlBase account:appDelegate.account]])
  1192. _metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.account, k_serverUrl_root]];
  1193. else
  1194. _metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.account, serverUrl]];
  1195. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  1196. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectoryWithUrlBase:appDelegate.urlBase account:appDelegate.account];
  1197. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1198. NSArray *recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.account, serverUrl] page:0 limit:0 sorted:@"fileName" ascending:NO];
  1199. // [CCUtility getGroupBySettings]
  1200. NSString *sort = [[NCUtility shared] getSortedForViewWithKey:k_layout_view_main];
  1201. BOOL ascending = [[NCUtility shared] getAscendingForViewWithKey:k_layout_view_main];
  1202. BOOL directoryOnTop = [[NCUtility shared] getDirectoryOnTopForViewWithKey:k_layout_view_main];
  1203. CCSectionDataSourceMetadata *sectionDataSourceTemp = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupBy:nil filterTypeFileImage:NO filterTypeFileVideo:NO filterLivePhoto:YES sort:sort ascending:ascending directoryOnTop:directoryOnTop account:appDelegate.account];
  1204. dispatch_async(dispatch_get_main_queue(), ^{
  1205. sectionDataSource = sectionDataSourceTemp;
  1206. [self tableViewReloadData];
  1207. });
  1208. });
  1209. // BLINK
  1210. if (self.blinkFileNamePath != nil) {
  1211. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  1212. for (NSString *key in sectionDataSource.allRecordsDataSource) {
  1213. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:key];
  1214. NSString *metadataFileNamePath = [NSString stringWithFormat:@"%@/%@", metadata.serverUrl, metadata.fileName];
  1215. if ([metadataFileNamePath isEqualToString:self.blinkFileNamePath]) {
  1216. for (NSString *key in sectionDataSource.ocIdIndexPath) {
  1217. if ([key isEqualToString:metadata.ocId]) {
  1218. NSIndexPath *indexPath = [sectionDataSource.ocIdIndexPath objectForKey:key];
  1219. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
  1220. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  1221. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  1222. if (cell) {
  1223. self.blinkFileNamePath = nil;
  1224. [[NCUtility shared] blinkWithCell:cell];
  1225. }
  1226. });
  1227. }
  1228. }
  1229. }
  1230. }
  1231. });
  1232. }
  1233. }
  1234. - (NSArray *)getMetadatasFromSelectedRows:(NSArray *)selectedRows
  1235. {
  1236. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  1237. if (selectedRows.count > 0) {
  1238. for (NSIndexPath *selectionIndex in selectedRows) {
  1239. NSString *ocId = [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:selectionIndex.section]] objectAtIndex:selectionIndex.row];
  1240. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  1241. [metadatas addObject:metadata];
  1242. }
  1243. }
  1244. return metadatas;
  1245. }
  1246. - (NSArray *)getMetadatasFromSectionDataSource:(NSInteger)section
  1247. {
  1248. NSInteger totSections =[sectionDataSource.sections count] ;
  1249. if ((totSections < (section + 1)) || ((section + 1) > totSections)) {
  1250. return nil;
  1251. }
  1252. id valueSection = [sectionDataSource.sections objectAtIndex:section];
  1253. return [sectionDataSource.sectionArrayRow objectForKey:valueSection];
  1254. }
  1255. #pragma --------------------------------------------------------------------------------------------
  1256. #pragma mark - ==== Table ====
  1257. #pragma --------------------------------------------------------------------------------------------
  1258. - (void)tableViewToggle
  1259. {
  1260. [self tableViewSelect:!_isSelectedMode];
  1261. }
  1262. - (void)tableViewSelect:(BOOL)toggle
  1263. {
  1264. _isSelectedMode = toggle;
  1265. // chiudiamo eventuali swipe aperti
  1266. if (_isSelectedMode)
  1267. [self.tableView setEditing:NO animated:NO];
  1268. [self.tableView setAllowsMultipleSelectionDuringEditing:_isSelectedMode];
  1269. [self.tableView setEditing:_isSelectedMode animated:YES];
  1270. if (_isSelectedMode)
  1271. [self setUINavigationBarSelected];
  1272. else
  1273. [self setUINavigationBarDefault];
  1274. [selectOcId removeAllObjects];
  1275. [self setTitle];
  1276. }
  1277. - (void)tableViewReloadData
  1278. {
  1279. // store selected cells before relod
  1280. NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
  1281. // reload table view
  1282. [self.tableView reloadData];
  1283. // selected cells stored
  1284. for (NSIndexPath *path in indexPaths)
  1285. [self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
  1286. [self setTableViewHeader];
  1287. [self setTableViewFooter];
  1288. if (self.tableView.editing)
  1289. [self setTitle];
  1290. [self.tableView reloadEmptyDataSet];
  1291. }
  1292. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  1293. {
  1294. if (tableView.editing == 1) {
  1295. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1296. if (!metadata || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata])
  1297. return NO;
  1298. if (metadata == nil || metadata.status != k_metadataStatusNormal)
  1299. return NO;
  1300. else
  1301. return YES;
  1302. } else {
  1303. [selectOcId removeAllObjects];
  1304. }
  1305. return YES;
  1306. }
  1307. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  1308. {
  1309. return 60;
  1310. }
  1311. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  1312. {
  1313. return [[sectionDataSource.sectionArrayRow allKeys] count];
  1314. }
  1315. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  1316. {
  1317. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  1318. }
  1319. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  1320. {
  1321. return 0.0f;
  1322. }
  1323. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  1324. {
  1325. float shift;
  1326. UIVisualEffectView *visualEffectView;
  1327. NSString *titleSection = @"";
  1328. if (![self indexPathIsValid:[NSIndexPath indexPathForRow:0 inSection:section]])
  1329. return nil;
  1330. if ([[sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]])
  1331. titleSection = [sectionDataSource.sections objectAtIndex:section];
  1332. if ([[sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSDate class]])
  1333. titleSection = [CCUtility getTitleSectionDate:[sectionDataSource.sections objectAtIndex:section]];
  1334. // Format title
  1335. UIVisualEffect *blurEffect;
  1336. blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  1337. visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  1338. visualEffectView.backgroundColor = [NCBrandColor.sharedInstance.brandElement colorWithAlphaComponent:0.2];
  1339. if ([[CCUtility getGroupBySettings] isEqualToString:@"alphabetic"]) {
  1340. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  1341. shift = - 35;
  1342. else
  1343. shift = - 20;
  1344. } else shift = - 10;
  1345. // Title
  1346. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, -12, 0, 44)];
  1347. titleLabel.backgroundColor = [UIColor clearColor];
  1348. titleLabel.textColor = NCBrandColor.sharedInstance.textView;
  1349. titleLabel.font = [UIFont systemFontOfSize:12];
  1350. titleLabel.textAlignment = NSTextAlignmentLeft;
  1351. titleLabel.text = titleSection;
  1352. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  1353. [visualEffectView.contentView addSubview:titleLabel];
  1354. // Elements
  1355. UILabel *elementLabel= [[UILabel alloc]initWithFrame:CGRectMake(shift, -12, 0, 44)];
  1356. elementLabel.backgroundColor = [UIColor clearColor];
  1357. elementLabel.textColor = NCBrandColor.sharedInstance.textView;
  1358. elementLabel.font = [UIFont systemFontOfSize:12];
  1359. elementLabel.textAlignment = NSTextAlignmentRight;
  1360. elementLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  1361. NSArray *metadatas = [self getMetadatasFromSectionDataSource:section];
  1362. NSUInteger rowsCount = [metadatas count];
  1363. if (rowsCount == 0) return nil;
  1364. if (rowsCount == 1) elementLabel.text = [NSString stringWithFormat:@"%lu %@", (unsigned long)rowsCount, NSLocalizedString(@"_element_",nil)];
  1365. if (rowsCount > 1) elementLabel.text = [NSString stringWithFormat:@"%lu %@", (unsigned long)rowsCount, NSLocalizedString(@"_elements_",nil)];
  1366. [visualEffectView.contentView addSubview:elementLabel];
  1367. return visualEffectView;
  1368. }
  1369. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  1370. {
  1371. return [sectionDataSource.sections indexOfObject:title];
  1372. }
  1373. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  1374. {
  1375. if ([[CCUtility getGroupBySettings] isEqualToString:@"alphabetic"])
  1376. return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
  1377. else
  1378. return nil;
  1379. }
  1380. /*
  1381. -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  1382. {
  1383. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
  1384. }
  1385. }
  1386. */
  1387. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  1388. {
  1389. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1390. tableShare *shareCell;
  1391. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata] || (_metadataFolder != nil && [[NCManageDatabase sharedInstance] isTableInvalidated:_metadataFolder])) {
  1392. return [CCCellMain new];
  1393. }
  1394. for (tableShare *share in appDelegate.shares) {
  1395. if ([share.serverUrl isEqualToString:metadata.serverUrl] && [share.fileName isEqualToString:metadata.fileName]) {
  1396. shareCell = share;
  1397. break;
  1398. }
  1399. }
  1400. UITableViewCell *cell = [[NCMainCommon shared] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:_metadataFolder serverUrl:self.serverUrl autoUploadFileName:_autoUploadFileName autoUploadDirectory:_autoUploadDirectory tableShare:shareCell livePhoto:livePhoto];
  1401. // NORMAL - > MAIN
  1402. if ([cell isKindOfClass:[CCCellMain class]]) {
  1403. // Comment tap
  1404. if (metadata.commentsUnread) {
  1405. UITapGestureRecognizer *tapComment = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionComment:)];
  1406. [tapComment setNumberOfTapsRequired:1];
  1407. ((CCCellMain *)cell).comment.userInteractionEnabled = YES;
  1408. [((CCCellMain *)cell).comment addGestureRecognizer:tapComment];
  1409. }
  1410. // Share add Tap
  1411. UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionShared:)];
  1412. [tapShare setNumberOfTapsRequired:1];
  1413. ((CCCellMain *)cell).viewShared.userInteractionEnabled = YES;
  1414. [((CCCellMain *)cell).viewShared addGestureRecognizer:tapShare];
  1415. // More
  1416. if ([self canOpenMenuAction:metadata]) {
  1417. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  1418. [tapMore setNumberOfTapsRequired:1];
  1419. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  1420. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  1421. }
  1422. // MGSwipeButton
  1423. ((CCCellMain *)cell).delegate = self;
  1424. // LEFT
  1425. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:self.cellFavouriteImage backgroundColor:NCBrandColor.sharedInstance.yellowFavorite padding:25]];
  1426. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  1427. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  1428. //centerIconOverText
  1429. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  1430. [favoriteButton centerIconOverText];
  1431. // RIGHT
  1432. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:self.cellTrashImage backgroundColor:[UIColor redColor] padding:25]];
  1433. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  1434. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  1435. //centerIconOverText
  1436. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  1437. [deleteButton centerIconOverText];
  1438. }
  1439. // TRANSFER
  1440. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  1441. // gesture Transfer
  1442. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  1443. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  1444. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  1445. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  1446. }
  1447. return cell;
  1448. }
  1449. - (void)setTableViewHeader
  1450. {
  1451. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:appDelegate.account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  1452. NSString *trimmedRichWorkspaceText = [self.richWorkspaceText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  1453. if (serverVersionMajor < k_nextcloud_version_18_0 || trimmedRichWorkspaceText.length == 0 ) {
  1454. [self.tableView.tableHeaderView setFrame:CGRectMake(self.tableView.tableHeaderView.frame.origin.x, self.tableView.tableHeaderView.frame.origin.y, self.tableView.frame.size.width, heightSearchBar)];
  1455. } else {
  1456. [self.viewRichWorkspace setFrame:CGRectMake(self.tableView.tableHeaderView.frame.origin.x, self.tableView.tableHeaderView.frame.origin.y, self.tableView.frame.size.width, heightRichWorkspace)];
  1457. }
  1458. if (self.searchController.isActive == true) {
  1459. [self.tableView.tableHeaderView setFrame:CGRectMake(self.tableView.tableHeaderView.frame.origin.x, self.tableView.tableHeaderView.frame.origin.y, self.tableView.frame.size.width, 0)];
  1460. }
  1461. [self.viewRichWorkspace setNeedsLayout];
  1462. [self.viewRichWorkspace loadWithRichWorkspaceText:self.richWorkspaceText];
  1463. }
  1464. - (void)setTableViewFooter
  1465. {
  1466. UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
  1467. [footerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  1468. UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
  1469. [footerLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  1470. UIFont *appFont = [UIFont systemFontOfSize:12];
  1471. footerLabel.font = appFont;
  1472. footerLabel.textColor = [UIColor grayColor];
  1473. footerLabel.backgroundColor = [UIColor clearColor];
  1474. footerLabel.textAlignment = NSTextAlignmentCenter;
  1475. NSString *folders;
  1476. NSString *files;
  1477. NSString *footerText;
  1478. if (sectionDataSource.directories > 1) {
  1479. folders = [NSString stringWithFormat:@"%ld %@", (long)sectionDataSource.directories, NSLocalizedString(@"_folders_", nil)];
  1480. } else if (sectionDataSource.directories == 1){
  1481. folders = [NSString stringWithFormat:@"%ld %@", (long)sectionDataSource.directories, NSLocalizedString(@"_folder_", nil)];
  1482. } else {
  1483. folders = @"";
  1484. }
  1485. if (sectionDataSource.files > 1) {
  1486. files = [NSString stringWithFormat:@"%ld %@ %@", (long)sectionDataSource.files, NSLocalizedString(@"_files_", nil), [CCUtility transformedSize:sectionDataSource.totalSize]];
  1487. } else if (sectionDataSource.files == 1){
  1488. files = [NSString stringWithFormat:@"%ld %@ %@", (long)sectionDataSource.files, NSLocalizedString(@"_file_", nil), [CCUtility transformedSize:sectionDataSource.totalSize]];
  1489. } else {
  1490. files = @"";
  1491. }
  1492. if ([folders isEqualToString:@""]) {
  1493. footerText = files;
  1494. } else if ([files isEqualToString:@""]) {
  1495. footerText = folders;
  1496. } else {
  1497. footerText = [NSString stringWithFormat:@"%@, %@", folders, files];
  1498. }
  1499. footerLabel.text = footerText;
  1500. [footerView addSubview:footerLabel];
  1501. [self.tableView setTableFooterView:footerView];
  1502. }
  1503. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  1504. {
  1505. CCCellMain *cell = [tableView cellForRowAtIndexPath:indexPath];
  1506. // settiamo il record file.
  1507. self.metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1508. if (!self.metadata)
  1509. return;
  1510. // se non può essere selezionata deseleziona
  1511. if ([cell isEditing] == NO)
  1512. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  1513. // se siamo in modalità editing impostiamo il titolo dei selezioati e usciamo subito
  1514. if (self.tableView.editing) {
  1515. [selectOcId addObject:self.metadata.ocId];
  1516. [self setTitle];
  1517. return;
  1518. }
  1519. if (self.metadata.status != k_metadataStatusNormal && self.metadata.status != k_metadataStatusDownloadError) {
  1520. return;
  1521. }
  1522. // file
  1523. if (self.metadata.directory == NO) {
  1524. // se il file esiste andiamo direttamente al delegato altrimenti carichiamolo
  1525. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  1526. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_downloadedFile object:nil userInfo:@{@"metadata": self.metadata, @"selector": selectorLoadFileView, @"errorCode": @(0), @"errorDescription": @""}];
  1527. } else {
  1528. if (_metadataFolder.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.account]) {
  1529. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorE2EENotEnabled forced:true];
  1530. } else {
  1531. if (([self.metadata.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadata.typeFile isEqualToString: k_metadataTypeFile_audio]) && _metadataFolder.e2eEncrypted == NO) {
  1532. [self shouldPerformSegue:self.metadata selector:@""];
  1533. } else if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_document] && [[NCUtility shared] isDirectEditingWithAccount:self.metadata.account contentType:self.metadata.contentType] != nil) {
  1534. if (NCCommunication.shared.isNetworkReachable) {
  1535. [self shouldPerformSegue:self.metadata selector:@""];
  1536. } else {
  1537. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_go_online_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorOffline forced:true];
  1538. }
  1539. } else if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_document] && [[NCUtility shared] isRichDocument:self.metadata]) {
  1540. if (NCCommunication.shared.isNetworkReachable) {
  1541. [self shouldPerformSegue:self.metadata selector:@""];
  1542. } else {
  1543. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_go_online_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorOffline forced:true];
  1544. }
  1545. } else {
  1546. if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  1547. [self shouldPerformSegue:self.metadata selector:selectorLoadFileView];
  1548. }
  1549. [[NCNetworking shared] downloadWithMetadata:self.metadata selector:selectorLoadFileView setFavorite:false completion:^(NSInteger errorCode) { }];
  1550. }
  1551. }
  1552. }
  1553. }
  1554. if (self.metadata.directory) {
  1555. [self performSegueDirectoryWithMetadata:self.metadata blinkFileNamePath:self.blinkFileNamePath];
  1556. }
  1557. }
  1558. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
  1559. {
  1560. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1561. [selectOcId removeObject:metadata.ocId];
  1562. [self setTitle];
  1563. }
  1564. - (void)didSelectAll
  1565. {
  1566. for (int i = 0; i < self.tableView.numberOfSections; i++) {
  1567. for (int j = 0; j < [self.tableView numberOfRowsInSection:i]; j++) {
  1568. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
  1569. tableMetadata *metadata = [[NCMainCommon shared] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  1570. [selectOcId addObject:metadata.ocId];
  1571. [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
  1572. }
  1573. }
  1574. [self setTitle];
  1575. }
  1576. - (BOOL)indexPathIsValid:(NSIndexPath *)indexPath
  1577. {
  1578. if (!indexPath)
  1579. return NO;
  1580. NSInteger section = indexPath.section;
  1581. NSInteger row = indexPath.row;
  1582. NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.tableView] - 1;
  1583. if (section > lastSectionIndex || lastSectionIndex < 0)
  1584. return NO;
  1585. NSInteger rowCount = [self.tableView numberOfRowsInSection:indexPath.section] - 1;
  1586. if (rowCount < 0)
  1587. return NO;
  1588. return row <= rowCount;
  1589. }
  1590. #pragma --------------------------------------------------------------------------------------------
  1591. #pragma mark ===== Navigation ====
  1592. #pragma --------------------------------------------------------------------------------------------
  1593. - (void)shouldPerformSegue:(tableMetadata *)metadata selector:(NSString *)selector
  1594. {
  1595. // if background return
  1596. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return;
  1597. if (self.view.window == NO)
  1598. return;
  1599. // Collapsed ma siamo già in detail esci
  1600. if (self.splitViewController.isCollapsed) {
  1601. if (appDelegate.activeDetail.isViewLoaded && appDelegate.activeDetail.view.window) return;
  1602. }
  1603. // Metadata for push detail
  1604. self.metadataForPushDetail = metadata;
  1605. self.selectorForPushDetail = selector;
  1606. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  1607. }
  1608. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  1609. {
  1610. tableMetadata *metadata;
  1611. NSMutableArray *photoDataSource = [NSMutableArray new];
  1612. UINavigationController *navigationController = segue.destinationViewController;
  1613. NCDetailViewController *detailViewController = (NCDetailViewController *)navigationController.topViewController;
  1614. if ([sender isKindOfClass:[tableMetadata class]]) {
  1615. metadata = sender;
  1616. [photoDataSource addObject:sender];
  1617. } else {
  1618. metadata = self.metadataForPushDetail;
  1619. for (NSString *ocId in sectionDataSource.allOcId) {
  1620. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  1621. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  1622. [photoDataSource addObject:metadata];
  1623. }
  1624. }
  1625. detailViewController.metadata = metadata;
  1626. detailViewController.selector = self.selectorForPushDetail;
  1627. [detailViewController setTitle:metadata.fileNameView];
  1628. }
  1629. // can i go to next viewcontroller
  1630. - (void)performSegueDirectoryWithMetadata:(tableMetadata *)metadata blinkFileNamePath:(NSString *)blinkFileNamePath
  1631. {
  1632. NSString *nomeDir;
  1633. if (self.tableView.editing == NO) {
  1634. // E2EE Check enable
  1635. if (metadata.e2eEncrypted && [CCUtility isEndToEndEnabled:appDelegate.account] == NO) {
  1636. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorE2EENotEnabled forced:true];
  1637. return;
  1638. }
  1639. nomeDir = metadata.fileName;
  1640. NSString *serverUrlPush = [CCUtility stringAppendServerUrl:metadata.serverUrl addFileName:nomeDir];
  1641. CCMain *viewController = [appDelegate.listMainVC objectForKey:serverUrlPush];
  1642. if (!viewController) {
  1643. viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMain"];
  1644. viewController.serverUrl = serverUrlPush;
  1645. viewController.titleMain = metadata.fileNameView;
  1646. viewController.blinkFileNamePath = blinkFileNamePath;
  1647. // save self
  1648. [appDelegate.listMainVC setObject:viewController forKey:serverUrlPush];
  1649. [self.navigationController pushViewController:viewController animated:YES];
  1650. } else {
  1651. if (viewController.isViewLoaded) {
  1652. viewController.titleMain = metadata.fileNameView;
  1653. viewController.blinkFileNamePath = blinkFileNamePath;
  1654. // Fix : Application tried to present modally an active controller
  1655. if ([self.navigationController isBeingPresented]) {
  1656. // being presented
  1657. } else if ([self.navigationController isMovingToParentViewController]) {
  1658. // being pushed
  1659. } else {
  1660. [self.navigationController pushViewController:viewController animated:YES];
  1661. }
  1662. }
  1663. }
  1664. }
  1665. }
  1666. @end