# -*- coding: utf-8 -*-

#  Copyright © 2013  B. Clausius <barcc@gmx.de>
#
#  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 3 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, see <http://www.gnu.org/licenses/>.

from __future__ import print_function, division

import sys, os


class ConnectHelper(object):
    def __init__(self):
        self.handlers = {}
        
    def _connect(self, gobj, signal, func, *args):
        handlers = self.handlers.setdefault(gobj, [])
        handlers.append(gobj.connect(signal, func, *args))
        
    def _connect_after(self, gobj, signal, func, *args):
        handlers = self.handlers.setdefault(gobj, [])
        handlers.append(gobj.connect_after(signal, func, *args))
        
    def _disconnect(self, gobj):
        try:
            handlers = self.handlers[gobj]
        except KeyError:
            return
        while handlers:
            gobj.disconnect(handlers.pop())
        del self.handlers[gobj]
        
    def disconnect_all(self):
        for gobj, handlers in self.handlers.items():
            while handlers:
                gobj.disconnect(handlers.pop())
        self.handlers.clear()
        
        
