#!/usr/bin/env python
#
# Copyright (C) 2010 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2010 Ken VanDine <ken.vandine@canonical.com>
#
# A poster for Gwibber
#


######################################################################
# Setup path
from os.path import join, dirname, exists, realpath, abspath
import sys

LAUNCH_DIR = abspath(sys.path[0])
SOURCE_DIR = join(LAUNCH_DIR, "..", "gwibber")

# If we were invoked from a Gwibber source directory add that as the
# preferred module path ...
if exists(join(SOURCE_DIR, "client.py")):
    sys.path.insert(0, realpath(dirname(SOURCE_DIR)))
    try:
        from gwibber.microblog.util import log
        log.logger.name = "Gwibber Poster"
        log.logger.info("Running from the source tree")
        from gwibber import client
    finally:
        del sys.path[0]
else:
    from gwibber.microblog.util import log
    log.logger.name = "Gwibber Poster"
    log.logger.info("Running from the source tree")
    from gwibber import client
######################################################################

from gwibber.lib.gtk import widgets
import gtk
import gwibber.gwui, gwibber.util, gwibber.resources
import gettext
from gettext import lgettext as _

######################################################################
# Options 
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-p", "--persist", action="store_true",
                  dest="persist", default=False,
                  help=_("Don't exit after posting"))
parser.add_option("-w", "--window", action="store_true",
                  dest="window", default=False,
                  help=_("Don't exit when the window loses focus, also implies -d"))
parser.add_option("--decorate", action="store_true",
                  dest="decorate", default=False,
                  help=_("Display window decorations"))
parser.add_option("-d", "--debug", action="store_true",
                  dest="debug", default=False,
                  help=_("Log debug messages"))
parser.add_option("-m", "--message", dest="message",
                  help=_("Message to post"))
(options, args) = parser.parse_args()
if options.debug:
  log.logger.setLevel(log.logging.DEBUG)
else:
  log.logger.setLevel(log.logging.INFO)
######################################################################

class GwibberPosterWindow(gtk.Window):
  def __init__(self):
    gtk.Window.__init__(self)
    self.set_default_size(420,140)
    self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
    self.set_title(_("Gwibber Poster"))
    self.set_icon_from_file(gwibber.resources.get_ui_asset("gwibber.svg"))

    self.connect("delete-event", self.on_window_close)

    if not options.decorate and not options.window:
      self.set_decorated(False)

    if not options.window:
      self.connect("focus-out-event", self.on_window_close)

    self.poster = widgets.GwibberPosterVBox()
    self.poster.connect("expose-event", self.on_expose_input)

    self.poster.input.connect("submit", self.on_input_activate)
    self.poster.button_send.connect("clicked", self.on_button_send_clicked)


    self.add(self.poster)
    self.show_all()

  def on_expose_input(self, *args):
    if options.message:
      self.poster.input.set_text(options.message)
  
  def on_input_activate(self, *args):
    if not options.persist:
      self.on_window_close()

  def on_button_send_clicked(self, *args):
    if not options.persist:
      self.on_window_close()

  def on_window_close(self, *args):
    gtk.main_quit()


w = GwibberPosterWindow()
gtk.main()
