#!/usr/bin/env python

# Authors:
#   Jason Gerard DeRose <jderose@novacut.com>
#
# dmedia: distributed media library
# Copyright (C) 2010 Jason Gerard DeRose <jderose@novacut.com>
#
# This file is part of `dmedia`.
#
# `dmedia` is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# `dmedia` is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with `dmedia`.  If not, see <http://www.gnu.org/licenses/>.

import sys
from subprocess import check_call
import optparse
import dmedia
from dmedia.metastore import MetaStore
from dmedia import ui

parser = optparse.OptionParser(
    version=dmedia.__version__,
)
parser.add_option('--browser',
    action='store_true',
    default=False,
    help='open dmedia HTML5 UI in default browser',
)
(options, args) = parser.parse_args()


store = MetaStore()
app = ui.create_app()
store.update(app)


if options.browser:
    uri = store.get_auth_uri() + '/dmedia/app/browser'
    check_call(['xdg-open', uri])
    sys.exit()

uri = store.get_uri() + '/dmedia/app/browser'


import gtk
import gobject

gobject.threads_init()


window = gtk.Window()
window.set_title('test')
window.set_default_size(800, 450)
window.connect('destroy', gtk.main_quit)

box = gtk.VBox()
window.add(box)

view = ui.CouchView()
box.pack_start(view, True, True, 2)

# Enable inspector
#settings = view.get_settings()
#settings.set_property('enable-developer-extras', True)
#inspector = view.get_web_inspector()
#inspector.set_property('javascript-profiling-enabled', True)

#def on_inspect(*args):
#    view2 = webkit.WebView()
#    box.pack_start(view2, True, True, 2)
#    return view2

#inspector.connect('inspect-web-view', on_inspect)


window.show_all()
html = ui.load_datafile('browser.html')
view.load_string(html, 'text/html', 'UTF-8', uri)


gtk.main()
