Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 55 additions & 31 deletions lib/python/gladevcp/iconview.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@
# Force the log level for this module
# LOG.setLevel(logger.DEBUG) # One of DEBUG, INFO, WARNING, ERROR, CRITICAL

# constants
### Constants ##
# Sort order
_ASCENDING = 0
_DESCENDING = 1
_FOLDERFIRST = 2
_FILEFIRST = 3
# Sort by date
_DATE_NONE = 0
_DATE_ALL = 1
_DATE_FILESONLY = 2
COL_PATH = 0
COL_PIXBUF = 1
COL_IS_DIRECTORY = 2
Expand Down Expand Up @@ -81,8 +86,10 @@ class IconFileSelection(Gtk.Box):
"~", GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'filetypes' : (GObject.TYPE_STRING, 'file filter', 'Sets the filter for the file types to be shown',
"ngc,py", GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'sortorder' : (GObject.TYPE_INT, 'sorting order', '0 = ASCENDING, 1 = DESCENDING", 2 = FOLDERFIRST, 3 = FILEFIRST',
'sortorder' : (GObject.TYPE_INT, 'sorting order', '0 = ASCENDING, 1 = DESCENDING, 2 = FOLDERFIRST, 3 = FILEFIRST',
0, 3, 2, GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'sortbydate' : (GObject.TYPE_INT, 'sort by date', '0 = none, 1 = files and foders, 2 = files only',
0, 2, 0, GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
}
__gproperties = __gproperties__

Expand All @@ -100,12 +107,13 @@ def __init__(self):

# set some default values'
self.icon_size = 48
self.start_dir = os.path.expanduser('/')
self.start_dir = None
self.cur_dir = self.start_dir
self.user_dir = os.path.expanduser('~')
self.jump_to_dir = os.path.expanduser('/tmp')
self.filetypes = ("ngc,py")
self.sortorder = _FOLDERFIRST
self.sort_by_date = _DATE_NONE
# This will hold the path we will return
self.path = ""
self.button_state = {}
Expand Down Expand Up @@ -303,58 +311,68 @@ def _fill_store(self):
# we don't want to add hidden files
if fl[0] == '.':
continue
# append files with date
if os.path.isdir(os.path.join(self.cur_dir, fl)):
try:
os.listdir(os.path.join(self.cur_dir, fl))
dirs.append(fl)
dirs.append((fl, os.path.getmtime(os.path.join(self.cur_dir, fl))))
number += 1
except OSError:
#print ("no rights for ", os.path.join(self.cur_dir, fl), " skip that dir")
continue
# append dirs with date
else:
try:
name, ext = fl.rsplit(".", 1)
if "*" in self.filetypes:
files.append(fl)
number += 1
elif ext in self.filetypes:
files.append(fl)
if "*" in self.filetypes or ext in self.filetypes:
files.append((fl, os.path.getmtime(os.path.join(self.cur_dir, fl))))
number += 1
except ValueError as e:
LOG.debug("Tried to split filename without extension ({0}).".format(e))
LOG.debug(f"Tried to split filename ({fl}) without extension ({e}).")
except Exception as e:
LOG.exception(e)
pass

if self.sortorder not in [_ASCENDING, _DESCENDING, _FOLDERFIRST, _FILEFIRST]:
self.sortorder = _FOLDERFIRST

# sort ascending/descending (file/folder mixed)
if self.sortorder == _ASCENDING or self.sortorder == _DESCENDING:
allobjects = dirs
allobjects.extend(files)
allobjects.sort(reverse = not self.sortorder == _ASCENDING)

for obj in allobjects:
allobjects = dirs + files
if self.sort_by_date:
allobjects.sort(key = lambda x: x[1], reverse = self.sortorder == _ASCENDING)
else:
allobjects.sort(key = lambda x: x[0].casefold(), reverse = not self.sortorder == _ASCENDING)

for obj, date in allobjects:
if os.path.isdir(os.path.join(self.cur_dir, obj)):
self.store.append([obj, self.dirIcon, True])
else:
icon = self._get_icon(obj)
self.store.append([obj, icon, False])

dirs.sort(key = None, reverse = False)
files.sort(key = None, reverse = False)
if self.sortorder == _FOLDERFIRST:
for dir in dirs:
self.store.append([dir, self.dirIcon, True])
for file in files:
icon = self._get_icon(file)
self.store.append([file, icon, False])
elif self.sortorder == _FILEFIRST:
for file in files:
icon = self._get_icon(file)
self.store.append([file, icon, False])
for dir in dirs:
self.store.append([dir, self.dirIcon, True])

# 2. sort folders/files separately (sortorder _FOLDERFIRST or _FILEFIRST)
else:
if self.sort_by_date:
files.sort(key = lambda x: x[1], reverse = True)
else:
files.sort(key = lambda x: x[0].casefold(), reverse = False)
if self.sort_by_date == _DATE_ALL:
dirs.sort (key = lambda x: x[1], reverse = True)
else:
dirs.sort (key = lambda x: x[0].casefold(), reverse = False)

if self.sortorder == _FOLDERFIRST:
for dir, date in dirs:
self.store.append([dir, self.dirIcon, True])
for file, date in files:
icon = self._get_icon(file)
self.store.append([file, icon, False])
else:
for file, date in files:
icon = self._get_icon(file)
self.store.append([file, icon, False])
for dir, date in dirs:
self.store.append([dir, self.dirIcon, True])
except Exception as e:
LOG.exception(e)
pass
Expand Down Expand Up @@ -599,6 +617,12 @@ def do_set_property(self, property, value):
else:
self.sortorder = value
self._fill_store()
if name == 'sortbydate':
if value not in [_DATE_NONE, _DATE_ALL, _DATE_FILESONLY]:
raise AttributeError('unknown property of sortbydate %s' % value)
else:
self.sort_by_date = value
self._fill_store()
else:
raise AttributeError('unknown iconview set_property %s' % property.name)
except Exception as e:
Expand Down
Loading