#!/usr/bin/python
#-*- coding: UTF-8 -*-

"""cicm
Cicm is a free software that creates, edits
and manages image collections.
"""

__author__  = "Tachara (tortured.flame@gmail.com)"
__date__ = "$Date 2010/31/03 18:07:00 $"
__copyright__ = "Copyright (c) 2010 Tachara"

import sys
import os
import logging
logging.basicConfig(filename='cicm.log', format='%(asctime)s | %(name)s | %(levelname)s | %(message)s', level=logging.DEBUG)

from db import FileDatabase
from gui2db import Gui2Db
from tkgui import Main
from tkgraphics import gallery_with_slideshow
from tagview import TagView
from helpview import HelpView

import keybindings

if __name__ == "__main__":

    dbname = 'master.sqlite'
    if len(sys.argv) > 1:
        dbname = sys.argv[1]
    logging.debug('Database: %s', dbname)

    run_init = False
    if not os.path.exists(dbname):
        dbname.debug('run init on %s', dbname)
        run_init = True
    db = FileDatabase(dbname)
    if run_init:
        db.initialize()
    mainview = Main()

    glue = Gui2Db(db, mainview)

    tags = db.list_tags()
    tview = TagView(mainview.sidebar, tags)

    actions = {
        'quit' : lambda e: mainview.quit(),
        'delete_view': mainview.delete_current_view,
        'add_tags' : lambda e: mainview.text_query('Add tags: '),
        'add_selected_tags': lambda e: glue.add_tags_from_tagview(e, tview),
        'add_images' : glue.add_files,
        'add_folder' : glue.add_directory,
        'remove_tags': glue.remove_tags_from_files,
        'jump_to_tag': lambda e: mainview.text_query('Jump to tag: ',
            accept_func=lambda t, o: tview.jump_to(t)),
        'focus_sidebar': lambda e: mainview.focus_sidebar(),
        'focus_main_view': lambda e: mainview.focus_main_view(),
        'toggle_selection_tags': glue.toggle_selection_tags,
        'toggle_collections': glue.toggle_collections,
        'help': lambda e: mainview.new_view(HelpView(mainview.root)),
    }
    keybindings.make_bindings(keybindings.appwide, actions, mainview.root.bind_all)
    buttons = {
            'Add collection': glue.add_collection,
            'Remove collection': glue.remove_collection,
            'Remove deleted': glue.remove_deleted_files,
    }
    for label in buttons:
        mainview.add_menubutton(label, buttons[label])

    # Custom virtual events do not need to be user bindable
    # since their generation is user bindable. -> no dictionaries used.
    mainview.root.bind_all('<<TagViewSearch>>', glue.search)
    mainview.root.bind_all('<<TagViewEdit>>',
            lambda e: mainview.text_query('Edit tag:', tview.selection()[0]))
    mainview.root.bind_all('<<MainQueryAccept>>', glue.add_or_rename_tags)
    mainview.root.bind_all('<<GallerySelectionChanged>>', glue.update_selection_tags)
    mainview.root.bind_all('<<SlideShowNext>>', glue.update_selection_tags)
    mainview.root.bind_all('<<SlideShowPrev>>', glue.update_selection_tags)
    mainview.root.bind_all('<<MainViewChanged>>', glue.update_selection_tags)

    mainview.add_sidebar(tview, 'main_tags')
    mainview.display()


@@ -1099,24 +1099,116 @@
 debar, tags)
+%0A    def jump_to_tag(tag, orig):%0A        tview.jump_to(tag)%0A        tview.widget.focus_set()
 %0A%0A    action
@@ -1685,37 +1685,19 @@
 unc=
-lambda t, o: tview.
 jump_to
-(t)
+_tag
 ),%0A 

Make jumping to a tag focus the tagview.