Ejemplo n.º 1
0
// New reads an ini.Map formated file from r and returns an Entry that
// represents the Desktop file that was read.
func New(r io.Reader) (*Entry, error) {
	m, err := ini.New(r)
	if err != nil {
		return nil, err
	}

	// Create the entry.
	e := &Entry{
		Type:            ParseType(m.String(dent, "Type")),
		Version:         m.String(dent, "Version"),
		Name:            m.String(dent, "Name"),
		GenericName:     m.String(dent, "GenericName"),
		Comment:         m.String(dent, "Comment"),
		Icon:            m.String(dent, "Icon"),
		URL:             m.String(dent, "URL"),
		NoDisplay:       m.Bool(dent, "NoDisplay"),
		Hidden:          m.Bool(dent, "Hidden"),
		OnlyShowIn:      m.List(dent, "OnlyShowIn"),
		NotShowIn:       m.List(dent, "NotShowIn"),
		DBusActivatable: m.Bool(dent, "DBusActivatable"),
		TryExec:         m.String(dent, "TryExec"),
		Exec:            m.String(dent, "Exec"),
		Path:            m.String(dent, "Path"),
		Terminal:        m.Bool(dent, "Terminal"),
		Actions:         getActions(m),
		MimeType:        m.List(dent, "MimeType"),
		Categories:      m.List(dent, "Categories"),
		Keywords:        m.List(dent, "Keywords"),
		StartupNotify:   m.Bool(dent, "StartupNotify"),
		StartupWMClass:  m.String(dent, "StartupWMClass"),
		X:               make(map[string]map[string]string),
	}

	// Validate the entry.
	if e.Type == None {
		return nil, ErrMissingType
	}
	if e.Type > None && e.Type < Unknown && e.Name == "" {
		return nil, ErrMissingName
	}
	if e.Type == Link && e.URL == "" {
		return nil, ErrMissingURL
	}

	// Search for extended keys.
	for k, v := range m.M[dent] {
		a := strings.SplitN(k, "-", 3)
		if a[0] != "X" || len(a) < 3 {
			continue
		}
		if e.X[a[1]] == nil {
			e.X[a[1]] = make(map[string]string)
		}
		e.X[a[1]][a[2]] = v
	}

	return e, nil
}
Ejemplo n.º 2
0
// NewInfo creates a new Info using the given io.Reader.
func NewInfo(r io.Reader) (*Info, error) {
	m, err := ini.New(r)
	if err != nil {
		return nil, err
	}

	info := new(Info)
	info.Path, err = url.QueryUnescape(m.String(tinfo, "Path"))
	if err != nil {
		return nil, err
	}
	info.DeletionDate, err = time.Parse(rfc3339, m.String(tinfo, "DeletionDate"))
	if err != nil {
		return nil, err
	}
	return info, nil
}
Ejemplo n.º 3
0
// New creates a new UserDirs struct buy reading from the given
// io.Reader.
func New(r io.Reader) (*UserDirs, error) {
	m, err := ini.New(r)
	if err != nil {
		return nil, err
	}

	return &UserDirs{
		Desktop:     parse(m.String("", "XDG_DESKTOP_DIR")),
		Documents:   parse(m.String("", "XDG_DOCUMENTS_DIR")),
		Download:    parse(m.String("", "XDG_DOWNLOAD_DIR")),
		Music:       parse(m.String("", "XDG_MUSIC_DIR")),
		Pictures:    parse(m.String("", "XDG_PICTURES_DIR")),
		PublicShare: parse(m.String("", "XDG_PUBLICSHARE_DIR")),
		Templates:   parse(m.String("", "XDG_TEMPLATES_DIR")),
		Videos:      parse(m.String("", "XDG_VIDEOS_DIR")),
	}, nil
}