#!/usr/bin/python

#
#
# blueman - main blueman executable
# (c) 2007 Valmantas Paliksa <walmis at balticum-tv dot lt>
#
# This library 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.1 of the License, or (at your option) any later version.
# 
# This library 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 Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#




import gtk
import gtk.glade
import locale
import gettext

import blueman.Globals as Globals

import blueman.constants

#print constants.PREFIX
#print constants.SVN_REVISION
#print constants.VERSION
from blueman.constants import PREFIX
from blueman.constants import SVN_REVISION
from blueman.constants import VERSION
Globals.SVN_REVISION = SVN_REVISION
Globals.VERSION = VERSION

GLADE_DIR = PREFIX + "/share/blueman/"
IMAGE_DIR = PREFIX + "/share/blueman/icons"

Globals.KILL = False

lc_path = PREFIX + "/share/locale"
gettext.bindtextdomain('blueman', lc_path)
gettext.textdomain('blueman')

gtk.glade.bindtextdomain('blueman', lc_path)
gtk.glade.textdomain('blueman')

Globals._ = _ = gettext.gettext
Globals.ngettext = ngettext = gettext.ngettext

gtk.gdk.threads_init()

from blueman.Main.settings import settings
from blueman.Bluetooth.adapter import adapter
from blueman.Bluetooth.manager import manager
from blueman.Bluetooth.services import services
from blueman.Bluetooth.bonding import bonding
from blueman.Bluetooth.inquiry import inquiry
from blueman.Bluetooth.connectivity import connectivity
from blueman.Bluetooth.dbusAgent import dbusAgent

from blueman.Gui.main_window import main_window
from blueman.Gui.toolbar import toolbar
from blueman.Gui.main_list import main_list
from blueman.Gui.statusbar import statusbar
from blueman.Gui.statusicon import statusicon
from blueman.Gui.notification import notification
from blueman.Gui.statusbar_with_stats import statusbar_with_stats

from blueman.Main.debug import OUT
OUT.set_info_level(9)
OUT.set_warn_level(9)
Globals.dbg = OUT
                



def deinit_adapter():
        Globals.dbg.info("Deinitializing current adapter")
        
        Globals.Bonding.Deinit()
        Globals.Connectivity.Deinit()
        Globals.Inquiry.Deinit()	
        
        del Globals.adapter
        del Globals.Inquiry
        del Globals.Connectivity
        del Globals.Bonding
        
        
def init_adapter(hci):
        global adapter, Bonding, CInquiry, Connectivity, Agent, manager
        Globals.dbg.info("Initializing adapter %s" % hci)
                

        Globals.Bonding = bonding(hci)
        Globals.Inquiry = inquiry(hci)
        Globals.adapter = adapter(hci)
        Globals.Connectivity = connectivity()
        
        Globals.Statusbar.push("Ready")

Globals.init_adapter = init_adapter
Globals.deinit_adapter = deinit_adapter

Globals.PREFIX = PREFIX
Globals.GLADE_DIR = GLADE_DIR
Globals.IMAGE_DIR = IMAGE_DIR

xml = gtk.glade.XML(GLADE_DIR + "/blueman.glade")

Globals.Settings = settings()

Globals.Statusbar = statusbar("main",xml)

Globals.window = main_window(xml)

Globals.toolbar = toolbar(xml)


########## Initialize adapters ############
Globals.Manager = manager()
Globals.Services = services()

Agent = None

adapters = Globals.Manager.ListAdapters()
if len(adapters) == 0:
        Globals.dbg.warn("No adapters found")
        Globals.Statusbar.push(_("No Adapters Found"))
        Globals.toolbar.disable_all_buttons()	
elif not "/org/bluez/"+Globals.Settings.adapter in adapters:	
        Globals.dbg.warn("Adapter specified in preferences is not availabe, switching to another")
        Globals.Settings.adapter = adapters[0].split("/")[3]
        init_adapter(Globals.Settings.adapter)
else:
        Globals.dbg.info("Adapter Found", 6)
        init_adapter(Globals.Settings.adapter)

if Globals.Settings.passkey_agent:
        
        if Agent == None:
                try:
                        Agent = dbusAgent("/org/bluez/blueman")
                        Agent.register()
        
                except Exception, reason:
                        Globals.dbg.warn(reason)
                        Globals.Statusbar.push(_("Unable to register passkey agent."))


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

Globals.main_list = main_list(xml)

status_icon = statusicon(xml)

Globals.Notification = notification(status_icon)

stats = statusbar_with_stats()
stats.start_update()

#gtk.gdk.threads_enter()
gtk.main()
#gtk.gdk.threads_leave()
