chiark / gitweb /
Prevent 2 instances of the same version starting on windows, and instead load the...
[cura.git] / Cura / gui / app.py
index 048d03d3ed5d6a0a199c906a2121a8152c58416f..e557c2eca61b2e10a3eea9e5e6143afd5962e23a 100644 (file)
@@ -11,16 +11,38 @@ import warnings
 #Only import the _core to save import time
 import wx._core
 
+
 class CuraApp(wx.App):
        def __init__(self, files):
                if platform.system() == "Windows" and not 'PYCHARM_HOSTED' in os.environ:
-                       super(CuraApp, self).__init__(redirect = True, filename = 'output.txt')
+                       super(CuraApp, self).__init__(redirect=True, filename='output.txt')
                else:
-                       super(CuraApp, self).__init__(redirect = False)
+                       super(CuraApp, self).__init__(redirect=False)
 
                self.mainWindow = None
                self.splash = None
                self.loadFiles = files
+               
+               if sys.platform.startswith('win') and len(files) > 0:
+                       #Check for an already running instance, if another instance is running load files in there
+                       from Cura.util import version
+                       from ctypes import windll
+                       import ctypes
+                       import socket
+                       import threading
+
+                       other_hwnd = windll.user32.FindWindowA(None, ctypes.c_char_p('Cura - ' + version.getVersion()))
+                       portNr = 0xCA00 + sum(map(ord, version.getVersion(False)))
+                       if other_hwnd != 0:
+                               sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+                               sock.sendto('\0'.join(files), ("127.0.0.1", portNr))
+
+                               windll.user32.SetForegroundWindow(other_hwnd)
+                               return
+
+                       socketListener = threading.Thread(target=self.Win32SocketListener, args=(portNr,))
+                       socketListener.daemon = True
+                       socketListener.start()
 
                if sys.platform.startswith('darwin'):
                        #Do not show a splashscreen on OSX, as by Apple guidelines
@@ -35,6 +57,14 @@ class CuraApp(wx.App):
                except Exception as e:
                        warnings.warn("File at {p} cannot be read: {e}".format(p=path, e=str(e)))
 
+       def Win32SocketListener(self, port):
+               import socket
+               sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+               sock.bind(("127.0.0.1", port))
+               while True:
+                       data, addr = sock.recvfrom(2048)
+                       self.mainWindow.OnDropFiles(data.split('\0'))
+
        def afterSplashCallback(self):
                #These imports take most of the time and thus should be done after showing the splashscreen
                import webbrowser
@@ -44,8 +74,22 @@ class CuraApp(wx.App):
                from Cura.util import resources
                from Cura.util import version
 
+               resources.setupLocalization(profile.getPreference('language'))  # it's important to set up localization at very beginning to install _
+
+               #If we do not have preferences yet, try to load it from a previous Cura install
+               if profile.getMachineSetting('machine_type') == 'unknown':
+                       try:
+                               otherCuraInstalls = profile.getAlternativeBasePaths()
+                               otherCuraInstalls.sort()
+                               if len(otherCuraInstalls) > 0:
+                                       profile.loadPreferences(os.path.join(otherCuraInstalls[-1], 'preferences.ini'))
+                                       profile.loadProfile(os.path.join(otherCuraInstalls[-1], 'current_profile.ini'))
+                       except:
+                               import traceback
+                               print traceback.print_exc()
+
                #If we haven't run it before, run the configuration wizard.
-               if profile.getPreference('machine_type') == 'unknown':
+               if profile.getMachineSetting('machine_type') == 'unknown':
                        if platform.system() == "Windows":
                                exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
                        else:
@@ -68,9 +112,11 @@ class CuraApp(wx.App):
                        if newVersion is not None:
                                if self.splash is not None:
                                        self.splash.Show(False)
-                               if wx.MessageBox('A new version of Cura is available, would you like to download?', 'New version available', wx.YES_NO | wx.ICON_INFORMATION) == wx.YES:
+                               if wx.MessageBox(_("A new version of Cura is available, would you like to download?"), _("New version available"), wx.YES_NO | wx.ICON_INFORMATION) == wx.YES:
                                        webbrowser.open(newVersion)
                                        return
+               if profile.getMachineSetting('machine_name') == '':
+                       return
                self.mainWindow = mainWindow.mainWindow()
                if self.splash is not None:
                        self.splash.Show(False)
@@ -80,20 +126,24 @@ class CuraApp(wx.App):
                setFullScreenCapable(self.mainWindow)
 
 if platform.system() == "Darwin":
-       import ctypes, objc
-       _objc = ctypes.PyDLL(objc._objc.__file__)
+       try:
+               import ctypes, objc
+               _objc = ctypes.PyDLL(objc._objc.__file__)
 
-       # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
-       _objc.PyObjCObject_New.restype = ctypes.py_object
-       _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
+               # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
+               _objc.PyObjCObject_New.restype = ctypes.py_object
+               _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
 
-       def setFullScreenCapable(frame):
-               frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
+               def setFullScreenCapable(frame):
+                       frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
 
-               NSWindowCollectionBehaviorFullScreenPrimary = 1<<7
-               window = frameobj.window()
-               newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
-               window.setCollectionBehavior_(newBehavior)
+                       NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
+                       window = frameobj.window()
+                       newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
+                       window.setCollectionBehavior_(newBehavior)
+       except:
+               def setFullScreenCapable(frame):
+                       pass
 
 else:
        def setFullScreenCapable(frame):