| 1 | from PyQt4.QtCore import * |
|---|
| 2 | from PyQt4.QtGui import * |
|---|
| 3 | from PyQt4.QtNetwork import * |
|---|
| 4 | from PyQt4.Qsci import * |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import sys |
|---|
| 8 | |
|---|
| 9 | from CQt.CQApplication import CQApplication |
|---|
| 10 | from EditorWindow import EditorWindow |
|---|
| 11 | |
|---|
| 12 | from DataSocketCommunicators import FileNameSender |
|---|
| 13 | |
|---|
| 14 | import sys, os, errno, tempfile |
|---|
| 15 | |
|---|
| 16 | from Messaging import stdMsg, dbgMsg, errMsg, setDebugging |
|---|
| 17 | |
|---|
| 18 | setDebugging(0) |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | def win_enum_callback_twedit(hwnd, results): |
|---|
| 24 | import win32gui |
|---|
| 25 | import re |
|---|
| 26 | exprChecker=re.compile(".*CC3D-Twedit$") |
|---|
| 27 | if exprChecker.match(win32gui.GetWindowText(hwnd)): |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | results.append(hwnd) |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | def showTweditWindowInForeground(): |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | import win32gui |
|---|
| 39 | import win32con |
|---|
| 40 | handle = [] |
|---|
| 41 | win32gui.EnumWindows(win_enum_callback_twedit, handle) |
|---|
| 42 | dbgMsg("this is twedit window handle,",handle[0] ) |
|---|
| 43 | if len(handle): |
|---|
| 44 | |
|---|
| 45 | win32gui.SetActiveWindow(handle[0]) |
|---|
| 46 | win32gui.BringWindowToTop(handle[0]) |
|---|
| 47 | win32gui.SetFocus(handle[0]) |
|---|
| 48 | win32gui.SetForegroundWindow(handle[0]) |
|---|
| 49 | win32gui.ShowWindow(handle[0],win32con.SW_RESTORE) |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | class Twedit(object): |
|---|
| 54 | def __init__(self): |
|---|
| 55 | |
|---|
| 56 | import sys |
|---|
| 57 | self.lockfile = os.path.normpath(tempfile.gettempdir() + '/' + os.path.basename(__file__) + '.lock') |
|---|
| 58 | if sys.platform == 'win32': |
|---|
| 59 | try: |
|---|
| 60 | |
|---|
| 61 | if(os.path.exists(self.lockfile)): |
|---|
| 62 | os.unlink(self.lockfile) |
|---|
| 63 | self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR) |
|---|
| 64 | except OSError, e: |
|---|
| 65 | if e.errno == 13: |
|---|
| 66 | dbgMsg("Another instance is already running, quitting.") |
|---|
| 67 | raise |
|---|
| 68 | |
|---|
| 69 | dbgMsg(e.errno) |
|---|
| 70 | raise |
|---|
| 71 | else: |
|---|
| 72 | import fcntl, sys |
|---|
| 73 | self.fp = open(self.lockfile, 'w') |
|---|
| 74 | try: |
|---|
| 75 | fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB) |
|---|
| 76 | except IOError: |
|---|
| 77 | dbgMsg("Another instance is already running, quitting.") |
|---|
| 78 | sys.exit(-1) |
|---|
| 79 | |
|---|
| 80 | def __del__(self): |
|---|
| 81 | import sys |
|---|
| 82 | if sys.platform == 'win32': |
|---|
| 83 | if hasattr(self, 'fd'): |
|---|
| 84 | os.close(self.fd) |
|---|
| 85 | os.unlink(self.lockfile) |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | def main(self,argv): |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | app = CQApplication(argv) |
|---|
| 94 | |
|---|
| 95 | self.mainWindow = EditorWindow() |
|---|
| 96 | self.mainWindow.setArgv(argv) |
|---|
| 97 | |
|---|
| 98 | self.mainWindow.show() |
|---|
| 99 | self.mainWindow.processCommandLine() |
|---|
| 100 | app.exec_() |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | if __name__ == '__main__': |
|---|
| 106 | |
|---|
| 107 | try: |
|---|
| 108 | twedit=Twedit() |
|---|
| 109 | except OSError,e: |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | for fileName in sys.argv[1:]: |
|---|
| 114 | datagram=fileName |
|---|
| 115 | |
|---|
| 116 | fileSender=FileNameSender(datagram) |
|---|
| 117 | fileSender.send() |
|---|
| 118 | |
|---|
| 119 | if sys.platform == 'win32': |
|---|
| 120 | showTweditWindowInForeground() |
|---|
| 121 | |
|---|
| 122 | sys.exit() |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | twedit.main(sys.argv[1:]) |
|---|
| 126 | |
|---|
| 127 | |
|---|