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

"""
/***************************************************************************
 QProto
                                 A QGIS plugin
 QProto algorithm
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2018-04-19
        copyright            : (C) 2018 by Matteo Ghetta (Faunalia)
        email                : matteo.ghetta@faunalia.eu
        updated              : 2026 by Stefano Campus & Codex
        updated email        : skampus@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
"""

__author__ = 'Matteo Ghetta (Faunalia); 2026 update by Stefano Campus'
__date__ = '2018-04-19'
__copyright__ = '(C) 2018 by Matteo Ghetta (Faunalia); 2026 update by Stefano Campus & Codex'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os
import sys
import inspect

from qgis.core import QgsApplication
from .qproto_provider import QProtoProvider

cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]

if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)


class QProtoPlugin(object):

    def __init__(self):
        self.provider = None

    def initGui(self):
        registry = QgsApplication.processingRegistry()

        if self.provider is not None:
            try:
                registry.removeProvider(self.provider)
            except RuntimeError:
                pass

        existing_provider = registry.providerById('QProto')
        if existing_provider is not None:
            registry.removeProvider(existing_provider)

        self.provider = QProtoProvider()
        registry.addProvider(self.provider)

    def unload(self):
        if self.provider is None:
            return

        registry = QgsApplication.processingRegistry()
        try:
            registry.removeProvider(self.provider)
        except RuntimeError:
            pass
        finally:
            self.provider = None
