#!/usr/bin/env python
# Copyright 2011-2012 Dafydd Crosby
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met: 

# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer. 
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution. 

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
qwelog - the personal log so simple, you'll use it
-h, --help	show options
-f, --file	change which file to write to
"""

from Tkinter import Tk, Entry, Button, Frame
import os, sys, getopt
from datetime import datetime

class QwelogDialog():
    def __init__(self, parent):
        self.top = Frame(parent)
        self.top.pack()
        
        self.thing = ""

        self.entry = Entry(self.top)
        self.entry.focus()
        self.entry.bind("<Return>", self.add_thing)
        self.entry.pack()
 
        button = Button(self.top, text="Add thing", command=self.add_thing)
        button.bind("<Return>", self.add_thing)
        button.pack()

    def add_thing(self, event=""):
        self.thing = self.entry.get()
        self.top.destroy()

def get_home_dir():
    """
    I broke this out just in case this gets ported to a non-Linux system
    """
    return os.environ['HOME']

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hf:', ['--help', '--file='])
    except getopt.GetoptError, err:
        print(err)
        print(__doc__)
        sys.exit(2)

    filepath = get_home_dir()+"/qwelog.txt"
    for o, a in opts:
        if o in ("-h", "--help"):
            print(__doc__)
            sys.exit(0)
        elif o in ("-f", "--file="):
            filepath = a

    try:
        log = open(filepath, "a")
    except IOError, err:
        print(err)
        sys.exit(1)

    # Create dialog window
    root = Tk()
    root.title("qwelog")
    thing = QwelogDialog(root)
    root.wait_window(thing.top)

    # Make sure there is something to write
    if thing.thing:
        timestamp = datetime.now()
        log.write(timestamp.strftime("%Y-%m-%d-%H:%M ") + thing.thing + "\n")

    log.close()

if __name__ == "__main__":
    main()

# I love me some consistent codestyle
# vim:ts=4:sw=4:et
