#!/usr/bin/env python

# reportbug-ng - Report a bug in Debian's BTS.
# Copyright (C) 2007  Bastian Venthur
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import gettext
gettext.install('reportbug-ng', '/usr/share/locale', unicode=1)

import sys, os
sys.path = sys.path + [os.curdir, '/usr/share/reportbug-ng']

from ui.MyMainWindow import MyMainWindow

import signal
import qt
from optparse import OptionParser
import logging

from lib.Settings import Settings

CONFIGFILE = os.path.expanduser("~/.reportbug-ng")


if __name__ == "__main__":
    # Get Options
    description = """\
Report a bug in Debian's BTS. The optional paremter QUERY behaves exactly like the query inside the program. \
Supported queries are: packagename, bugnumber, maintainer@foo.bar, src:package, from:submitter@foo.bar, severity:foo and tag:bar."""
    usage = "%prog [Options] [Query]"
    version = """
Copyright (C) 2007 Bastian Venthur <venthur at debian org>

Homepage: http://reportbug-ng.alioth.debian.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
    parser = OptionParser(usage=usage, version=version, description=description)
    parser.add_option('-l', '--loglevel', type='choice', choices=['critical', 
        'error', 'warning', 'info', 'debug', 'notset'], dest='loglevel',
        help='Which loglevel to use [default: warning]. Valid loglevels are: critical, error, warning, info, debug, notset',
        metavar='LEVEL')
    
    options, args = parser.parse_args()

    # Initialize logging
    loglevel = {'critical' : logging.CRITICAL,
                'error'    : logging.ERROR,
                'warning'  : logging.WARNING,
                'info'     : logging.INFO,
                'debug'    : logging.DEBUG,
                'notset'   : logging.NOTSET
                }.get(options.loglevel, logging.WARNING)
        
    logging.basicConfig(level=loglevel, format='%(name)-12s %(levelname)-8s %(message)s')
    logging.info('Logger initialized with level %s.' % options.loglevel)

    settings = Settings(CONFIGFILE)
    settings.load()

    app = qt.QApplication(sys.argv)
    app.connect(app, qt.SIGNAL("lastWindowClosed()"), app, qt.SLOT("quit()"))
    
    mw = MyMainWindow(settings, args)
    app.setMainWidget(mw)
    mw.show()

    signal.signal(signal.SIGINT, signal.SIG_DFL) 
    app.exec_loop()
    
    settings.save()    
   
    