#!/usr/bin/python

# translation of tkgetpics.py to wxgetpics.py

#For Debian, simply apt-get install libwxgtk2.4-python

from wxPython.wx import *
import os, sys, shutil

ID_VIEWBUTTON=101
ID_COPYBUTTON=102
ID_MOVEBUTTON=103
ID_QUITBUTTON=104
ID_ABOUTBUTTON=105
ID_DELETEBUTTON=106

ID_VIEW=201
ID_COPY=202
ID_MOVE=203
ID_QUIT=204
ID_ABOUT=205
ID_DELETE=206


class MainWindow(wxFrame):
    def __init__(self,parent,id,title):
        wxFrame.__init__(self,parent,wxID_ANY, title, size = ( 300, 300 ),
                    style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE|wxWANTS_CHARS)
        self.CreateStatusBar()
        
         
        # Setting up the menu.
        filemenu= wxMenu()
        filemenu.Append(ID_VIEW, "&View"," View images on camera ")
        filemenu.Append(ID_COPY, "&Copy", " Copy images to drive ")
        filemenu.Append(ID_MOVE, "&Move", " Move images to drive ")
        filemenu.Append(ID_DELETE, "&Delete", " Delete all images from camera ")
        filemenu.Append(ID_QUIT,"&Quit"," Terminate the program ")
        filemenu.Append(ID_ABOUT, "&About"," Information about this program ")
        filemenu.AppendSeparator()
        # Creating the menubar.
        menuBar = wxMenuBar()
        menuBar.Append(filemenu,"&Actions") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.        
        EVT_MENU(self, ID_VIEW, self.OnViewClick) # attach the menu-events
        EVT_MENU(self, ID_COPY, self.OnCopyClick) 
        EVT_MENU(self, ID_MOVE, self.OnMoveClick) 
        EVT_MENU(self, ID_QUIT, self.OnQuitClick) 
        EVT_MENU(self, ID_ABOUT, self.OnAboutClick) 
        EVT_MENU(self, ID_DELETE, self.OnDeleteClick) 
      
        
        self.topsizer = wxBoxSizer(wxHORIZONTAL)
        self.buttonsizer = wxBoxSizer(wxVERTICAL)
        self.bottomsizer = wxBoxSizer(wxHORIZONTAL)
        self.mainsizer = wxBoxSizer(wxVERTICAL)
        s = "wxgetpics.py, utility to\ncopy, move, delete,\nor view images on a\nFujifilm 3800 digital camera\n\nYou may change Destination directory"
        self.label1 = wxStaticText(self, 4, s, style=wxALIGN_CENTRE)
        self.topsizer.Add(self.label1,1,wxEXPAND)
        self.edit = wxTextCtrl(self, 1, "directory", style=wxTE_CENTER)
        self.buttonsizer.Add(self.edit,1,wxEXPAND)
        
        self.text = wxTextCtrl(self, 1, size=(300,200), style=wxTE_MULTILINE)
        self.text.SetValue("Status display");
        self.bottomsizer.Add(self.text,1,wxEXPAND)
                
        #  buttons - view copy move quit about delete
        self.viewbutton=wxButton(self,ID_VIEWBUTTON, " View Images on Camera ")
        self.copybutton=wxButton(self,ID_COPYBUTTON, " Copy Images from camera to directory ")
        self.movebutton=wxButton(self,ID_MOVEBUTTON, " Move Images from camera to directory ")
        self.deletebutton=wxButton(self,ID_DELETEBUTTON, " Delete Images from Camera ")
        self.quitbutton=wxButton(self,ID_QUITBUTTON, " Quit Program ")
        self.aboutbutton=wxButton(self,ID_ABOUTBUTTON, " About wxGetPics ")
        
        EVT_BUTTON(self, ID_ABOUTBUTTON, self.OnAboutClick)
        EVT_BUTTON(self, ID_VIEWBUTTON, self.OnViewClick)
        EVT_BUTTON(self, ID_COPYBUTTON, self.OnCopyClick)
        EVT_BUTTON(self, ID_MOVEBUTTON, self.OnMoveClick)
        EVT_BUTTON(self, ID_DELETEBUTTON, self.OnDeleteClick)
        EVT_BUTTON(self, ID_QUITBUTTON, self.OnQuitClick)
        
        self.buttonsizer.Add(self.viewbutton, 1,wxEXPAND)
        self.buttonsizer.Add(self.copybutton,1,wxEXPAND)
        self.buttonsizer.Add(self.movebutton,1,wxEXPAND)
        self.buttonsizer.Add(self.deletebutton,1,wxEXPAND)
        self.buttonsizer.Add(self.quitbutton,1,wxEXPAND)
        self.buttonsizer.Add(self.aboutbutton,1,wxEXPAND)
        
        self.mainsizer.Add(self.topsizer,0,wxEXPAND)
        self.mainsizer.Add(self.buttonsizer,0,wxEXPAND)
        self.mainsizer.Add(self.bottomsizer,0,wxEXPAND)
        self.SetSizer(self.mainsizer)
        self.mainsizer.Fit(self)
        self.getdate(self.edit)
        self.Show(true)

    def buttonsenable(self): 
        self.viewbutton.Enable(true)
        self.copybutton.Enable(true)
        self.movebutton.Enable(true)
        self.quitbutton.Enable(true)
        self.aboutbutton.Enable(true)
        self.deletebutton.Enable(true)
        self.SetCursor(wxSTANDARD_CURSOR)
        return 0

    def buttonsdisable(self):
        self.viewbutton.Enable(false)
        self.copybutton.Enable(false)
        self.movebutton.Enable(false)
        self.quitbutton.Enable(false)
        self.aboutbutton.Enable(false)
        self.deletebutton.Enable(false)       
        self.SetCursor(wxHOURGLASS_CURSOR)
        return 0

    def hlp(self):
        h = '''wxgetpics by k.g.moffat \
        \nwritten using wxPython. \
        \n(for Debian, apt-get install libwxgtk2.4-python)\n \
        \nUse to View, Copy, Move or Delete \
        \npictures from a Fuji3800 (easily edited for other models) \
        \nDestination directory defaults to home/images/current date - \
        \n ~/images/yyyymmdd \
        \nDepends on mount point /mnt/camera existing, \
        \npointing to your camera or other usb-mass-storage device \
        \nwith the appropriate directory structure, and /mnt/camera \
        \nbeing included in /etc/fstab as mountable by user \
        \nAlso, gqview must be installed. \
        \n \
        \nToDo: preferences file, check for existence of targets '''

        d = wxMessageDialog(self, h, "wxgetpics.py",wxOK )
        d.ShowModal()
        d.Destroy()
        return 0

    def OnAboutClick(self,e):
        self.buttonsdisable()
        self.hlp()
        self.buttonsenable()
        return 0

    def OnViewClick(self, e):
        self.buttonsdisable()
        self.text.AppendText("\nStarting...")
        dirstr="/mnt/camera/dcim/100_fuji/"
        self.text.AppendText("\nAttempting camera mount.\n")
        result = os.system("mount /mnt/camera")
        if(result != 0):
            self.text.AppendText("\nMount unsuccessful!\nDoes mountpoint /mnt/camera exist?\n")
            
        result = os.system("ls /mnt/camera/dcim/100_fuji/")
        if(result != 0):
            self.text.AppendText("\nFuji 3800 not connected!\n")
            
            self.buttonsenable()
            
            return 1
            
        self.text.AppendText("\nOpening viewer\n")
        viewstr = "gqview " + dirstr
        result = os.system(viewstr)
        self.text.AppendText("\nUnmounting camera\n")
        result = os.system("umount /mnt/camera")
        if(result != 0):
            self.text.AppendText("\nCamera unmount failed!\n")
        self.text.AppendText("\n\nDone.\n")
        self.buttonsenable()
        return 0

    def getdate(self,e):
        f = os.popen('date +%Y%m%d')
        d = f.read()
        f.close()
        #cwd = os.getcwd()
        e.SetValue("/home/ken/images/" + d[:-1]) # remove \n 
        return 0

    def OnDeleteClick(self, e):
        self.buttonsdisable()
        d = wxMessageDialog(self, "Really DELETE ALL?", "Verify!",wxYES_NO|wxNO_DEFAULT|wxICON_WARNING)
        if d.ShowModal() == wxID_NO:
            self.buttonsenable()
            d.Destroy()
            return 1
        else:
            if d.ShowModal() == wxID_YES:
                d.Destroy()
                if (os.path.exists("/mnt/camera/dcim/100_fuji")) or (self.mountcamera() == 0) :
                    self.text.AppendText("\nDeleting all pics from camera.\nHope this is ok.\nToo late now!\n")
                    s = "rm /mnt/camera/dcim/100_fuji/*.*"
                    result = os.system(s)
                    if(result != 0):
                        self.text.AppendText("\nDelete unsuccessful, Unmounting camera\n");
                    else:
                        self.text.AppendText("\nAll images deleted, Unmounting camera\n")
                    result = os.system("umount /mnt/camera")
                    if(result != 0):
                        self.text.AppendText("\nCamera unmount failed.\n")
                    self.text.AppendText("\n\nDone.\n")
                else:
                    self.text.AppendText("\nNo camera!\n")
        self.buttonsenable()
        return 0

    def OnCopyClick(self, e):
        self.buttonsdisable()
        os.system("umount /mnt/camera")
        d = wxMessageDialog(self, "Okay to Copy Images to Disk?", "Verify!",wxYES_NO|wxYES_DEFAULT|wxICON_QUESTION)
        if d.ShowModal() == wxID_NO:
            self.buttonsenable()
            d.Destroy()
            return 1
        d.Destroy()
        if self.mountcamera() == 0:
            print "Mounted."
            #copy pictures.
            self.copypics()
        else:
            print ("Mount failed.")
        self.buttonsenable()
        return 0
        
    def copypics(self):
        dirstr=self.edit.GetValue()
        mk = "mkdir -p " + dirstr
        result = os.system(mk)
        if result != 0:
            self.text.AppendText("\nCan not " + mk + '\n')
            return 1
        
        # change to step through a directory one image at a time.
        # docopy(src, dest)
        
        cpstr = "cp /mnt/camera/dcim/100_fuji/*.* " + dirstr   
        self.text.AppendText("\nStarting Copy\n")
        
        result = self.docopy('/mnt/camera/dcim/100_fuji/', dirstr)
        
        #result = os.system(cpstr)
        
        if result != 0:
            self.text.AppendText("\nError during copy...\n")
            result = os.system("umount /mnt/camera")
            if(result != 0):
                self.text.AppendText("\nCamera unmount failed.\n")
            return 1
        self.text.AppendText("\nCopy complete.\n")
        self.text.AppendText("\nDone.\n")
        self.text.AppendText("\nOpening Viewer\n")
        viewstr = "gqview " + dirstr
        result = os.system(viewstr)
        if result != 0:
            self.text.AppendText("\nNo viewer?!\n")
            self.buttonsenable()
            os.system("umount /mnt/camera")
            return 1
        self.text.AppendText("\nDone.\n")
        os.system("umount /mnt/camera")
        return 0
        
    
    def docopy(self,src,dest):
        images = os.listdir(src)
        for i in images:
            if os.path.exists(dest + '/' + i ):
                print dest +'/'+  i  + ' exists. Skipped.'
            else:
                print'copying ' + src + i +' to ' + dest
                shutil.copy(src + i,dest+'/') # shutil.copyfile
            
        return 0
        
    def OnMoveClick(self,e):
        self.buttonsdisable()
        os.system("umount /mnt/camera")
        d = wxMessageDialog(self, "Okay to MOVE Images to Disk, DELETING from camera\nand, if it exists, OVERWRITING images in " + self.edit.GetValue() + "?", "Verify!",wxYES_NO|wxNO_DEFAULT|wxICON_WARNING)
        if d.ShowModal() == wxID_NO:
            self.buttonsenable()
            d.Destroy()
            return 1
        d.Destroy()
        if self.mountcamera() == 0:
            print("Camera mounted.")
            #move pictures.
            self.movepics()
        else:
            print ("Mount failed.")
        os.system("umount /mnt/camera")
        self.text.AppendText("\nDone\n")
        self.buttonsenable()
        return 0 
        
    def movepics(self):
        dirstr = self.edit.GetValue()
        print "dirstr="+dirstr
        if os.path.exists(dirstr):
            # dialog to exit or overwrite files.
            d = wxMessageDialog(self, "Directory exists. OVERWRITING images in " + self.edit.GetValue() + "?", "Verify!",wxYES_NO|wxNO_DEFAULT|wxICON_WARNING)
            if d.ShowModal() == wxID_NO:
                self.buttonsenable()
                d.Destroy()
                return 1
            d.Destroy()
        
        self.text.AppendText("\nMaking directory\n")
        mk = "mkdir -p " + dirstr
        result = os.system(mk)
        if result != 0:
            self.text.AppendText("\nCan not " + mk + '\n')
            return 1
        mvstr = "mv /mnt/camera/dcim/100_fuji/*.* " + dirstr   
        self.text.AppendText("\nAttempting move\n")
        result = os.system(mvstr)
        if result != 0:
            self.text.AppendText("\nError during move...\n")
            result = os.system("umount /mnt/camera")
            if(result != 0):
                self.text.AppendText("\nCamera unmount failed.\n")
            return 1
        self.text.AppendText("\nMove complete\n")
        self.text.AppendText("\nOpening Viewer\n")
        viewstr = "gqview " + dirstr
        result = os.system(viewstr)
        if result != 0:
            self.text.AppendText("\nNo viewer?!\n")
            self.buttonsenable()
            os.system("umount /mnt/camera")
            return 1
        self.text.AppendText("\nDone.\n")
        os.system("umount /mnt/camera")
        return 0
    
    def mountcamera(self):
        self.text.AppendText("\nAttempting camera mount.\n")
        result = os.system("mount /mnt/camera")
        if(result != 0):
            self.text.AppendText("\nMount unsuccessful!\nDoes mountpoint /mnt/camera exist?\n")
            return 1
        result = os.system("ls /mnt/camera/dcim/100_fuji/")
        if(result != 0):
            self.text.AppendText("\nFuji 3800 not connected!\n")
            return 1
        self.text.AppendText("\nCamera mounted.\n")
        return 0

    def OnQuitClick(self, e):
        #self.buttonsdisable()
        #d = wxMessageDialog(self, "Really Quit?", "quit?",wxYES_NO|wxICON_QUESTION)
        #if d.ShowModal() == wxID_YES:
            self.Close(true)
        #self.buttonsenable()
        #return 0


app= wxPySimpleApp()
frame = MainWindow(None, -1, "wxgetpics.py")
frame.Show(1)
app.MainLoop()

