Exemplo n.º 1
0
 def InitMountItems(self):
     # Failsafe conditional in case of errors reading user home directory
     home_exists = os.path.isdir(PATH_home)
     if home_exists:
         home_item = self.AppendItem(self.root_item, GT(u'Home directory'), PATH_home,
                 ImageList.GetImageIndex(u'folder-home'),
                 expImage=ImageList.GetImageIndex(u'folder-home-open'))
         
         self.mount_list.append(home_item)
     
     # List storage devices currently mounted on system
     stdevs = GetMountedStorageDevices()
     
     for DEV in stdevs:
         # Do not re-add home directory in case it is mounted on its own partition
         if DEV.MountPoint == PATH_home:
             continue
         
         add_item = os.path.ismount(DEV.MountPoint)
         
         if add_item:
             for PITEM in self.mount_list:
                 if DEV.MountPoint == PITEM.Path:
                     add_item = False
                     break
         
         if add_item:
             Logger.Debug(__name__, u'Adding new mount PathItem instance: {}'.format(DEV.Label))
             
             self.mount_list.append(self.AppendItem(self.root_item, DEV.Label, DEV.MountPoint,
                     ImageList.GetImageIndex(DEV.Type)))
             continue
         
         else:
             Logger.Debug(__name__, u'PathItem instance for "{}" directory already exists'.format(DEV.MountPoint))
Exemplo n.º 2
0
 def Expand(self, item):
     if isinstance(item, PathItem):
         if item.IsFile():
             return False
         
         dirs = []
         files = []
         
         if not self.ItemHasChildren(item):
             # FIXME: Should use regular expressions for filter
             item_path = item.GetPath()
             
             try:
                 for LABEL in os.listdir(item_path):
                     # Ignore filtered items
                     filtered = False
                     for FILTER in self.exclude_pattern:
                         if LABEL.startswith(FILTER):
                             filtered = True
                             break
                     
                     if not filtered:
                         child_path = ConcatPaths((item_path, LABEL))
                         
                         if os.path.isdir(child_path) and os.access(child_path, os.R_OK):
                             dirs.append((LABEL, child_path,))
                         
                         elif os.path.isfile(child_path) and os.access(child_path, os.R_OK):
                             files.append((LABEL, child_path,))
                 
                 # Sort directories first
                 for DIR, PATH in sorted(dirs):
                     child = self.AppendItem(item, DIR, PATH)
                     self.SetItemImage(child, ImageList.GetImageIndex(u'folder'), wx.TreeItemIcon_Normal)
                     self.SetItemImage(child, ImageList.GetImageIndex(u'folder-open'), wx.TreeItemIcon_Expanded)
                     
                     item.AddChild(child)
                 
                 for FILE, PATH in sorted(files):
                     child = self.AppendItem(item, FILE, PATH)
                     self.SetItemImage(child, child.ImageIndex, wx.TreeItemIcon_Normal)
                     
                     item.AddChild(child)
             
             except OSError:
                 Logger.Warn(__name__, u'No such file or directory: {}'.format(item_path))
     
     # Recursively expand parent items
     parent = self.GetItemParent(item)
     if parent:
         self.Expand(parent)
     
     if isinstance(item, PathItem):
         item = item.GetBaseItem()
     
     return wx.TreeCtrl.Expand(self, item)
Exemplo n.º 3
0
    def __init__(self, item, path, label=None):
        if path == None:
            # So that calls to os.path.exists(PathItem.Path) do not raise exception
            path = wx.EmptyString

        self.Item = item
        self.Path = path
        self.Label = label
        self.Children = []
        self.Type = None

        if self.Path:
            self.Type = GetFileMimeType(self.Path)

            executables_binary = (u'x-executable', )

            executables_text = (
                u'x-python',
                u'x-shellscript',
            )

            # Don't use MIME type 'inode' for directories (symlinks are inodes)
            if os.path.isdir(self.Path):
                self.Type = u'folder'

            elif self.Type.startswith(u'image'):
                self.Type = u'image'

            elif self.Type.startswith(u'audio'):
                self.Type = u'audio'

            elif self.Type.startswith(u'video'):
                self.Type = u'video'

            else:
                # Exctract second part of MIME type
                self.Type = self.Type.split(u'/')[-1]

                if self.Type in executables_binary:
                    self.Type = u'executable-binary'

                elif self.Type in executables_text:
                    self.Type = u'executable-script'

            self.ImageIndex = ImageList.GetImageIndex(self.Type)

            # Use generic 'file' image as default
            if self.ImageIndex == ImageList.GetImageIndex(u'failsafe'):
                self.ImageIndex = ImageList.GetImageIndex(u'file')

            Logger.Debug(
                __name__,
                u'PathItem type: {} ({})'.format(self.Type, self.Path))
