Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Mergin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,28 @@
from .processing.provider import MerginProvider
import processing

from pyplugin_installer.installer import QgsPluginInstaller

MERGIN_CLIENT_LOG = os.path.join(QgsApplication.qgisSettingsDirPath(), "mergin-client-log.txt")
os.environ["MERGIN_CLIENT_LOG"] = MERGIN_CLIENT_LOG

# store method that will be monkeypatched
_original_method = QgsPluginInstaller.installPlugin


def install_plugin(self, key, quiet=False, stable=True):
"""
On Windows we need to release lock on geodiff library and unload plugin before
performing an update. See https://github.com/MerginMaps/qgis-mergin-plugin/issues/504
and https://github.com/MerginMaps/geodiff/issues/205
"""
if key == "Mergin" and os.name == "nt":
from qgis.utils import unloadPlugin

unloadPlugin(key)

_original_method(self, key, quiet, stable)


class MerginPlugin:
def __init__(self, iface):
Expand Down Expand Up @@ -185,6 +204,11 @@ def initGui(self):

QgsProject.instance().layersAdded.connect(self.add_context_menu_actions)

# monkeypatch plugin installer to allow unlocking geodiff library and unloading plugin before installing
# see https://github.com/MerginMaps/qgis-mergin-plugin/issues/504, https://github.com/MerginMaps/geodiff/issues/205
if os.name == "nt":
QgsPluginInstaller.installPlugin = install_plugin

def add_action(
self,
icon_name,
Expand Down Expand Up @@ -547,6 +571,16 @@ def unload(self):

QgsApplication.processingRegistry().removeProvider(self.provider)

# unlock geodiff library and revert monkeypatching
if os.name == "nt":
from _ctypes import FreeLibrary
from .mergin.deps import pygeodiff

geodiff = pygeodiff.GeoDiff()
FreeLibrary(geodiff.clib.lib._handle)

QgsPluginInstaller.installPlugin = _original_method

def view_local_changes(self):
project_path = QgsProject.instance().homePath()
if not project_path:
Expand Down