Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1351 from PucklaMotzer09/file_dialog_favorites_la…
…nguage Correct paths for favorites in file dialog
- Loading branch information
Showing
8 changed files
with
315 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dialog | ||
|
||
import ( | ||
"os" | ||
|
||
"fyne.io/fyne" | ||
"fyne.io/fyne/storage" | ||
) | ||
|
||
func getFavoriteLocations() (map[string]fyne.ListableURI, error) { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
homeURI := storage.NewFileURI(homeDir) | ||
|
||
favoriteNames := getFavoriteOrder() | ||
favoriteLocations := make(map[string]fyne.ListableURI) | ||
for _, favName := range favoriteNames { | ||
var uri fyne.URI | ||
var err1 error | ||
if favName == "Home" { | ||
uri = homeURI | ||
} else { | ||
uri, err1 = storage.Child(homeURI, favName) | ||
} | ||
if err1 != nil { | ||
err = err1 | ||
continue | ||
} | ||
|
||
listURI, err1 := storage.ListerForURI(uri) | ||
if err1 != nil { | ||
err = err1 | ||
continue | ||
} | ||
favoriteLocations[favName] = listURI | ||
} | ||
|
||
return favoriteLocations, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// +build linux openbsd freebsd netbsd | ||
// +build !android | ||
|
||
package dialog | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"fyne.io/fyne" | ||
"fyne.io/fyne/storage" | ||
) | ||
|
||
func getFavoriteLocation(homeURI fyne.URI, name string) (fyne.URI, error) { | ||
cmdName := "xdg-user-dir" | ||
fallback, err := storage.Child(homeURI, name) | ||
var fallbackErr error | ||
if err != nil { | ||
fallbackErr = fmt.Errorf("couldn't get fallback: %s", err) | ||
} | ||
|
||
if _, err := exec.LookPath(cmdName); err != nil { | ||
if fallbackErr != nil { | ||
return nil, fallbackErr | ||
} | ||
return fallback, fmt.Errorf("%s not found in PATH. using fallback paths", cmdName) | ||
} | ||
|
||
cmd := exec.Command(cmdName, name) | ||
stdout := bytes.NewBufferString("") | ||
|
||
cmd.Stdout = stdout | ||
err = cmd.Run() | ||
if err != nil { | ||
if fallbackErr != nil { | ||
return nil, fallbackErr | ||
} | ||
return fallback, err | ||
} | ||
|
||
loc := stdout.String() | ||
// Remove \n at the end | ||
loc = loc[:len(loc)-1] | ||
locURI := storage.NewFileURI(loc) | ||
|
||
if locURI.String() == homeURI.String() { | ||
if fallbackErr != nil { | ||
return nil, fallbackErr | ||
} | ||
return fallback, fmt.Errorf("this computer does not have a %s folder", name) | ||
} | ||
|
||
return locURI, nil | ||
} | ||
|
||
func getFavoriteLocations() (map[string]fyne.ListableURI, error) { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
homeURI := storage.NewFileURI(homeDir) | ||
|
||
favoriteNames := getFavoriteOrder() | ||
arguments := map[string]string{ | ||
"Documents": "DOCUMENTS", | ||
"Downloads": "DOWNLOADS", | ||
} | ||
|
||
favoriteLocations := make(map[string]fyne.ListableURI) | ||
for _, favName := range favoriteNames { | ||
var uri fyne.URI | ||
var err1 error | ||
if favName == "Home" { | ||
uri = homeURI | ||
} else { | ||
favURI, err2 := getFavoriteLocation(homeURI, arguments[favName]) | ||
if err2 != nil { | ||
err1 = err2 | ||
} | ||
uri = favURI | ||
} | ||
if err1 != nil { | ||
err = err1 | ||
continue | ||
} | ||
|
||
listURI, err1 := storage.ListerForURI(uri) | ||
if err1 != nil { | ||
err = err1 | ||
continue | ||
} | ||
favoriteLocations[favName] = listURI | ||
} | ||
|
||
return favoriteLocations, err | ||
} |
Oops, something went wrong.