@@ -15,9 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
15
15
#if DEBUG
16
16
let env = ProcessInfo . processInfo. environment
17
17
if let path = env [ " UI_TESTING_PATH " ] {
18
- if let main = windowController as? MainWindowController {
19
- main. path. stringValue = path
20
- }
18
+ windowController. path. stringValue = path
21
19
}
22
20
#endif
23
21
}
@@ -26,7 +24,180 @@ class AppDelegate: NSObject, NSApplicationDelegate {
26
24
return true
27
25
}
28
26
27
+ func application( _ sender: NSApplication , openFile filename: String ) -> Bool {
28
+ #if !DEBUG
29
+ let url = URL ( fileURLWithPath: filename)
30
+ loadOptionsFromFile ( url)
31
+ return true
32
+ #endif
33
+
34
+ return false
35
+ }
36
+
37
+ // MARK: load/save
38
+
39
+ func loadOptionsFromFile( _ fileURL : URL ) {
40
+ let dict : NSDictionary
41
+ do {
42
+ let data = try Data ( contentsOf: fileURL)
43
+ dict = try JSONSerialization . jsonObject ( with: data, options: [ ] ) as! NSDictionary
44
+ }
45
+ catch _ {
46
+ print ( " failed to read saved data " )
47
+ return
48
+ }
49
+
50
+ let od = windowController. path. delegate
51
+ windowController. path. delegate = nil
52
+
53
+ //folder or file
54
+ do {
55
+ if let folderBookmarkData = Data ( base64Encoded: dict [ " path " ] as? String ?? " " ) {
56
+ var isStale = false
57
+ if let url = try URL ( resolvingBookmarkData: folderBookmarkData, options: . withSecurityScope, relativeTo: nil , bookmarkDataIsStale: & isStale) {
58
+ windowController. folderURL = url
59
+ windowController. path. stringValue = url. path
60
+ }
61
+ }
62
+ if let gitBookmarkData = Data ( base64Encoded: dict [ " gitPath " ] as? String ?? " " ) {
63
+ var isStale = false
64
+ if let gitURL = try URL ( resolvingBookmarkData: gitBookmarkData, options: . withSecurityScope, relativeTo: nil , bookmarkDataIsStale: & isStale) {
65
+ windowController. gitURL = gitURL
66
+ }
67
+ }
68
+ }
69
+ catch _ {
70
+ print ( " failed to load bookmarks " )
71
+ }
72
+
73
+ //search options
74
+ windowController. recursive. state = ( dict [ " recursive " ] as? Bool ?? false ) ? NSOnState : NSOffState
75
+ windowController. hiddenFiles. state = ( dict [ " hiddenFiles " ] as? Bool ?? false ) ? NSOnState : NSOffState
76
+ windowController. patterns. stringValue = dict [ " patterns " ] as? String ?? " "
77
+ windowController. foldersToSkip. stringValue = dict [ " foldersToSkip " ] as? String ?? " "
78
+
79
+ //change options
80
+ windowController. authorOptions. selectCell ( withTag: dict [ " authorOptions " ] as? Int ?? 1 )
81
+ windowController. fixedAuthor. stringValue = dict [ " fixedAuthor " ] as? String ?? " "
82
+ windowController. dateOptions. selectCell ( withTag: dict [ " dateOptions " ] as? Int ?? 1 )
83
+ windowController. fixedDate. dateValue = Date ( timeIntervalSince1970: dict [ " fixedDate " ] as? Double ?? 0 )
84
+
85
+ windowController. ownerOptions. selectCell ( withTag: dict [ " ownerOptions " ] as? Int ?? 1 )
86
+ windowController. fixedOwner. stringValue = dict [ " fixedOwner " ] as? String ?? " "
87
+ windowController. yearOptions. selectCell ( withTag: dict [ " yearOptions " ] as? Int ?? 1 )
88
+ windowController. fixedYear. stringValue = dict [ " fixedYear " ] as? String ?? " "
89
+ windowController. copyrightYearTillNow. state = ( dict [ " copyrightYearTillNow " ] as? Bool ?? false ) ? NSOnState : NSOffState
90
+ windowController. licenseOptions. selectCell ( withTag: dict [ " licenseOptions " ] as? Int ?? 10 )
91
+ windowController. theLicenseURL. stringValue = dict [ " theLicenseURL " ] as? String ?? " "
92
+ let urlStr = dict [ " loadedLicenseURL " ] as? String
93
+ windowController. loadedLicenseURL = urlStr != nil ? URL ( string: urlStr!) : nil
94
+
95
+ //the template to change
96
+ windowController. theLicenseText. string = dict [ " theLicenseText " ] as? String ?? " "
97
+ windowController. newTemplate. string = dict [ " newTemplate " ] as? String ?? " "
98
+
99
+ //operations
100
+ windowController. backupOld. state = ( dict [ " backupOld " ] as? Bool ?? false ) ? NSOnState : NSOffState
101
+ windowController. writeLicenseFile. state = ( dict [ " writeLicenseFile " ] as? Bool ?? false ) ? NSOnState : NSOffState
102
+ windowController. removeOld. stringValue = dict [ " removeOld " ] as? String ?? " "
103
+
104
+ windowController. path. delegate = od
105
+
106
+ windowController. touchedOptions = true
107
+ windowController. updateUI ( )
108
+ }
109
+
110
+ func saveOptionsToFile( _ fileURL : URL ) {
111
+ let dict = NSMutableDictionary ( )
112
+
113
+ //folder or file
114
+ var bookmarkFolder : Data
115
+ var bookmarkGit : Data
116
+ do {
117
+ if let url = windowController. folderURL {
118
+ bookmarkFolder = try url. bookmarkData ( options: . withSecurityScope, includingResourceValuesForKeys: nil , relativeTo: nil )
119
+ dict [ " path " ] = bookmarkFolder. base64EncodedString ( )
120
+ }
121
+ if let url = windowController. gitURL {
122
+ bookmarkGit = try url. bookmarkData ( options: . withSecurityScope, includingResourceValuesForKeys: nil , relativeTo: nil )
123
+ dict [ " gitPath " ] = bookmarkGit. base64EncodedString ( )
124
+ }
125
+ }
126
+ catch _ {
127
+ print ( " failed to save a bookmark " )
128
+ }
129
+
130
+ //search options
131
+ dict [ " recursive " ] = windowController. recursive. state == NSOnState
132
+ dict [ " hiddenFiles " ] = windowController. hiddenFiles. state == NSOnState
133
+ dict [ " patterns " ] = windowController. patterns. stringValue
134
+ dict [ " foldersToSkip " ] = windowController. foldersToSkip. stringValue
135
+
136
+ //change options
137
+ dict [ " authorOptions " ] = windowController. authorOptions. selectedTag ( )
138
+ dict [ " fixedAuthor " ] = windowController. fixedAuthor. stringValue
139
+ dict [ " dateOptions " ] = windowController. dateOptions. selectedTag ( )
140
+ dict [ " fixedDate " ] = windowController. fixedDate. dateValue. timeIntervalSince1970
141
+
142
+ dict [ " ownerOptions " ] = windowController. ownerOptions. selectedTag ( )
143
+ dict [ " fixedOwner " ] = windowController. fixedOwner. stringValue
144
+ dict [ " yearOptions " ] = windowController. yearOptions. selectedTag ( )
145
+ dict [ " fixedYear " ] = windowController. fixedYear. stringValue
146
+ dict [ " copyrightYearTillNow " ] = windowController. copyrightYearTillNow. state == NSOnState
147
+ dict [ " licenseOptions " ] = windowController. licenseOptions. selectedTag ( )
148
+ dict [ " theLicenseURL " ] = windowController. theLicenseURL. stringValue
149
+ dict [ " loadedLicenseURL " ] = windowController. loadedLicenseURL? . absoluteString ?? " "
150
+
151
+ //the template to change
152
+ dict [ " theLicenseText " ] = windowController. theLicenseText. string ?? " "
153
+ dict [ " newTemplate " ] = windowController. newTemplate. string ?? " "
154
+
155
+ //operations
156
+ dict [ " backupOld " ] = windowController. backupOld. state == NSOnState
157
+ dict [ " writeLicenseFile " ] = windowController. writeLicenseFile. state == NSOnState
158
+ dict [ " removeOld " ] = windowController. removeOld. stringValue
159
+ print ( dict)
160
+ //write it
161
+ do {
162
+ let data = try JSONSerialization . data ( withJSONObject: dict, options: . prettyPrinted)
163
+ try data. write ( to: fileURL)
164
+ }
165
+ catch _ {
166
+ print ( " failed to write data " )
167
+ }
168
+ }
169
+
29
170
// MARK: IB
171
+
172
+ @IBAction func load( _ sender: AnyObject ) {
173
+ let panel = NSOpenPanel ( )
174
+ panel. title = " Load settings "
175
+ panel. allowedFileTypes = [ " crmconfig " ]
176
+
177
+ panel. begin { ( result) -> Void in
178
+ if result == NSFileHandlingPanelOKButton {
179
+ if let fileURL = panel. urls. first {
180
+ self . loadOptionsFromFile ( fileURL)
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ @IBAction func save( _ sender: AnyObject ) {
187
+ let panel = NSSavePanel ( )
188
+ panel. title = " Save settings "
189
+ panel. nameFieldStringValue = " Copyright Configuration "
190
+ panel. allowedFileTypes = [ " crmconfig " ]
191
+ panel. allowsOtherFileTypes = false
192
+ panel. canCreateDirectories = true
193
+ panel. begin { ( result) -> Void in
194
+ if result == NSFileHandlingPanelOKButton {
195
+ if let fileURL = panel. url {
196
+ self . saveOptionsToFile ( fileURL)
197
+ }
198
+ }
199
+ }
200
+ }
30
201
31
- @IBOutlet var windowController : NSWindowController !
202
+ @IBOutlet var windowController : MainWindowController !
32
203
}
0 commit comments