laurent@782: #!/usr/bin/env python
laurent@782: # -*- coding: utf-8 -*-
laurent@782: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
andrej@1571: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
andrej@1571: #
andrej@1571: # See COPYING file for copyrights details.
andrej@1571: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
andrej@1571: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
andrej@1571: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
laurent@782: 
andrej@1853: 
andrej@1853: from __future__ import absolute_import
laurent@782: import os
laurent@782: import shutil
laurent@782: 
laurent@782: import wx
Laurent@951: import wx.lib.buttons
laurent@782: 
andrej@1853: from editors.EditorPanel import EditorPanel
Laurent@814: from util.BitmapLibrary import GetBitmap
Laurent@951: from controls import FolderTree
laurent@783: 
andrej@1736: 
laurent@782: class FileManagementPanel(EditorPanel):
andrej@1730: 
laurent@782:     def _init_Editor(self, parent):
laurent@782:         self.Editor = wx.Panel(parent)
andrej@1730: 
laurent@782:         main_sizer = wx.BoxSizer(wx.HORIZONTAL)
andrej@1730: 
laurent@782:         left_sizer = wx.BoxSizer(wx.VERTICAL)
andrej@1745:         main_sizer.AddSizer(left_sizer, 1, border=5, flag=wx.GROW | wx.ALL)
andrej@1730: 
laurent@815:         managed_dir_label = wx.StaticText(self.Editor, label=_(self.TagName) + ":")
andrej@1745:         left_sizer.AddWindow(managed_dir_label, border=5, flag=wx.GROW | wx.BOTTOM)
andrej@1730: 
andrej@2301:         FILTER = _("All files (*.*)|*.*|CSV files (*.csv)|*.csv")
laurent@782:         self.ManagedDir = FolderTree(self.Editor, self.Folder, FILTER)
laurent@782:         left_sizer.AddWindow(self.ManagedDir, 1, flag=wx.GROW)
andrej@1730: 
laurent@782:         managed_treectrl = self.ManagedDir.GetTreeCtrl()
laurent@782:         self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, managed_treectrl)
laurent@782:         if self.EnableDragNDrop:
laurent@782:             self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, managed_treectrl)
andrej@1730: 
laurent@782:         button_sizer = wx.BoxSizer(wx.VERTICAL)
andrej@1730:         main_sizer.AddSizer(button_sizer, border=5,
andrej@1768:                             flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL)
andrej@1730: 
laurent@782:         for idx, (name, bitmap, help) in enumerate([
laurent@782:                 ("DeleteButton", "remove_element", _("Remove file from left folder")),
laurent@782:                 ("LeftCopyButton", "LeftCopy", _("Copy file from right folder to left")),
laurent@801:                 ("RightCopyButton", "RightCopy", _("Copy file from left folder to right")),
laurent@784:                 ("EditButton", "edit", _("Edit file"))]):
andrej@1768:             button = wx.lib.buttons.GenBitmapButton(
andrej@1768:                 self.Editor,
andrej@1768:                 bitmap=GetBitmap(bitmap),
andrej@1768:                 size=wx.Size(28, 28), style=wx.NO_BORDER)
laurent@782:             button.SetToolTipString(help)
laurent@782:             setattr(self, name, button)
laurent@782:             if idx > 0:
laurent@782:                 flag = wx.TOP
laurent@782:             else:
laurent@782:                 flag = 0
laurent@782:             self.Bind(wx.EVT_BUTTON, getattr(self, "On" + name), button)
laurent@782:             button_sizer.AddWindow(button, border=20, flag=flag)
andrej@1730: 
laurent@782:         right_sizer = wx.BoxSizer(wx.VERTICAL)
andrej@1745:         main_sizer.AddSizer(right_sizer, 1, border=5, flag=wx.GROW | wx.ALL)
andrej@1730: 
laurent@782:         if wx.Platform == '__WXMSW__':
laurent@782:             system_dir_label = wx.StaticText(self.Editor, label=_("My Computer:"))
laurent@782:         else:
laurent@782:             system_dir_label = wx.StaticText(self.Editor, label=_("Home Directory:"))
andrej@1745:         right_sizer.AddWindow(system_dir_label, border=5, flag=wx.GROW | wx.BOTTOM)
andrej@1730: 
laurent@782:         self.SystemDir = FolderTree(self.Editor, self.HomeDirectory, FILTER, False)
laurent@782:         right_sizer.AddWindow(self.SystemDir, 1, flag=wx.GROW)
andrej@1730: 
laurent@782:         system_treectrl = self.SystemDir.GetTreeCtrl()
laurent@782:         self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, system_treectrl)
andrej@1730: 
laurent@782:         self.Editor.SetSizer(main_sizer)
andrej@1730: 
laurent@782:     def __init__(self, parent, controler, name, folder, enable_dragndrop=False):
laurent@782:         self.Folder = os.path.realpath(folder)
laurent@782:         self.EnableDragNDrop = enable_dragndrop
andrej@1730: 
laurent@783:         if wx.Platform == '__WXMSW__':
laurent@783:             self.HomeDirectory = "/"
laurent@783:         else:
laurent@783:             self.HomeDirectory = os.path.expanduser("~")
andrej@1730: 
laurent@782:         EditorPanel.__init__(self, parent, name, None, None)
andrej@1730: 
laurent@782:         self.Controler = controler
andrej@1730: 
laurent@784:         self.EditableFileExtensions = []
laurent@784:         self.EditButton.Hide()
andrej@1730: 
laurent@782:         self.SetIcon(GetBitmap("FOLDER"))
andrej@1730: 
laurent@782:     def __del__(self):
laurent@782:         self.Controler.OnCloseEditor(self)
andrej@1730: 
laurent@782:     def GetTitle(self):
laurent@815:         return _(self.TagName)
andrej@1730: 
laurent@784:     def SetEditableFileExtensions(self, extensions):
laurent@784:         self.EditableFileExtensions = extensions
laurent@784:         if len(self.EditableFileExtensions) > 0:
laurent@784:             self.EditButton.Show()
andrej@1730: 
laurent@782:     def RefreshView(self):
laurent@782:         self.ManagedDir.RefreshTree()
laurent@782:         self.SystemDir.RefreshTree()
laurent@782:         self.RefreshButtonsState()
andrej@1730: 
laurent@782:     def RefreshButtonsState(self):
laurent@782:         managed_filepath = self.ManagedDir.GetPath()
laurent@782:         system_filepath = self.SystemDir.GetPath()
andrej@1730: 
laurent@782:         self.DeleteButton.Enable(os.path.isfile(managed_filepath))
laurent@782:         self.LeftCopyButton.Enable(os.path.isfile(system_filepath))
laurent@782:         self.RightCopyButton.Enable(os.path.isfile(managed_filepath))
laurent@784:         if len(self.EditableFileExtensions) > 0:
laurent@784:             self.EditButton.Enable(
laurent@784:                 os.path.isfile(managed_filepath) and
laurent@784:                 os.path.splitext(managed_filepath)[1] in self.EditableFileExtensions)
andrej@1730: 
laurent@782:     def OnTreeItemChanged(self, event):
laurent@782:         self.RefreshButtonsState()
laurent@782:         event.Skip()
andrej@1730: 
laurent@782:     def OnDeleteButton(self, event):
laurent@782:         filepath = self.ManagedDir.GetPath()
laurent@782:         if os.path.isfile(filepath):
andrej@1847:             _folder, filename = os.path.split(filepath)
andrej@1730: 
andrej@1730:             dialog = wx.MessageDialog(self,
andrej@1768:                                       _("Do you really want to delete the file '%s'?") % filename,
andrej@1768:                                       _("Delete File"),
andrej@1768:                                       wx.YES_NO | wx.ICON_QUESTION)
laurent@782:             remove = dialog.ShowModal() == wx.ID_YES
laurent@782:             dialog.Destroy()
andrej@1730: 
laurent@782:             if remove:
laurent@782:                 os.remove(filepath)
laurent@782:                 self.ManagedDir.RefreshTree()
laurent@782:         event.Skip()
laurent@782: 
laurent@784:     def OnEditButton(self, event):
laurent@784:         filepath = self.ManagedDir.GetPath()
andrej@1766:         if os.path.isfile(filepath) and \
andrej@1766:            os.path.splitext(filepath)[1] in self.EditableFileExtensions:
laurent@789:             self.Controler._OpenView(filepath + "::")
laurent@784:         event.Skip()
andrej@1730: 
laurent@782:     def CopyFile(self, src, dst):
laurent@782:         if os.path.isfile(src):
andrej@1847:             _src_folder, src_filename = os.path.split(src)
laurent@782:             if os.path.isfile(dst):
andrej@1847:                 dst_folder, _dst_filename = os.path.split(dst)
laurent@782:             else:
laurent@782:                 dst_folder = dst
andrej@1730: 
laurent@782:             dst_filepath = os.path.join(dst_folder, src_filename)
laurent@782:             if os.path.isfile(dst_filepath):
andrej@1768:                 dialog = wx.MessageDialog(
andrej@1768:                     self,
andrej@1768:                     _("The file '%s' already exist.\nDo you want to replace it?") % src_filename,
andrej@1768:                     _("Replace File"), wx.YES_NO | wx.ICON_QUESTION)
laurent@782:                 copy = dialog.ShowModal() == wx.ID_YES
laurent@782:                 dialog.Destroy()
laurent@782:             else:
laurent@782:                 copy = True
andrej@1730: 
laurent@782:             if copy:
laurent@782:                 shutil.copyfile(src, dst_filepath)
laurent@782:                 return dst_filepath
laurent@782:         return None
laurent@782: 
laurent@782:     def OnLeftCopyButton(self, event):
laurent@782:         filepath = self.CopyFile(self.SystemDir.GetPath(), self.ManagedDir.GetPath())
laurent@782:         if filepath is not None:
laurent@782:             self.ManagedDir.RefreshTree()
laurent@782:             self.ManagedDir.SetPath(filepath)
laurent@782:         event.Skip()
laurent@782: 
laurent@782:     def OnRightCopyButton(self, event):
laurent@782:         filepath = self.CopyFile(self.ManagedDir.GetPath(), self.SystemDir.GetPath())
laurent@782:         if filepath is not None:
laurent@782:             self.SystemDir.RefreshTree()
laurent@782:             self.SystemDir.SetPath(filepath)
laurent@782:         event.Skip()
andrej@1730: 
laurent@782:     def OnTreeBeginDrag(self, event):
laurent@782:         filepath = self.ManagedDir.GetPath()
laurent@782:         if os.path.isfile(filepath):
laurent@782:             relative_filepath = filepath.replace(os.path.join(self.Folder, ""), "")
laurent@782:             data = wx.TextDataObject(str(("'%s'" % relative_filepath, "Constant")))
laurent@782:             dragSource = wx.DropSource(self)
laurent@782:             dragSource.SetData(data)
laurent@782:             dragSource.DoDragDrop()