AppDelegate.m 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. //
  2. // AppDelegate.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 "AppDelegate.h"
  24. #import "CCGraphics.h"
  25. #import "NCBridgeSwift.h"
  26. #import "NCAutoUpload.h"
  27. #import "NCPushNotificationEncryption.h"
  28. #import <QuartzCore/QuartzCore.h>
  29. @import Firebase;
  30. @class NCViewerRichdocument;
  31. @interface AppDelegate() <TOPasscodeViewControllerDelegate>
  32. @end
  33. @implementation AppDelegate
  34. + (void)initialize
  35. {
  36. [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": [CCUtility getUserAgent]}];
  37. }
  38. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  39. {
  40. BOOL isSimulatorOrTestFlight = [[NCUtility shared] isSimulatorOrTestFlight];
  41. if (![CCUtility getDisableCrashservice] && NCBrandOptions.sharedInstance.disable_crash_service == false) {
  42. [FIRApp configure];
  43. }
  44. [CCUtility createDirectoryStandard];
  45. [CCUtility emptyTemporaryDirectory];
  46. // Networking
  47. [[NCCommunicationCommon shared] setupWithDelegate:[NCNetworking shared]];
  48. [[NCCommunicationCommon shared] setupWithUserAgent:[CCUtility getUserAgent]];
  49. NSInteger logLevel = [CCUtility getLogLevel];
  50. [[NCCommunicationCommon shared] setFileLogWithLevel:logLevel];
  51. NSString *versionApp = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
  52. NSString *versionNextcloudiOS = [NSString stringWithFormat:[NCBrandOptions sharedInstance].textCopyrightNextcloudiOS, versionApp];
  53. if (isSimulatorOrTestFlight) {
  54. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@ (Simulator / TestFlight)", (unsigned long)logLevel, versionNextcloudiOS]];
  55. } else {
  56. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@", (unsigned long)logLevel, versionNextcloudiOS]];
  57. }
  58. //
  59. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initializeMain:) name:k_notificationCenter_initializeMain object:nil];
  60. // Set account, if no exists clear all
  61. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  62. if (tableAccount == nil) {
  63. // remove all the keys Chain
  64. [CCUtility deleteAllChainStore];
  65. // remove all the App group key
  66. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
  67. } else {
  68. // FIX 3.0.5 lost urlbase
  69. if (tableAccount.urlBase.length == 0) {
  70. NSString *user = [tableAccount.user stringByAppendingString:@" "];
  71. NSString *urlBase = [tableAccount.account stringByReplacingOccurrencesOfString:user withString:@""];
  72. tableAccount.urlBase = urlBase;
  73. [[NCManageDatabase sharedInstance] updateAccount:tableAccount];
  74. tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  75. }
  76. [self settingAccount:tableAccount.account urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
  77. }
  78. // UserDefaults
  79. self.ncUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:[NCBrandOptions sharedInstance].capabilitiesGroups];
  80. // Background Fetch
  81. [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  82. self.listProgressMetadata = [NSMutableDictionary new];
  83. self.listFilesVC = [NSMutableDictionary new];
  84. self.listMainVC = [NSMutableDictionary new];
  85. self.listFavoriteVC = [NSMutableDictionary new];
  86. self.listOfflineVC = [NSMutableDictionary new];
  87. // Push Notification
  88. [application registerForRemoteNotifications];
  89. // Display notification
  90. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  91. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  92. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { }];
  93. //AV Session
  94. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
  95. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  96. // ProgressView Detail
  97. self.progressViewDetail = [[UIProgressView alloc] initWithProgressViewStyle: UIProgressViewStyleBar];
  98. // Quick Actions
  99. if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]) {
  100. [self configDynamicShortcutItems];
  101. UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
  102. if (shortcutItem)
  103. [self handleShortCutItem:shortcutItem];
  104. }
  105. // Start Timer
  106. self.timerUpdateApplicationIconBadgeNumber = [NSTimer scheduledTimerWithTimeInterval:k_timerUpdateApplicationIconBadgeNumber target:self selector:@selector(updateApplicationIconBadgeNumber) userInfo:nil repeats:YES];
  107. [self startTimerErrorNetworking];
  108. // Store review
  109. if ([[NCUtility shared] isSimulatorOrTestFlight] == false) {
  110. NCStoreReview *review = [NCStoreReview new];
  111. [review incrementAppRuns];
  112. [review showStoreReview];
  113. }
  114. // Detect Dark mode
  115. if (@available(iOS 13.0, *)) {
  116. if ([CCUtility getDarkModeDetect]) {
  117. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  118. [CCUtility setDarkMode:YES];
  119. } else {
  120. [CCUtility setDarkMode:NO];
  121. }
  122. }
  123. }
  124. if ([NCBrandOptions sharedInstance].disable_intro) {
  125. [CCUtility setIntro:YES];
  126. if (self.account.length == 0) {
  127. [self openLoginView:nil selector:k_intro_login openLoginWeb:false];
  128. }
  129. } else {
  130. if ([CCUtility getIntro] == NO) {
  131. UIViewController *introViewController = [[UIStoryboard storyboardWithName:@"NCIntro" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
  132. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: introViewController];
  133. self.window.rootViewController = navController;
  134. [self.window makeKeyAndVisible];
  135. }
  136. }
  137. // init home
  138. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  139. // Passcode
  140. dispatch_async(dispatch_get_main_queue(), ^{
  141. [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
  142. });
  143. // Auto upload
  144. self.networkingAutoUpload = [NCNetworkingAutoUpload new];
  145. [[NCCommunicationCommon shared] writeLog:@"Application did finish launching"];
  146. return YES;
  147. }
  148. //
  149. // L' applicazione si dimetterà dallo stato di attivo
  150. //
  151. - (void)applicationWillResignActive:(UIApplication *)application
  152. {
  153. if (self.account.length == 0) { return; }
  154. // Dismiss FileViewInFolder
  155. if (self.activeFileViewInFolder != nil ) {
  156. [self.activeFileViewInFolder dismissViewControllerAnimated:false completion:^{
  157. self.activeFileViewInFolder = nil;
  158. }];
  159. }
  160. [self updateApplicationIconBadgeNumber];
  161. }
  162. //
  163. // L' applicazione entrerà in primo piano (attivo solo dopo il background)
  164. //
  165. - (void)applicationWillEnterForeground:(UIApplication *)application
  166. {
  167. if (self.account.length == 0) { return; }
  168. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_applicationWillEnterForeground object:nil];
  169. // Request Passcode
  170. [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
  171. // Initialize Auto upload
  172. [[NCAutoUpload sharedInstance] initStateAutoUpload];
  173. // Read active directory
  174. [self.activeFiles reloadDataSourceNetworkWithForced:true];
  175. // Required unsubscribing / subscribing
  176. [self pushNotification];
  177. // RichDocument
  178. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_richdocumentGrabFocus object:nil];
  179. // Request Service Server Nextcloud
  180. [[NCService shared] startRequestServicesServer];
  181. [[NCCommunicationCommon shared] writeLog:@"The application Will enter in foreground"];
  182. }
  183. //
  184. // L' applicazione entrerà in primo piano (attivo sempre)
  185. //
  186. - (void)applicationDidBecomeActive:(UIApplication *)application
  187. {
  188. if (self.account.length == 0) { return; }
  189. // Brand
  190. #if defined(HC)
  191. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  192. if (account.hcIsTrial == true || account.hcTrialExpired == true || account.hcNextGroupExpirationGroupExpired == true) {
  193. HCTrial *vc = [[UIStoryboard storyboardWithName:@"HCTrial" bundle:nil] instantiateInitialViewController];
  194. vc.account = account;
  195. [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
  196. }
  197. #endif
  198. [[NCNetworking shared] verifyUploadZombie];
  199. [[NCCommunicationCommon shared] writeLog:@"The application did become active"];
  200. }
  201. //
  202. // L' applicazione è entrata nello sfondo
  203. //
  204. - (void)applicationDidEnterBackground:(UIApplication *)application
  205. {
  206. if (self.account.length == 0) { return; }
  207. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_applicationDidEnterBackground object:nil];
  208. [self passcodeWithAutomaticallyPromptForBiometricValidation:false];
  209. }
  210. //
  211. // L'applicazione terminerà
  212. //
  213. - (void)applicationWillTerminate:(UIApplication *)application
  214. {
  215. [[NCCommunicationCommon shared] writeLog:@"bye bye"];
  216. }
  217. // NotificationCenter
  218. - (void)initializeMain:(NSNotification *)notification
  219. {
  220. if (self.account.length == 0) return;
  221. // Clear error certificate
  222. [CCUtility setCertificateError:self.account error:NO];
  223. // Setting Theming
  224. [self settingThemingColorBrand];
  225. // If AVPlayer in play -> Stop
  226. if (self.player != nil && self.player.rate != 0) {
  227. [self.player pause];
  228. }
  229. // close detail
  230. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_menuDetailClose object:nil];
  231. // Not Photos Video in library ? then align and Init Auto Upload
  232. NSArray *recordsPhotoLibrary = [[NCManageDatabase sharedInstance] getPhotoLibraryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", self.account]];
  233. if ([recordsPhotoLibrary count] == 0) {
  234. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  235. }
  236. // Start Auto Upload
  237. [[NCAutoUpload sharedInstance] initStateAutoUpload];
  238. // Start services
  239. [[NCCommunicationCommon shared] writeLog:@"Request Service Server Nextcloud"];
  240. [[NCService shared] startRequestServicesServer];
  241. // Registeration push notification
  242. [self pushNotification];
  243. //Registeration domain File Provider
  244. //FileProviderDomain *fileProviderDomain = [FileProviderDomain new];
  245. //[fileProviderDomain removeAllDomains];
  246. //[fileProviderDomain registerDomains];
  247. [[NCCommunicationCommon shared] writeLog:@"initialize Main"];
  248. }
  249. #pragma --------------------------------------------------------------------------------------------
  250. #pragma mark ===== Login / checkErrorNetworking =====
  251. #pragma --------------------------------------------------------------------------------------------
  252. - (void)checkErrorNetworking
  253. {
  254. // test
  255. if (self.account.length == 0) { return; }
  256. // check unauthorized server (401)
  257. if ([CCUtility getPassword:self.account].length == 0) {
  258. [self openLoginView:self.window.rootViewController selector:k_intro_login openLoginWeb:true];
  259. }
  260. // check certificate untrusted (-1202)
  261. if ([CCUtility getCertificateError:self.account]) {
  262. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_ssl_certificate_untrusted_", nil) message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
  263. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  264. [[NCNetworking shared] wrtiteCertificateWithDirectoryCertificate:[CCUtility getDirectoryCerificates]];
  265. [self startTimerErrorNetworking];
  266. }]];
  267. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  268. [self startTimerErrorNetworking];
  269. }]];
  270. [self.window.rootViewController presentViewController:alertController animated:YES completion:^{
  271. // Stop timer error network
  272. [self.timerErrorNetworking invalidate];
  273. }];
  274. }
  275. }
  276. - (void)openLoginView:(UIViewController *)viewController selector:(NSInteger)selector openLoginWeb:(BOOL)openLoginWeb
  277. {
  278. // use appConfig [MDM]
  279. if ([NCBrandOptions sharedInstance].use_configuration) {
  280. if (!(_appConfigView.isViewLoaded && _appConfigView.view.window)) {
  281. self.appConfigView = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCAppConfigView"];
  282. [self showLoginViewController:self.appConfigView forContext:viewController];
  283. }
  284. return;
  285. }
  286. // only for personalized LoginWeb [customer]
  287. if ([NCBrandOptions sharedInstance].use_login_web_personalized) {
  288. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  289. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  290. self.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] loginBaseUrl];
  291. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  292. }
  293. return;
  294. }
  295. // normal login
  296. if (selector == k_intro_signup) {
  297. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  298. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  299. if (selector == k_intro_signup) {
  300. self.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] linkloginPreferredProviders];
  301. } else {
  302. self.activeLoginWeb.urlBase = self.urlBase;
  303. }
  304. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  305. }
  306. } else if ([NCBrandOptions sharedInstance].disable_intro && [NCBrandOptions sharedInstance].disable_request_login_url) {
  307. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  308. self.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] loginBaseUrl];
  309. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  310. } else if (openLoginWeb) {
  311. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  312. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  313. self.activeLoginWeb.urlBase = self.urlBase;
  314. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  315. }
  316. } else {
  317. if (!(_activeLogin.isViewLoaded && _activeLogin.view.window)) {
  318. _activeLogin = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  319. [self showLoginViewController:_activeLogin forContext:viewController];
  320. }
  321. }
  322. }
  323. -(void)showLoginViewController:(UIViewController *)viewController forContext:(UIViewController *)contextViewController
  324. {
  325. if (contextViewController == NULL) {
  326. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  327. navController.navigationBar.barStyle = UIBarStyleBlack;
  328. navController.navigationBar.tintColor = NCBrandColor.sharedInstance.customerText;
  329. navController.navigationBar.barTintColor = NCBrandColor.sharedInstance.customer;
  330. [navController.navigationBar setTranslucent:false];
  331. self.window.rootViewController = navController;
  332. [self.window makeKeyAndVisible];
  333. } else if ([contextViewController isKindOfClass:[UINavigationController class]]) {
  334. UINavigationController *navController = ((UINavigationController *)contextViewController);
  335. [navController pushViewController:viewController animated:true];
  336. } else {
  337. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  338. navController.modalPresentationStyle = UIModalPresentationFullScreen;
  339. navController.navigationBar.barStyle = UIBarStyleBlack;
  340. navController.navigationBar.tintColor = NCBrandColor.sharedInstance.customerText;
  341. navController.navigationBar.barTintColor = NCBrandColor.sharedInstance.customer;
  342. [navController.navigationBar setTranslucent:false];
  343. [contextViewController presentViewController:navController animated:true completion:nil];
  344. }
  345. }
  346. - (void)startTimerErrorNetworking
  347. {
  348. self.timerErrorNetworking = [NSTimer scheduledTimerWithTimeInterval:k_timerErrorNetworking target:self selector:@selector(checkErrorNetworking) userInfo:nil repeats:YES];
  349. }
  350. #pragma --------------------------------------------------------------------------------------------
  351. #pragma mark ===== Account & Communication =====
  352. #pragma --------------------------------------------------------------------------------------------
  353. - (void)settingAccount:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  354. {
  355. self.account = account;
  356. self.urlBase = urlBase;
  357. self.user = user;
  358. self.userID = userID;
  359. self.password = password;
  360. (void)[NCNetworkingNotificationCenter shared];
  361. [[NCCommunicationCommon shared] setupWithAccount:account user:user userId:userID password:password urlBase:urlBase];
  362. [self settingSetupCommunication:account];
  363. }
  364. - (void)deleteAccount:(NSString *)account wipe:(BOOL)wipe
  365. {
  366. // Push Notification
  367. tableAccount *accountPN = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  368. [self unsubscribingNextcloudServerPushNotification:accountPN.account urlBase:accountPN.urlBase user:accountPN.user withSubscribing:false];
  369. [self settingAccount:nil urlBase:nil user:nil userID:nil password:nil];
  370. /* DELETE ALL FILES LOCAL FS */
  371. NSArray *results = [[NCManageDatabase sharedInstance] getTableLocalFilesWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account] sorted:@"ocId" ascending:NO];
  372. for (tableLocalFile *result in results) {
  373. [CCUtility removeFileAtPath:[CCUtility getDirectoryProviderStorageOcId:result.ocId]];
  374. }
  375. // Clear database
  376. [[NCManageDatabase sharedInstance] clearDatabaseWithAccount:account removeAccount:true];
  377. [CCUtility clearAllKeysEndToEnd:account];
  378. [CCUtility clearAllKeysPushNotification:account];
  379. [CCUtility setCertificateError:account error:false];
  380. [CCUtility setPassword:account password:nil];
  381. if (wipe) {
  382. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  383. if ([listAccount count] > 0) {
  384. NSString *newAccount = listAccount[0];
  385. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:newAccount];
  386. [self settingAccount:newAccount urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
  387. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  388. } else {
  389. [self openLoginView:self.window.rootViewController selector:k_intro_login openLoginWeb:false];
  390. }
  391. }
  392. }
  393. - (void)settingSetupCommunication:(NSString *)account
  394. {
  395. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  396. if (serverVersionMajor > 0) {
  397. [[NCCommunicationCommon shared] setupWithNextcloudVersion:serverVersionMajor];
  398. }
  399. [[NCCommunicationCommon shared] setupWithWebDav:[[NCUtility shared] getWebDAVWithAccount:account]];
  400. [[NCCommunicationCommon shared] setupWithDav:[[NCUtility shared] getDAV]];
  401. }
  402. #pragma --------------------------------------------------------------------------------------------
  403. #pragma mark ===== Push Notifications =====
  404. #pragma --------------------------------------------------------------------------------------------
  405. - (void)pushNotification
  406. {
  407. if (self.account.length == 0 || self.pushKitToken.length == 0) { return; }
  408. for (tableAccount *result in [[NCManageDatabase sharedInstance] getAllAccount]) {
  409. NSString *token = [CCUtility getPushNotificationToken:result.account];
  410. if (![token isEqualToString:self.pushKitToken]) {
  411. if (token != nil) {
  412. // unsubscribing + subscribing
  413. [self unsubscribingNextcloudServerPushNotification:result.account urlBase:result.urlBase user:result.user withSubscribing:true];
  414. } else {
  415. [self subscribingNextcloudServerPushNotification:result.account urlBase:result.urlBase user:result.user];
  416. }
  417. }
  418. }
  419. }
  420. - (void)subscribingNextcloudServerPushNotification:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user
  421. {
  422. if (self.account.length == 0 || self.pushKitToken.length == 0) { return; }
  423. [[NCPushNotificationEncryption sharedInstance] generatePushNotificationsKeyPair:account];
  424. NSString *pushTokenHash = [[NCEndToEndEncryption sharedManager] createSHA512:self.pushKitToken];
  425. NSData *pushPublicKey = [CCUtility getPushNotificationPublicKey:account];
  426. NSString *pushDevicePublicKey = [[NSString alloc] initWithData:pushPublicKey encoding:NSUTF8StringEncoding];
  427. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  428. [[NCCommunication shared] subscribingPushNotificationWithServerUrl:urlBase account:account user:user password:[CCUtility getPassword:account] pushTokenHash:pushTokenHash devicePublicKey:pushDevicePublicKey proxyServerUrl:proxyServerPath customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSString *deviceIdentifier, NSString *signature, NSString *publicKey, NSInteger errorCode, NSString *errorDescription) {
  429. if (errorCode == 0) {
  430. NSString *userAgent = [NSString stringWithFormat:@"%@ (Strict VoIP)", [CCUtility getUserAgent]];
  431. [[NCCommunication shared] subscribingPushProxyWithProxyServerUrl:proxyServerPath pushToken:self.pushKitToken deviceIdentifier:deviceIdentifier signature:signature publicKey:publicKey userAgent:userAgent completionHandler:^(NSInteger errorCode, NSString *errorDescription) {
  432. if (errorCode == 0) {
  433. [[NCCommunicationCommon shared] writeLog:@"Subscribed to Push Notification server & proxy successfully"];
  434. [CCUtility setPushNotificationToken:account token:self.pushKitToken];
  435. [CCUtility setPushNotificationDeviceIdentifier:account deviceIdentifier:deviceIdentifier];
  436. [CCUtility setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:signature];
  437. [CCUtility setPushNotificationSubscribingPublicKey:account publicKey:publicKey];
  438. }
  439. }];
  440. }
  441. }];
  442. }
  443. - (void)unsubscribingNextcloudServerPushNotification:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user withSubscribing:(BOOL)subscribing
  444. {
  445. if (self.account.length == 0) { return; }
  446. NSString *deviceIdentifier = [CCUtility getPushNotificationDeviceIdentifier:account];
  447. NSString *signature = [CCUtility getPushNotificationDeviceIdentifierSignature:account];
  448. NSString *publicKey = [CCUtility getPushNotificationSubscribingPublicKey:account];
  449. [[NCCommunication shared] unsubscribingPushNotificationWithServerUrl:urlBase account:account user:user password:[CCUtility getPassword:account] customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDescription) {
  450. if (errorCode == 0) {
  451. NSString *userAgent = [NSString stringWithFormat:@"%@ (Strict VoIP)", [CCUtility getUserAgent]];
  452. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  453. [[NCCommunication shared] unsubscribingPushProxyWithProxyServerUrl:proxyServerPath deviceIdentifier:deviceIdentifier signature:signature publicKey:publicKey userAgent:userAgent completionHandler:^(NSInteger errorCode, NSString *errorDescription) {
  454. if (errorCode == 0) {
  455. [[NCCommunicationCommon shared] writeLog:@"Unsubscribed to Push Notification server & proxy successfully."];
  456. [CCUtility setPushNotificationPublicKey:account data:nil];
  457. [CCUtility setPushNotificationSubscribingPublicKey:account publicKey:nil];
  458. [CCUtility setPushNotificationPrivateKey:account data:nil];
  459. [CCUtility setPushNotificationToken:account token:nil];
  460. [CCUtility setPushNotificationDeviceIdentifier:account deviceIdentifier:nil];
  461. [CCUtility setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:nil];
  462. if (self.pushKitToken != nil && subscribing) {
  463. [self subscribingNextcloudServerPushNotification:account urlBase:urlBase user:user];
  464. }
  465. }
  466. }];
  467. }
  468. }];
  469. }
  470. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  471. {
  472. //Called when a notification is delivered to a foreground app.
  473. completionHandler(UNNotificationPresentationOptionAlert);
  474. }
  475. -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response withCompletionHandler:(nonnull void (^)(void))completionHandler
  476. {
  477. completionHandler();
  478. }
  479. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  480. {
  481. self.pushKitToken = [self stringWithDeviceToken:deviceToken];
  482. [self pushNotification];
  483. }
  484. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  485. {
  486. NSString *message = [userInfo objectForKey:@"subject"];
  487. if (message) {
  488. NSArray *results = [[NCManageDatabase sharedInstance] getAllAccount];
  489. for (tableAccount *result in results) {
  490. if ([CCUtility getPushNotificationPrivateKey:result.account]) {
  491. NSData *decryptionKey = [CCUtility getPushNotificationPrivateKey:result.account];
  492. NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:decryptionKey];
  493. if (decryptedMessage) {
  494. NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
  495. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  496. NSInteger nid = [[json objectForKey:@"nid"] integerValue];
  497. BOOL delete = [[json objectForKey:@"delete"] boolValue];
  498. BOOL deleteAll = [[json objectForKey:@"delete-all"] boolValue];
  499. if (delete) {
  500. [self removeNotificationWithNotificationId:nid usingDecryptionKey:decryptionKey];
  501. } else if (deleteAll) {
  502. [self cleanAllNotifications];
  503. }
  504. }
  505. }
  506. }
  507. }
  508. completionHandler(UIBackgroundFetchResultNoData);
  509. }
  510. - (void)cleanAllNotifications
  511. {
  512. [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
  513. }
  514. - (void)removeNotificationWithNotificationId:(NSInteger)notificationId usingDecryptionKey:(NSData *)key
  515. {
  516. // Check in pending notifications
  517. [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
  518. for (UNNotificationRequest *notificationRequest in requests) {
  519. NSString *message = [notificationRequest.content.userInfo objectForKey:@"subject"];
  520. NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:key];
  521. if (decryptedMessage) {
  522. NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
  523. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  524. NSInteger nid = [[json objectForKey:@"nid"] integerValue];
  525. if (nid == notificationId) {
  526. [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationRequest.identifier]];
  527. }
  528. }
  529. }
  530. }];
  531. // Check in delivered notifications
  532. [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
  533. for (UNNotification *notification in notifications) {
  534. NSString *message = [notification.request.content.userInfo objectForKey:@"subject"];
  535. NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:key];
  536. if (decryptedMessage) {
  537. NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
  538. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  539. NSInteger nid = [[json objectForKey:@"nid"] integerValue];
  540. if (nid == notificationId) {
  541. [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]];
  542. }
  543. }
  544. }
  545. }];
  546. }
  547. - (NSString *)stringWithDeviceToken:(NSData *)deviceToken
  548. {
  549. const char *data = [deviceToken bytes];
  550. NSMutableString *token = [NSMutableString string];
  551. for (NSUInteger i = 0; i < [deviceToken length]; i++) {
  552. [token appendFormat:@"%02.2hhX", data[i]];
  553. }
  554. return [token copy];
  555. }
  556. #pragma --------------------------------------------------------------------------------------------
  557. #pragma mark ===== Quick Actions - ShotcutItem =====
  558. #pragma --------------------------------------------------------------------------------------------
  559. - (void)configDynamicShortcutItems
  560. {
  561. NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
  562. UIApplicationShortcutIcon *shortcutMediaIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"media"];
  563. UIApplicationShortcutItem *shortcutMedia = [[UIApplicationShortcutItem alloc] initWithType:[NSString stringWithFormat:@"%@.media", bundleId] localizedTitle:NSLocalizedString(@"_media_", nil) localizedSubtitle:nil icon:shortcutMediaIcon userInfo:nil];
  564. // add the array to our app
  565. if (shortcutMedia)
  566. [UIApplication sharedApplication].shortcutItems = @[shortcutMedia];
  567. }
  568. - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
  569. {
  570. BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];
  571. completionHandler(handledShortCutItem);
  572. }
  573. - (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem
  574. {
  575. BOOL handled = NO;
  576. NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
  577. NSString *shortcutMedia = [NSString stringWithFormat:@"%@.media", bundleId];
  578. if ([shortcutItem.type isEqualToString:shortcutMedia] && self.account) {
  579. dispatch_async(dispatch_get_main_queue(), ^{
  580. UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
  581. if ([splitViewController isKindOfClass:[UISplitViewController class]]) {
  582. UINavigationController *navigationControllerMaster = (UINavigationController *)splitViewController.viewControllers.firstObject;
  583. if ([navigationControllerMaster isKindOfClass:[UINavigationController class]]) {
  584. UITabBarController *tabBarController = (UITabBarController *)navigationControllerMaster.topViewController;
  585. if ([tabBarController isKindOfClass:[UITabBarController class]]) {
  586. if (splitViewController.isCollapsed) {
  587. [navigationControllerMaster popToRootViewControllerAnimated:false];
  588. UINavigationController *navigationControllerMaster = (UINavigationController *)splitViewController.viewControllers.firstObject;
  589. if ([navigationControllerMaster isKindOfClass:[UINavigationController class]]) {
  590. UITabBarController *tabBarController = (UITabBarController *)navigationControllerMaster.topViewController;
  591. if ([tabBarController isKindOfClass:[UITabBarController class]]) {
  592. [tabBarController setSelectedIndex: k_tabBarApplicationIndexMedia];
  593. }
  594. }
  595. } else {
  596. if ([tabBarController isKindOfClass:[UITabBarController class]]) {
  597. [tabBarController setSelectedIndex: k_tabBarApplicationIndexMedia];
  598. }
  599. }
  600. }
  601. }
  602. }
  603. });
  604. handled = YES;
  605. }
  606. return handled;
  607. }
  608. #pragma --------------------------------------------------------------------------------------------
  609. #pragma mark ===== ApplicationIconBadgeNumber =====
  610. #pragma --------------------------------------------------------------------------------------------
  611. - (void)updateApplicationIconBadgeNumber
  612. {
  613. if (self.account.length == 0) { return; }
  614. NSInteger counterDownload = [[NCOperationQueue shared] downloadCount];
  615. NSInteger counterUpload = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"status == %d OR status == %d OR status == %d", k_metadataStatusWaitUpload, k_metadataStatusInUpload, k_metadataStatusUploading]].count;
  616. NSInteger total = counterDownload + counterUpload;
  617. [UIApplication sharedApplication].applicationIconBadgeNumber = total;
  618. UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
  619. if ([splitViewController isKindOfClass:[UISplitViewController class]]) {
  620. UINavigationController *navigationController = (UINavigationController *)[splitViewController.viewControllers firstObject];
  621. if ([navigationController isKindOfClass:[UINavigationController class]]) {
  622. UITabBarController *tabBarController = (UITabBarController *)navigationController.topViewController;
  623. if ([tabBarController isKindOfClass:[UITabBarController class]]) {
  624. UITabBarItem *tabBarItem = [tabBarController.tabBar.items objectAtIndex:0];
  625. if (total > 0) {
  626. [tabBarItem setBadgeValue:[NSString stringWithFormat:@"%li", (unsigned long)total]];
  627. } else {
  628. [tabBarItem setBadgeValue:nil];
  629. }
  630. }
  631. }
  632. }
  633. }
  634. #pragma --------------------------------------------------------------------------------------------
  635. #pragma mark ===== TabBarController =====
  636. #pragma --------------------------------------------------------------------------------------------
  637. - (void)createTabBarController:(UITabBarController *)tabBarController
  638. {
  639. UITabBarItem *item;
  640. NSLayoutConstraint *constraint;
  641. CGFloat safeAreaBottom = safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  642. // File
  643. item = [tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexFile];
  644. [item setTitle:NSLocalizedString(@"_home_", nil)];
  645. item.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"tabBarFiles"] width:50 height:50 color:NCBrandColor.sharedInstance.brandElement];
  646. item.selectedImage = item.image;
  647. // Favorites
  648. item = [tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexFavorite];
  649. [item setTitle:NSLocalizedString(@"_favorites_", nil)];
  650. item.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:NCBrandColor.sharedInstance.brandElement];
  651. item.selectedImage = item.image;
  652. // (PLUS INVISIBLE)
  653. item = [tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexPlusHide];
  654. item.title = @"";
  655. item.image = nil;
  656. item.enabled = false;
  657. // Media
  658. item = [tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexMedia];
  659. [item setTitle:NSLocalizedString(@"_media_", nil)];
  660. item.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"media"] width:50 height:50 color:NCBrandColor.sharedInstance.brandElement];
  661. item.selectedImage = item.image;
  662. // More
  663. item = [tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexMore];
  664. [item setTitle:NSLocalizedString(@"_more_", nil)];
  665. item.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"tabBarMore"] width:50 height:50 color:NCBrandColor.sharedInstance.brandElement];
  666. item.selectedImage = item.image;
  667. // Plus Button
  668. int buttonSize = 57;
  669. UIImage *buttonImage = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"tabBarPlus"] width:120 height:120 color:UIColor.whiteColor];
  670. UIButton *buttonPlus = [UIButton buttonWithType:UIButtonTypeCustom];
  671. buttonPlus.tag = 99;
  672. buttonPlus.accessibilityLabel = NSLocalizedString(@"_accessibility_add_upload_", nil);
  673. [buttonPlus setImage:buttonImage forState:UIControlStateNormal];
  674. buttonPlus.backgroundColor = NCBrandColor.sharedInstance.brandElement;
  675. buttonPlus.layer.cornerRadius = buttonSize / 2;
  676. buttonPlus.layer.masksToBounds = NO;
  677. buttonPlus.layer.shadowOffset = CGSizeMake(0, 0);
  678. buttonPlus.layer.shadowRadius = 3.0f;
  679. buttonPlus.layer.shadowOpacity = 0.5;
  680. [buttonPlus addTarget:self action:@selector(handleTouchTabbarCenter:) forControlEvents:UIControlEventTouchUpInside];
  681. [buttonPlus setTranslatesAutoresizingMaskIntoConstraints:NO];
  682. [tabBarController.tabBar addSubview:buttonPlus];
  683. if (safeAreaBottom > 0) {
  684. // X
  685. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:tabBarController.tabBar attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
  686. [tabBarController.view addConstraint:constraint];
  687. // Y
  688. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:tabBarController.tabBar attribute:NSLayoutAttributeTop multiplier:1.0 constant:-(buttonSize / 2)];
  689. [tabBarController.view addConstraint:constraint];
  690. // Width
  691. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:buttonSize];
  692. [tabBarController.view addConstraint:constraint];
  693. // Height
  694. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:buttonSize];
  695. [tabBarController.view addConstraint:constraint];
  696. } else {
  697. // X
  698. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:tabBarController.tabBar attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
  699. [tabBarController.view addConstraint:constraint];
  700. // Y
  701. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tabBarController.tabBar attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-(buttonSize / 2)];
  702. [tabBarController.view addConstraint:constraint];
  703. // Width
  704. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:buttonSize];
  705. [tabBarController.view addConstraint:constraint];
  706. // Height
  707. constraint = [NSLayoutConstraint constraintWithItem:buttonPlus attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:buttonSize];
  708. [tabBarController.view addConstraint:constraint];
  709. }
  710. }
  711. - (void)handleTouchTabbarCenter:(id)sender
  712. {
  713. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", self.account, self.activeServerUrl]];
  714. if ([tableDirectory.permissions containsString:@"CK"]) {
  715. [self showMenuInViewController:self.window.rootViewController];
  716. } else {
  717. [[NCContentPresenter shared] messageNotification:@"_warning_" description:@"_no_permission_add_file_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:k_CCErrorInternalError forced:false];
  718. }
  719. }
  720. #pragma --------------------------------------------------------------------------------------------
  721. #pragma mark ===== Theming Color =====
  722. #pragma --------------------------------------------------------------------------------------------
  723. - (void)settingThemingColorBrand
  724. {
  725. if (self.account.length == 0) { return; }
  726. if ([NCBrandOptions sharedInstance].use_themingColor) {
  727. NSString *themingColor = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.account elements:NCElementsJSON.shared.capabilitiesThemingColor];
  728. NSString *themingColorElement = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.account elements:NCElementsJSON.shared.capabilitiesThemingColorElement];
  729. NSString *themingColorText = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.account elements:NCElementsJSON.shared.capabilitiesThemingColorText];
  730. [CCGraphics settingThemingColor:themingColor themingColorElement:themingColorElement themingColorText:themingColorText];
  731. BOOL isTooLight = NCBrandColor.sharedInstance.brandElement.isTooLight;
  732. BOOL isTooDark = NCBrandColor.sharedInstance.brandElement.isTooDark;
  733. if (isTooLight) {
  734. NCBrandColor.sharedInstance.brandElement = [NCBrandColor.sharedInstance.brandElement darkerBy:30];
  735. } else if (isTooDark) {
  736. NCBrandColor.sharedInstance.brandElement = [NCBrandColor.sharedInstance.brandElement lighterBy:30];
  737. }
  738. } else {
  739. BOOL isTooLight = NCBrandColor.sharedInstance.customer.isTooLight;
  740. BOOL isTooDark = NCBrandColor.sharedInstance.customer.isTooDark;
  741. if (isTooLight) {
  742. NCBrandColor.sharedInstance.brandElement = [NCBrandColor.sharedInstance.customer darkerBy:30];
  743. } else if (isTooDark) {
  744. NCBrandColor.sharedInstance.brandElement = [NCBrandColor.sharedInstance.customer lighterBy:30];
  745. } else {
  746. NCBrandColor.sharedInstance.brandElement = NCBrandColor.sharedInstance.customer;
  747. }
  748. NCBrandColor.sharedInstance.brand = NCBrandColor.sharedInstance.customer;
  749. NCBrandColor.sharedInstance.brandText = NCBrandColor.sharedInstance.customerText;
  750. }
  751. [NCBrandColor.sharedInstance setDarkMode];
  752. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  753. [[NCCollectionCommon shared] createImagesThemingColor];
  754. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_changeTheming object:nil];
  755. });
  756. }
  757. - (void)changeTheming:(UIViewController *)viewController tableView:(UITableView *)tableView collectionView:(UICollectionView *)collectionView form:(BOOL)form
  758. {
  759. [NCBrandColor.sharedInstance setDarkMode];
  760. [self.window setTintColor:NCBrandColor.sharedInstance.textView];
  761. //Tab bar
  762. UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
  763. if ([splitViewController isKindOfClass:[UISplitViewController class]]) {
  764. UINavigationController *masterNavigationController = [splitViewController.viewControllers firstObject];
  765. if ([masterNavigationController isKindOfClass:[UINavigationController class]]) {
  766. UITabBarController *tabBarController = [masterNavigationController.viewControllers firstObject];
  767. if ([tabBarController isKindOfClass:[UITabBarController class]]) {
  768. tabBarController.tabBar.barTintColor = NCBrandColor.sharedInstance.backgroundView;
  769. tabBarController.tabBar.backgroundColor = NCBrandColor.sharedInstance.tabBar;
  770. tabBarController.tabBar.tintColor = NCBrandColor.sharedInstance.brandElement;
  771. [tabBarController.tabBar viewWithTag:99].backgroundColor = NCBrandColor.sharedInstance.brandElement;
  772. }
  773. }
  774. }
  775. // Nav bar
  776. [self configureNavBarForViewController:viewController];
  777. // View
  778. if (form) viewController.view.backgroundColor = NCBrandColor.sharedInstance.backgroundForm;
  779. else viewController.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  780. // TableView
  781. if (tableView) {
  782. if (form) tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm;
  783. else tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  784. tableView.separatorColor = NCBrandColor.sharedInstance.separator;
  785. [tableView reloadData];
  786. }
  787. // CollectionView
  788. if (collectionView) {
  789. if (form) collectionView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm;
  790. else collectionView.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  791. [collectionView reloadData];
  792. }
  793. }
  794. #pragma --------------------------------------------------------------------------------------------
  795. #pragma mark ===== Fetch =====
  796. #pragma --------------------------------------------------------------------------------------------
  797. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  798. {
  799. if (self.account.length == 0) {
  800. completionHandler(UIBackgroundFetchResultNoData);
  801. return;
  802. }
  803. [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch With Completion Handler"];
  804. // Verify new photo
  805. [[NCAutoUpload sharedInstance] initStateAutoUpload];
  806. // after 20 sec
  807. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  808. [[NCCommunicationCommon shared] writeLog:@"End 20 sec. perform Fetch With Completion Handler"];
  809. completionHandler(UIBackgroundFetchResultNoData);
  810. });
  811. }
  812. #pragma --------------------------------------------------------------------------------------------
  813. #pragma mark ===== Operation Networking & Session =====
  814. #pragma --------------------------------------------------------------------------------------------
  815. //
  816. // Method called by the system when all the background task has end
  817. //
  818. - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
  819. {
  820. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start handle Events For Background URLSession: %@", identifier]];
  821. [self updateApplicationIconBadgeNumber];
  822. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  823. self.backgroundSessionCompletionHandler = completionHandler;
  824. void (^completionHandler)() = self.backgroundSessionCompletionHandler;
  825. self.backgroundSessionCompletionHandler = nil;
  826. completionHandler();
  827. });
  828. }
  829. #pragma --------------------------------------------------------------------------------------------
  830. #pragma mark ===== OpenURL =====
  831. #pragma --------------------------------------------------------------------------------------------
  832. // Method called from iOS system to send a file from other app.
  833. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
  834. {
  835. if (self.account.length == 0) { return YES; }
  836. NSString *scheme = url.scheme;
  837. NSString *fileName;
  838. NSString *serverUrl;
  839. if ([scheme isEqualToString:@"nextcloud"]) {
  840. NSString *action = url.host;
  841. if ([action isEqualToString:@"open-file"]) {
  842. NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
  843. NSArray *queryItems = urlComponents.queryItems;
  844. NSString *user = [CCUtility valueForKey:@"user" fromQueryItems:queryItems];
  845. NSString *path = [CCUtility valueForKey:@"path" fromQueryItems:queryItems];
  846. NSString *link = [CCUtility valueForKey:@"link" fromQueryItems:queryItems];
  847. tableAccount *matchedAccount = nil;
  848. // verify parameter
  849. if (user.length == 0 || path.length == 0 || [[NSURL URLWithString:link] host].length == 0) {
  850. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_error_parameter_schema_", nil) preferredStyle:UIAlertControllerStyleAlert];
  851. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  852. [alertController addAction:okAction];
  853. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  854. } else {
  855. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  856. if (account) {
  857. NSURL *accountURL = [NSURL URLWithString:account.urlBase];
  858. NSString *accountUser = account.user;
  859. if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
  860. matchedAccount = account;
  861. } else {
  862. NSArray *accounts = [[NCManageDatabase sharedInstance] getAllAccount];
  863. for (tableAccount *account in accounts) {
  864. NSURL *accountURL = [NSURL URLWithString:account.urlBase];
  865. NSString *accountUser = account.user;
  866. if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
  867. matchedAccount = [[NCManageDatabase sharedInstance] setAccountActive:account.account];
  868. [self settingAccount:matchedAccount.account urlBase:matchedAccount.urlBase user:matchedAccount.user userID:matchedAccount.userID password:[CCUtility getPassword:matchedAccount.account]];
  869. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  870. }
  871. }
  872. }
  873. if (matchedAccount) {
  874. NSString *webDAV = [[NCUtility shared] getWebDAVWithAccount:self.account];
  875. if ([path containsString:@"/"]) {
  876. fileName = [path lastPathComponent];
  877. serverUrl = [NSString stringWithFormat:@"%@/%@/%@", matchedAccount.urlBase, webDAV, [path stringByDeletingLastPathComponent]];
  878. } else {
  879. fileName = path;
  880. serverUrl = [NSString stringWithFormat:@"%@/%@", matchedAccount.urlBase, webDAV];
  881. }
  882. [[NCCollectionCommon shared] openFileViewInFolderWithServerUrl:serverUrl fileName:fileName];
  883. } else {
  884. NSString *domain = [[NSURL URLWithString:link] host];
  885. NSString *fileName = [path lastPathComponent];
  886. NSString *message = [NSString stringWithFormat:NSLocalizedString(@"_account_not_available_", nil), user, domain, fileName];
  887. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  888. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  889. [alertController addAction:okAction];
  890. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  891. }
  892. }
  893. }
  894. }
  895. return YES;
  896. }
  897. NSError *error;
  898. NSLog(@"[LOG] the path is: %@", url.path);
  899. NSArray *splitedUrl = [url.path componentsSeparatedByString:@"/"];
  900. self.fileNameUpload = [NSString stringWithFormat:@"%@",[splitedUrl objectAtIndex:([splitedUrl count]-1)]];
  901. if (self.account) {
  902. [[NSFileManager defaultManager]removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:self.fileNameUpload] error:nil];
  903. [[NSFileManager defaultManager]moveItemAtPath:url.path toPath:[NSTemporaryDirectory() stringByAppendingString:self.fileNameUpload] error:&error];
  904. if (error == nil) {
  905. UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
  906. if ([splitViewController isKindOfClass:[UISplitViewController class]]) {
  907. UINavigationController *navigationControllerMaster = (UINavigationController *)splitViewController.viewControllers.firstObject;
  908. if ([navigationControllerMaster isKindOfClass:[UINavigationController class]]) {
  909. UIViewController *uploadNavigationViewController = [[UIStoryboard storyboardWithName:@"CCUploadFromOtherUpp" bundle:nil] instantiateViewControllerWithIdentifier:@"CCUploadNavigationViewController"];
  910. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  911. [navigationControllerMaster presentViewController:uploadNavigationViewController animated:YES completion:nil];
  912. });
  913. }
  914. }
  915. }
  916. }
  917. return YES;
  918. }
  919. #pragma --------------------------------------------------------------------------------------------
  920. #pragma mark ===== Passcode + Delegate =====
  921. #pragma --------------------------------------------------------------------------------------------
  922. - (void)passcodeWithAutomaticallyPromptForBiometricValidation:(BOOL)automaticallyPromptForBiometricValidation
  923. {
  924. LAContext *laContext = [LAContext new];
  925. NSError *error;
  926. BOOL isBiometryAvailable = false;
  927. if ([[CCUtility getPasscode] length] == 0 || [self.account length] == 0 || [CCUtility getNotPasscodeAtStart]) return;
  928. if (self.passcodeViewController == nil) {
  929. self.passcodeViewController = [[TOPasscodeViewController alloc] initWithStyle:TOPasscodeViewStyleTranslucentLight passcodeType:TOPasscodeTypeSixDigits];
  930. if (@available(iOS 13.0, *)) {
  931. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  932. self.passcodeViewController.style = TOPasscodeViewStyleTranslucentDark;
  933. }
  934. }
  935. self.passcodeViewController.delegate = self;
  936. self.passcodeViewController.keypadButtonShowLettering = false;
  937. if (CCUtility.getEnableTouchFaceID && [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  938. if (error == NULL) {
  939. if (laContext.biometryType == LABiometryTypeFaceID) {
  940. self.passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
  941. self.passcodeViewController.allowBiometricValidation = true;
  942. isBiometryAvailable = true;
  943. } else if (laContext.biometryType == LABiometryTypeTouchID) {
  944. self.passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
  945. self.passcodeViewController.allowBiometricValidation = true;
  946. isBiometryAvailable = true;
  947. } else {
  948. isBiometryAvailable = false;
  949. NSLog(@"No Biometric support");
  950. }
  951. }
  952. }
  953. [self.window.rootViewController presentViewController:self.passcodeViewController animated:YES completion:^{
  954. [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
  955. }];
  956. } else {
  957. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  958. [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
  959. });
  960. }
  961. }
  962. - (void)didInputCorrectPasscodeInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  963. {
  964. [passcodeViewController dismissViewControllerAnimated:YES completion:^{
  965. self.passcodeViewController = nil;
  966. }];
  967. }
  968. - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
  969. {
  970. return [code isEqualToString:[CCUtility getPasscode]];
  971. }
  972. - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  973. {
  974. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions sharedInstance] brand] reply:^(BOOL success, NSError * _Nullable error) {
  975. if (success) {
  976. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  977. [passcodeViewController dismissViewControllerAnimated:YES completion:^{
  978. self.passcodeViewController = nil;
  979. }];
  980. });
  981. }
  982. }];
  983. }
  984. - (void)enableTouchFaceID:(BOOL)automaticallyPromptForBiometricValidation
  985. {
  986. if (CCUtility.getEnableTouchFaceID && automaticallyPromptForBiometricValidation && self.passcodeViewController.view.window) {
  987. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions sharedInstance] brand] reply:^(BOOL success, NSError * _Nullable error) {
  988. if (success) {
  989. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  990. [self.passcodeViewController dismissViewControllerAnimated:YES completion:^{
  991. self.passcodeViewController = nil;
  992. }];
  993. });
  994. }
  995. }];
  996. }
  997. }
  998. @end