summaryrefslogtreecommitdiff
path: root/lib/extensions/install.py
blob: c6dadd422fd3285fdd02782faea3058f7b5575f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: UTF-8 -*-

import sys
import traceback
import os
from glob import glob
import wx
import inkex

from ..utils import guess_inkscape_config_path, get_bundled_dir
from ..i18n import _


class InstallerFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.path = guess_inkscape_config_path()

        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)

        text_sizer = wx.BoxSizer(wx.HORIZONTAL)

        text = (_('Ink/Stitch can install files ("add-ons") that make it easier to use Inkscape to create machine embroidery designs.  '
                  'These add-ons will be installed:') +
                "\n\n   • " + _("thread manufacturer color palettes") +
                "\n   • " + _("Ink/Stitch visual commands (Object -> Symbols...)"))

        static_text = wx.StaticText(panel, label=text)
        font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        static_text.SetFont(font)
        text_sizer.Add(static_text, proportion=0, flag=wx.ALL | wx.EXPAND, border=10)
        sizer.Add(text_sizer, proportion=3, flag=wx.ALL | wx.EXPAND, border=0)

        buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
        install_button = wx.Button(panel, wx.ID_ANY, _("Install"))
        install_button.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_TICK_MARK))
        buttons_sizer.Add(install_button, proportion=0, flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
        cancel_button = wx.Button(panel, wx.ID_CANCEL, _("Cancel"))
        buttons_sizer.Add(cancel_button, proportion=0, flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
        sizer.Add(buttons_sizer, proportion=1, flag=wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM)

        panel.SetSizer(sizer)
        panel.Layout()

        cancel_button.Bind(wx.EVT_BUTTON, self.cancel_button_clicked)
        install_button.Bind(wx.EVT_BUTTON, self.install_button_clicked)

    def cancel_button_clicked(self, event):
        self.Destroy()

    def chooser_button_clicked(self, event):
        dialog = wx.DirDialog(self, _("Choose Inkscape directory"))
        if dialog.ShowModal() != wx.ID_CANCEL:
            self.path_input.SetValue(dialog.GetPath())

    def install_button_clicked(self, event):
        if sys.platform == "win32":
            # On windows, the default icon shows as a broken image.  No idea
            # why.  Workaround: don't show an icon.
            style = wx.ICON_NONE
        else:
            style = 0

        try:
            self.install_addons('palettes')
            self.install_addons('symbols')
        except Exception:
            wx.MessageDialog(self,
                             _('Inkscape add-on installation failed') + ': \n' + traceback.format_exc(),
                             _('Installation Failed'),
                             wx.OK | style).ShowModal()
        else:
            wx.MessageDialog(self,
                             _('Inkscape add-on files have been installed.  Please restart Inkscape to load the new add-ons.'),
                             _('Installation Completed'),
                             wx.OK | style).ShowModal()

        self.Destroy()

    def install_addons(self, type):
        path = os.path.join(self.path, type)
        src_dir = get_bundled_dir(type)
        self.copy_files(glob(os.path.join(src_dir, "*")), path)

    if (sys.platform == "win32"):
        # If we try to just use shutil.copy it says the operation requires elevation.
        def copy_files(self, files, dest):
            import winutils

            if not os.path.exists(dest):
                os.makedirs(dest)

            winutils.copy(files, dest)
    else:
        def copy_files(self, files, dest):
            import shutil

            if not os.path.exists(dest):
                os.makedirs(dest)

            for palette_file in files:
                shutil.copy(palette_file, dest)


class Install(inkex.Effect):
    @classmethod
    def name(cls):
        return "install"

    def effect(self):
        app = wx.App()
        installer_frame = InstallerFrame(None, title=_("Ink/Stitch Add-ons Installer"), size=(550, 250))
        installer_frame.Show()
        app.MainLoop()