package fileproperties import uk.co.nickthecoder.glok.application.Platform /** * Also adds features for * Only applicable to unix-like operating systems (i.e. NOT Windows). */ class FileProperties : SimpleComponent() { class val lsPattern = Pattern.compile( "([^\\s]*)\\s*[^\\s]*\\s*([^\\s]*)\\s*([^\\s]*)\\s*([^\s]*)" ) override meth register() { if (!Platform.INSTANCE.isWindows()) { addFeatures( Context.FOLDER, 99 ).apply { val folderProperties = contextAction( "Folder Properties", ::filePropertiesAction ) val changeOwner = contextAction( "Change Owner", ::changeOwnerAction ) val changeGroup = contextAction( "Change Group", ::changeGroupAction ) val changePermissions = contextAction( "Change Permissions", ::changePermissionsAction ) menus( MenuFeature( "Folder", separator, folderProperties, changeOwner, changeGroup, changePermissions ) ) contextMenu( separator, folderProperties, changeOwner, changeGroup, changePermissions ) } addFeatures( Context.FILE ).apply { val fileProperties = contextAction( "File Properties", ::filePropertiesAction ) val changeOwner = contextAction( "Change Owner", ::changeOwnerAction ) val changeGroup = contextAction( "Change Group", ::changeGroupAction ) val changePermissions = contextAction( "Change Permissions", ::changePermissionsAction ) menus( MenuFeature( "File", separator, fileProperties, changeOwner, changeGroup, changePermissions ) ) contextMenu( separator, fileProperties, changeOwner, changeGroup, changePermissions ) } } } // ==== Actions ==== func filePropertiesAction( context : Context ) { openFileProperties( context.value as File ) } func changeOwnerAction( context : Context ) { ChangeOwnerForm( context.value as File ).promptInNewTab() } func changeGroupAction( context : Context ) { ChangeGroupForm( context.value as File ).promptInNewTab() } func changePermissionsAction( context : Context ) { ChangePermissionsForm( context.value as File ).promptInNewTab() } // ==== API ==== func openFileProperties( fileOrFolder : File ) { filePropertiesBehaviour( fileOrFolder ).openBehaviour() } func filePropertiesBehaviour( fileOrFolder : File ) : FormBehaviour { val title = if (fileOrFolder.isFolder()) "Folder Properties" else "File Properties" val form = FileOrFolderPropertiesForm( title, fileOrFolder ) return FileOrFolderPropertiesBehaviour( form ) } } /** * Displays file properties. All form elements are read-only. * See ChangeOwner, ChangeGroup and ChangePermissions. */ class FileOrFolderPropertiesForm( title : String, val fileOrFolder : File ) : Form( title ) { val name = stringProperty( "Name", fileOrFolder.name ).apply { readOnly = true } val location = stringProperty( "Location", fileOrFolder.parentFile.path ).apply { readOnly = true } val mimeType = stringProperty( "Mime Type", "" ).apply { readOnly = true columns = 30 } val owner = stringProperty( "Owner", "" ).apply { readOnly = true columns = 20 } val group = stringProperty( "Group", "" ).apply { readOnly = true columns = 20 } val size = stringProperty( "Size", toHumanBytes(fileOrFolder.length()) ).apply { readOnly = true columns = 10 } val permissions = stringProperty( "Permissions", "" ).apply { readOnly = true columns = 10 } init { val mime = guessMimeType( fileOrFolder ) if (mime != null) { mimeType.value = mime } // TODO Update owner, group val ls = $( ls -ld '$fileOrFolder' ).eval() val matcher = FileProperties.lsPattern.matcher( ls ) if (matcher.find()) { permissions.value = matcher.group(1) owner.value = matcher.group(2) group.value = matcher.group(3) } } override meth elements() = listOf( name, location, mimeType, owner, group, size, permissions ) } /** * Displays file properties. All form elements are read-only. * See ChangeOwner, ChangeGroup and ChangePermissions. */ class FileOrFolderPropertiesBehaviour( val form : FileOrFolderPropertiesForm ) : FormBehaviour( form.title, form ) { init { contexts.addAll( contextsForFile( form.fileOrFolder ) ) } override meth onOK() { closeCurrentBehaviour() } override meth onCancel() { closeCurrentBehaviour() } }