mail-notifier/mail-notifier.py

340 lines
14 KiB
Python
Raw Normal View History

2015-10-29 12:00:41 +03:00
#!/usr/bin/env python3
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QAction, QApplication, QCheckBox, QComboBox,
QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,
QMessageBox, QMenu, QPushButton, QSpinBox, QStyle, QSystemTrayIcon,
QTextEdit, QVBoxLayout, QInputDialog)
2015-10-31 20:38:11 +03:00
from PyQt5.QtCore import (QThread, QTimer, QFile, QSettings)
2015-10-29 12:00:41 +03:00
import imaplib
2015-10-31 20:38:11 +03:00
imaplib._MAXLINE = 400000
2015-10-29 12:00:41 +03:00
import subprocess
import resources_rc
2015-10-31 20:38:11 +03:00
from ui_settings import Ui_Settings
2016-03-23 13:18:15 +03:00
from ui_about import Ui_about
2015-10-31 20:38:11 +03:00
from PyQt5 import QtCore, QtGui, QtWidgets
import os
import socket
import time
2015-10-29 12:00:41 +03:00
#variables
programTitle = "Mail Notifier"
2016-03-23 13:18:15 +03:00
programVersion = "2.0"
settings = QSettings(os.path.expanduser("~")+"/.config/mail-notifier/settings.conf", QSettings.NativeFormat)
def GlobalSettingsExist():
2015-10-31 20:38:11 +03:00
if ((settings.contains("CheckInterval") and settings.value("CheckInterval") != "") and
(settings.contains("Notify") and settings.value("Notify") != "")):
return True
else:
return False
def AccountExist():
groups = settings.childGroups()
if (len(groups)) != 0:
settings.beginGroup(groups[0])
if ((settings.contains("MailServer") and settings.value("MailServer") != "") and
2015-10-31 20:38:11 +03:00
(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") != "")):
n = True
else:
n = False
settings.endGroup()
else:
n = False
if (n):
2015-10-31 20:38:11 +03:00
return True
else:
return False
2015-10-29 12:00:41 +03:00
class Window(QDialog):
def __init__(self):
super(Window, self).__init__()
# UI
self.createActions()
self.setTitle=programTitle
self.createTrayIcon()
self.trayIcon.setIcon(QIcon(":icons/mailbox_empty.png"))
self.trayIcon.setToolTip("You have no unread letters")
self.trayIcon.show()
2015-10-31 20:38:11 +03:00
# 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)
self.ui.comboAccounts.currentTextChanged.connect(self.comboAccounts_changed)
self.ui.btnAddAccount.clicked.connect(self.btnAddAccount_clicked)
2016-02-08 18:30:13 +03:00
self.ui.btnRenameAccount.clicked.connect(self.btnRenameAccount_clicked)
self.ui.btnSaveAccount.clicked.connect(self.btnSaveAccount_clicked)
self.ui.btnRemoveAccount.clicked.connect(self.btnRemoveAccount_clicked)
# Main timer
self.timer = QTimer(self)
self.timer.timeout.connect(mail_check)
2015-10-29 12:00:41 +03:00
# Menu actions
def createActions(self):
2016-03-23 13:18:15 +03:00
self.aboutShow = QAction(QIcon(':icons/mailbox_empty.png'),"&About", self, triggered=self.aboutShow)
self.checkNow = QAction(QIcon(':icons/check_now.png'),"&Check now", self, triggered=mail_check)
self.restoreAction = QAction(QIcon(":icons/settings.png"),"&Settings", self, triggered=self.showNormal)
self.quitAction = QAction(QIcon(':icons/menu_quit.png'),"&Quit", self, triggered=QApplication.instance().quit)
2015-10-29 12:00:41 +03:00
# UI functions
def createTrayIcon(self):
self.trayIconMenu = QMenu(self)
2016-03-23 13:18:15 +03:00
self.trayIconMenu.addAction(self.aboutShow)
2015-10-29 12:00:41 +03:00
self.trayIconMenu.addAction(self.checkNow)
2015-10-31 20:38:11 +03:00
self.trayIconMenu.addAction(self.restoreAction)
2015-10-29 12:00:41 +03:00
self.trayIconMenu.addAction(self.quitAction)
self.trayIcon = QSystemTrayIcon(self)
self.trayIcon.setContextMenu(self.trayIconMenu)
2015-10-31 20:38:11 +03:00
def SettingsRestore(self):
if (GlobalSettingsExist() and AccountExist()):
groups = settings.childGroups()
for i in range (len(groups)):
self.ui.comboAccounts.addItem(groups[i])
self.ui.comboAccounts.setCurrentText(groups[i])
settings.beginGroup(groups[i])
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")))
settings.endGroup()
if (self.ui.comboAccounts.count() == 0):
self.ui.comboAccounts.addItem("Default")
self.ui.comboAccounts.setCurrentText("Default")
2015-10-31 20:38:11 +03:00
self.ui.checkFreq.setValue(int(settings.value("CheckInterval")))
self.ui.boolifNotify.setChecked(bool(settings.value("Notify")))
2016-02-08 18:30:13 +03:00
def SettingsSave(self,account):
2015-10-31 20:38:11 +03:00
settings.setValue("CheckInterval",self.ui.checkFreq.value())
settings.setValue("Notify", self.ui.boolifNotify.isChecked())
2016-02-08 18:30:13 +03:00
settings.beginGroup(account)
2015-10-31 20:38:11 +03:00
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())
settings.endGroup()
def SettingsRemove(self,group):
settings.beginGroup(group)
settings.remove("")
settings.endGroup()
2015-10-29 12:00:41 +03:00
2015-10-31 20:38:11 +03:00
def btnOK_clicked(self):
2016-02-08 18:30:13 +03:00
self.SettingsSave(self.ui.comboAccounts.currentText())
2015-10-31 20:38:11 +03:00
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("")
self.stop()
self.start()
2015-10-31 20:38:11 +03:00
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 btnAddAccount_clicked(self):
GroupName = QInputDialog.getText(self,"Enter account name","Enter account name",QLineEdit.Normal,"")
if (GroupName[0]):
self.ui.comboAccounts.addItem(GroupName[0])
self.ui.comboAccounts.setCurrentText(GroupName[0])
2016-02-08 18:30:13 +03:00
def btnRenameAccount_clicked(self):
Index = self.ui.comboAccounts.currentIndex()
OldGroupName = self.ui.comboAccounts.currentText()
GroupName = QInputDialog.getText(self,"Enter account name","Enter account name",QLineEdit.Normal,self.ui.comboAccounts.currentText())
if (GroupName[0]):
2016-02-08 18:30:13 +03:00
self.SettingsSave(GroupName[0])
self.ui.comboAccounts.setItemText(Index, GroupName[0])
self.ui.comboAccounts.setCurrentText(GroupName[0])
self.SettingsRemove(OldGroupName)
def btnSaveAccount_clicked(self):
self.SettingsSave(self.ui.comboAccounts.currentText())
2016-02-10 11:45:23 +03:00
self.ui.lblTestOutput.setText("Account saved")
def btnRemoveAccount_clicked(self):
2016-02-10 07:50:37 +03:00
reply = QMessageBox.warning(self, 'Warning!', "Delete this account permanently?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if (reply == QMessageBox.Yes):
Index = self.ui.comboAccounts.currentIndex()
GroupName = self.ui.comboAccounts.currentText()
self.ui.comboAccounts.removeItem(Index)
self.SettingsRemove(GroupName)
def comboAccounts_changed(self):
self.ui.lblTestOutput.setText("")
settings.beginGroup(self.ui.comboAccounts.currentText())
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")))
settings.endGroup()
2016-03-23 13:18:15 +03:00
def aboutShow(self):
if (about.isMinimized):
about.hide()
about.show()
about.activateWindow()
def start(self):
if (GlobalSettingsExist() and AccountExist()):
CheckInterval = 1000*60*int(settings.value("CheckInterval"))
else:
CheckInterval = 1000*60*5
self.timer.setInterval (CheckInterval)
self.timer.start()
def stop (self):
self.timer.stop()
2016-03-23 13:18:15 +03:00
class About(QDialog):
def __init__(self):
super(About, self).__init__()
self.ui = Ui_about()
self.ui.setupUi(self)
self.setWindowFlags(QtCore.Qt.Tool)
self.setFixedSize(483,334)
self.ui.lblNameVersion.setText(programTitle + " " + programVersion)
2016-03-23 13:51:54 +03:00
f = QtCore.QFile(":/LICENSE.txt")
if f.open(QtCore.QIODevice.ReadOnly | QtCore.QFile.Text):
text = QtCore.QTextStream(f).readAll()
f.close()
self.ui.txtLicense.setPlainText(text)
2016-03-23 13:18:15 +03:00
def closeEvent(self, event):
event.ignore()
self.hide()
2015-10-31 20:38:11 +03:00
2015-10-29 12:00:41 +03:00
# Common functions
2015-10-31 20:38:11 +03:00
2015-10-29 12:00:41 +03:00
class Mail():
def __init__(self):
2015-10-31 20:38:11 +03:00
socket.setdefaulttimeout(5)
def login(self,mailserver,port,user,password,ssl):
2015-10-31 20:38:11 +03:00
try:
if ssl:
self.imap = imaplib.IMAP4_SSL(mailserver, port)
2015-10-31 20:38:11 +03:00
else:
self.imap = imaplib.IMAP4(mailserver, port)
self.imap.login(user, password)
2015-10-31 20:38:11 +03:00
return True
except:
print("Login error")
return False
2015-10-29 12:00:41 +03:00
def checkMail(self):
2015-10-31 20:38:11 +03:00
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"
2015-10-29 12:00:41 +03:00
def mail_check():
2016-02-08 18:30:13 +03:00
mail_count = 0
if (GlobalSettingsExist() and AccountExist()):
2015-10-31 20:38:11 +03:00
m = Mail()
groups = settings.childGroups()
for i in range (len(groups)):
settings.beginGroup(groups[i])
group = groups[i]
user = settings.value("Login")
password = settings.value("Password")
mailserver = settings.value("MailServer")
port = settings.value("Port")
ssl = settings.value("SSL")
settings.endGroup()
if m.login(mailserver,port,user,password,ssl):
2016-02-08 18:30:13 +03:00
if (mail_count == "ERROR" or m.checkMail() == "ERROR"):
mail_count = "ERROR"
else:
mail_count += m.checkMail()
2015-10-31 20:38:11 +03:00
else:
2016-02-10 07:50:37 +03:00
mail_count = "CONNECTION_ERROR"
2015-10-29 12:00:41 +03:00
else:
2016-02-10 07:50:37 +03:00
mail_count = "CONFIGURATION_ERROR"
2016-02-10 07:50:37 +03:00
# Parsing mail_count values
if mail_count == 0:
window.trayIcon.setToolTip ("You have no unread mail")
window.trayIcon.setIcon(QIcon(":icons/mailbox_empty.png"))
elif mail_count == "ERROR":
window.trayIcon.setIcon(QIcon(":icons/mailbox_error.png"))
window.trayIcon.setToolTip ("Error checking mail.")
2016-02-10 07:50:37 +03:00
elif mail_count == "CONNECTION_ERROR":
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.")
window.trayIcon.setIcon(QIcon(":icons/mailbox_error.png"))
elif mail_count == "CONFIGURATION_ERROR":
window.trayIcon.setIcon(QIcon(":icons/mailbox_error.png"))
window.trayIcon.setToolTip("Cannot find configuration file. You should give access to your mailbox")
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")
2015-10-29 12:00:41 +03:00
def notify(message):
2015-10-31 20:38:11 +03:00
if settings.value("Notify"):
subprocess.Popen(['notify-send', programTitle, message])
2015-10-29 12:00:41 +03:00
return
2015-10-31 20:38:11 +03:00
2016-03-23 13:18:15 +03:00
2015-10-29 12:00:41 +03:00
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
systemtray_timeout = 0
# Check if DE supports system tray
while not QSystemTrayIcon.isSystemTrayAvailable():
systemtray_timeout += 1
time.sleep (20)
if systemtray_timeout == 5:
QMessageBox.critical(None, "Mail notifier",
"I couldn't detect any system tray on this system.")
sys.exit(1)
2015-10-29 12:00:41 +03:00
QApplication.setQuitOnLastWindowClosed(False)
window = Window()
2016-03-23 13:18:15 +03:00
about = About()
if (GlobalSettingsExist() and AccountExist()):
2015-10-31 20:38:11 +03:00
window.hide()
else:
window.show()
2015-10-29 12:00:41 +03:00
# UI started. Starting required functions after UI start
mail_check()
window.start()
2015-10-29 12:00:41 +03:00
sys.exit(app.exec_())