Exemplo n.º 4
0
    def __init__(self,
                 parent,
                 w_id=wx.ID_ANY,
                 path=PATH_home,
                 exclude_pattern=[
                     u'.*',
                 ],
                 pos=wx.DefaultPosition,
                 size=wx.DefaultSize,
                 style=wx.TR_DEFAULT_STYLE,
                 validator=wx.DefaultValidator,
                 name=wx.TreeCtrlNameStr):

        wx.TreeCtrl.__init__(self,
                             parent,
                             w_id,
                             pos,
                             size,
                             style=style | wx.TR_HAS_BUTTONS | wx.TR_MULTIPLE
                             | wx.BORDER_NONE,
                             validator=validator,
                             name=name)

        self.AssignImageList()

        # FIXME: Use regular expressions
        #self.exclude_pattern = list(exclude_pattern)
        self.exclude_pattern = [u'.']

        self.current_path = path

        # NOTE: Use individual items children???
        self.item_list = []

        self.root_item = self.AddRoot(GT(u'System'),
                                      ImageList.GetImageIndex(u'computer'))

        self.COLOR_default = self.GetItemBackgroundColour(self.root_item)

        # List of sub-root items that shouldn't be deleted if they exist on filesystem
        # FIXME: Should not need to use a root list now with GetDeviceMountPoints function
        self.mount_list = []

        self.ctx_menu = wx.Menu()

        mitm_add = wx.MenuItem(self.ctx_menu, wx.ID_ADD, GT(u'Add to project'))
        mitm_expand = wx.MenuItem(self.ctx_menu, menuid.EXPAND, GT(u'Expand'))
        mitm_rename = wx.MenuItem(self.ctx_menu, menuid.RENAME, GT(u'Rename'))
        mitm_refresh = wx.MenuItem(self.ctx_menu, wx.ID_REFRESH,
                                   GT(u'Refresh'))

        self.ctx_menu.AppendItem(mitm_add)
        self.ctx_menu.AppendItem(mitm_expand)
        self.ctx_menu.AppendItem(mitm_rename)
        self.ctx_menu.AppendSeparator()
        self.ctx_menu.AppendItem(mitm_refresh)

        # FIXME: Hack
        self.trash = False

        if GetExecutable(u'gvfs-trash'):
            mitm_delete = wx.MenuItem(self.ctx_menu, wx.ID_DELETE,
                                      GT(u'Trash'))
            self.ctx_menu.InsertItem(2, mitm_delete)
            self.trash = True

        # Tells app if user is currently dragging an item from tree
        self.dragging = False

        # *** Event handlers *** #

        self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
        wx.EVT_KEY_DOWN(self, self.OnDoubleClick)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

        self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpand)
        self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnCollapse)

        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelect)

        self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)

        wx.EVT_MENU(self, menuid.EXPAND, self.OnMenuSelect)
        wx.EVT_MENU(self, menuid.RENAME, self.OnMenuSelect)
        wx.EVT_MENU(self, wx.ID_DELETE, self.OnMenuSelect)
        wx.EVT_MENU(self, wx.ID_REFRESH, self.OnRefresh)

        self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnEndLabelEdit)

        self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnDragBegin)
        self.Bind(wx.EVT_LEFT_UP, self.OnDragEnd)

        # *** Post-layout/event actions *** #

        self.InitMountItems()

        # Expand the user's home directory
        if self.GetHomeItem():
            self.InitDirectoryLayout()