Offer other profiles as a source on the setup wizard's entry page

Written-by: Codex (GPT-6.2 Sol)
Written-by: Claude Fable 5.2
This commit is contained in:
Rsclub2_2 committed 2026-11-18 22:28:04 +01:00
1 parent 76e086f4c0
commit dc164403be
6 files changed
+235 -4

No files matched your search

@@ -577,3 +577,10 @@ wizard.summary.yes=ja
wizard.summary.no=nein
wizard.summary.switchedOff=Wird abgeschaltet: {0}.
wizard.summary.logSeveral=In diesem Profil sind mehrere Logquellen eingeschaltet. Im Einstellungsfenster ändern.
wizard.source.loginTake=Anmeldung von Profil „{0}" übernehmen ({1})
wizard.source.loginTakeFrom=Anmeldung von einem anderen Profil übernehmen
wizard.source.stationTake=Station von Profil „{0}" übernehmen (Locator {1})
wizard.source.stationTakeFrom=Station von einem anderen Profil übernehmen
wizard.entry.donor=Profil
wizard.summary.takenFrom=Übernommen von Profil „{0}"
@@ -586,3 +586,10 @@ wizard.summary.yes=yes
wizard.summary.no=no
wizard.summary.switchedOff=Will be switched off: {0}.
wizard.summary.logSeveral=Several log sources are enabled in this profile. Change them in the settings window.
wizard.source.loginTake=Take the login from profile "{0}" ({1})
wizard.source.loginTakeFrom=Take the login from another profile
wizard.source.stationTake=Take the station from profile "{0}" (locator {1})
wizard.source.stationTakeFrom=Take the station from another profile
wizard.entry.donor=Profile
wizard.summary.takenFrom=Taken from profile "{0}"
@@ -160,6 +160,7 @@ private fun RadioOption(label: String, selected: Boolean, onSelect: () -> Unit)
@Composable
private fun EntryPage(state: SetupWizardState, strings: Strings) {
Heading(strings.wizardEntryTitle, strings.wizardEntryLead)
val oneDonor = state.donors.singleOrNull()
/* An axis with a single option is decided already and is not asked about. */
if (state.loginSources.size > 1) {
@@ -168,13 +169,25 @@ private fun EntryPage(state: SetupWizardState, strings: Strings) {
RadioOption(
label = when (source) {
WizardSource.NEW -> strings.wizardSourceLoginNew
WizardSource.TAKE -> state.loginDonor?.displayName.orEmpty()
// One other profile is named in the option; several get a picker below.
WizardSource.TAKE ->
if (oneDonor != null) strings.wizardSourceLoginTake(oneDonor.displayName, oneDonor.callsign)
else strings.wizardSourceLoginTakeFrom
WizardSource.KEEP -> strings.wizardSourceLoginKeep(state.storedCallsign)
},
selected = state.loginSource == source,
onSelect = { state.chooseLoginSource(source) },
)
}
if (state.loginSource == WizardSource.TAKE && oneDonor == null) {
Form.choice(
label = strings.wizardEntryDonor,
items = state.donors,
selected = state.loginDonor,
describe = { "${it.displayName} (${it.callsign})" },
onSelect = state::chooseLoginDonor,
)
}
}
}
@@ -184,13 +197,24 @@ private fun EntryPage(state: SetupWizardState, strings: Strings) {
RadioOption(
label = when (source) {
WizardSource.NEW -> strings.wizardSourceStationNew
WizardSource.TAKE -> state.stationDonor?.displayName.orEmpty()
WizardSource.TAKE ->
if (oneDonor != null) strings.wizardSourceStationTake(oneDonor.displayName, oneDonor.locator)
else strings.wizardSourceStationTakeFrom
WizardSource.KEEP -> strings.wizardSourceStationKeep(state.storedLocator)
},
selected = state.stationSource == source,
onSelect = { state.chooseStationSource(source) },
)
}
if (state.stationSource == WizardSource.TAKE && oneDonor == null) {
Form.choice(
label = strings.wizardEntryDonor,
items = state.donors,
selected = state.stationDonor,
describe = { "${it.displayName} (${it.locator})" },
onSelect = state::chooseStationDonor,
)
}
}
}
}
@@ -396,6 +420,9 @@ private fun SummaryPage(state: SetupWizardState, strings: Strings, saveFailed: B
}
Form.section(strings.wizardSummaryLogin) {
if (state.loginSource == WizardSource.TAKE) {
state.loginDonor?.let { Text(strings.wizardSummaryTakenFrom(it.displayName)) }
}
SummaryRow(strings.stationCallsign, state.callsign.ifBlank { strings.wizardSummaryNone })
// Never the password itself: the summary may be read over a shoulder.
SummaryRow(
@@ -406,6 +433,9 @@ private fun SummaryPage(state: SetupWizardState, strings: Strings, saveFailed: B
}
Form.section(strings.wizardSummaryStation) {
if (state.stationSource == WizardSource.TAKE) {
state.stationDonor?.let { Text(strings.wizardSummaryTakenFrom(it.displayName)) }
}
SummaryRow(strings.stationLocator, state.locator.ifBlank { strings.wizardSummaryNone })
SummaryRow(
strings.wizardSummaryBands,
@@ -5,14 +5,20 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.awt.ComposeDialog
import kst4contest.ApplicationConstants
import kst4contest.controller.LoginConfiguration
import kst4contest.controller.OperatorProfilePaths
import kst4contest.controller.OperatorProfileStore
import kst4contest.model.ChatPreferences
import kst4contest.utils.ApplicationFileUtils
import kst4contest.view.compose.Kst4ContestTheme
import kst4contest.view.compose.WindowPlacementOnScreen
import kst4contest.view.compose.openOnEventThread
import kst4contest.view.compose.tabs.LocalFileDialogOwner
import java.awt.Dialog
import java.awt.Dimension
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicReference
import javax.swing.WindowConstants
@@ -122,6 +128,30 @@ object SetupWizardWindow {
}
}
/** Another profile that might serve as a donor: who it is and where its preferences lie. */
internal class DonorCandidate(val profileId: String, val displayName: String, val file: Path)
/**
* Reads the candidates that can serve as donors: the file exists, can be read, and holds a
* login an operator entered.
*
* A candidate's file is opened through `ChatPreferences(Path)` and never through the
* constructor that takes a name below the application directory: that one seeds a missing
* file from the bundled template, and looking for a donor must not create a profile's file.
* Nothing here writes.
*/
internal fun loadDonors(candidates: List<DonorCandidate>): List<WizardDonor> =
candidates.mapNotNull { candidate ->
if (!Files.isRegularFile(candidate.file)) return@mapNotNull null
val prefs = ChatPreferences(candidate.file)
// A throwing read counts as failed, as for the profile being set up.
if (!runCatching { prefs.readPreferencesFromXmlFile() }.getOrDefault(false)) return@mapNotNull null
if (LoginConfiguration.isUnconfigured(prefs)) return@mapNotNull null
WizardDonor(candidate.profileId, candidate.displayName, prefs)
}
/** Decides whether the wizard opens for a profile, and opens it. */
object SetupWizardLauncher {
@@ -135,15 +165,47 @@ object SetupWizardLauncher {
*/
@JvmStatic
fun runIfNeeded(preferencesRelativeFileName: String, forced: Boolean): SetupWizardOutcome =
run(ChatPreferences(preferencesRelativeFileName), forced, SetupWizardWindow::show)
run(
ChatPreferences(preferencesRelativeFileName),
forced,
{ loadDonors(otherProfiles(preferencesRelativeFileName)) },
SetupWizardWindow::show,
)
/**
* Every registered profile but the one being set up, with the file its preferences lie
* in. Empty on an installation without a profile registry: there is only the root
* profile then, and it is the active one.
*/
private fun otherProfiles(activeRelativeFileName: String): List<DonorCandidate> =
OperatorProfileStore().loadProfiles().mapNotNull { profile ->
val relativeFileName = OperatorProfilePaths.preferencesRelativeFileName(profile)
if (relativeFileName == activeRelativeFileName) return@mapNotNull null
DonorCandidate(
profile.profileId,
profile.displayName,
Path.of(ApplicationFileUtils.getFilePath(ApplicationConstants.APPLICATION_NAME, relativeFileName)),
)
}
/** Without other profiles; for callers and tests that have none. */
internal fun run(
prefs: ChatPreferences,
forced: Boolean,
show: (SetupWizardState, ChatPreferences) -> SetupWizardOutcome,
): SetupWizardOutcome = run(prefs, forced, { emptyList() }, show)
/**
* @param prefs bound to the profile's file and not read yet
* @param donors reads the other profiles; called only once the wizard is certain to
* open, so an ordinary start reads no file but its own
* @param show opens the window; replaced in the tests
*/
internal fun run(
prefs: ChatPreferences,
forced: Boolean,
donors: () -> List<WizardDonor>,
show: (SetupWizardState, ChatPreferences) -> SetupWizardOutcome,
): SetupWizardOutcome {
/*
@@ -157,6 +219,6 @@ object SetupWizardLauncher {
if (!forced && !LoginConfiguration.isUnconfigured(prefs)) return SetupWizardOutcome.NOT_SHOWN
return show(SetupWizardState(prefs), prefs)
return show(SetupWizardState(prefs, donors()), prefs)
}
}
@@ -220,4 +220,56 @@ class SetupWizardContentScreenTest {
onNodeWithText("Will be switched off: network log synchronisation, AirScout.").assertExists()
}
private fun donor(id: String, name: String, callsign: String, locator: String) =
WizardDonor(id, name, ChatPreferences(Path.of("setup-wizard-screen-donor-unused.xml")).apply {
setStn_loginCallSign(callsign)
setStn_loginPassword("secret")
setStn_loginLocatorMainCat(locator)
setStn_bandActive144(true)
setLoginChatCategoryMain(ChatCategory(2))
setLoginToSecondChatEnabled(false)
})
private fun emptyProfile() =
ChatPreferences(Path.of("setup-wizard-screen-test-unused.xml")).apply {
setStn_loginCallSign("")
setStn_loginPassword("")
setStn_loginLocatorMainCat("JO54ME")
setStn_bandActive144(true)
setLoginChatCategoryMain(ChatCategory(2))
setLoginToSecondChatEnabled(false)
}
@Test
fun aSingleDonorIsNamedInTheOptionAndTakenWithOneClick() = runComposeUiTest {
val state = SetupWizardState(emptyProfile(), listOf(donor("a", "Conteststation", "DL0TEST", "JO50AA")))
show(state)
onNodeWithText("Take the login from profile \"Conteststation\" (DL0TEST)").performClick()
onNodeWithText("Take the station from profile \"Conteststation\" (locator JO50AA)").performClick()
assertEquals(WizardSource.TAKE, state.loginSource)
assertEquals("a", state.loginDonor?.profileId)
assertEquals(WizardSource.TAKE, state.stationSource)
onNodeWithText("Next").performClick()
assertEquals(WizardPage.PERSONAL, state.page)
assertEquals("DL0TEST", state.callsign)
}
@Test
fun withSeveralDonorsNextWaitsForAProfile() = runComposeUiTest {
val state = SetupWizardState(
emptyProfile(),
listOf(donor("a", "Conteststation", "DL0TEST", "JO50AA"), donor("b", "Portabel", "DL0TEST/P", "JN58QA")),
)
show(state)
onNodeWithText("Take the login from another profile").performClick()
onNodeWithText("Set up a new station").performClick()
onNodeWithText("Next").performClick()
assertEquals(WizardPage.ENTRY, state.page, "which profile is not said yet")
}
}
@@ -3,6 +3,7 @@ package kst4contest.view.compose.wizard
import kst4contest.model.ChatPreferences
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
@@ -130,4 +131,76 @@ class SetupWizardLauncherTest(@param:TempDir private val directory: Path) {
assertEquals(SetupWizardOutcome.SAVED, saveFromWizard(state, prefs, connect = false))
assertEquals(SetupWizardOutcome.SAVED_AND_CONNECT, saveFromWizard(state, prefs, connect = true))
}
private fun profileFile(name: String, callsign: String, password: String): Path {
val file = directory.resolve(name)
ChatPreferences::class.java.getResourceAsStream(ChatPreferences.PREFERENCE_RESOURCE).use { template ->
Files.copy(requireNotNull(template), file, StandardCopyOption.REPLACE_EXISTING)
}
ChatPreferences(file).apply {
assertTrue(readPreferencesFromXmlFile())
setStn_loginCallSign(callsign)
setStn_loginPassword(password)
assertTrue(writePreferencesToXmlFile())
}
return file
}
@Test
fun onlyConfiguredReadableProfilesBecomeDonors() {
val configured = profileFile("configured.xml", "DL0TEST", "secret")
val emptyLogin = profileFile("empty.xml", "", "")
val template = profileFile("template.xml", "DO5AMF", "changeMe")
val garbage = directory.resolve("garbage.xml").also { Files.writeString(it, "this is not xml <<<") }
val donors = loadDonors(
listOf(
DonorCandidate("a", "Conteststation", configured),
DonorCandidate("b", "Leer", emptyLogin),
DonorCandidate("c", "Vorlage", template),
DonorCandidate("d", "Kaputt", garbage),
DonorCandidate("e", "Fehlt", directory.resolve("missing.xml")),
)
)
assertEquals(listOf("a"), donors.map { it.profileId })
assertEquals("Conteststation", donors.single().displayName)
assertEquals("DL0TEST", donors.single().callsign)
}
@Test
fun aMissingDonorFileIsNotCreated() {
val missing = directory.resolve("missing.xml")
loadDonors(listOf(DonorCandidate("e", "Fehlt", missing)))
assertFalse(Files.exists(missing), "looking for a donor must not create its file")
}
@Test
fun readingADonorLeavesItsFileAlone() {
val configured = profileFile("configured.xml", "DL0TEST", "secret")
val before = Files.readAllBytes(configured)
loadDonors(listOf(DonorCandidate("a", "Conteststation", configured)))
assertArrayEquals(before, Files.readAllBytes(configured))
}
@Test
fun donorsAreOnlyLoadedWhenTheWizardOpens() {
var loaded = 0
val donors = { loaded++; emptyList<WizardDonor>() }
SetupWizardLauncher.run(ChatPreferences(configuredFile()), forced = false, donors, neverShown)
assertEquals(0, loaded, "a configured profile starts without reading other profiles")
var seen: List<WizardDonor>? = null
SetupWizardLauncher.run(ChatPreferences(templateFile()), forced = false, donors) { state, _ ->
seen = state.donors
SetupWizardOutcome.LATER
}
assertEquals(1, loaded)
assertEquals(emptyList<WizardDonor>(), seen)
}
}