backup-imap-gui/backup-imap.py
from imapbackup import IMAPBackup
import wx,sys,os
from wx import xrc, MessageDialog
import threading
class MainApp(wx.App):
imb=None
def OnInit(self):
if getattr(sys, 'frozen', False):
self.res = xrc.XmlResource(os.path.join(sys._MEIPASS, "files/tgui.xrc"))
else:
self.res = xrc.XmlResource('tgui.xrc')
self.init_frame()
return True
def init_frame(self):
self.frmMain = self.res.LoadFrame(None, 'frmMain')
if getattr(sys, 'frozen', False):
ico=os.path.join(sys._MEIPASS, "files/icon.ico")
self.frmMain.SetIcon(wx.Icon(ico))
else:
self.frmMain.SetIcon(wx.Icon("icon.ico"))
self.cmdRunBackup = xrc.XRCCTRL(self.frmMain, 'cmdRunBackup')
self.frmMain.Bind(wx.EVT_BUTTON, self.OnClickCmdRunBackup, id=xrc.XRCID('cmdRunBackup'))
self.frmMain.Bind(wx.EVT_CLOSE, self.OnClickClose)
self.cmdStopBackup = xrc.XRCCTRL(self.frmMain, 'cmdStopBackup')
self.frmMain.Bind(wx.EVT_BUTTON, self.OnClickCmdStopBackup, id=xrc.XRCID('cmdStopBackup'))
self.txtUser = xrc.XRCCTRL(self.frmMain, 'txtUser')
self.txtPassword = xrc.XRCCTRL(self.frmMain, 'txtPassword')
self.txtImap = xrc.XRCCTRL(self.frmMain, 'txtImap')
self.txtPort = xrc.XRCCTRL(self.frmMain, 'txtPort')
self.chkSSL = xrc.XRCCTRL(self.frmMain, 'chkSSL')
self.chkThunderbird = xrc.XRCCTRL(self.frmMain, 'chkThunderbird')
self.dirOutput=xrc.XRCCTRL(self.frmMain, 'dirOutput')
self.gauge=xrc.XRCCTRL(self.frmMain, 'gauge')
self.lblStatus=xrc.XRCCTRL(self.frmMain, 'lblStatus')
self.cmbShowPassword=xrc.XRCCTRL(self.frmMain,'cmbShowPassword')
self.frmMain.Bind(wx.EVT_BUTTON, self.OnClickShowPassword, id=xrc.XRCID('cmdShowPassword'))
self.frmMain.Show(True)
def OnClickShowPassword(self,evt):
if (self.txtPassword.GetValue()):
p=MessageDialog(self.frmMain, self.txtPassword.GetValue(), caption="Your password")
p.ShowModal()
def checkFields(self):
ret=True
msg=""
if (not self.txtUser.GetValue()):
ret=False
msg=msg+" Check Username! "
if (len(self.txtImap.GetValue())<4):
ret=False
msg=msg+" Check Imap server! "
if (len(self.txtPort.GetValue())<1):
ret=False
msg=msg+"Check Port! "
if (not self.dirOutput.GetPath()):
ret=False
msg=msg+" Check Destination Folder! "
if (ret==False):
self.lblStatus.SetForegroundColour((255,0,0))
self.lblStatus.SetLabel(msg)
else:
self.lblStatus.SetForegroundColour((0,0,0))
return ret
def OnClickCmdRunBackup(self,evt):
if (not self.checkFields()):
return
self.conf = {
"imap":self.txtImap.GetValue() ,
"port": self.txtPort.GetValue(),
"ssl": self.chkSSL.GetValue(),
"thunderbird": self.chkThunderbird.GetValue(),
"user": self.txtUser.GetValue(),
"pass": self.txtPassword.GetValue(),
"dest": self.dirOutput.GetPath()
}
cmdt = threading.Thread(target=self.makeBackup)
cmdt.daemon = True
cmdt.start()
self.cmdRunBackup.Enable(False)
def makeBackup(self):
print(self.conf)
self.lblStatus.SetForegroundColour((0,0,0))
wx.CallAfter(self.gauge.Pulse)
wx.CallAfter(self.lblStatus.SetLabel,"Backup mailbox in progress...")
os.chdir(self.conf['dest'])
try:
'''
with IMAPBackup(
host=self.conf['imap'],
user=self.conf['user'],
password=self.conf['pass'],
port=int(self.conf['port']),
usessl=self.conf['ssl'],
thunderbird=False,
#folders=['INBOX', 'INBOX.Sent'],
) as self.imb:
self.imb.download_all_messages()
wx.CallAfter(self.lblStatus.SetLabel,"Backup done.")
'''
self.imb= IMAPBackup(
host=self.conf['imap'],
user=self.conf['user'],
password=self.conf['pass'],
port=int(self.conf['port']),
usessl=self.conf['ssl'],
thunderbird=False,
)
self.imb.kill=False
self.imb.download_all_messages()
wx.CallAfter(self.lblStatus.SetLabel,"Backup done.")
except :
wx.CallAfter(self.lblStatus.SetLabel,"We have a problem! Please Check your parameters ")
wx.CallAfter(self.gauge.SetValue, 0)
wx.CallAfter(self.cmdRunBackup.Enable, True)
wx.CallAfter(self.gauge.SetValue, 0)
wx.CallAfter(self.cmdRunBackup.Enable, True)
def OnClickCmdStopBackup(self,evt):
print("Kill")
if (self.imb.mailserver):
self.imb.mailserver.logout()
wx.CallAfter(self.gauge.SetValue, 0)
wx.CallAfter(self.cmdRunBackup.Enable, True)
wx.CallAfter(self.lblStatus.SetLabel,"Operation canceled by the user.")
def OnClickClose(self,evt):
print("Quit")
sys.exit()
if __name__ == '__main__':
app = MainApp(False)
app.MainLoop()