added support for events, fixed path concatenation in Reminder class

This commit is contained in:
Patrick Ulbrich
2011-07-04 19:37:57 +02:00
parent 299271b52e
commit 2539bd7137
3 changed files with 195 additions and 49 deletions

View File

@@ -59,7 +59,9 @@ class ConfigWindow:
"treeview_accounts_row_activated" : self.__on_treeview_accounts_row_activated, \
"liststore_accounts_row_deleted" : self.__on_liststore_accounts_row_deleted, \
"liststore_accounts_row_inserted" : self.__on_liststore_accounts_row_inserted, \
"chk_enable_filter_toggled" : self.__on_chk_enable_filter_toggled \
"chk_enable_filter_toggled" : self.__on_chk_enable_filter_toggled, \
"chk_script0_toggled" : self.__on_chk_script0_toggled, \
"chk_script1_toggled" : self.__on_chk_script1_toggled \
})
self.window = builder.get_object("config_window")
@@ -103,15 +105,22 @@ class ConfigWindow:
#
# spam filter tab
#
self.chk_enable_filter = builder.get_object("chk_enable_filter")
self.textview_filter = builder.get_object("textview_filter")
self.textbuffer_filter = builder.get_object("textbuffer_filter")
#
# events tab
# TODO
#
self.chk_script0 = builder.get_object("chk_script0")
self.filechooser_script0 = builder.get_object("filechooser_script0")
self.chk_script1 = builder.get_object("chk_script1")
self.filechooser_script1 = builder.get_object("filechooser_script1")
#
# about tab
#
self.image_logo = builder.get_object("image_logo")
pb = GdkPixbuf.Pixbuf.new_from_file_at_size(get_data_file("mailnag.svg"), 200, 200)
self.image_logo.set_from_pixbuf(pb)
@@ -131,6 +140,18 @@ class ConfigWindow:
self.chk_enable_filter.set_active(int(cfg.get('filter', 'filter_on')))
self.textbuffer_filter.set_text(cfg.get('filter', 'filter_text'))
self.chk_script0.set_active(int(cfg.get('script', 'script0_on')))
tmp = cfg.get('script', 'script0_file')
if len(tmp) > 0:
self.filechooser_script0.set_filename(tmp)
self.chk_script1.set_active(int(cfg.get('script', 'script1_on')))
tmp = cfg.get('script', 'script1_file')
if len(tmp) > 0:
self.filechooser_script1.set_filename(tmp)
self.accounts.load()
for acc in self.accounts.account: # load accounts into treeview
@@ -151,6 +172,16 @@ class ConfigWindow:
start, end = self.textbuffer_filter.get_bounds()
cfg.set('filter', 'filter_text', self.textbuffer_filter.get_text(start, end, True))
cfg.set('script', 'script0_on', int(self.chk_script0.get_active()))
tmp = self.filechooser_script0.get_filename()
if tmp == None: tmp = ""
cfg.set('script', 'script0_file', tmp)
cfg.set('script', 'script1_on', int(self.chk_script1.get_active()))
tmp = self.filechooser_script1.get_filename()
if tmp == None: tmp = ""
cfg.set('script', 'script1_file', tmp)
on, name, server, user, password, imap, folder, port = self.accounts.get_cfg()
cfg.set('account', 'on', on)
cfg.set('account', 'name', name)
@@ -295,6 +326,14 @@ class ConfigWindow:
self.textview_filter.set_sensitive(self.chk_enable_filter.get_active())
def __on_chk_script0_toggled(self, widget):
self.filechooser_script0.set_sensitive(self.chk_script0.get_active())
def __on_chk_script1_toggled(self, widget):
self.filechooser_script1.set_sensitive(self.chk_script1.get_active())
def __save_and_quit(self):
self.save_config()
keyring.remove(self.accounts.account) # delete obsolete entries from Keyring

View File

