Skip to content

Commit

Permalink
saving in fyne the log data in all supported format
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Thienel <[email protected]>
  • Loading branch information
ftl committed Dec 5, 2024
1 parent eb02215 commit 77383f6
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 72 deletions.
140 changes: 80 additions & 60 deletions core/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type View interface {
BringToFront()
ShowFilename(string)
SelectOpenFile(callback func(filename string, err error), title string, dir string, extensions ...string)
SelectSaveFile(title string, dir string, filename string, patterns ...string) (string, bool, error)
SelectSaveFile(callback func(filename string, err error), title string, dir string, filename string, patterns ...string)
ShowInfoDialog(title string, format string, args ...any)
ShowErrorDialog(string, ...any)
}
Expand Down Expand Up @@ -399,7 +399,6 @@ func proposeFilename(contestName, callsign string) string {
}

func (c *Controller) New() {
var err error
newContest, ok := c.NewContest.Run()
if !ok {
return
Expand All @@ -413,49 +412,54 @@ func (c *Controller) New() {
}
proposedFilename := proposeFilename(proposedName, c.Settings.Station().Callsign.BaseCall) + ".log"

filename, ok, err := c.view.SelectSaveFile("New Logfile", c.configuration.LogDirectory(), proposedFilename, "*.log")
if !ok {
return
}
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
c.view.SelectSaveFile(c.new(newContest), "New Logfile", c.configuration.LogDirectory(), proposedFilename, "log")
}

store := store.NewFileStore(filename)
err = store.Clear()
if err != nil {
c.view.ShowErrorDialog("Cannot create %s: %v", filepath.Base(newContest.Filename), err)
return
}
func (c *Controller) new(newContest newcontest.Result) func(filename string, err error) {
return func(filename string, err error) {
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
if filename == "" {
return
}

c.Settings.Reset()
c.Settings.SelectContestIdentifier(newContest.Identifier)
c.Settings.EnterContestName(newContest.Name)
c.Keyer.SetSettings(c.configuration.KeyerSettings())
store := store.NewFileStore(filename)
err = store.Clear()
if err != nil {
c.view.ShowErrorDialog("Cannot create %s: %v", filepath.Base(newContest.Filename), err)
return
}

err = store.WriteStation(c.Settings.Station())
if err != nil {
c.view.ShowErrorDialog("Cannot save as %s: %v", filepath.Base(newContest.Filename), err)
return
}
err = store.WriteContest(c.Settings.Contest())
if err != nil {
c.view.ShowErrorDialog("Cannot save as %s: %v", filepath.Base(newContest.Filename), err)
return
}
err = store.WriteKeyer(c.Keyer.KeyerSettings())
if err != nil {
c.view.ShowErrorDialog("Cannot save as %s: %v", filepath.Base(newContest.Filename), err)
return
}
c.Settings.Reset()
c.Settings.SelectContestIdentifier(newContest.Identifier)
c.Settings.EnterContestName(newContest.Name)
c.Keyer.SetSettings(c.configuration.KeyerSettings())

c.Settings.SetWriter(store)
c.Keyer.SetWriter(store)
c.changeLogbook(filename, store, logbook.New(c.clock))
c.Refresh()
err = store.WriteStation(c.Settings.Station())
if err != nil {
c.view.ShowErrorDialog("Cannot save as %s: %v", filepath.Base(newContest.Filename), err)
return
}
err = store.WriteContest(c.Settings.Contest())
if err != nil {
c.view.ShowErrorDialog("Cannot save as %s: %v", filepath.Base(newContest.Filename), err)
return
}
err = store.WriteKeyer(c.Keyer.KeyerSettings())
if err != nil {
c.view.ShowErrorDialog("Cannot save as %s: %v", filepath.Base(newContest.Filename), err)
return
}

c.Settings.SetWriter(store)
c.Keyer.SetWriter(store)
c.changeLogbook(filename, store, logbook.New(c.clock))
c.Refresh()

c.OpenSettings()
c.OpenSettings()
}
}

func (c *Controller) Open() {
Expand Down Expand Up @@ -496,14 +500,17 @@ func (c *Controller) open(filename string, err error) {

func (c *Controller) SaveAs() {
proposedName := c.proposeFilename() + ".log"
filename, ok, err := c.view.SelectSaveFile("Save Logfile As", c.configuration.LogDirectory(), proposedName, "*.log")
if !ok {
return
}
c.view.SelectSaveFile(c.save, "Save Logfile As", c.configuration.LogDirectory(), proposedName, "log")
}

func (c *Controller) save(filename string, err error) {
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
if filename == "" {
return
}

store := store.NewFileStore(filename)
err = store.Clear()
Expand Down Expand Up @@ -546,14 +553,17 @@ func (c *Controller) SaveAs() {

func (c *Controller) ExportCabrillo() {
proposedName := c.proposeFilename() + ".cabrillo"
filename, ok, err := c.view.SelectSaveFile("Export Cabrillo File", c.configuration.LogDirectory(), proposedName, "*.cabrillo")
if !ok {
return
}
c.view.SelectSaveFile(c.exportCabrillo, "Export Cabrillo File", c.configuration.LogDirectory(), proposedName, "cabrillo", "cab")
}

func (c *Controller) exportCabrillo(filename string, err error) {
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
if filename == "" {
return
}

file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
Expand All @@ -576,14 +586,17 @@ func (c *Controller) ExportCabrillo() {

func (c *Controller) ExportADIF() {
proposedName := c.proposeFilename() + ".adif"
filename, ok, err := c.view.SelectSaveFile("Export ADIF File", c.configuration.LogDirectory(), proposedName, "*.adif")
if !ok {
return
}
c.view.SelectSaveFile(c.exportADIF, "Export ADIF File", c.configuration.LogDirectory(), proposedName, "adif", "adi")
}

func (c *Controller) exportADIF(filename string, err error) {
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
if filename == "" {
return
}

file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
Expand All @@ -600,14 +613,17 @@ func (c *Controller) ExportADIF() {

func (c *Controller) ExportCSV() {
proposedName := c.proposeFilename() + ".csv"
filename, ok, err := c.view.SelectSaveFile("Export CSV File", c.configuration.LogDirectory(), proposedName, "*.csv")
if !ok {
return
}
c.view.SelectSaveFile(c.exportCSV, "Export CSV File", c.configuration.LogDirectory(), proposedName, "csv")
}

func (c *Controller) exportCSV(filename string, err error) {
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
if filename == "" {
return
}

file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
Expand All @@ -627,12 +643,16 @@ func (c *Controller) ExportCSV() {

func (c *Controller) ExportCallhistory() {
proposedName := c.proposeFilename() + ".txt"
filename, ok, err := c.view.SelectSaveFile("Export Call History File", c.configuration.LogDirectory(), proposedName, "*.txt")
if !ok {
return
}
c.view.SelectSaveFile(c.exportCallhistory, "Export Call History File", c.configuration.LogDirectory(), proposedName, "txt")
}

func (c *Controller) exportCallhistory(filename string, err error) {
if err != nil {
c.view.ShowErrorDialog("Cannot select a file: %v", err)
return
}
if filename == "" {
return
}

file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
Expand Down
10 changes: 10 additions & 0 deletions fyneui/mainMenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import "fyne.io/fyne/v2"

type MainMenuController interface {
Open()
SaveAs()
ExportCabrillo()
ExportADIF()
ExportCSV()
ExportCallhistory()
OpenContestRulesPage()
OpenContestUploadPage()
OpenConfigurationFile()
Expand Down Expand Up @@ -132,18 +137,23 @@ func (m *mainMenu) onFileOpen() {
}

func (m *mainMenu) onFileSaveAs() {
m.controller.SaveAs()
}

func (m *mainMenu) onFileExportCabrillo() {
m.controller.ExportCabrillo()
}

func (m *mainMenu) onFileExportADIF() {
m.controller.ExportADIF()
}

func (m *mainMenu) onFileExportCSV() {
m.controller.ExportCSV()
}

func (m *mainMenu) onFileExportCallhistory() {
m.controller.ExportCallhistory()
}

func (m *mainMenu) onFileOpenRules() {
Expand Down
54 changes: 47 additions & 7 deletions fyneui/mainWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ func (w *mainWindow) SelectOpenFile(callback func(string, error), title string,
}
log.Printf("OPEN FILE in %s with extensions %v", dir, extensions)

dialogCallback := func(reader fyne.URIReadCloser, err error) {
dialogCallback := func(r fyne.URIReadCloser, err error) {
defer func() {
if reader != nil {
reader.Close()
if r != nil {
r.Close()
}
}()
if err != nil {
callback("", err)
return
}
if reader == nil {
if r == nil {
callback("", nil)
return
}
filename := reader.URI().Path()
filename := r.URI().Path()
log.Printf("file selected to open: %s", filename)
callback(filename, nil)
}
Expand All @@ -86,8 +86,48 @@ func (w *mainWindow) SelectOpenFile(callback func(string, error), title string,
fileDialog.Show()
}

func (w *mainWindow) SelectSaveFile(title string, dir string, filename string, patterns ...string) (string, bool, error) {
return "", false, nil
func (w *mainWindow) SelectSaveFile(callback func(filename string, err error), title string, dir string, proposedFilename string, extensions ...string) {
dirURI, err := storage.ListerForURI(storage.NewFileURI(dir))
if err != nil {
callback("", err)
return
}
log.Printf("SAVE FILE %s in %s with extensions %v", proposedFilename, dir, extensions)

dialogCallback := func(w fyne.URIWriteCloser, err error) {
defer func() {
if w != nil {
w.Close()
}
}()
if err != nil {
callback("", err)
return
}
if w == nil {
callback("", nil)
return
}
filename := w.URI().Path()
log.Printf("file selected to save: %s", filename)
callback(filename, nil)
}

fileDialog := dialog.NewFileSave(dialogCallback, w.window)
fileDialog.SetView(dialog.ListView)
fileDialog.Resize(fyne.NewSize(1000, 600))
// fileDialog.SetTitleText(title) // TODO: activate with fyne 2.6
fileDialog.SetConfirmText("Save")
fileDialog.SetDismissText("Cancel")
fileDialog.SetLocation(dirURI)
if len(extensions) > 0 {
filterExtensions := make([]string, len(extensions), len(extensions))
for i, extension := range extensions {
filterExtensions[i] = "." + extension
}
fileDialog.SetFilter(storage.NewExtensionFileFilter(filterExtensions))
}
fileDialog.Show()
}

func (w *mainWindow) ShowInfoDialog(title string, format string, a ...any) {
Expand Down
13 changes: 8 additions & 5 deletions ui/mainWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (w *mainWindow) SelectOpenFile(callback func(string, error), title string,
dlg, err := gtk.FileChooserDialogNewWith1Button(title, &w.window.Window, gtk.FILE_CHOOSER_ACTION_OPEN, "Open", gtk.RESPONSE_ACCEPT)
if err != nil {
errors.Wrap(err, "cannot create a file selection dialog to open a file")
return
}
defer dlg.Destroy()

Expand Down Expand Up @@ -106,10 +107,11 @@ func (w *mainWindow) SelectOpenFile(callback func(string, error), title string,
callback(dlg.GetFilename(), nil)
}

func (w *mainWindow) SelectSaveFile(title string, dir string, filename string, patterns ...string) (string, bool, error) {
func (w *mainWindow) SelectSaveFile(callback func(string, error), title string, dir string, filename string, patterns ...string) {
dlg, err := gtk.FileChooserDialogNewWith1Button(title, &w.window.Window, gtk.FILE_CHOOSER_ACTION_SAVE, "Save", gtk.RESPONSE_ACCEPT)
if err != nil {
return "", false, errors.Wrap(err, "cannot create a file selection dialog to save a file")
errors.Wrap(err, "cannot create a file selection dialog to save a file")
return
}
defer dlg.Destroy()

Expand All @@ -123,7 +125,8 @@ func (w *mainWindow) SelectSaveFile(title string, dir string, filename string, p
if len(patterns) > 0 {
filter, err := gtk.FileFilterNew()
if err != nil {
return "", false, errors.Wrap(err, "cannot create a file selection dialog to save a file")
errors.Wrap(err, "cannot create a file selection dialog to save a file")
return
}
for _, pattern := range patterns {
filter.AddPattern(pattern)
Expand All @@ -133,10 +136,10 @@ func (w *mainWindow) SelectSaveFile(title string, dir string, filename string, p

result := dlg.Run()
if result != gtk.RESPONSE_ACCEPT {
return "", false, nil
return
}

return dlg.GetFilename(), true, nil
callback(dlg.GetFilename(), nil)
}

func (w *mainWindow) ShowInfoDialog(title string, format string, a ...any) {
Expand Down

0 comments on commit 77383f6

Please sign in to comment.