CSSession+Sharing.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "CSSession+Sharing.h"
  17. #import <spice-client.h>
  18. static const NSString *const kDefaultShareReadme =
  19. @"You have not selected a shared directory. This is a temporary directory "
  20. "that can be deleted at any time. You can access these files on the host "
  21. "at ~/Documents/Public relative to UTM's sandbox (if enabled). To select a "
  22. "permanent shared directory, shut down the VM and select a shared "
  23. "directory from the VM details screen.";
  24. @implementation CSSession (Sharing)
  25. - (void)setSharedDirectory:(NSString *)path readOnly:(BOOL)readOnly {
  26. g_object_set(self.session, "shared-dir", [path cStringUsingEncoding:NSUTF8StringEncoding], NULL);
  27. g_object_set(self.session, "share-dir-ro", readOnly, NULL);
  28. }
  29. - (NSURL *)defaultPublicShare {
  30. NSFileManager *fileManager = [NSFileManager defaultManager];
  31. NSURL *documentsDir = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
  32. NSURL *publicShare = [documentsDir URLByAppendingPathComponent:@"Public" isDirectory:YES];
  33. BOOL isDir = NO;
  34. if (![fileManager fileExistsAtPath:publicShare.path isDirectory:&isDir] || !isDir) {
  35. [fileManager removeItemAtURL:publicShare error:nil]; // remove file if exists
  36. [fileManager createDirectoryAtURL:publicShare withIntermediateDirectories:NO attributes:nil error:nil];
  37. }
  38. return publicShare;
  39. }
  40. - (void)createDefaultShareReadme {
  41. NSFileManager *fileManager = [NSFileManager defaultManager];
  42. NSURL *readme = [self.defaultPublicShare URLByAppendingPathComponent:@"README.txt"];
  43. if (![fileManager fileExistsAtPath:readme.path]) {
  44. [kDefaultShareReadme writeToURL:readme atomically:YES encoding:NSASCIIStringEncoding error:nil];
  45. }
  46. }
  47. @end