2018-06-24 19:55:13 +00:00
# -*- coding: UTF-8 -*-
2018-04-29 01:26:53 +00:00
import sys
import traceback
import os
from os . path import realpath , dirname
from glob import glob
from threading import Thread
import socket
import errno
import time
import logging
import wx
import inkex
2018-05-02 01:21:07 +00:00
2018-07-05 01:16:49 +00:00
from . . utils import guess_inkscape_config_path , get_bundled_dir
2018-04-29 01:26:53 +00:00
2018-06-24 19:55:13 +00:00
class InstallerFrame ( wx . Frame ) :
2018-04-29 01:26:53 +00:00
def __init__ ( self , * args , * * kwargs ) :
wx . Frame . __init__ ( self , * args , * * kwargs )
2018-06-29 00:32:09 +00:00
self . path = guess_inkscape_config_path ( )
2018-04-29 01:26:53 +00:00
panel = wx . Panel ( self )
sizer = wx . BoxSizer ( wx . VERTICAL )
2018-06-24 19:55:13 +00:00
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 " ) + \
2018-06-29 00:32:09 +00:00
" \n • " + _ ( " Ink/Stitch visual commands (Object -> Symbols...) " )
2018-06-24 19:55:13 +00:00
static_text = wx . StaticText ( panel , label = text )
2018-04-29 01:26:53 +00:00
font = wx . Font ( 12 , wx . DEFAULT , wx . NORMAL , wx . NORMAL )
2018-06-24 19:55:13 +00:00
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 )
2018-04-29 01:26:53 +00:00
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 )
2018-06-24 19:55:13 +00:00
sizer . Add ( buttons_sizer , proportion = 1 , flag = wx . ALIGN_RIGHT | wx . ALIGN_BOTTOM )
2018-04-29 01:26:53 +00:00
2018-06-24 19:55:13 +00:00
panel . SetSizer ( sizer )
2018-04-29 01:26:53 +00:00
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 ) :
2018-06-24 19:55:13 +00:00
dialog = wx . DirDialog ( self , _ ( " Choose Inkscape directory " ) )
2018-04-29 01:26:53 +00:00
if dialog . ShowModal ( ) != wx . ID_CANCEL :
self . path_input . SetValue ( dialog . GetPath ( ) )
def install_button_clicked ( self , event ) :
try :
2018-06-24 19:55:13 +00:00
self . install_addons ( ' palettes ' )
self . install_addons ( ' symbols ' )
2018-04-29 01:26:53 +00:00
except Exception , e :
wx . MessageDialog ( self ,
2018-06-24 19:55:13 +00:00
_ ( ' Inkscape add-on installation failed ' ) + ' : \n ' + traceback . format_exc ( ) ,
2018-04-29 01:26:53 +00:00
_ ( ' Installation Failed ' ) ,
wx . OK ) . ShowModal ( )
else :
wx . MessageDialog ( self ,
2018-06-24 19:55:13 +00:00
_ ( ' Inkscape add-on files have been installed. Please restart Inkscape to load the new add-ons. ' ) ,
2018-04-29 01:26:53 +00:00
_ ( ' Installation Completed ' ) ,
wx . OK ) . ShowModal ( )
self . Destroy ( )
2018-06-24 19:55:13 +00:00
def install_addons ( self , type ) :
2018-06-29 00:32:09 +00:00
path = os . path . join ( self . path , type )
2018-07-05 01:16:49 +00:00
src_dir = get_bundled_dir ( type )
2018-06-24 19:55:13 +00:00
self . copy_files ( glob ( os . path . join ( src_dir , " * " ) ) , path )
2018-04-29 01:26:53 +00:00
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
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 )
2018-06-24 19:55:13 +00:00
class Install ( inkex . Effect ) :
2018-04-29 01:26:53 +00:00
def effect ( self ) :
app = wx . App ( )
2018-06-29 00:32:09 +00:00
installer_frame = InstallerFrame ( None , title = _ ( " Ink/Stitch Add-ons Installer " ) , size = ( 550 , 250 ) )
2018-04-29 01:26:53 +00:00
installer_frame . Show ( )
app . MainLoop ( )