chiark / gitweb /
Added 14.01.1 UM2 firmware to fix the abort-print bug. Updated "new version" dialog.
[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'):
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                         portNr = 0xCA00 + sum(map(ord, version.getVersion(False)))
35                         if len(files) > 0:
36                                 try:
37                                         other_hwnd = windll.user32.FindWindowA(None, ctypes.c_char_p('Cura - ' + version.getVersion()))
38                                         if other_hwnd != 0:
39                                                 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
40                                                 sock.sendto('\0'.join(files), ("127.0.0.1", portNr))
41
42                                                 windll.user32.SetForegroundWindow(other_hwnd)
43                                                 return
44                                 except:
45                                         pass
46
47                         socketListener = threading.Thread(target=self.Win32SocketListener, args=(portNr,))
48                         socketListener.daemon = True
49                         socketListener.start()
50
51                 if sys.platform.startswith('darwin'):
52                         #Do not show a splashscreen on OSX, as by Apple guidelines
53                         self.afterSplashCallback()
54                 else:
55                         from Cura.gui import splashScreen
56                         self.splash = splashScreen.splashScreen(self.afterSplashCallback)
57
58         def MacOpenFile(self, path):
59                 try:
60                         self.mainWindow.OnDropFiles([path])
61                 except Exception as e:
62                         warnings.warn("File at {p} cannot be read: {e}".format(p=path, e=str(e)))
63
64         def Win32SocketListener(self, port):
65                 import socket
66                 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
67                 sock.bind(("127.0.0.1", port))
68                 while True:
69                         data, addr = sock.recvfrom(2048)
70                         self.mainWindow.OnDropFiles(data.split('\0'))
71
72         def afterSplashCallback(self):
73                 #These imports take most of the time and thus should be done after showing the splashscreen
74                 import webbrowser
75                 from Cura.gui import mainWindow
76                 from Cura.gui import configWizard
77                 from Cura.gui import newVersionDialog
78                 from Cura.util import profile
79                 from Cura.util import resources
80                 from Cura.util import version
81
82                 resources.setupLocalization(profile.getPreference('language'))  # it's important to set up localization at very beginning to install _
83
84                 #If we do not have preferences yet, try to load it from a previous Cura install
85                 if profile.getMachineSetting('machine_type') == 'unknown':
86                         try:
87                                 otherCuraInstalls = profile.getAlternativeBasePaths()
88                                 otherCuraInstalls.sort()
89                                 if len(otherCuraInstalls) > 0:
90                                         profile.loadPreferences(os.path.join(otherCuraInstalls[-1], 'preferences.ini'))
91                                         profile.loadProfile(os.path.join(otherCuraInstalls[-1], 'current_profile.ini'))
92                         except:
93                                 import traceback
94                                 print traceback.print_exc()
95
96                 #If we haven't run it before, run the configuration wizard.
97                 if profile.getMachineSetting('machine_type') == 'unknown':
98                         if platform.system() == "Windows":
99                                 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
100                         else:
101                                 #Check if we need to copy our examples
102                                 exampleFile = os.path.expanduser('~/CuraExamples/UltimakerRobot_support.stl')
103                                 if not os.path.isfile(exampleFile):
104                                         try:
105                                                 os.makedirs(os.path.dirname(exampleFile))
106                                         except:
107                                                 pass
108                                         for filename in glob.glob(os.path.normpath(os.path.join(resources.resourceBasePath, 'example', '*.*'))):
109                                                 shutil.copy(filename, os.path.join(os.path.dirname(exampleFile), os.path.basename(filename)))
110                         self.loadFiles = [exampleFile]
111                         if self.splash is not None:
112                                 self.splash.Show(False)
113                         configWizard.configWizard()
114
115                 if profile.getPreference('check_for_updates') == 'True':
116                         newVersion = version.checkForNewerVersion()
117                         if newVersion is not None:
118                                 if self.splash is not None:
119                                         self.splash.Show(False)
120                                 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:
121                                         webbrowser.open(newVersion)
122                                         return
123                 if profile.getMachineSetting('machine_name') == '':
124                         return
125                 self.mainWindow = mainWindow.mainWindow()
126                 if self.splash is not None:
127                         self.splash.Show(False)
128                 self.mainWindow.Show()
129                 self.mainWindow.OnDropFiles(self.loadFiles)
130                 if profile.getPreference('last_run_version') != version.getVersion(False):
131                         profile.putPreference('last_run_version', version.getVersion(False))
132                         newVersionDialog.newVersionDialog().Show()
133
134                 setFullScreenCapable(self.mainWindow)
135
136 if platform.system() == "Darwin":
137         try:
138                 import ctypes, objc
139                 _objc = ctypes.PyDLL(objc._objc.__file__)
140
141                 # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
142                 _objc.PyObjCObject_New.restype = ctypes.py_object
143                 _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
144
145                 def setFullScreenCapable(frame):
146                         frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
147
148                         NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
149                         window = frameobj.window()
150                         newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
151                         window.setCollectionBehavior_(newBehavior)
152         except:
153                 def setFullScreenCapable(frame):
154                         pass
155
156 else:
157         def setFullScreenCapable(frame):
158                 pass