Esempio n. 1
0
func NewUser(userPath string) (*User, error) {
	info, err := users.GetUserInfoByUid(getUidFromUserPath(userPath))
	if err != nil {
		return nil, err
	}

	var u = &User{}
	u.setPropString(&u.UserName, "UserName", info.Name)
	u.setPropString(&u.Uid, "Uid", info.Uid)
	u.setPropString(&u.Gid, "Gid", info.Gid)
	u.setPropString(&u.HomeDir, "HomeDir", info.Home)
	u.setPropString(&u.Shell, "Shell", info.Shell)
	u.setPropString(&u.IconFile, "IconFile", "")
	u.setPropString(&u.BackgroundFile, "BackgroundFile", "")

	u.setPropBool(&u.AutomaticLogin, "AutomaticLogin",
		users.IsAutoLoginUser(info.Name))

	u.updatePropLocked()
	u.updatePropAccountType()

	u.setPropStrv(&u.IconList, "IconList", u.getAllIcons())

	kFile, err := dutils.NewKeyFileFromFile(
		path.Join(userConfigDir, info.Name))
	if err != nil {
		//Create user config file
		u.setPropString(&u.IconFile, "IconFile", defaultUserIcon)
		u.setPropString(&u.BackgroundFile, "BackgroundFile", defaultUserBackground)
		u.writeUserConfig()
		return u, nil
	}
	defer kFile.Free()

	var write bool = false
	icon, _ := kFile.GetString("User", "Icon")
	u.setPropString(&u.IconFile, "IconFile", icon)
	if len(u.IconFile) == 0 {
		u.setPropString(&u.IconFile, "IconFile", defaultUserIcon)
		write = true
	}

	bg, _ := kFile.GetString("User", "Background")
	u.setPropString(&u.BackgroundFile, "BackgroundFile", bg)
	if len(u.BackgroundFile) == 0 {
		u.setPropString(&u.BackgroundFile, "BackgroundFile", defaultUserBackground)
		write = true
	}

	if write {
		u.writeUserConfig()
	}

	_, hisIcons, _ := kFile.GetStringList("User", "HistoryIcons")
	u.setPropStrv(&u.HistoryIcons, "HistoryIcons", hisIcons)

	return u, nil
}
Esempio n. 2
0
func doWriteUserConfig(config, icon, bg string, hisIcons []string) error {
	if !dutils.IsFileExist(config) {
		err := dutils.CreateFile(config)
		if err != nil {
			return err
		}
	}

	kFile, err := dutils.NewKeyFileFromFile(config)
	if err != nil {
		return err
	}
	defer kFile.Free()

	kFile.SetString("User", "Icon", icon)
	kFile.SetString("User", "Background", bg)
	kFile.SetStringList("User", "HistoryIcons", hisIcons)
	_, content, err := kFile.ToData()

	return dutils.WriteStringToFile(config, content)
}