1# -*- Mode: python -*-
2
3import dbus
4import gtk
5
6def add_note(msg):
7
8    def get_reminder(desc):
9        get_reminder.reminder = None
10
11        # callback functions for the GUI
12        def no_reminder(button, ww):
13            get_reminder.reminder = ""
14            ww.destroy()
15
16        def date_time_cb(button, ww, cal, cb, hours, minutes):
17            ymd = list(cal.get_date())
18            ymd[1] += 1
19            get_reminder.reminder = "/".join([str(v) for v in ymd])
20            if cb.get_active():
21                get_reminder.reminder += "".join([" at ", "%02d" % hours.get_value_as_int(), ":", "%02d" % minutes.get_value_as_int()])
22            ww.destroy()
23
24        def day_selected(cal, exp):
25            ymd = list(cal.get_date())
26            ymd[1] += 1
27            exp.set_label("/".join(str(vv) for vv in ymd))
28
29        def custom(button, ww, entry):
30            get_reminder.reminder = entry.get_text()
31            ww.destroy()
32
33
34        # Check if the user wants a reminder in a dialog box
35        win = gtk.Window()
36        win.set_title("Reminder")
37        win.connect("destroy", gtk.main_quit)
38        win.set_position(gtk.WIN_POS_CENTER)
39        table = gtk.Table(2,7)
40        win.add(table)
41        table.attach(gtk.Label(desc), 0, 2, 0, 1)
42        # no reminder
43        button = gtk.Button("No reminder")
44        button.connect("clicked", no_reminder, win)
45        table.attach(button, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=0, ypadding=4)
46        table.attach(gtk.HSeparator(), 0, 2, 2, 3)
47        # date / time reminder
48        button = gtk.Button("Date/Time")
49        table.attach(button, 0, 1, 3, 4, xoptions=gtk.FILL, yoptions=gtk.EXPAND | gtk.FILL, ypadding=4)
50        hbox = gtk.HBox()
51        table.attach(hbox, 1, 2, 3, 4)
52        cal = gtk.Calendar()
53        exp = gtk.Expander()
54        day_selected(cal, exp)
55        cal.connect("day-selected", day_selected, exp)
56        exp.add(cal)
57        hbox.pack_start(exp)
58        cb = gtk.CheckButton("at")
59        hbox.pack_start(cb, False, False)
60        hours = gtk.SpinButton(gtk.Adjustment(12.0, 0.0, 24.0, 1.0, 5.0, 0.0))
61        hours.set_numeric(True)
62        hours.set_wrap(True)
63        hbox.pack_start(hours, False, False)
64        hbox.pack_start(gtk.Label(":"), False, False)
65        minutes = gtk.SpinButton(gtk.Adjustment(0.0, 0.0, 59.0, 1.0, 5.0, 0.0))
66        minutes.set_numeric(True)
67        minutes.set_wrap(True)
68        hbox.pack_start(minutes, False, False)
69        button.connect("clicked", date_time_cb, win, cal, cb, hours, minutes)
70        # custom
71        button = gtk.Button("custom")
72        table.attach(button, 0, 1, 4, 5, xoptions=gtk.FILL, yoptions=0, ypadding=4)
73        entry = gtk.Entry()
74        button.connect("clicked", custom, win, entry)
75        table.attach(entry, 1, 2, 4, 5)
76
77        # "Show note" toggle option
78        table.attach(gtk.HSeparator(), 0, 2, 5, 6)
79        cb = gtk.CheckButton("Show note")
80        table.attach(cb, 0, 2, 6, 7)
81
82        win.show_all()
83        win.present()
84        gtk.main()
85        return (get_reminder.reminder, cb.get_active())
86
87    title = msg.Subject
88    # set up contents: opening tag
89    content = ["<note-content>"]
90    # title
91    content.append(title)
92    content.append("\n\n")
93    # reminder if wanted
94    (reminder, show_note) = get_reminder("\n".join([msg.From, msg.Subject]))
95    if reminder == None:
96        return
97    if reminder:
98        content.extend(["!", reminder, "\n"])
99    # link back to email
100    msgid = msg.MessageID
101    if msgid[0] != "<":
102        msgid = "<" + msgid
103    if msgid[-1] != ">":
104        msgid += ">"
105    msgid = msgid.replace("<", "&lt;").replace(">", "&gt;")
106    link = '<link:cm-mail uri="%s/%s">%s</link:cm-mail>' % (clawsmail.get_folderview_selected_folder().get_identifier(), msgid, msg.Subject)
107    content.append(link)
108    #closing tag
109    content.append("</note-content>")
110
111    # create a tomboy note
112    bus = dbus.SessionBus()
113    rc = bus.get_object('org.gnome.Tomboy', '/org/gnome/Tomboy/RemoteControl')
114    rc_iface = dbus.Interface(rc, dbus_interface='org.gnome.Tomboy.RemoteControl')
115    uri = rc_iface.CreateNamedNote(title)
116    rc_iface.SetNoteContentsXml(uri, "".join(content))
117    rc_iface.DisplayNote(uri)
118    if not show_note:
119        rc_iface.HideNote(uri)
120
121
122# iterate over all notes
123for msg in clawsmail.get_summaryview_selected_message_list():
124    add_note(msg)
125
126
127
128
129
130
131