version 1.0
This commit is contained in:
167
mail-notifier.py
167
mail-notifier.py
@@ -5,14 +5,32 @@ from PyQt5.QtWidgets import (QAction, QApplication, QCheckBox, QComboBox,
|
||||
QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,
|
||||
QMessageBox, QMenu, QPushButton, QSpinBox, QStyle, QSystemTrayIcon,
|
||||
QTextEdit, QVBoxLayout)
|
||||
from PyQt5.QtCore import (QThread, QTimer)
|
||||
from PyQt5.QtCore import (QThread, QTimer, QFile, QSettings)
|
||||
import imaplib
|
||||
imaplib._MAXLINE = 400000
|
||||
import subprocess
|
||||
import res
|
||||
from ui_settings import Ui_Settings
|
||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||
import os
|
||||
import socket
|
||||
import hashlib, uuid
|
||||
|
||||
#variables
|
||||
timers = []
|
||||
programTitle = "Mail Notifier"
|
||||
settings = QSettings(os.path.expanduser("~")+"/.config/mail-notify/settings.conf", QSettings.NativeFormat)
|
||||
def SettingsExist():
|
||||
if ((settings.contains("CheckInterval") and settings.value("CheckInterval") != "") and
|
||||
(settings.contains("Notify") and settings.value("Notify") != "") and
|
||||
(settings.contains("MailServer") and settings.value("MailServer") != "") and
|
||||
(settings.contains("Port") and settings.value("Port") != "") and
|
||||
(settings.contains("Login") and settings.value("Login") != "") and
|
||||
(settings.contains("Password") and settings.value("Password") != "") and
|
||||
(settings.contains("SSL") and settings.value("SSL") != "")):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
class Window(QDialog):
|
||||
def __init__(self):
|
||||
@@ -25,66 +43,138 @@ class Window(QDialog):
|
||||
self.trayIcon.setIcon(QIcon(":icons/mailbox_empty.png"))
|
||||
self.trayIcon.setToolTip("You have no unread letters")
|
||||
self.trayIcon.show()
|
||||
|
||||
# setup settings
|
||||
self.ui = Ui_Settings()
|
||||
self.ui.setupUi(self)
|
||||
self.setWindowIcon(QIcon(os.path.dirname(os.path.realpath(__file__))+"/icons/mailbox_empty.png"))
|
||||
self.SettingsRestore()
|
||||
|
||||
self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).clicked.connect(self.btnOK_clicked)
|
||||
self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel).clicked.connect(self.btnCancel_clicked)
|
||||
self.ui.btnTestConnection.clicked.connect(self.btnTestConnection_clicked)
|
||||
# Menu actions
|
||||
def createActions(self):
|
||||
self.quitAction = QAction(QIcon(':icons/menu_quit.png'),"&Quit", self,
|
||||
triggered=QApplication.instance().quit)
|
||||
self.checkNow = QAction(QIcon(':icons/check_now.png'),"&Check now", self,
|
||||
triggered=self.checkNow)
|
||||
# self.restoreAction = QAction("&Restore", self,
|
||||
# triggered=self.showNormal)
|
||||
triggered=mail_check)
|
||||
self.restoreAction = QAction(QIcon(":icons/settings.png"),"&Settings", self,
|
||||
triggered=self.showNormal)
|
||||
|
||||
# UI functions
|
||||
def createTrayIcon(self):
|
||||
self.trayIconMenu = QMenu(self)
|
||||
self.trayIconMenu.addAction(self.checkNow)
|
||||
self.trayIconMenu.addAction(self.restoreAction)
|
||||
self.trayIconMenu.addAction(self.quitAction)
|
||||
#self.trayIconMenu.addAction(self.restoreAction)
|
||||
self.trayIcon = QSystemTrayIcon(self)
|
||||
self.trayIcon.setContextMenu(self.trayIconMenu)
|
||||
|
||||
def SettingsRestore(self):
|
||||
if SettingsExist():
|
||||
self.ui.checkFreq.setValue(int(settings.value("CheckInterval")))
|
||||
self.ui.boolifNotify.setChecked(bool(settings.value("Notify")))
|
||||
self.ui.txtboxMailServer.setText(settings.value("MailServer"))
|
||||
self.ui.txtboxPort.setText(settings.value("Port"))
|
||||
self.ui.txtboxLogin.setText(settings.value("Login"))
|
||||
self.ui.txtboxPassword.setText(settings.value("Password"))
|
||||
self.ui.boolifSSL.setChecked(bool(settings.value("SSL")))
|
||||
def SettingsSave(self):
|
||||
settings.setValue("CheckInterval",self.ui.checkFreq.value())
|
||||
settings.setValue("Notify", self.ui.boolifNotify.isChecked())
|
||||
settings.setValue("MailServer",self.ui.txtboxMailServer.text())
|
||||
settings.setValue("Port",self.ui.txtboxPort.text())
|
||||
settings.setValue("Login",self.ui.txtboxLogin.text())
|
||||
settings.setValue("Password",self.ui.txtboxPassword.text())
|
||||
settings.setValue("SSL",self.ui.boolifSSL.isChecked())
|
||||
|
||||
def mailboxEmpty(self):
|
||||
self.trayIcon.setToolTip ("You have no unread mail")
|
||||
self.trayIcon.setIcon(QIcon(":icons/mailbox_empty.png"))
|
||||
|
||||
def mailboxFull(self):
|
||||
self.trayIcon.setToolTip ("You have "+ str(Mail().checkMail())+" unread letters")
|
||||
self.trayIcon.setIcon(QIcon(":icons/mailbox_full.png"))
|
||||
notify ("You have "+ str(Mail().checkMail())+" unread letters")
|
||||
|
||||
def checkNow (self):
|
||||
if Mail().checkMail() == 0:
|
||||
self.mailboxEmpty()
|
||||
notify("You have no unread mail")
|
||||
else:
|
||||
self.mailboxFull()
|
||||
|
||||
def btnOK_clicked(self):
|
||||
self.SettingsSave()
|
||||
|
||||
if (settings.value("MailServer") == "" or settings.value("Port") == "" or settings.value("Login") == "" or settings.value("Password") == ""):
|
||||
QMessageBox.critical(self, "Warning","You should fill all fields in IMAP settings!")
|
||||
self.show()
|
||||
mail_check()
|
||||
self.ui.lblTestOutput.setText("")
|
||||
|
||||
def btnCancel_clicked(self):
|
||||
self.SettingsRestore()
|
||||
self.ui.lblTestOutput.setText("")
|
||||
|
||||
def btnTestConnection_clicked(self):
|
||||
try:
|
||||
if self.ui.boolifSSL.isChecked:
|
||||
self.imap = imaplib.IMAP4_SSL(self.ui.txtboxMailServer.text(), self.ui.txtboxPort.text())
|
||||
else:
|
||||
self.imap = imaplib.IMAP4(self.ui.txtboxMailServer.text(), self.ui.txtboxPort.text())
|
||||
self.imap.login(self.ui.txtboxLogin.text(), self.ui.txtboxPassword.text())
|
||||
output = "Connection was established successfully"
|
||||
except:
|
||||
output = "Unable to establish connection to mailbox"
|
||||
finally:
|
||||
self.ui.lblTestOutput.setText(output)
|
||||
|
||||
def closeEvent(self, event):
|
||||
print ("Closing the app")
|
||||
|
||||
|
||||
# Common functions
|
||||
|
||||
class Mail():
|
||||
def __init__(self):
|
||||
self.user= 'YOUR_MAILBOX_LOGIN'
|
||||
self.password= 'YOUR_MAILBOX_PASSWORD'
|
||||
self.M = imaplib.IMAP4_SSL('MAIL_SERVER', 'PORT(i.e 993)')
|
||||
self.M.login(self.user, self.password)
|
||||
socket.setdefaulttimeout(5)
|
||||
self.user = settings.value("Login")
|
||||
self.password = settings.value("Password")
|
||||
self.mailserver = settings.value("MailServer")
|
||||
self.port = settings.value("Port")
|
||||
|
||||
def login(self):
|
||||
try:
|
||||
if settings.value("SSL"):
|
||||
self.imap = imaplib.IMAP4_SSL(self.mailserver, self.port)
|
||||
|
||||
else:
|
||||
self.imap = imaplib.IMAP4(self.mailserver, self.port)
|
||||
self.imap.login(self.user, self.password)
|
||||
return True
|
||||
except:
|
||||
print("Login error")
|
||||
return False
|
||||
|
||||
def checkMail(self):
|
||||
self.M.select()
|
||||
self.unRead = self.M.search(None, 'UnSeen')
|
||||
return len(self.unRead[1][0].split())
|
||||
try:
|
||||
self.imap.select()
|
||||
self.unRead = self.imap.search(None, 'UNSEEN')
|
||||
return len(self.unRead[1][0].split())
|
||||
except:
|
||||
print("Unable to check mail")
|
||||
return "ERROR"
|
||||
|
||||
def mail_check():
|
||||
if Mail().checkMail() == 0:
|
||||
window.mailboxEmpty()
|
||||
if SettingsExist():
|
||||
m = Mail()
|
||||
if m.login():
|
||||
mail_count = m.checkMail()
|
||||
if mail_count == 0:
|
||||
window.trayIcon.setToolTip ("You have no unread mail")
|
||||
window.trayIcon.setIcon(QIcon(":icons/mailbox_empty.png"))
|
||||
else:
|
||||
window.trayIcon.setToolTip ("You have "+ str(mail_count)+" unread letters")
|
||||
window.trayIcon.setIcon(QIcon(":icons/mailbox_full.png"))
|
||||
notify ("You have "+ str(mail_count) +" unread letters")
|
||||
else:
|
||||
window.trayIcon.setToolTip("Unable to establish connection to mailbox. Check your mail settings and make sure that you have not network problems.")
|
||||
notify("Unable to establish connection to mailbox. Check your mail settings and make sure that you have not network problems.")
|
||||
else:
|
||||
window.mailboxFull()
|
||||
window.trayIcon.setToolTip("Cannot find configuration file. You should give access to your mailbox")
|
||||
def notify(message):
|
||||
subprocess.Popen(['notify-send', programTitle, message])
|
||||
if settings.value("Notify"):
|
||||
subprocess.Popen(['notify-send', programTitle, message])
|
||||
return
|
||||
|
||||
def hash(password):
|
||||
salt = uuid.uuid4().hex
|
||||
return hashlib.sha512(password.encode('utf-8') + salt.encode('utf-8')).hexdigest()
|
||||
|
||||
class Thread(QThread):
|
||||
def __init__(self):
|
||||
@@ -93,7 +183,11 @@ class Thread(QThread):
|
||||
def run(self):
|
||||
timer = QTimer()
|
||||
timer.timeout.connect(mail_check)
|
||||
timer.start(1000*60*5)
|
||||
if SettingsExist():
|
||||
CheckInterval = 1000*60*int(settings.value("CheckInterval"))
|
||||
else:
|
||||
CheckInterval = 1000*60*5
|
||||
timer.start(CheckInterval)
|
||||
timers.append(timer)
|
||||
|
||||
self.exec_()
|
||||
@@ -107,7 +201,10 @@ if __name__ == '__main__':
|
||||
sys.exit(1)
|
||||
QApplication.setQuitOnLastWindowClosed(False)
|
||||
window = Window()
|
||||
window.hide()
|
||||
if (SettingsExist() == True):
|
||||
window.hide()
|
||||
else:
|
||||
window.show()
|
||||
# UI started. Starting required functions after UI start
|
||||
mail_check()
|
||||
thread_instance = Thread()
|
||||
|
||||
Reference in New Issue
Block a user