From 711050055339f6a14f0c3da4d3d28f707b97a102 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Mon, 17 Aug 2020 17:40:59 +0200 Subject: Initial port from Unity Mail --- ayatanawebmail/common.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100755 ayatanawebmail/common.py (limited to 'ayatanawebmail/common.py') diff --git a/ayatanawebmail/common.py b/ayatanawebmail/common.py new file mode 100755 index 0000000..95d3797 --- /dev/null +++ b/ayatanawebmail/common.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Ayatana Webmail, message actions dialog +# Authors: Robert Tari +# License: GNU GPL 3 or higher; http://www.gnu.org/licenses/gpl.html + +import gettext +import os +import psutil +import subprocess +import webbrowser +from gi.repository import Gio +from ayatanawebmail.appdata import APPEXECUTABLE, APPNAME +from ayatanawebmail.accounts import DialogAccounts + +try: + g_oTranslation = gettext.translation(APPNAME) +except IOError: + g_oTranslation = gettext.NullTranslations() + +g_oTranslation.install() + +g_dctDefaultURLs = {'Home': 'https://mail.google.com/mail/', 'Compose': 'https://mail.google.com/mail/#compose', 'Inbox': 'https://mail.google.com/mail/#inbox', 'Sent': 'https://mail.google.com/mail/#sent'} +g_oSettings = Gio.Settings.new('org.ayatana.webmail') +g_lstAccounts = [] + +def getDataPath(strPath): + + try: + + strExecPath = os.path.split(APPEXECUTABLE)[0] + strDataPath = os.getcwd().replace(strExecPath, '') + strRelativePath = os.path.join(strDataPath, strPath.lstrip('/')) + + if os.path.exists(strRelativePath): + return strRelativePath + + except: + + pass + + return strPath + +def isRunning(): + + nCount = 0 + + for oProc in psutil.process_iter(): + + strName = oProc.name + + if not isinstance(strName, str): + + strName = oProc.name() + + if strName == 'python3' or strName == 'python': + + lstCmdline = oProc.cmdline + + if not isinstance(lstCmdline, list): + lstCmdline = oProc.cmdline() + + for strCmd in lstCmdline: + + if strCmd.endswith('ayatana-webmail'): + nCount += 1 + + elif strName.endswith('ayatana-webmail'): + + nCount += 1 + + return nCount + +def resolveURL(strURL): + + if strURL.startswith('Exec:'): + subprocess.Popen(strURL[5:], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) + elif strURL.startswith('http'): + webbrowser.open_new_tab(strURL) + +def openURLOrCommand(strURL): + + if strURL in g_dctDefaultURLs: + + strURL0 = g_lstAccounts[0][strURL] + + if len(g_lstAccounts) > 1: + + for dctAccount in g_lstAccounts[1:]: + + if dctAccount[strURL] != strURL0: + + oDialogAccounts = DialogAccounts(strURL, getDataPath, g_lstAccounts) + oDialogAccounts.run() + strURL = oDialogAccounts.strURL + oDialogAccounts.destroy() + + if strURL: + resolveURL(strURL) + + return + + resolveURL(strURL0) + + elif strURL.startswith('Exec:') or strURL.startswith('http'): + + resolveURL(strURL) + + else: + + print('Unknown URL name!') + print('Possible URL names: "Home", "Compose", "Inbox", "Sent", "Exec:...", "http..."') -- cgit v1.2.3