Skip to content

Commit

Permalink
scripting: expose icon in config
Browse files Browse the repository at this point in the history
* Allows to get/set built-in icons
iconURL is URL in UTMConfig, but stored as string in config.plist, same is done for scripting interface.
  • Loading branch information
naveenrajm7 committed Dec 23, 2024
1 parent 8ef19ff commit 26fe0de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Scripting/UTM.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@
<record-type name="qemu configuration" code="QeCf" description="QEMU virtual machine configuration.">
<property name="name" code="pnam" type="text"
description="Virtual machine name."/>

<property name="icon" code="IcOn" type="text"
description="Virtual machine icon."/>

<property name="notes" code="NoTe" type="text"
description="User-specified notes."/>
Expand Down Expand Up @@ -567,6 +570,9 @@
<property name="name" code="pnam" type="text"
description="Virtual machine name."/>

<property name="icon" code="IcOn" type="text"
description="Virtual machine icon."/>

<property name="notes" code="NoTe" type="text"
description="User-specified notes."/>

Expand Down
18 changes: 18 additions & 0 deletions Scripting/UTMScriptingConfigImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extension UTMScriptingConfigImpl {
private func serializeQemuConfiguration(_ config: UTMQemuConfiguration) -> [AnyHashable : Any] {
[
"name": config.information.name,
"icon": config.information.iconURL?.deletingPathExtension().lastPathComponent ?? "",
"notes": config.information.notes ?? "",
"architecture": config.system.architecture.rawValue,
"machine": config.system.target.rawValue,
Expand Down Expand Up @@ -200,6 +201,7 @@ extension UTMScriptingConfigImpl {
private func serializeAppleConfiguration(_ config: UTMAppleConfiguration) -> [AnyHashable : Any] {
[
"name": config.information.name,
"icon": config.information.iconURL?.deletingPathExtension().lastPathComponent ?? "",
"notes": config.information.notes ?? "",
"memory": config.system.memorySize,
"cpuCores": config.system.cpuCount,
Expand Down Expand Up @@ -307,6 +309,13 @@ extension UTMScriptingConfigImpl {
if let name = record["name"] as? String, !name.isEmpty {
config.information.name = name
}
if let icon = record["icon"] as? String, !icon.isEmpty {
if let url = UTMConfigurationInfo.builtinIcon(named: icon) {
config.information.iconURL = url
} else {
throw ConfigurationError.iconNotFound(icon: icon)
}
}
if let notes = record["notes"] as? String, !notes.isEmpty {
config.information.notes = notes
}
Expand Down Expand Up @@ -530,6 +539,13 @@ extension UTMScriptingConfigImpl {
if let name = record["name"] as? String, !name.isEmpty {
config.information.name = name
}
if let icon = record["icon"] as? String, !icon.isEmpty {
if let url = UTMConfigurationInfo.builtinIcon(named: icon) {
config.information.iconURL = url
} else {
throw ConfigurationError.iconNotFound(icon: icon)
}
}
if let notes = record["notes"] as? String, !notes.isEmpty {
config.information.notes = notes
}
Expand Down Expand Up @@ -652,13 +668,15 @@ extension UTMScriptingConfigImpl {
case invalidDriveDescription
case indexNotFound(index: Int)
case deviceNotSupported
case iconNotFound(icon: String)

var errorDescription: String? {
switch self {
case .identifierNotFound(let id): return String.localizedStringWithFormat(NSLocalizedString("Identifier '%@' cannot be found.", comment: "UTMScriptingConfigImpl"), String(describing: id))
case .invalidDriveDescription: return NSLocalizedString("Drive description is invalid.", comment: "UTMScriptingConfigImpl")
case .indexNotFound(let index): return String.localizedStringWithFormat(NSLocalizedString("Index %lld cannot be found.", comment: "UTMScriptingConfigImpl"), index)
case .deviceNotSupported: return NSLocalizedString("This device is not supported by the target.", comment: "UTMScriptingConfigImpl")
case .iconNotFound(let icon): return String.localizedStringWithFormat(NSLocalizedString("The icon named '%@' cannot be found in the built-in icons.", comment: "UTMScriptingConfigImpl"), icon)
}
}
}
Expand Down

0 comments on commit 26fe0de

Please sign in to comment.