#!/usr/bin/python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE

import sys
import os
import gtk
from desktopcouch.records.server import CouchDatabase
from desktopcouch.records.record import Record

# Check if we are working in the source tree or from the installed 
# package and mangle the python path accordingly
if os.path.dirname(sys.argv[0]) != ".":
    if sys.argv[0][0] == "/":
        fullPath = os.path.dirname(sys.argv[0])
    else:
        fullPath = os.getcwd() + "/" + os.path.dirname(sys.argv[0])
else:
    fullPath = os.getcwd()
sys.path.insert(0, os.path.dirname(fullPath))

from jotty import AboutJottyDialog, PreferencesJottyDialog
from jotty.jottyconfig import getdatapath

class JottyWindow(gtk.Window):
    __gtype_name__ = "JottyWindow"

    def __init__(self):
        """__init__ - This function is typically not called directly.
        Creation a JottyWindow requires redeading the associated ui
        file and parsing the ui definition extrenally,
        and then calling JottyWindow.finish_initializing().

        Use the convenience function NewJottyWindow to create
        JottyWindow object.

        """
        pass

    def finish_initializing(self, builder):
        """finish_initalizing should be called after parsing the ui definition
        and creating a JottyWindow object with it in order to finish
        initializing the start of the new JottyWindow instance.

        """
        #get a reference to the builder and set up the signals
        self.builder = builder
        self.builder.connect_signals(self)

        #uncomment the following code to read in preferences at start up
        #dlg = JottyPreferencesDialog.NewJottyPreferencesDialog()
        #self.preferences = dlg.get_preferences()

        #code for other initialization actions should be added here
        self.database = CouchDatabase("jotty", create=True)

    def about(self, widget, data=None):
        """about - display the about box for what_the_heck """
        about = AboutJottyDialog.NewAboutJottyDialog()
        response = about.run()
        about.destroy()

    def preferences(self, widget, data=None):
        """preferences - display the preferences window for jotty """
        prefs = PreferencesJottyDialog.NewPreferencesJottyDialog()
        response = prefs.run()
        if response == gtk.RESPONSE_OK:
            #make any updates based on changed preferences here
            pass
        prefs.destroy()

    def quit(self, widget, data=None):
        """quit - signal handler for closing the JottyWindow"""
        self.destroy()

    def on_destroy(self, widget, data=None):
        """on_destroy - called when the JottyWindow is close. """
        #clean up code for saving application state should be added here

        gtk.main_quit()

    def save_file(self, widget, data=None):
        #get the title for the note
        title = self.builder.get_object("entry1").get_text()

        #get the text to save
        buff = self.builder.get_object("textview1").get_buffer()
        start_iter = buff.get_start_iter()
        end_iter = buff.get_end_iter()
        text = buff.get_text(start_iter,end_iter)

        #get all the records
        record_type = "http://wiki.ubuntu.com/Quickly/JottyDoc"
        results = self.database.get_records(record_type = record_type, 
                                            create_view = True)

        #update a record that has the same title
        for result in results:
            document = result.value
            if document["title"] == title:
                key = result.key
                self.database.update_fields(key, {"text":text})
                return
        
        #if no records had the title, create it  
        new_rec = Record({"record_type":record_type ,
                            "title":title, "text":text})
        self.database.put_record(new_rec)

    def open_file(self, widget, data=None):
        #get the name of the document to open
        title = self.builder.get_object("entry1").get_text()
        text = ""

        #get all the records
        record_type = "http://wiki.ubuntu.com/Quickly/JottyDoc"
        results = self.database.get_records(record_type = record_type, 
                                            create_view = True)

        #get the text if there is a matching title
        for result in results:
            document = result.value
            if document["title"] == title:
                text = document["text"]
        
        #set the UI to display the string
        buff = self.builder.get_object("textview1").get_buffer()
        buff.set_text(text)


    def new_file(self, widget, data=None):
        self.builder.get_object("entry1").set_text("Note Title")
        buff = self.builder.get_object("textview1").get_buffer()
        buff.set_text("")


def NewJottyWindow():
    """NewJottyWindow - returns a fully instantiated
    JottyWindow object. Use this function rather than
    creating a JottyWindow directly.
    """

    #look for the ui file that describes the ui
    ui_filename = os.path.join(getdatapath(), 'ui', 'JottyWindow.ui')
    if not os.path.exists(ui_filename):
        ui_filename = None

    builder = gtk.Builder()
    builder.add_from_file(ui_filename)
    window = builder.get_object("jotty_window")
    window.finish_initializing(builder)
    return window

if __name__ == "__main__":
    window = NewJottyWindow()
    window.show()
    gtk.main()

