Skip to content

Commit

Permalink
feat: 支持控制中心个性化头像设置
Browse files Browse the repository at this point in the history
修改账户头像的目录,支持账户个性化头像设置

Log: 实现控制中心个性化头像设置功能
Resolve: linuxdeepin/developer-center#3796
Influence: 控制中心账户头像设置
  • Loading branch information
dengbo11 authored and fly602 committed Sep 4, 2024
1 parent ea6505f commit a133a46
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 53 deletions.
2 changes: 2 additions & 0 deletions accounts1/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (d *Daemon) Start() error {
if d.manager != nil {
return nil
}

_userStandardIcons = getUserStandardIcons()
_userStandardIcons = getUserStandardIcons()
service := loader.GetService()
d.manager = NewManager(service)
Expand Down
15 changes: 9 additions & 6 deletions accounts1/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import (
)

const (
actConfigDir = "/var/lib/AccountsService"
userConfigDir = actConfigDir + "/deepin/users"
userIconsDir = actConfigDir + "/icons"
userCustomIconsDir = actConfigDir + "/icons/local"
actConfigDir = "/var/lib/AccountsService"
userConfigDir = actConfigDir + "/deepin/users"
userIconsDir = actConfigDir + "/icons"

userIconGuest = actConfigDir + "/icons/guest.png"
actConfigFile = actConfigDir + "/accounts.ini"
actConfigGroupGroup = "Accounts"
actConfigKeyGuest = "AllowGuest"
Expand Down Expand Up @@ -139,7 +137,7 @@ func NewManager(service *dbusutil.Service) *Manager {
m.usersMap = make(map[string]*User)
m.userAddedChanMap = make(map[string]chan string)

m.GuestIcon = userIconGuest
m.GuestIcon = getRandomIcon()
m.AllowGuest = isGuestUserEnabled()

m.initUsers(getUserPaths())
Expand Down Expand Up @@ -555,6 +553,11 @@ func isGuestUserEnabled() bool {
return ret
}

func getUserCustomIconsDir(homeDir string) string {
path := "/.local/share/icons"
return filepath.Join(homeDir, path)
}

func (m *Manager) checkAuth(sender dbus.Sender) error {
return checkAuth(polkitActionUserAdministration, string(sender))
}
Expand Down
26 changes: 15 additions & 11 deletions accounts1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package accounts
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
Expand All @@ -29,7 +28,6 @@ import (
)

const (
defaultUserIcon = "file:///var/lib/AccountsService/icons/default.png"
defaultUserBackgroundDir = "/usr/share/wallpapers/deepin/"

controlCenterPath = "/usr/bin/dde-control-center"
Expand Down Expand Up @@ -298,16 +296,11 @@ func (u *User) setIconFile(iconURI string) (string, bool, error) {
}()
}

dest := getNewUserCustomIconDest(u.UserName)
err = os.MkdirAll(path.Dir(dest), 0755)
err = dutils.CopyFile(tmp, iconFile)
if err != nil {
return "", false, err
}
err = dutils.CopyFile(tmp, dest)
if err != nil {
return "", false, err
}
return dutils.EncodeURI(dest, dutils.SCHEME_FILE), true, nil
return dutils.EncodeURI(iconFile, dutils.SCHEME_FILE), true, nil
}

type configChange struct {
Expand Down Expand Up @@ -752,7 +745,7 @@ func loadUserConfigInfo(u *User) {
u.SystemAccount = false
u.Layout = getDefaultLayout()
u.setLocale(getDefaultLocale())
u.IconFile = defaultUserIcon
u.IconFile = getRandomIcon()
defaultUserBackground := getDefaultUserBackground()
u.DesktopBackgrounds = []string{defaultUserBackground}
u.GreeterBackground = defaultUserBackground
Expand Down Expand Up @@ -804,10 +797,21 @@ func loadUserConfigInfo(u *User) {
icon, _ := kf.GetString(confGroupUser, confKeyIcon)
u.IconFile = icon
if u.IconFile == "" {
u.IconFile = defaultUserIcon
u.IconFile = getRandomIcon()
isSave = true
}

u.customIcon, _ = kf.GetString(confGroupUser, confKeyCustomIcon)

// CustomIcon is the newly added field in the configuration file
if u.customIcon == "" {
if !isStrInArray(u.IconFile, u.IconList) {
// u.IconFile is a custom icon, not a standard icon
u.customIcon = u.IconFile
isSave = true
}
}

u.IconList = u.getAllIcons()

_, desktopBgs, _ := kf.GetStringList(confGroupUser, confKeyDesktopBackgrounds)
Expand Down
25 changes: 3 additions & 22 deletions accounts1/user_ifc.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,26 +456,6 @@ func (u *User) SetIconFile(sender dbus.Sender, iconURI string) *dbus.Error {
return dbusutil.ToError(err)
}

// if iconURI not in iconList, need to create temp icon file
if !isStrInArray(iconURI, u.IconList) {
// copy file to temp file, update icon file
iconFile, err = copyTempIconFile(iconFile, u.UserName)
if err != nil {
logger.Warningf("copy temp file failed, err: %v", err)
return dbusutil.ToError(err)
}
// remove file
defer func() {
err := os.Remove(iconFile)
if err != nil {
logger.Warningf("remove temp file failed, err: %v", err)
return
}
}()
// if temp icon file is create, update icon URI
iconURI = dutils.EncodeURI(iconFile, dutils.SCHEME_FILE)
}

if !gdkpixbuf.IsSupportedImage(iconFile) {
err := fmt.Errorf("%q is not a image file", iconFile)
logger.Debug(err)
Expand Down Expand Up @@ -541,8 +521,9 @@ func (u *User) DeleteIconFile(sender dbus.Sender, icon string) *dbus.Error {
logger.Debug("[DeleteIconFile] icon:", icon)

dir, err := filepath.Abs(filepath.Dir(icon))
if err != nil || dir != userCustomIconsDir {
return dbusutil.ToError(fmt.Errorf("%s is not in %s", icon, userCustomIconsDir))
customDir := getUserCustomIconsDir(u.HomeDir)
if err != nil || dir != customDir {
return dbusutil.ToError(fmt.Errorf("%s is not in %s", icon, customDir))
}

base := filepath.Base(icon)
Expand Down
48 changes: 34 additions & 14 deletions accounts1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"

"github.com/godbus/dbus/v5"
"github.com/linuxdeepin/dde-daemon/accounts1/users"
polkit "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.policykit1"
"github.com/linuxdeepin/go-lib/encoding/kv"
"github.com/linuxdeepin/go-lib/graphic"
"github.com/linuxdeepin/go-lib/strv"
"github.com/linuxdeepin/go-lib/utils"
)

Expand Down Expand Up @@ -79,28 +80,47 @@ func (code ErrCodeType) String() string {

// return icons uris
func getUserStandardIcons() []string {
imgs, err := graphic.GetImagesInDir(userIconsDir)
if err != nil {
return nil
var paths []string
var icons []string

if err := filepath.Walk(userIconsDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

subPaths := []string{"dimensional", "flat"}

if info.IsDir() && strv.Strv(subPaths).Contains(info.Name()) {
paths = append(paths, path)
}

return nil
},
); err != nil {
logger.Warning("failed to walk usr icon path", err)
return icons
}

var icons []string
for _, img := range imgs {
img = utils.EncodeURI(img, utils.SCHEME_FILE)
if strings.Contains(img, "guest") || img == defaultUserIcon {
continue
for _, path := range paths {
imgs, err := graphic.GetImagesInDir(path)
if err != nil {
logger.Warning("failed to get user icon images", err)
return nil
}

icons = append(icons, img)
for _, img := range imgs {
img = utils.EncodeURI(img, utils.SCHEME_FILE)
icons = append(icons, img)
}
}

return icons
}

func getNewUserCustomIconDest(username string) string {
ns := time.Now().UnixNano()
base := username + "-" + strconv.FormatInt(ns, 36)
return filepath.Join(userCustomIconsDir, base)
// 从系统的用户头像中随机获取一张用户图片
func getRandomIcon() string {
return _userStandardIcons[rand.Intn(len(_userStandardIcons))]
}

func isStrInArray(str string, array []string) bool {
Expand Down

0 comments on commit a133a46

Please sign in to comment.