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