@@ -465,38 +465,21 @@ def delete_pid(): # delete file mailnag.pid
os.popen("rm " + pid_file) # delete it
def user_scripts(event, data): # process user scripts
if event == "on_email_arrival":
if cfg.get('script', 'script0_on') == '1' and data > 0: # on new emails
pathfile = cfg.get('script', 'script0_file') # get script pathfile
if pathfile != '' and os.path.exists(pathfile): # not empty and existing
pid.append(subprocess.Popen(pathfile)) # execute
def user_scripts(event, data):
if event == "on_mail_check":
if cfg.get('script', 'script0_on') == '1':
script_file = cfg.get('script', 'script0_file')
if script_file != '' and os.path.exists(script_file):
pid.append(subprocess.Popen("%s %s" % (script_file, data), shell = True))
else:
print 'Warning: cannot execute script:', pathfile
if cfg.get('script', 'script1_on') == '1' and data == 0: # on no new emails
pathfile = cfg.get('script', 'script1_file')
if pathfile != '' and os.path.exists(pathfile):
pid.append(subprocess.Popen(pathfile))
print 'Warning: cannot execute script:', script_file
if (data != '0') and (cfg.get('script', 'script1_on') == '1'):
script_file = cfg.get('script', 'script1_file')
if script_file != '' and os.path.exists(script_file):
pid.append(subprocess.Popen("%s %s" % (script_file, data), shell = True))
else:
print 'Warning: cannot execute script:', pathfile
elif cfg.get('script', 'script2_on') == '1' and event == "on_email_clicked":
pathfile = cfg.get('script', 'script2_file')
if pathfile != '' and os.path.exists(pathfile):
pid.append(subprocess.Popen([pathfile, data[0], data[1], data[2]])) # call script with 'sender, datetime, subject'
else:
print 'Warning: cannot execute script:', pathfile
elif cfg.get('script', 'script3_on') == '1' and event == "on_account_clicked":
pathfile = cfg.get('script', 'script3_file')
if pathfile != '' and os.path.exists(pathfile):
pid.append(subprocess.Popen([pathfile, data[0], data[1]])) # call script with account_name
else:
print 'Warning: cannot execute script:', pathfile
else:
return False
print 'Warning: cannot execute script:', script_file
def commandExecutor(command, context_menu_command=None):
@@ -552,10 +535,14 @@ class MailChecker:
new_mails = 0
summary = ""
body = ""
script_data = ""
for mail in self.mail_list:
if not self.reminder.contains(mail.id):
new_mails += 1
script_data += ' "<%s> %s"' % (mail.sender, mail.subject)
script_data = str(new_mails) + script_data
if all_mails == 0:
# no mails (e.g. email client has been launched) -> close notification
@@ -581,7 +568,7 @@ class MailChecker:
self.notification.update(summary, body, "mail-unread")
self.notification.show()
user_scripts("on_email_arrival", new_mails) # process user scripts
user_scripts("on_mail_check", script_data) # process user scripts
self.reminder.save(self.mail_list)
sys.stdout.flush() # write stdout to log file
@@ -615,7 +602,7 @@ class Reminder(dict):
def load(self): # load last known messages from mailnag.dat
remember = cfg.get('general', 'remember')
dat_file = user_path + 'mailnag.dat'
dat_file = os.path.join(user_path, 'mailnag.dat')
we_have_a_file = os.path.exists(dat_file) # check if file exists
if remember == '1' and we_have_a_file:
f = open(dat_file, 'r') # open file again
@@ -630,7 +617,7 @@ class Reminder(dict):
def save(self, mail_list): # save mail ids to file
dat_file = user_path + 'mailnag.dat'
dat_file = os.path.join(user_path, 'mailnag.dat')
f = open(dat_file, 'w') # open the file for overwrite
for m in mail_list:
try:

View File

@@ -378,27 +378,147 @@
</packing>
</child>
<child>
<object class="GtkGrid" id="grid2">
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="n_columns">2</property>
<property name="border_width">6</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<placeholder/>
<object class="GtkCheckButton" id="chk_script0">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="chk_script0_toggled" swapped="no"/>
<child>
<object class="GtkLabel" id="label12">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;On mail check&lt;/b&gt;</property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
<object class="GtkBox" id="box3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">12</property>
<property name="margin_bottom">18</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkLabel" id="label13">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">The following script will be executed on every single mail check.
Mailnag passes the total count of new mails to this script,
followed by &lt;i&gt;"&amp;lt;sender&amp;gt; &amp;lt;subject&amp;gt;"&lt;/i&gt; pairs.</property>
<property name="use_markup">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFileChooserButton" id="filechooser_script0">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
<object class="GtkCheckButton" id="chk_script1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="chk_script1_toggled" swapped="no"/>
<child>
<object class="GtkLabel" id="label11">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">&lt;b&gt;On mail arrival&lt;/b&gt;</property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
<object class="GtkBox" id="box4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">12</property>
<property name="margin_bottom">18</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkLabel" id="label14">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">The following script will be executed whenever new mails arrive.
Mailnag passes the total count of new mails to this script,
followed by &lt;i&gt;"&amp;lt;sender&amp;gt; &amp;lt;subject&amp;gt;"&lt;/i&gt; pairs.</property>
<property name="use_markup">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFileChooserButton" id="filechooser_script1">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>