"""
FtpCube
Copyright (C) Michael Gilfix
This file is part of FtpCube.
You should have received a file COPYING containing license terms
along with this program; if not, write to Michael Gilfix
(mgilfix@eecs.tufts.edu) for a copy.
This version of FtpCube is open source; you can redistribute it and/or
modify it under the terms listed in the file COPYING.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""
import dialog
import messages
import utils
import wx
class ChmodWindow(dialog.DialogWindow):
"""Change permissions window dialog.
This dialog allows users to toggle the permission settings for files. Permissions are
described in unix style format, with user/group/other permissions. Each category has
the unix style read, write, and exec entries. During initialization, the window is
given an octal permission description that is then translated into the various permission
boxes. When window entry has completed, the permissions can then be extracted back in
octal form."""
# User permission IDs
idCHECK_UREAD = wx.NewId()
idCHECK_UWRITE = wx.NewId()
idCHECK_UEXEC = wx.NewId()
# Group permission IDs
idCHECK_GREAD = wx.NewId()
idCHECK_GWRITE = wx.NewId()
idCHECK_GEXEC = wx.NewId()
# Other permission IDs
idCHECK_OREAD = wx.NewId()
idCHECK_OWRITE = wx.NewId()
idCHECK_OEXEC = wx.NewId()
def __init__(self, parent, file, perm, title=None):
"""Creates a change permission window and populates the permission check box settings
with the specified octal permission."""
if __debug__:
print "Making chmod window."
self.file = file
self.mode = perm
if title is None:
title = _("Ftpcube - Change File Permissions")
dialog.DialogWindow.__init__(self, parent, title)
self.SetIcon(utils.getAppIcon())
panel = self.getDialogPanel()
# Construct the user permissions section
self.uread = wx.CheckBox(panel, self.idCHECK_UREAD, _("Read"))
self.uwrite = wx.CheckBox(panel, self.idCHECK_UWRITE, _("Write"))
self.uexec = wx.CheckBox(panel, self.idCHECK_UEXEC, _("Exec"))
ufsizer = wx.GridSizer(1, 3)
ufsizer.AddMany([
(self.uread, 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.uwrite, 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.uexec, 0, wx.ALIGN_CENTER_HORIZONTAL),
])
ubox = wx.StaticBox(panel, -1, _("User Access"))
usizer = wx.StaticBoxSizer(ubox, wx.HORIZONTAL)
usizer.Add(ufsizer, 1, wx.EXPAND)
# Construct the group permissions section
self.gread = wx.CheckBox(panel, self.idCHECK_GREAD, _("Read"))
self.gwrite = wx.CheckBox(panel, self.idCHECK_GWRITE, _("Write"))
self.gexec = wx.CheckBox(panel, self.idCHECK_GEXEC, _("Exec"))
gfsizer = wx.GridSizer(1, 3)
gfsizer.AddMany([
(self.gread, 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.gwrite, 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.gexec, 0, wx.ALIGN_CENTER_HORIZONTAL),
])
gbox = wx.StaticBox(panel, -1, _("Group Access"))
gsizer = wx.StaticBoxSizer(gbox, wx.HORIZONTAL)
gsizer.Add(gfsizer, 1, wx.EXPAND)
# Construct the others permissions section
self.oread = wx.CheckBox(panel, self.idCHECK_OREAD, _("Read"))
self.owrite = wx.CheckBox(panel, self.idCHECK_OWRITE, _("Write"))
self.oexec = wx.CheckBox(panel, self.idCHECK_OEXEC, _("Exec"))
ofsizer = wx.GridSizer(1, 3)
ofsizer.AddMany([
(self.oread, 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.owrite, 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.oexec, 0, wx.ALIGN_CENTER_HORIZONTAL),
])
obox = wx.StaticBox(panel, -1, _("Other Access"))
osizer = wx.StaticBoxSizer(obox, wx.HORIZONTAL)
osizer.Add(ofsizer, 1, wx.EXPAND)
file_label = wx.StaticText(panel, -1, _("Permissions for %(file)s")
%{ 'file' : self.file })
fsizer = wx.FlexGridSizer(7, 3)
fsizer.AddMany([
((10, 10)), ((10, 10)), ((10, 10)),
((10, 5)), (usizer, 1, wx.EXPAND), ((10, 5)),
((10, 10)), ((10, 10)), ((10, 10)),
((10, 5)), (gsizer, 1, wx.EXPAND), ((10, 5)),
((10, 10)), ((10, 10)), ((10, 10)),
((10, 5)), (osizer, 1, wx.EXPAND), ((10, 5)),
((10, 10)), ((10, 10)), ((10, 10)),
((10, 5)), (file_label, 0, wx.ALIGN_CENTER_HORIZONTAL), ((10, 5)),
])
fsizer.AddGrowableCol(1)
sizer = wx.FlexGridSizer(3, 3)
sizer.AddMany([
((10, 10)), ((10, 10)), ((10, 10)),
((10, 10)), (fsizer, 1, wx.EXPAND), ((10, 10)),
((10, 10)), ((10, 10)), ((10, 10)),
])
sizer.AddGrowableCol(1)
sizer.AddGrowableRow(1)
panel.SetAutoLayout(True)
panel.SetSizer(sizer)
sizer.Fit(panel)
sizer.SetSizeHints(panel)
try:
self.displayPermissions()
except TypeError:
messages.displayErrorDialog(self, _("Attempted to display invalid permissions"))
self.Destroy()
return
self.SetSizeHints(300, -1)
self.renderDialog()
def getFile(self):
"""Returns the file associated with the permissions change window."""
return self.file
def getPermissions(self):
"""Returns the permissions represented by the window's control configurations in
octal format."""
if self.mode is None:
return self.mode
perm = 0
if self.uread.GetValue():
perm |= (4 << 6)
if self.uwrite.GetValue():
perm |= (2 << 6)
if self.uexec.GetValue():
perm |= (1 << 6)
if self.gread.GetValue():
perm |= (4 << 3)
if self.gwrite.GetValue():
perm |= (2 << 3)
if self.gexec.GetValue():
perm |= (1 << 3)
if self.oread.GetValue():
perm |= 4
if self.owrite.GetValue():
perm |= 2
if self.oexec.GetValue():
perm |= 1
return perm
def displayPermissions(self):
"""Displays the permissions associated with the creation of the dialog by populating
the permission check boxes.
The permission value is expected to be an integer in octal format. Any other
format will result in a TypeError exception."""
if not isinstance(self.mode, int):
raise TypeError, _("Permission must be an int")
if self.mode & (4 << 6):
self.uread.SetValue(True)
if self.mode & (2 << 6):
self.uwrite.SetValue(True)
if self.mode & (1 << 6):
self.uexec.SetValue(True)
if self.mode & (4 << 3):
self.gread.SetValue(True)
if self.mode & (2 << 3):
self.gwrite.SetValue(True)
if self.mode & (1 << 3):
self.gexec.SetValue(True)
if self.mode & 4:
self.oread.SetValue(True)
if self.mode & 2:
self.owrite.SetValue(True)
if self.mode & 1:
self.oexec.SetValue(True)
|