update copyright year
[claws.git] / src / plugins / python / examples / auto / startup
1 # -*- coding: utf-8 -*-
2
3 import gi
4 gi.require_version("Gtk", "3.0")
5 from gi.repository import Gtk
6
7 # lists to store information for cleanup in the shutdown script
8 mainwindow_merge_ids = []
9 mainwindow_actions = []
10
11 # function definitions
12
13 def add_python_documentation_menu_item():
14     # Adds a Help -> Python API documentation menu item
15     def pydoc_cb(action):
16         # callback for "Python API documentation" action
17         import os, tempfile, subprocess, pydoc
18         working_directory = os.getcwd()  # store current working directory
19         os.chdir(tempfile.gettempdir())  # switch to a temporary directory
20         pydoc.writedoc(clawsmail)        # write out API documentation to $TEMP/clawsmail.html
21         subprocess.Popen(["xdg-open", "clawsmail.html"]) # start html viewer in the background
22         os.chdir(working_directory)      # switch back to original working directory
23
24     global mainwindow_merge_ids
25     global mainwindow_actions
26
27     # create "Python API documentation" menu item
28     group = clawsmail.get_mainwindow_action_group()
29     ui_manager = clawsmail.get_mainwindow_ui_manager()
30     action = Gtk.Action(name="pydoc", label="Python API documentation")
31     action.connect("activate", pydoc_cb)
32     group.add_action(action)
33     merge_id = ui_manager.new_merge_id()
34     ui_manager.add_ui(
35         merge_id, "/Menu/Help", "pydoc", "pydoc",
36         Gtk.UIManagerItemType.MENUITEM, True)
37     mainwindow_merge_ids.append(merge_id)
38     mainwindow_actions.append(action)
39
40 def add_mark_thread_read_menu_item():
41     # Adds an Edit -> Mark thread as read menu item
42     def thread_read_cb(action):
43         # callback for "Mark thread as read" action
44         selected_messages = clawsmail.get_summaryview_selected_message_list()
45         group = clawsmail.get_mainwindow_action_group()
46         group.get_action("Edit/SelectThread").activate()
47         group.get_action("Message/Mark/MarkRead").activate()
48         clawsmail.summaryview_select_messages(selected_messages)
49
50     global mainwindow_merge_ids
51     global mainwindow_actions
52
53     # create "Mark thread read" menu item
54     group = clawsmail.get_mainwindow_action_group()
55     ui_manager = clawsmail.get_mainwindow_ui_manager()
56     action = Gtk.Action(name="ThreadRead", label="Mark thread as read")
57     action.connect("activate", thread_read_cb)
58     group.add_action_with_accel(action, None)
59     merge_id = ui_manager.new_merge_id()
60     ui_manager.add_ui(
61         merge_id, "/Menu/Edit", "ThreadRead", "ThreadRead",
62         Gtk.UIManagerItemType.MENUITEM, False)
63     mainwindow_merge_ids.append(merge_id)
64     mainwindow_actions.append(action)
65
66 def add_dbus_interface():
67     # exports an interface to Claws Mail on the session D-Bus
68     #
69     # Example invokation to trigger an update of the summary view from the command line:
70     # dbus-send --session --type=method_call --dest=org.ClawsMail.PythonPlugin \
71     #     /org/ClawsMail/PythonPlugin \
72     #     org.ClawsMail.PythonPlugin.MainWindow.TriggerGtkAction \
73     #     string:'View/UpdateSummary'
74     try:
75         import dbus
76         import dbus.service
77         from dbus.mainloop.glib import DBusGMainLoop
78     except ImportError:
79         print("Can't setup D-Bus interface: D-Bus Python bindings not available.")
80         return None
81
82     class ClawsMailService(dbus.service.Object):
83         @dbus.service.method("org.ClawsMail.PythonPlugin.MainWindow", in_signature='s', out_signature='')
84         def TriggerGtkAction(self, action_path):
85             action = clawsmail.get_mainwindow_action_group().get_action(action_path)
86             if action:
87                 action.activate()
88             else:
89                 print('No such action: {}'.format(action_path))
90
91     loop = DBusGMainLoop(set_as_default=True)
92     session_bus = dbus.SessionBus()
93     name = dbus.service.BusName("org.ClawsMail.PythonPlugin", session_bus)
94     object = ClawsMailService(session_bus, '/org/ClawsMail/PythonPlugin')
95     return name
96
97 # call the functions that have been defined above, or comment the functions that you want to omit
98 add_python_documentation_menu_item()
99 add_mark_thread_read_menu_item()
100 dbus_interface = add_dbus_interface()