#!/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>
#
# Launch script for Gwibber Error
#

import sys
from os.path import join, dirname, exists, realpath, abspath
import optparse
import gtk, wnck

######################################################################
# Don't run again if we are already running
progname = "gwibber-error"
screen = wnck.screen_get_default()
while gtk.events_pending():
  gtk.main_iteration()
for w in screen.get_windows():
  if w.get_application().get_name() == progname:
    import time
    w.move_to_workspace(screen.get_active_workspace())
    w.activate(int(time.time()))
    print int(time.time())
    quit()


######################################################################
# Setup path
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, "error.py")):
    sys.path.insert(0, realpath(dirname(SOURCE_DIR)))
    try:
        from gwibber.microblog.util import log
        log.logger.name = "Gwibber Error"
        log.logger.info("Running from the source tree")
        from gwibber import error
    finally:
        del sys.path[0]
else:
    from gwibber.microblog.util import log
    log.logger.name = "Gwibber Error"
    log.logger.info("Running from the system path")
    from gwibber import error

######################################################################
# Options 
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-m", action="store",
                  dest="message", default=None,
                  help="Error message")
parser.add_option("-t", action="store",
                  dest="title", default=None,
                  help="Error title")
parser.add_option("-c", action="store",
                  dest="condition", default=None,
                  help="Error condition")
parser.add_option("-p", action="store",
                  dest="protocol", default=None,
                  help="Error protocol")
parser.add_option("-u", action="store",
                  dest="username", default=None,
                  help="Error username")
parser.add_option("-e", action="store",
                  dest="type", default=None,
                  help="Error message")
(options, args) = parser.parse_args()

######################################################################

dlg = error.GwibberErrorService()
dlg.ShowDialog(options.message, options.title, options.condition, options.protocol, options.username, options.type)
