feat(qsl): copy a card design from another profile

A second profile is usually the same operator with a different rig or a
different locator — same callsign, same cards. Having to redraw a design
because the antenna changed is work nobody should do.

The designer now lists the designs the active profile cannot see and copies a
chosen one into it. Designs it already sees — its own, and the shared ones —
are left out: they are not something to copy.

A COPY, not a move or a share. The original profile keeps its design untouched
and the duplicate is free to diverge, which it usually will: a second profile
exists because something differs, and that something often ends up on the card.

The PICTURES are copied too, and that is the part worth getting right. Stored
documents reference photos by name relative to the template's own asset folder,
so duplicating the JSON alone would point the new design at files that are not
in its folder. It is validated against its own folder afterwards, and a failure
rolls the row back rather than leaving a design whose pictures are missing.
Sharing the source's folder was the other option and a worse one: deleting
either design would then have emptied the other.
This commit is contained in:
2026-08-13 18:08:14 +02:00
parent 4e2a5877d6
commit 380472bda8
6 changed files with 166 additions and 4 deletions
+105 -1
View File
@@ -31,7 +31,7 @@ import (
const (
keyQSLEmailSubject = "qsl.email_subject"
keyQSLEmailBody = "qsl.email_body"
keyQSLAutoSend = "qsl.auto_send" // "1" → render+send an eQSL on log when an e-mail and default template exist
keyQSLAutoSend = "qsl.auto_send" // "1" → render+send an eQSL on log when an e-mail and default template exist
keyQSLDefaultMsg = "qsl.default_message" // fallback QSL message printed on the card when the QSO's own QSLMSG is empty
)
@@ -748,3 +748,107 @@ func sampleQSO() qso.QSO {
QSLMsg: "TNX FB QSO — 73!",
}
}
// QSLForeignTemplate is one card design belonging to ANOTHER profile, offered
// for copying into the active one.
type QSLForeignTemplate struct {
ID int64 `json:"id"`
Name string `json:"name"`
ProfileID int64 `json:"profile_id"`
ProfileName string `json:"profile_name"`
}
// QSLListOtherProfileTemplates lists the designs the ACTIVE profile cannot see.
//
// A second profile is usually the same operator with a different rig or a
// different locator — same callsign, same cards. Redrawing a design because the
// antenna changed is work nobody should have to do, and the designs a profile
// already sees (its own, and the shared ones) are deliberately left out: they
// are not something to copy.
func (a *App) QSLListOtherProfileTemplates() ([]QSLForeignTemplate, error) {
if a.qslTemplates == nil || a.profiles == nil {
return nil, fmt.Errorf("db not initialized")
}
active := int64(-1)
if p, err := a.profiles.Active(a.ctx); err == nil {
active = p.ID
}
names := map[int64]string{}
if list, err := a.ListProfiles(); err == nil {
for _, p := range list {
names[p.ID] = p.Name
}
}
all, err := a.qslTemplates.List(a.ctx)
if err != nil {
return nil, err
}
out := []QSLForeignTemplate{}
for _, r := range all {
// Shared designs (no profile) already appear in every profile's list.
if r.ProfileID == nil || *r.ProfileID == active {
continue
}
out = append(out, QSLForeignTemplate{
ID: r.ID, Name: r.Name, ProfileID: *r.ProfileID,
ProfileName: firstNonEmptyStr(names[*r.ProfileID], fmt.Sprintf("profile %d", *r.ProfileID)),
})
}
return out, nil
}
// QSLCopyTemplateToActiveProfile duplicates another profile's design into the
// active one and returns the new id.
//
// A COPY, not a move or a share. The original profile keeps its design
// untouched, and the copy is free to diverge — a second profile usually exists
// because something differs, and that something often ends up on the card.
func (a *App) QSLCopyTemplateToActiveProfile(id int64) (int64, error) {
if a.qslTemplates == nil {
return 0, fmt.Errorf("db not initialized")
}
src, err := a.qslTemplates.Get(a.ctx, id)
if err != nil {
return 0, err
}
// The stored document references photos by NAME, relative to the template's
// own asset folder — so copying the JSON alone would point the new design at
// files that are not in its folder, and the save would fail validation.
//
// The files are copied too, giving the duplicate its own folder. Sharing the
// source's would mean deleting one design silently emptied the other.
rec := qslcard.Record{Name: src.Name + " (copy)", JSON: src.JSON}
if p, e := a.profiles.Active(a.ctx); e == nil {
rec.ProfileID = &p.ID
}
// Saved first: a new template needs its id before it can own a folder.
if err := a.qslTemplates.Save(a.ctx, &rec); err != nil {
return 0, err
}
srcDir := qslcard.TemplateDir(a.qslDir(), id)
dstDir := qslcard.TemplateDir(a.qslDir(), rec.ID)
if err := copyDirContents(srcDir, dstDir); err != nil && !os.IsNotExist(err) {
// Roll back rather than leave a design whose pictures are missing.
_ = a.qslTemplates.Delete(a.ctx, rec.ID)
return 0, fmt.Errorf("copy template assets: %w", err)
}
t, err := qslcard.Parse([]byte(rec.JSON))
if err != nil {
_ = a.qslTemplates.Delete(a.ctx, rec.ID)
return 0, err
}
if err := qslcard.Validate(t, qslcard.PhotoExistsIn(dstDir)); err != nil {
_ = a.qslTemplates.Delete(a.ctx, rec.ID)
return 0, fmt.Errorf("copied design is incomplete: %w", err)
}
applog.Printf("qsl: copied template %q (id %d) into the active profile as %q (id %d)",
src.Name, id, rec.Name, rec.ID)
return rec.ID, nil
}
func firstNonEmptyStr(a, b string) string {
if strings.TrimSpace(a) != "" {
return a
}
return b
}