-
Notifications
You must be signed in to change notification settings - Fork 24
/
ImportFileManager.m
141 lines (102 loc) · 4.9 KB
/
ImportFileManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2022 DolphiniOS Project
// SPDX-License-Identifier: GPL-2.0-or-later
#import "ImportFileManager.h"
#import "Swift.h"
#import "GameFileCacheManager.h"
#import "LocalizationUtil.h"
#import "MainSceneCoordinator.h"
@implementation ImportFileManager {
UIWindow* _window;
}
+ (ImportFileManager*)shared {
static ImportFileManager* sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (void)showWindowOnScene:(UIWindowScene*)scene {
self->_window = [[UIWindow alloc] initWithWindowScene:scene];
self->_window.frame = [UIScreen mainScreen].bounds;
self->_window.rootViewController = [[UIViewController alloc] init];
self->_window.windowLevel = UIWindowLevelAlert;
UIWindow* topWindow = scene.windows.lastObject;
self->_window.windowLevel = topWindow.windowLevel + 1;
[self->_window makeKeyAndVisible];
}
- (void)hideWindow {
[self->_window setHidden:true];
self->_window = nil;
}
- (void)presentViewControllerOnWindow:(UIViewController*)controller {
[self->_window.rootViewController presentViewController:controller animated:true completion:nil];
}
- (void)importFileAtUrl:(NSURL*)url {
UIWindowScene* mainScene = [MainSceneCoordinator shared].mainScene;
if (mainScene == nil) {
return;
}
[self showWindowOnScene:mainScene];
if (![url startAccessingSecurityScopedResource]) {
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:DOLCoreLocalizedString(@"Error") message:@"Failed to start accessing security scoped resource." preferredStyle:UIAlertControllerStyleAlert];
[errorAlert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"OK") style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
[self hideWindow];
}]];
[self presentViewControllerOnWindow:errorAlert];
return;
}
void (^finish)(void) = ^void() {
[url stopAccessingSecurityScopedResource];
[[GameFileCacheManager sharedManager] rescanAndFetchMetadataWithCompletionHandler:nil];
[self hideWindow];
};
NSString* sourcePath = [url path];
NSString* destinationPath = [[UserFolderUtil getSoftwareFolder] stringByAppendingPathComponent:[sourcePath lastPathComponent]];
NSFileManager* fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:destinationPath]) {
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:DOLCoreLocalizedString(@"Error") message:@"This software has already been imported." preferredStyle:UIAlertControllerStyleAlert];
[errorAlert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"OK") style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
finish();
}]];
[self presentViewControllerOnWindow:errorAlert];
return;
}
UIAlertController* alert = [UIAlertController alertControllerWithTitle:DOLCoreLocalizedString(@"Import") message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"Copy") style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
NSError* error = nil;
if (![fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error]) {
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:DOLCoreLocalizedString(@"Error") message:[NSString stringWithFormat:@"The copy operation failed.\n\n%@", error.localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[errorAlert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"OK") style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
finish();
}]];
[self presentViewControllerOnWindow:errorAlert];
} else {
finish();
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"Move") style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
NSError* error = nil;
if (![fileManager moveItemAtPath:sourcePath toPath:destinationPath error:&error]) {
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:DOLCoreLocalizedString(@"Error") message:[NSString stringWithFormat:@"The move operation failed.\n\n%@", error.localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[errorAlert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"OK") style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
finish();
}]];
[self presentViewControllerOnWindow:errorAlert];
} else {
finish();
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:DOLCoreLocalizedString(@"Cancel") style:UIAlertActionStyleCancel
handler:^(UIAlertAction* action) {
finish();
}]];
[self presentViewControllerOnWindow:alert];
}
@end