#!/usr/bin/python2
# This file is part of SSHconf.
#
#    SSHconf 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 3 of the License, or
#    (at your option) any later version.
#
#    SSHconf 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 SSHconf.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Frank "ancho" Becker 
#
# Copyright 2010 Frank Becker <ancho@gmx.net>
'''
Created on 14.01.2010

@author: frank
'''
from PyQt4 import QtGui, Qt
from PyKDE4.kdeui import KStatusNotifierItem, KMenu
import logging

class SSHConfSystemTrayIcon():
    '''
    A SystemTrayIcon for SSHConf
    '''


    def __init__(self, parent, handler):
        self.parent = parent
        self.handler = handler
        self.trayIconMenu = KMenu()
        self.hostMenu = QtGui.QMenu("Hosts",self.trayIconMenu)
        self.log = logging.getLogger("SSHConfSystemTrayIcon")
        self.__createKStatusNotifierItem()
        self.__createMainMenu()
        
        
    
    def get_sub_menu(self, name):
        submenu = QtGui.QMenu(name,self.trayIconMenu)

        self.__create_actions(name)
        
        submenu.addActions(self.actions)
        return submenu

    def __createKStatusNotifierItem(self):
        self.sysTray = KStatusNotifierItem(self.parent)
        self.sysTray.setCategory(KStatusNotifierItem.ApplicationStatus)
        self.sysTray.setAssociatedWidget(self.parent)
        self.sysTray.setIconByName("openssh_trans")
        self.sysTray.setStatus(KStatusNotifierItem.Active)
        self.sysTray.setContextMenu(self.trayIconMenu)

    def __createMainMenu(self):
        addAction = QtGui.QAction("Add new Host", self.parent, triggered=self.handler.new_host)
        self.trayIconMenu.addAction(addAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addMenu( self.hostMenu )
        
        
    
    def __create_actions(self, name):
        self.actions = []
        
        editAction = QtGui.QAction("edit", self.parent)
        connAction = QtGui.QAction("connect", self.parent)
        
        editCall = lambda: self.handler.edit_host(name)
        connCall = lambda: self.handler.connect_host(name)
        
        self.parent.connect(editAction,Qt.SIGNAL("triggered()"), editCall)
        self.parent.connect(connAction,Qt.SIGNAL("triggered()"), connCall)
        
        self.actions.append(connAction)
        self.actions.append(editAction)
            
    def update_menu(self, hostnames):
         
        self.hostMenu.clear()
     
        for hostname in hostnames:
            self.log.debug("add menu " + hostname)
            self.hostMenu.addMenu( self.get_sub_menu(hostname) )
            
        self.sysTray.setContextMenu( self.trayIconMenu )
        
        
        