Skip to content

Commit fac0135

Browse files
committed
save/load config
1 parent 0255dfb commit fac0135

File tree

5 files changed

+484
-149
lines changed

5 files changed

+484
-149
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "2047E2EDDEDEC0F33DD5EFEE76F06B28FF502F04",
3+
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {
4+
5+
},
6+
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
7+
"2047E2EDDEDEC0F33DD5EFEE76F06B28FF502F04" : 9223372036854775807,
8+
"1510FF3A5601E035E47D33706645B10508EC3DB8" : 9223372036854775807
9+
},
10+
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "9F16901C-B36F-4572-80F2-05829785679E",
11+
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
12+
"2047E2EDDEDEC0F33DD5EFEE76F06B28FF502F04" : "CoprightModifier\/",
13+
"1510FF3A5601E035E47D33706645B10508EC3DB8" : "DDUtils\/"
14+
},
15+
"DVTSourceControlWorkspaceBlueprintNameKey" : "CopyrightModifier",
16+
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
17+
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "CopyrightModifier.xcodeproj",
18+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
19+
{
20+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "github.com:Daij-Djan\/DDUtils.git",
21+
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
22+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "1510FF3A5601E035E47D33706645B10508EC3DB8"
23+
},
24+
{
25+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "github.com:Daij-Djan\/CopyrightModifier.git",
26+
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
27+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "2047E2EDDEDEC0F33DD5EFEE76F06B28FF502F04"
28+
}
29+
]
30+
}

CopyrightModifier/AppDelegate.swift

Lines changed: 175 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1515
#if DEBUG
1616
let env = ProcessInfo.processInfo.environment
1717
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
2119
}
2220
#endif
2321
}
@@ -26,7 +24,180 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2624
return true
2725
}
2826

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+
29170
// 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+
}
30201

31-
@IBOutlet var windowController: NSWindowController!
202+
@IBOutlet var windowController: MainWindowController!
32203
}

CopyrightModifier/Info.plist

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
<dict>
55
<key>CFBundleDevelopmentRegion</key>
66
<string>en</string>
7+
<key>CFBundleDocumentTypes</key>
8+
<array>
9+
<dict>
10+
<key>CFBundleTypeExtensions</key>
11+
<array>
12+
<string>crmconfig</string>
13+
</array>
14+
<key>CFBundleTypeIconFile</key>
15+
<string>myicon</string>
16+
<key>CFBundleTypeName</key>
17+
<string>Copyright Modifier Configuration</string>
18+
<key>CFBundleTypeRole</key>
19+
<string>Editor</string>
20+
<key>LSHandlerRank</key>
21+
<string>Owner</string>
22+
<key>LSItemContentTypes</key>
23+
<array>
24+
<string>crm.config</string>
25+
</array>
26+
<key>LSTypeIsPackage</key>
27+
<integer>0</integer>
28+
</dict>
29+
</array>
730
<key>CFBundleExecutable</key>
831
<string>$(EXECUTABLE_NAME)</string>
932
<key>CFBundleIconFile</key>
@@ -30,5 +53,27 @@
3053
<string>MainMenu</string>
3154
<key>NSPrincipalClass</key>
3255
<string>NSApplication</string>
56+
<key>UTExportedTypeDeclarations</key>
57+
<array>
58+
<dict>
59+
<key>UTTypeConformsTo</key>
60+
<array>
61+
<string>public.text</string>
62+
</array>
63+
<key>UTTypeDescription</key>
64+
<string>Copyright Modifier Configuration</string>
65+
<key>UTTypeIconFile</key>
66+
<string>myicon</string>
67+
<key>UTTypeIdentifier</key>
68+
<string>crm.config</string>
69+
<key>UTTypeTagSpecification</key>
70+
<dict>
71+
<key>public.filename-extension</key>
72+
<array>
73+
<string>crmconfig</string>
74+
</array>
75+
</dict>
76+
</dict>
77+
</array>
3378
</dict>
3479
</plist>

0 commit comments

Comments
 (0)