Skip to content

Commit

Permalink
drive rename works
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaf committed Apr 16, 2024
1 parent bc3e11c commit 6227f8b
Showing 1 changed file with 66 additions and 46 deletions.
112 changes: 66 additions & 46 deletions cmd/onedriver-launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"
"unsafe"

"github.com/coreos/go-systemd/v22/unit"
Expand Down Expand Up @@ -59,6 +58,9 @@ func main() {
os.Exit(0)
}

// loading config can emit an unformatted log message, so we do this first
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"})

// command line options override config options
config := common.LoadConfig(*configPath)
if *cacheDir != "" {
Expand All @@ -68,7 +70,6 @@ func main() {
config.LogLevel = *logLevel
}

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"})
zerolog.SetGlobalLevel(common.StringToLevel(config.LogLevel))

log.Info().Msgf("onedriver-launcher %s", common.Version())
Expand Down Expand Up @@ -313,77 +314,96 @@ func newMountRow(config common.Config, mount string) (*gtk.ListBoxRow, *gtk.Swit
if accountName != "" {
accountLabel, _ := gtk.LabelNew(accountName)
popoverBox.Add(accountLabel)
separator, _ := gtk.SeparatorMenuItemNew()
popoverBox.Add(separator)
}

// create a button to enable/disable the mountpoint
unitEnabledBtn, _ := gtk.CheckButtonNewWithLabel(" Start drive on login")
unitEnabledBtn.SetTooltipText("Start this drive automatically when you login")
enabled, err := systemd.UnitIsEnabled(unitName)
if err == nil {
unitEnabledBtn.SetActive(enabled)
} else {
log.Error().Err(err).Msg("Error checking unit enabled state.")
}
unitEnabledBtn.Connect("toggled", func() {
log.Info().
Str("signal", "toggled").
Str("mount", mount).
Str("unitName", unitName).
Bool("enabled", unitEnabledBtn.GetActive()).
Msg("Changing systemd unit enabled state.")
err := systemd.UnitSetEnabled(unitName, unitEnabledBtn.GetActive())
if err != nil {
log.Error().
Err(err).
Str("unit", unitName).
Msg("Could not change systemd unit enabled state.")
}
})
popoverBox.PackStart(unitEnabledBtn, false, true, 0)

// rename the mount by rewriting the .xdg-volume-info file
renameMountpointBtn, _ := gtk.ModelButtonNew()
renameMountpointBtn.SetLabel("Change display name")
renameMountpointBtn.SetTooltipText("Change the label that your file browser uses for this drive")
renameMountpointBtn.Connect("clicked", func(button *gtk.ModelButton) {
newName := "test drive" //TODO get the new drive name
renameMountpointEntry, _ := gtk.EntryNew()
renameMountpointEntry.SetTooltipText("Change the label that your file browser uses for this drive")
renameMountpointEntry.SetText(driveName)
// runs on enter
renameMountpointEntry.Connect("activate", func(entry *gtk.Entry) {
newName, err := entry.GetText()
ctx := log.With().
Str("signal", "clicked").
Str("mount", mount).
Str("unitName", unitName).
Str("name", newName).
Str("oldName", driveName).
Str("newName", newName).
Logger()
if err != nil {
ctx.Error().Err(err).Msg("Failed to get new drive name.")
return
}
if driveName == newName {
ctx.Info().Msg("New name is same as old name, ignoring.")
return
}
ctx.Info().
Msg("Renaming mount.")
err := systemd.UnitSetActive(unitName, true)
popover.GrabFocus()
entry.SetProgressFraction(0.1)
entry.SetCanDefault(false)
defer entry.SetCanDefault(true)
defer entry.SetProgressFraction(0)

err = systemd.UnitSetActive(unitName, true)
if err != nil {
ctx.Error().Err(err).Msg("Failed to rename mount.")
ctx.Error().Err(err).Msg("Failed to start mount for rename.")
return
}
mountToggle.SetActive(true)

if ui.PollUntilAvail(mount, -1) {
entry.SetProgressFraction(0.25)
xdgVolumeInfo := common.TemplateXDGVolumeInfo(newName)
driveName = newName
//FIXME why does this not work???
err = ioutil.WriteFile(filepath.Join(mount, ".xdg-volume-info"), []byte(xdgVolumeInfo), 0644)
if err != nil {
ctx.Error().Err(err).Msg("Failed to rename mount.")
ctx.Error().Err(err).Msg("Failed to write new mount name.")
return
}
entry.SetProgressFraction(0.5)
} else {
ctx.Error().Err(err).Msg("Mount never became ready.")
}
// update label in UI now
label.SetMarkup(fmt.Sprintf("%s <span style=\"italic\" weight=\"light\">(%s)</span> ",
newName, tildePath,
))
// and reset the drive again so people's file browsers actually pick it up
ctx.Info().Msg("Remounting drive to ensure file browser gets the new name.")
systemd.UnitSetActive(unitName, false)
time.Sleep(100 * time.Millisecond) // probably unnecessary
systemd.UnitSetActive(unitName, true)

ui.Dialog("Drive rename will take effect on next filesystem start.", gtk.MESSAGE_INFO, nil)
ctx.Info().Msg("Drive rename will take effect on next filesystem start.")
})
popoverBox.Add(renameMountpointEntry)

separator, _ := gtk.SeparatorMenuItemNew()
popoverBox.Add(separator)

// create a button to enable/disable the mountpoint
unitEnabledBtn, _ := gtk.CheckButtonNewWithLabel(" Start drive on login")
unitEnabledBtn.SetTooltipText("Start this drive automatically when you login")
enabled, err := systemd.UnitIsEnabled(unitName)
if err == nil {
unitEnabledBtn.SetActive(enabled)
} else {
log.Error().Err(err).Msg("Error checking unit enabled state.")
}
unitEnabledBtn.Connect("toggled", func() {
log.Info().
Str("signal", "toggled").
Str("mount", mount).
Str("unitName", unitName).
Bool("enabled", unitEnabledBtn.GetActive()).
Msg("Changing systemd unit enabled state.")
err := systemd.UnitSetEnabled(unitName, unitEnabledBtn.GetActive())
if err != nil {
log.Error().
Err(err).
Str("unit", unitName).
Msg("Could not change systemd unit enabled state.")
}
})
popoverBox.PackStart(renameMountpointBtn, false, true, 0)
popoverBox.PackStart(unitEnabledBtn, false, true, 0)

// button to delete the mount
deleteMountpointBtn, _ := gtk.ModelButtonNew()
Expand Down

0 comments on commit 6227f8b

Please sign in to comment.