Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1881: 
kinsamanka@3750: 
edouard@3573: import os
edouard@3817: import sys
Edouard@3111: import wx
andrej@1732: import subprocess
edouard@3573: from dialogs import MessageBoxOnce
andrej@1736: 
edouard@3482: 
edouard@3482: def _get_inkscape_path():
Edouard@3111:     """ Return the Inkscape binary path """
Edouard@3111: 
edouard@3817:     if sys.platform.startswith('win32'):
edouard@3817:         import winreg
Edouard@3111:         inkcmd = None
edouard@3477:         tries = [(winreg.HKEY_LOCAL_MACHINE, 'Software\\Classes\\svgfile\\shell\\Inkscape\\command'),
edouard@3477:                  (winreg.HKEY_LOCAL_MACHINE, 'Software\\Classes\\inkscape.svg\\shell\\open\\command'),
edouard@3477:                  (winreg.HKEY_CURRENT_USER, 'Software\\Classes\\inkscape.svg\\shell\\open\\command')]
edouard@3477: 
edouard@3477:         for subreg, key in tries:
Edouard@2705:             try:
edouard@3477:                 inkcmd = winreg.QueryValue(subreg, key)
edouard@3477:                 break;
Edouard@3111:             except OSError:
edouard@3477:                 pass
edouard@3477: 
edouard@3477:         if inkcmd is None:
edouard@3477:             return None
Edouard@2705: 
edouard@3901:         return inkcmd.replace('"%1"', '').strip().replace('"', '').encode()
Edouard@3111: 
Edouard@2628:     else:
Edouard@3111:         try:
Edouard@3111:             return subprocess.check_output("command -v inkscape", shell=True).strip()
Edouard@3111:         except subprocess.CalledProcessError:
Edouard@3111:             return None
andrej@1736: 
edouard@3482: _inkscape_path = None
edouard@3482: def get_inkscape_path():
edouard@3482:     """ Return the Inkscape binary path """
edouard@3482: 
edouard@3482:     global _inkscape_path
edouard@3482: 
edouard@3482:     if _inkscape_path is not None:
edouard@3482:         return _inkscape_path
edouard@3482: 
edouard@3482:     _inkscape_path = _get_inkscape_path()
edouard@3482:     return _inkscape_path
edouard@3482: 
edouard@3482: 
edouard@3482: def _get_inkscape_version():
edouard@3482:     inkpath = get_inkscape_path()
edouard@3482:     if inkpath is None:
edouard@3482:         return None
edouard@3890:     version_string = subprocess.check_output(
edouard@3890:             [inkpath,"--version"], 
edouard@3890:             stderr=subprocess.STDOUT)
edouard@3890:     if version_string:
edouard@3890:         return list(map(int,version_string.split()[1].split(b'.')))
edouard@3890:     return [0,0]
edouard@3482: 
edouard@3482: _inkscape_version = None
edouard@3482: def get_inkscape_version():
edouard@3482:     global _inkscape_version
edouard@3482: 
edouard@3482:     if _inkscape_version is not None:
edouard@3482:         return _inkscape_version
edouard@3482: 
edouard@3482:     _inkscape_version = _get_inkscape_version()
edouard@3482:     return _inkscape_version
edouard@3482: 
kinsamanka@3750: if "SNAP" in os.environ:
edouard@3573:     def open_svg(svgfile):
edouard@3573:         MessageBoxOnce("Launching Inkscape with xdg-open",
edouard@3573:                 "Confined app can't launch Inkscape directly.\n"+
edouard@3573:                     "Instead, SVG file is passed to xdg-open.\n"+
edouard@3573:                     "Please select Inskape when proposed.\n\n"+
edouard@3573:                     "Notes: \n"+
edouard@3573:                     " - Inkscape must be installed on you system.\n"+
edouard@3573:                     " - If no choice is proposed, use file manager to change SVG file properties.\n",
edouard@3573:                 "DocSVGSnapWarning")
edouard@3573: 
edouard@3573:         subprocess.Popen(["xdg-open",svgfile])
edouard@3573: else:
edouard@3573:     def open_svg(svgfile):
edouard@3573:         """ Generic function to open SVG file """
edouard@3573:         inkpath = get_inkscape_path()
edouard@3573:         if inkpath is None:
edouard@3573:             wx.MessageBox("Inkscape is not found or installed !")
edouard@3573:         else:
edouard@3573:             subprocess.Popen([inkpath,svgfile])