CCManageCameraUpload.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. //
  2. // CCManageCameraUpload.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/09/15.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCManageCameraUpload.h"
  24. #import "AppDelegate.h"
  25. #ifdef CUSTOM_BUILD
  26. #import "CustomSwift.h"
  27. #else
  28. #import "Nextcloud-Swift.h"
  29. #endif
  30. @implementation CCManageCameraUpload
  31. // From Settings
  32. - (id)initWithCoder:(NSCoder *)aDecoder
  33. {
  34. if (self = [super initWithCoder:aDecoder]) {
  35. [self initializeForm];
  36. }
  37. return self;
  38. }
  39. // From Photos
  40. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  41. {
  42. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  43. if (self) {
  44. UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
  45. self.navigationItem.rightBarButtonItem = doneButton;
  46. [self initializeForm];
  47. }
  48. return self;
  49. }
  50. - (void)initializeForm
  51. {
  52. XLFormDescriptor *form ;
  53. XLFormSectionDescriptor *section;
  54. XLFormRowDescriptor *row;
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  56. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_uploading_from_camera_", nil)];
  57. // Camera Upload
  58. section = [XLFormSectionDescriptor formSection];
  59. [form addFormSection:section];
  60. section.footerTitle = NSLocalizedString(@"_photo_folder_photocamera_", nil);
  61. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"cameraupload" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_upload_camera_", nil)];
  62. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) row.value = @1;
  63. else row.value = @0;
  64. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  65. [section addFormRow:row];
  66. // Camera Upload Photo
  67. section = [XLFormSectionDescriptor formSection];
  68. [form addFormSection:section];
  69. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadphoto" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_upload_foto_camera_", nil)];
  70. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  71. if ([CCCoreData getCameraUploadPhotoActiveAccount:app.activeAccount]) row.value = @1;
  72. else row.value = @0;
  73. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  74. [section addFormRow:row];
  75. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadwwanphoto" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_wifi_only_", nil)];
  76. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  77. if ([CCCoreData getCameraUploadWWanPhotoActiveAccount:app.activeAccount] == YES) row.value = @1;
  78. else row.value = @0;
  79. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  80. [section addFormRow:row];
  81. // Camera Upload Video
  82. section = [XLFormSectionDescriptor formSection];
  83. [form addFormSection:section];
  84. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadvideo" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_upload_video_camera_", nil)];
  85. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  86. if ([CCCoreData getCameraUploadVideoActiveAccount:app.activeAccount]) row.value = @1;
  87. else row.value = @0;
  88. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  89. [section addFormRow:row];
  90. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadwwanvideo" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_wifi_only_", nil)];
  91. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  92. if ([CCCoreData getCameraUploadWWanVideoActiveAccount:app.activeAccount] == YES) row.value = @1;
  93. else row.value = @0;
  94. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  95. [section addFormRow:row];
  96. // Camera Upload Background
  97. section = [XLFormSectionDescriptor formSection];
  98. [form addFormSection:section];
  99. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadbackground" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_upload_camera_background_", nil)];
  100. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  101. if ([CCCoreData getCameraUploadBackgroundActiveAccount:app.activeAccount]) row.value = @1;
  102. else row.value = @0;
  103. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  104. [section addFormRow:row];
  105. // Camera Upload All Photo
  106. section = [XLFormSectionDescriptor formSection];
  107. [form addFormSection:section];
  108. NSString *title = NSLocalizedString(@"_upload_camera_fullphotos_", nil);
  109. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadallphotos" rowType:XLFormRowDescriptorTypeBooleanSwitch title:title];
  110. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  111. row.value = 0;
  112. if ([CCCoreData getCameraUploadFullPhotosActiveAccount:app.activeAccount]) row.value = @1;
  113. else row.value = @0;
  114. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  115. [section addFormRow:row];
  116. // Camera Upload create subfolder
  117. section = [XLFormSectionDescriptor formSection];
  118. [form addFormSection:section];
  119. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"camerauploadcreatesubfolder" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_upload_camera_create_subfolder_", nil)];
  120. row.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  121. if ([CCCoreData getCameraUploadCreateSubfolderActiveAccount:app.activeAccount]) row.value = @1;
  122. else row.value = @0;
  123. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  124. [section addFormRow:row];
  125. // end
  126. section = [XLFormSectionDescriptor formSection];
  127. [form addFormSection:section];
  128. self.form = form;
  129. }
  130. // Apparirà
  131. - (void)viewWillAppear:(BOOL)animated
  132. {
  133. [super viewWillAppear:animated];
  134. self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  135. // Color
  136. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  137. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  138. // Request permission for camera roll access
  139. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  140. switch (status) {
  141. case PHAuthorizationStatusRestricted:
  142. NSLog(@"[LOG] user can't grant access to camera roll");
  143. break;
  144. case PHAuthorizationStatusDenied:
  145. NSLog(@"[LOG] user denied access to camera roll");
  146. break;
  147. default:
  148. break;
  149. }
  150. }];
  151. [self reloadForm];
  152. }
  153. - (void)changeTheming
  154. {
  155. if (self.isViewLoaded && self.view.window)
  156. [app changeTheming:self];
  157. }
  158. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  159. {
  160. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  161. if ([rowDescriptor.tag isEqualToString:@"cameraupload"]) {
  162. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  163. // Create Folder cameraUpload
  164. if (app.activeMain)
  165. [app.activeMain createFolderCameraUpload];
  166. [CCCoreData setCameraUpload:YES activeAccount:app.activeAccount];
  167. // Default
  168. [CCCoreData setCameraUploadFolderName:nil activeAccount:app.activeAccount];
  169. [CCCoreData setCameraUploadFolderPath:nil activeUrl:app.activeUrl activeAccount:app.activeAccount];
  170. // verifichiamo che almeno uno dei servizi (foto video) siano attivi, in caso contrario attiviamo le foto
  171. if ([CCCoreData getCameraUploadPhotoActiveAccount:app.activeAccount] == NO && [CCCoreData getCameraUploadVideoActiveAccount:app.activeAccount] == NO)
  172. [CCCoreData setCameraUploadPhoto:YES activeAccount:app.activeAccount];
  173. // Settings date
  174. if([CCCoreData getCameraUploadPhotoActiveAccount:app.activeAccount])
  175. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  176. if([CCCoreData getCameraUploadVideoActiveAccount:app.activeAccount])
  177. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  178. } else {
  179. [CCCoreData setCameraUpload:NO activeAccount:app.activeAccount];
  180. [CCCoreData setCameraUploadFullPhotosActiveAccount:NO activeAccount:app.activeAccount];
  181. [CCCoreData setCameraUploadDatePhoto:NULL];
  182. [CCCoreData setCameraUploadDateVideo:NULL];
  183. // remove
  184. [[NCManageDatabase sharedInstance] clearTable:[tableAutomaticUpload class] account:app.activeAccount];
  185. }
  186. // Initialize Camera Upload
  187. [[NSNotificationCenter defaultCenter] postNotificationName:@"initStateCameraUpload" object:nil];
  188. [self reloadForm];
  189. }
  190. if ([rowDescriptor.tag isEqualToString:@"camerauploadbackground"]) {
  191. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  192. BOOL isLocationIsEnabled = NO;
  193. if (app.activePhotosCameraUpload)
  194. [app.activePhotosCameraUpload checkIfLocationIsEnabled];
  195. if(isLocationIsEnabled == YES) {
  196. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_cameraupload_background_title_", nil) message:[CCUtility localizableBrand:@"_cameraupload_background_msg_" table:nil] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  197. [alertView show];
  198. [CCCoreData setCameraUploadBackground:YES activeAccount:app.activeAccount];
  199. } else {
  200. [self reloadForm];
  201. }
  202. } else {
  203. [CCCoreData setCameraUploadBackground:NO activeAccount:app.activeAccount];
  204. [[CCManageLocation sharedSingleton] stopSignificantChangeUpdates];
  205. }
  206. }
  207. if ([rowDescriptor.tag isEqualToString:@"camerauploadallphotos"]) {
  208. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  209. [[NSNotificationCenter defaultCenter] postNotificationName:@"setupCameraUploadFull" object:nil];
  210. [CCCoreData setCameraUploadFullPhotosActiveAccount:YES activeAccount:app.activeAccount];
  211. } else {
  212. [[NCManageDatabase sharedInstance] clearTable:[tableAutomaticUpload class] account:app.activeAccount];
  213. [CCCoreData setCameraUploadFullPhotosActiveAccount:NO activeAccount:app.activeAccount];
  214. }
  215. }
  216. if ([rowDescriptor.tag isEqualToString:@"camerauploadphoto"]) {
  217. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  218. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  219. } else {
  220. [CCCoreData setCameraUploadDatePhoto:NULL];
  221. }
  222. [CCCoreData setCameraUploadPhoto:[[rowDescriptor.value valueData] boolValue] activeAccount:app.activeAccount];
  223. }
  224. if ([rowDescriptor.tag isEqualToString:@"camerauploadwwanphoto"]) {
  225. [CCCoreData setCameraUploadWWanPhoto:[[rowDescriptor.value valueData] boolValue] activeAccount:app.activeAccount];
  226. }
  227. if ([rowDescriptor.tag isEqualToString:@"camerauploadvideo"]) {
  228. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  229. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  230. } else {
  231. [CCCoreData setCameraUploadDateVideo:NULL];
  232. }
  233. [CCCoreData setCameraUploadVideo:[[rowDescriptor.value valueData] boolValue] activeAccount:app.activeAccount];
  234. }
  235. if ([rowDescriptor.tag isEqualToString:@"camerauploadwwanvideo"]) {
  236. [CCCoreData setCameraUploadWWanVideo:[[rowDescriptor.value valueData] boolValue] activeAccount:app.activeAccount];
  237. }
  238. if ([rowDescriptor.tag isEqualToString:@"camerauploadcreatesubfolder"]) {
  239. [CCCoreData setCameraUploadCreateSubfolderActiveAccount:[[rowDescriptor.value valueData] boolValue] activeAccount:app.activeAccount];
  240. }
  241. }
  242. - (void)done:(XLFormRowDescriptor *)sender
  243. {
  244. [self dismissViewControllerAnimated:YES completion:nil];
  245. }
  246. - (void)reloadForm
  247. {
  248. self.form.delegate = nil;
  249. XLFormRowDescriptor *rowCameraupload = [self.form formRowWithTag:@"cameraupload"];
  250. XLFormRowDescriptor *rowCamerauploadphoto = [self.form formRowWithTag:@"camerauploadphoto"];
  251. XLFormRowDescriptor *rowCamerauploadcryptatedphoto = [self.form formRowWithTag:@"camerauploadcryptatedphoto"];
  252. XLFormRowDescriptor *rowCamerauploadwwanphoto = [self.form formRowWithTag:@"camerauploadwwanphoto"];
  253. XLFormRowDescriptor *rowCamerauploadvideo = [self.form formRowWithTag:@"camerauploadvideo"];
  254. XLFormRowDescriptor *rowCamerauploadcryptatedvideo = [self.form formRowWithTag:@"camerauploadcryptatedvideo"];
  255. XLFormRowDescriptor *rowCamerauploadwwanvideo = [self.form formRowWithTag:@"camerauploadwwanvideo"];
  256. XLFormRowDescriptor *rowCamerauploadBackground = [self.form formRowWithTag:@"camerauploadbackground"];
  257. XLFormRowDescriptor *rowCamerauploadAllPhotos = [self.form formRowWithTag:@"camerauploadallphotos"];
  258. XLFormRowDescriptor *rowCamerauploadCreateSubfolder = [self.form formRowWithTag:@"camerauploadcreatesubfolder"];
  259. // - STATUS ---------------------
  260. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount])
  261. [rowCameraupload setValue:@1]; else [rowCameraupload setValue:@0];
  262. if ([CCCoreData getCameraUploadPhotoActiveAccount:app.activeAccount])
  263. [rowCamerauploadphoto setValue:@1]; else [rowCamerauploadphoto setValue:@0];
  264. if ([CCCoreData getCameraUploadWWanPhotoActiveAccount:app.activeAccount])
  265. [rowCamerauploadwwanphoto setValue:@1]; else [rowCamerauploadwwanphoto setValue:@0];
  266. if ([CCCoreData getCameraUploadVideoActiveAccount:app.activeAccount])
  267. [rowCamerauploadvideo setValue:@1]; else [rowCamerauploadvideo setValue:@0];
  268. if ([CCCoreData getCameraUploadWWanVideoActiveAccount:app.activeAccount])
  269. [rowCamerauploadwwanvideo setValue:@1]; else [rowCamerauploadwwanvideo setValue:@0];
  270. if ([CCCoreData getCameraUploadBackgroundActiveAccount:app.activeAccount])
  271. [rowCamerauploadBackground setValue:@1]; else [rowCamerauploadBackground setValue:@0];
  272. if ([CCCoreData getCameraUploadFullPhotosActiveAccount:app.activeAccount])
  273. [rowCamerauploadAllPhotos setValue:@1]; else [rowCamerauploadAllPhotos setValue:@0];
  274. if ([CCCoreData getCameraUploadCreateSubfolderActiveAccount:app.activeAccount])
  275. [rowCamerauploadCreateSubfolder setValue:@1]; else [rowCamerauploadCreateSubfolder setValue:@0];
  276. // - HIDDEN ---------------------
  277. rowCamerauploadphoto.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  278. rowCamerauploadcryptatedphoto.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  279. rowCamerauploadwwanphoto.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  280. rowCamerauploadvideo.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  281. rowCamerauploadcryptatedvideo.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  282. rowCamerauploadwwanvideo.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  283. rowCamerauploadBackground.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  284. rowCamerauploadAllPhotos.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  285. rowCamerauploadCreateSubfolder.hidden = [NSString stringWithFormat:@"$%@==0", @"cameraupload"];
  286. // ----------------------
  287. [self.tableView reloadData];
  288. self.form.delegate = self;
  289. }
  290. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  291. {
  292. NSString *sectionName;
  293. switch (section)
  294. {
  295. case 0:
  296. sectionName = NSLocalizedString(@"_uploading_from_camera_", nil);
  297. break;
  298. case 1:
  299. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = NSLocalizedString(@"_upload_automatic_photos_", nil);
  300. else sectionName = @"";
  301. break;
  302. case 2:
  303. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = NSLocalizedString(@"_upload_automatic_videos_", nil);
  304. else sectionName = @"";
  305. break;
  306. case 3:
  307. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = NSLocalizedString(@"_upload_camera_background_", nil);
  308. else sectionName = @"";
  309. break;
  310. case 4:
  311. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = NSLocalizedString(@"_upload_camera_fullphotos_", nil);
  312. else sectionName = @"";
  313. break;
  314. case 5:
  315. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = NSLocalizedString(@"_upload_camera_create_subfolder_", nil);
  316. else sectionName = @"";
  317. break;
  318. }
  319. return sectionName;
  320. }
  321. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  322. {
  323. NSString *sectionName;
  324. switch (section)
  325. {
  326. case 0:
  327. sectionName = [CCUtility localizableBrand:@"_photo_folder_photocamera_" table:nil];
  328. break;
  329. case 3:
  330. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = [CCUtility localizableBrand:@"_photo_folder_background_" table:nil];
  331. else sectionName = @"";
  332. break;
  333. case 4:
  334. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = [CCUtility localizableBrand:@"_upload_camera_fullphotos_footer_" table:nil];
  335. else sectionName = @"";
  336. break;
  337. case 5:
  338. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) sectionName = [CCUtility localizableBrand:@"_upload_camera_create_subfolder_footer_" table:nil];
  339. else sectionName = @"";
  340. break;
  341. }
  342. return sectionName;
  343. }
  344. @end