CCManageCameraUpload.m 19 KB

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