import threading

# GTK stuff
from gi.repository import GObject, Gtk

# Evolution stuff
import evolution

EVOLUTION_IN_OWN_THREAD = False

def do_evolution():
    task_personal = evolution.ecal.list_task_sources()[0][1]
    evo = evolution.ecal.open_calendar_source( \
                                    task_personal,
                                    evolution.ecal.CAL_SOURCE_TYPE_TODO)
    print "All objects"
    print evo.get_all_objects()

def my_main():
    print "Starting thread"
    do_evolution()
    print "Done"

def action(widget, data=None):
    print "You clicked the button"
    do_evolution()
    print "Done"

def destroy(widget, data=None):
    print "Turning off GTK"
    Gtk.main_quit()

GObject.threads_init()

t = threading.Thread(target=my_main)

# Build simple GTK hello world
window = Gtk.Window()
window.set_border_width(10)
button = Gtk.Button("Hello World")
window.add(button)
window.show_all()

window.connect("destroy", destroy)
if not EVOLUTION_IN_OWN_THREAD:
    button.connect("clicked", action)
else:
    button.connect("clicked", destroy)

# This starts thread communicating with evolution
if EVOLUTION_IN_OWN_THREAD:
    print "Starting, in 2 seconds it will freeze"
    GObject.timeout_add(2000, t.start)

# Start GTK main
Gtk.main()

print ">>> Shutdown"
