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