chiark / gitweb /
Prevent 2 instances of the same version starting on windows, and instead load the...
[cura.git] / Cura / gui / app.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import sys
5 import os
6 import platform
7 import shutil
8 import glob
9 import warnings
10
11 #Only import the _core to save import time
12 import wx._core
13
14
15 class CuraApp(wx.App):
16         def __init__(self, files):
17                 if platform.system() == "Windows" and not 'PYCHARM_HOSTED' in os.environ:
18                         super(CuraApp, self).__init__(redirect=True, filename='output.txt')
19                 else:
20                         super(CuraApp, self).__init__(redirect=False)
21
22                 self.mainWindow = None
23                 self.splash = None
24                 self.loadFiles = files
25                 
26                 if sys.platform.startswith('win') and len(files) > 0:
27                         #Check for an already running instance, if another instance is running load files in there
28                         from Cura.util import version
29                         from ctypes import windll
30                         import ctypes
31                         import socket
32                         import threading
33
34                         other_hwnd = windll.user32.FindWindowA(None, ctypes.c_char_p('Cura - ' + version.getVersion()))
35                         portNr = 0xCA00 + sum(map(ord, version.getVersion(False)))
36                         if other_hwnd != 0:
37                                 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
38                                 sock.sendto('\0'.join(files), ("127.0.0.1", portNr))
39
40                                 windll.user32.SetForegroundWindow(other_hwnd)
41                                 return
42
43                         socketListener = threading.Thread(target=self.Win32SocketListener, args=(portNr,))
44                         socketListener.daemon = True
45                         socketListener.start()
46
47                 if sys.platform.startswith('darwin'):
48                         #Do not show a splashscreen on OSX, as by Apple guidelines
49                         self.afterSplashCallback()
50                 else:
51                         from Cura.gui import splashScreen
52                         self.splash = splashScreen.splashScreen(self.afterSplashCallback)
53
54         def MacOpenFile(self, path):
55                 try:
56                         self.mainWindow.OnDropFiles([path])
57                 except Exception as e:
58                         warnings.warn("File at {p} cannot be read: {e}".format(p=path, e=str(e)))
59
60         def Win32SocketListener(self, port):
61                 import socket
62                 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
63                 sock.bind(("127.0.0.1", port))
64                 while True:
65                         data, addr = sock.recvfrom(2048)
66                         self.mainWindow.OnDropFiles(data.split('\0'))
67
68         def afterSplashCallback(self):
69                 #These imports take most of the time and thus should be done after showing the splashscreen
70                 import webbrowser
71                 from Cura.gui import mainWindow
72                 from Cura.gui import configWizard
73                 from Cura.util import profile
74                 from Cura.util import resources
75                 from Cura.util import version
76
77                 resources.setupLocalization(profile.getPreference('language'))  # it's important to set up localization at very beginning to install _
78
79                 #If we do not have preferences yet, try to load it from a previous Cura install
80                 if profile.getMachineSetting('machine_type') == 'unknown':
81                         try:
82                                 otherCuraInstalls = profile.getAlternativeBasePaths()
83                                 otherCuraInstalls.sort()
84                                 if len(otherCuraInstalls) > 0:
85                                         profile.loadPreferences(os.path.join(otherCuraInstalls[-1], 'preferences.ini'))
86                                         profile.loadProfile(os.path.join(otherCuraInstalls[-1], 'current_profile.ini'))
87                         except:
88                                 import traceback
89                                 print traceback.print_exc()
90
91                 #If we haven't run it before, run the configuration wizard.
92                 if profile.getMachineSetting('machine_type') == 'unknown':
93                         if platform.system() == "Windows":
94                                 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
95                         else:
96                                 #Check if we need to copy our examples
97                                 exampleFile = os.path.expanduser('~/CuraExamples/UltimakerRobot_support.stl')
98                                 if not os.path.isfile(exampleFile):
99                                         try:
100                                                 os.makedirs(os.path.dirname(exampleFile))
101                                         except:
102                                                 pass
103                                         for filename in glob.glob(os.path.normpath(os.path.join(resources.resourceBasePath, 'example', '*.*'))):
104                                                 shutil.copy(filename, os.path.join(os.path.dirname(exampleFile), os.path.basename(filename)))
105                         self.loadFiles = [exampleFile]
106                         if self.splash is not None:
107                                 self.splash.Show(False)
108                         configWizard.configWizard()
109
110                 if profile.getPreference('check_for_updates') == 'True':
111                         newVersion = version.checkForNewerVersion()
112                         if newVersion is not None:
113                                 if self.splash is not None:
114                                         self.splash.Show(False)
115                                 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:
116                                         webbrowser.open(newVersion)
117                                         return
118                 if profile.getMachineSetting('machine_name') == '':
119                         return
120                 self.mainWindow = mainWindow.mainWindow()
121                 if self.splash is not None:
122                         self.splash.Show(False)
123                 self.mainWindow.Show()
124                 self.mainWindow.OnDropFiles(self.loadFiles)
125
126                 setFullScreenCapable(self.mainWindow)
127
128 if platform.system() == "Darwin":
129         try:
130                 import ctypes, objc
131                 _objc = ctypes.PyDLL(objc._objc.__file__)
132
133                 # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
134                 _objc.PyObjCObject_New.restype = ctypes.py_object
135                 _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
136
137                 def setFullScreenCapable(frame):
138                         frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
139
140                         NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
141                         window = frameobj.window()
142                         newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
143                         window.setCollectionBehavior_(newBehavior)
144         except:
145                 def setFullScreenCapable(frame):
146                         pass
147
148 else:
149         def setFullScreenCapable(frame):
150                 pass