[refactor] Move all dbus action in a specific class
This commit is contained in:
parent
815b27ae23
commit
bcfe2af140
1 changed files with 78 additions and 63 deletions
141
mms2mail
141
mms2mail
|
@ -81,51 +81,16 @@ class MMS2Mail:
|
||||||
mbox_file = self.config.get('mail', 'mailbox',
|
mbox_file = self.config.get('mail', 'mailbox',
|
||||||
fallback=f"/var/mail/{self.user}")
|
fallback=f"/var/mail/{self.user}")
|
||||||
self.mailbox = mailbox.mbox(mbox_file)
|
self.mailbox = mailbox.mbox(mbox_file)
|
||||||
if self.disable_dbus:
|
self.dbus = None
|
||||||
return
|
|
||||||
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
||||||
self.bus = dbus.SessionBus()
|
|
||||||
|
|
||||||
def get_bus(self):
|
def set_dbus(self, dbusmmsd):
|
||||||
"""
|
"""
|
||||||
Return the DBus SessionBus.
|
Return the DBus SessionBus.
|
||||||
|
|
||||||
:rtype dbus.SessionBus()
|
:param dbusmmsd: The DBus MMSd abstraction class
|
||||||
:return: an active SessionBus
|
:type dbusmmsd: DbusMMSd()
|
||||||
"""
|
"""
|
||||||
if self.disable_dbus:
|
self.dbus = dbusmmsd
|
||||||
return None
|
|
||||||
return self.bus
|
|
||||||
|
|
||||||
def mark_mms_read(self, dbus_path):
|
|
||||||
"""
|
|
||||||
Ask mmsd to mark the mms as read.
|
|
||||||
|
|
||||||
:param dbus_path: the mms dbus path
|
|
||||||
:type dbus_path: str
|
|
||||||
"""
|
|
||||||
if self.disable_dbus:
|
|
||||||
return None
|
|
||||||
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
|
|
||||||
dbus_path),
|
|
||||||
'org.ofono.mms.Message')
|
|
||||||
log.debug(f"Marking MMS as read {dbus_path}")
|
|
||||||
message.MarkRead()
|
|
||||||
|
|
||||||
def delete_mms(self, dbus_path):
|
|
||||||
"""
|
|
||||||
Ask mmsd to delete the mms.
|
|
||||||
|
|
||||||
:param dbus_path: the mms dbus path
|
|
||||||
:type dbus_path: str
|
|
||||||
"""
|
|
||||||
if self.disable_dbus:
|
|
||||||
return None
|
|
||||||
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
|
|
||||||
dbus_path),
|
|
||||||
'org.ofono.mms.Message')
|
|
||||||
log.debug(f"Deleting MMS {dbus_path}")
|
|
||||||
message.Delete()
|
|
||||||
|
|
||||||
def check_mms(self, path):
|
def check_mms(self, path):
|
||||||
"""
|
"""
|
||||||
|
@ -155,6 +120,14 @@ class MMS2Mail:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def message_added(self, name, value, member, path, interface):
|
||||||
|
"""Trigger conversion on MessageAdded signal."""
|
||||||
|
if value['Status'] == 'downloaded' or value['Status'] == 'received':
|
||||||
|
log.debug(f"New incoming MMS found ({name.split('/')[-1]})")
|
||||||
|
self.convert(value['Attachments'][0][2], name)
|
||||||
|
else:
|
||||||
|
log.debug(f"New outgoing MMS found ({name.split('/')[-1]})")
|
||||||
|
|
||||||
def convert(self, path, dbus_path=None):
|
def convert(self, path, dbus_path=None):
|
||||||
"""
|
"""
|
||||||
Convert a provided mms file to a mail stored in a mbox.
|
Convert a provided mms file to a mail stored in a mbox.
|
||||||
|
@ -257,15 +230,17 @@ class MMS2Mail:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
# Ask mmsd to mark message as read and delete it
|
# Ask mmsd to mark message as read and delete it
|
||||||
self.mark_mms_read(dbus_path)
|
if self.disable_dbus:
|
||||||
|
return
|
||||||
|
self.dbus.mark_mms_read(dbus_path)
|
||||||
if self.delete:
|
if self.delete:
|
||||||
self.delete_mms(dbus_path)
|
self.dbus.delete_mms(dbus_path)
|
||||||
|
|
||||||
|
|
||||||
class DbusWatcher():
|
class DbusMMSd():
|
||||||
"""Use DBus Signal notification to watch for new MMS."""
|
"""Use DBus communication with mmsd."""
|
||||||
|
|
||||||
def __init__(self, mms2mail):
|
def __init__(self, mms2mail=None):
|
||||||
"""
|
"""
|
||||||
Return a DBusWatcher instance.
|
Return a DBusWatcher instance.
|
||||||
|
|
||||||
|
@ -273,16 +248,61 @@ class DbusWatcher():
|
||||||
:type mms2mail: mms2mail()
|
:type mms2mail: mms2mail()
|
||||||
"""
|
"""
|
||||||
self.mms2mail = mms2mail
|
self.mms2mail = mms2mail
|
||||||
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||||
|
self.bus = dbus.SessionBus()
|
||||||
|
|
||||||
|
def set_mms2mail(self, mms2mail):
|
||||||
|
"""
|
||||||
|
Set mms2mail instance handling dbus event.
|
||||||
|
|
||||||
|
:param mms2mail: An mms2mail instance to convert new mms
|
||||||
|
:type mms2mail: mms2mail()
|
||||||
|
"""
|
||||||
|
self.mms2mail = mms2mail
|
||||||
|
|
||||||
|
def mark_mms_read(self, dbus_path):
|
||||||
|
"""
|
||||||
|
Ask mmsd to mark the mms as read.
|
||||||
|
|
||||||
|
:param dbus_path: the mms dbus path
|
||||||
|
:type dbus_path: str
|
||||||
|
"""
|
||||||
|
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
|
||||||
|
dbus_path),
|
||||||
|
'org.ofono.mms.Message')
|
||||||
|
log.debug(f"Marking MMS as read {dbus_path}")
|
||||||
|
message.MarkRead()
|
||||||
|
|
||||||
|
def delete_mms(self, dbus_path):
|
||||||
|
"""
|
||||||
|
Ask mmsd to delete the mms.
|
||||||
|
|
||||||
|
:param dbus_path: the mms dbus path
|
||||||
|
:type dbus_path: str
|
||||||
|
"""
|
||||||
|
if self.disable_dbus:
|
||||||
|
return None
|
||||||
|
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
|
||||||
|
dbus_path),
|
||||||
|
'org.ofono.mms.Message')
|
||||||
|
log.debug(f"Deleting MMS {dbus_path}")
|
||||||
|
message.Delete()
|
||||||
|
|
||||||
|
def add_signal_receiver(self):
|
||||||
|
"""Add a signal receiver to the current bus."""
|
||||||
|
if self.mms2mail:
|
||||||
|
self.bus.add_signal_receiver(self.mms2mail.message_added,
|
||||||
|
bus_name="org.ofono.mms",
|
||||||
|
signal_name="MessageAdded",
|
||||||
|
member_keyword="member",
|
||||||
|
path_keyword="path",
|
||||||
|
interface_keyword="interface")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Run the watcher mainloop."""
|
"""Run the dbus mainloop."""
|
||||||
bus = self.mms2mail.get_bus()
|
|
||||||
bus.add_signal_receiver(self.message_added,
|
|
||||||
bus_name="org.ofono.mms",
|
|
||||||
signal_name="MessageAdded",
|
|
||||||
member_keyword="member",
|
|
||||||
path_keyword="path",
|
|
||||||
interface_keyword="interface")
|
|
||||||
mainloop = GLib.MainLoop()
|
mainloop = GLib.MainLoop()
|
||||||
log.info("Starting DBus watcher mainloop")
|
log.info("Starting DBus watcher mainloop")
|
||||||
try:
|
try:
|
||||||
|
@ -291,14 +311,6 @@ class DbusWatcher():
|
||||||
log.info("Stopping DBus watcher mainloop")
|
log.info("Stopping DBus watcher mainloop")
|
||||||
mainloop.quit()
|
mainloop.quit()
|
||||||
|
|
||||||
def message_added(self, name, value, member, path, interface):
|
|
||||||
"""Trigger conversion on MessageAdded signal."""
|
|
||||||
if value['Status'] == 'downloaded' or value['Status'] == 'received':
|
|
||||||
log.debug(f"New incoming MMS found ({name.split('/')[-1]})")
|
|
||||||
self.mms2mail.convert(value['Attachments'][0][2], name)
|
|
||||||
else:
|
|
||||||
log.debug(f"New outgoing MMS found ({name.split('/')[-1]})")
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Run the different functions handling mms and mail."""
|
"""Run the different functions handling mms and mail."""
|
||||||
|
@ -324,18 +336,21 @@ def main():
|
||||||
after a few minutes /!\\")
|
after a few minutes /!\\")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
d = DbusMMSd()
|
||||||
m = MMS2Mail(args.delete, args.force_read,
|
m = MMS2Mail(args.delete, args.force_read,
|
||||||
args.disable_dbus, args.force_unlock)
|
args.disable_dbus, args.force_unlock)
|
||||||
|
m.set_dbus(d)
|
||||||
|
|
||||||
if args.files:
|
if args.files:
|
||||||
for mms_file in args.files:
|
for mms_file in args.files:
|
||||||
m.convert(mms_file)
|
m.convert(mms_file)
|
||||||
elif args.watcher:
|
elif args.watcher:
|
||||||
log.info("Starting mms2mail in daemon mode")
|
log.info("Starting mms2mail in daemon mode")
|
||||||
w = DbusWatcher(m)
|
d.set_mms2mail(m)
|
||||||
w.run()
|
d.add_signal_receiver()
|
||||||
else:
|
else:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
d.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue