|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a thread class populating and updating the QtHelp |
|
8 documentation database. |
|
9 """ |
|
10 |
|
11 from __future__ import unicode_literals |
|
12 |
|
13 import os |
|
14 |
|
15 from PyQt5.QtCore import pyqtSignal, qVersion, QThread, Qt, QMutex, \ |
|
16 QDateTime, QDir, QLibraryInfo, QFileInfo |
|
17 from PyQt5.QtHelp import QHelpEngineCore |
|
18 |
|
19 from eric6config import getConfig |
|
20 |
|
21 |
|
22 class HelpDocsInstaller(QThread): |
|
23 """ |
|
24 Class implementing the worker thread populating and updating the QtHelp |
|
25 documentation database. |
|
26 |
|
27 @signal errorMessage(str) emitted, if an error occurred during |
|
28 the installation of the documentation |
|
29 @signal docsInstalled(bool) emitted after the installation has finished |
|
30 """ |
|
31 errorMessage = pyqtSignal(str) |
|
32 docsInstalled = pyqtSignal(bool) |
|
33 |
|
34 def __init__(self, collection): |
|
35 """ |
|
36 Constructor |
|
37 |
|
38 @param collection full pathname of the collection file (string) |
|
39 """ |
|
40 super(HelpDocsInstaller, self).__init__() |
|
41 |
|
42 self.__abort = False |
|
43 self.__collection = collection |
|
44 self.__mutex = QMutex() |
|
45 |
|
46 def stop(self): |
|
47 """ |
|
48 Public slot to stop the installation procedure. |
|
49 """ |
|
50 if not self.isRunning(): |
|
51 return |
|
52 |
|
53 self.__mutex.lock() |
|
54 self.__abort = True |
|
55 self.__mutex.unlock() |
|
56 self.wait() |
|
57 |
|
58 def installDocs(self): |
|
59 """ |
|
60 Public method to start the installation procedure. |
|
61 """ |
|
62 self.start(QThread.LowPriority) |
|
63 |
|
64 def run(self): |
|
65 """ |
|
66 Public method executed by the thread. |
|
67 """ |
|
68 engine = QHelpEngineCore(self.__collection) |
|
69 engine.setupData() |
|
70 changes = False |
|
71 |
|
72 qt4Docs = ["designer", "linguist", "qt"] |
|
73 qt5Docs = [ |
|
74 "activeqt", "qdoc", "qmake", "qt3d", "qt3drenderer", |
|
75 "qtandroidextras", "qtassistant", "qtbluetooth", "qtcanvas3d", |
|
76 "qtconcurrent", "qtcore", "qtdbus", "qtdesigner", "qtdoc", |
|
77 "qtenginio", "qtenginiooverview", "qtenginoqml", |
|
78 "qtgraphicaleffects", "qtgui", "qthelp", "qtimageformats", |
|
79 "qtlabscontrols", "qtlinguist", "qtlocation", "qtmaxextras", |
|
80 "qtmultimedia", "qtmultimediawidgets", "qtnetwork", "qtnfc", |
|
81 "qtopengl", "qtplatformheaders", "qtpositioning", "qtprintsupport", |
|
82 "qtqml", "qtquick", "qtquickcontrols", "qtquickdialogs", |
|
83 "qtquickextras", "qtquicklayouts", "qtscript", "qtscripttools", |
|
84 "qtsensors", "qtserialbus", "qtserialport", "qtsql", "qtsvg", |
|
85 "qttestlib", "qtuitools", "qtwebchannel", "qtwebengine", |
|
86 "qtwebenginewidgets", "qtwebkit", "qtwebkitexamples", |
|
87 "qtwebsockets", "qtwebview", "qtwidgets", "qtwinextras", |
|
88 "qtx11extras", "qtxml", "qtxmlpatterns"] |
|
89 for qtDocs, version in [(qt4Docs, 4), (qt5Docs, 5)]: |
|
90 for doc in qtDocs: |
|
91 changes |= self.__installQtDoc(doc, version, engine) |
|
92 self.__mutex.lock() |
|
93 if self.__abort: |
|
94 engine = None |
|
95 self.__mutex.unlock() |
|
96 return |
|
97 self.__mutex.unlock() |
|
98 |
|
99 changes |= self.__installEric6Doc(engine) |
|
100 engine = None |
|
101 del engine |
|
102 self.docsInstalled.emit(changes) |
|
103 |
|
104 def __installQtDoc(self, name, version, engine): |
|
105 """ |
|
106 Private method to install/update a Qt help document. |
|
107 |
|
108 @param name name of the Qt help document (string) |
|
109 @param version Qt version of the help documens (integer) |
|
110 @param engine reference to the help engine (QHelpEngineCore) |
|
111 @return flag indicating success (boolean) |
|
112 """ |
|
113 versionKey = "qt_version_{0}@@{1}".format(version, name) |
|
114 info = engine.customValue(versionKey, "") |
|
115 lst = info.split('|') |
|
116 |
|
117 dt = QDateTime() |
|
118 if len(lst) and lst[0]: |
|
119 dt = QDateTime.fromString(lst[0], Qt.ISODate) |
|
120 |
|
121 qchFile = "" |
|
122 if len(lst) == 2: |
|
123 qchFile = lst[1] |
|
124 |
|
125 if version == 4: |
|
126 docsPath = QDir( |
|
127 QLibraryInfo.location(QLibraryInfo.DocumentationPath) + |
|
128 QDir.separator() + "qch") |
|
129 elif version == 5: |
|
130 docsPath = QLibraryInfo.location(QLibraryInfo.DocumentationPath) |
|
131 if not os.path.isdir(docsPath) or \ |
|
132 len(QDir(docsPath).entryList(["*.qch"])) == 0: |
|
133 # Qt installer is a bit buggy; it's missing a symbolic link |
|
134 docsPathList = QDir.fromNativeSeparators(docsPath).split("/") |
|
135 docsPath = os.sep.join( |
|
136 docsPathList[:-3] + |
|
137 ["Docs", "Qt-{0}".format(qVersion()[:3])]) |
|
138 docsPath = QDir(docsPath) |
|
139 else: |
|
140 # unsupported Qt version |
|
141 return False |
|
142 |
|
143 files = docsPath.entryList(["*.qch"]) |
|
144 if not files: |
|
145 engine.setCustomValue( |
|
146 versionKey, |
|
147 QDateTime().toString(Qt.ISODate) + '|') |
|
148 return False |
|
149 |
|
150 for f in files: |
|
151 if f.startswith(name + "."): |
|
152 fi = QFileInfo(docsPath.absolutePath() + QDir.separator() + f) |
|
153 namespace = QHelpEngineCore.namespaceName( |
|
154 fi.absoluteFilePath()) |
|
155 if not namespace: |
|
156 continue |
|
157 |
|
158 if dt.isValid() and \ |
|
159 namespace in engine.registeredDocumentations() and \ |
|
160 fi.lastModified().toString(Qt.ISODate) == \ |
|
161 dt.toString(Qt.ISODate) and \ |
|
162 qchFile == fi.absoluteFilePath(): |
|
163 return False |
|
164 |
|
165 if namespace in engine.registeredDocumentations(): |
|
166 engine.unregisterDocumentation(namespace) |
|
167 |
|
168 if not engine.registerDocumentation(fi.absoluteFilePath()): |
|
169 self.errorMessage.emit( |
|
170 self.tr( |
|
171 """<p>The file <b>{0}</b> could not be""" |
|
172 """ registered. <br/>Reason: {1}</p>""") |
|
173 .format(fi.absoluteFilePath, engine.error()) |
|
174 ) |
|
175 return False |
|
176 |
|
177 engine.setCustomValue( |
|
178 versionKey, |
|
179 fi.lastModified().toString(Qt.ISODate) + '|' + |
|
180 fi.absoluteFilePath()) |
|
181 return True |
|
182 |
|
183 return False |
|
184 |
|
185 def __installEric6Doc(self, engine): |
|
186 """ |
|
187 Private method to install/update the eric6 help documentation. |
|
188 |
|
189 @param engine reference to the help engine (QHelpEngineCore) |
|
190 @return flag indicating success (boolean) |
|
191 """ |
|
192 versionKey = "eric6_ide" |
|
193 info = engine.customValue(versionKey, "") |
|
194 lst = info.split('|') |
|
195 |
|
196 dt = QDateTime() |
|
197 if len(lst) and lst[0]: |
|
198 dt = QDateTime.fromString(lst[0], Qt.ISODate) |
|
199 |
|
200 qchFile = "" |
|
201 if len(lst) == 2: |
|
202 qchFile = lst[1] |
|
203 |
|
204 docsPath = QDir(getConfig("ericDocDir") + QDir.separator() + "Help") |
|
205 |
|
206 files = docsPath.entryList(["*.qch"]) |
|
207 if not files: |
|
208 engine.setCustomValue( |
|
209 versionKey, QDateTime().toString(Qt.ISODate) + '|') |
|
210 return False |
|
211 |
|
212 for f in files: |
|
213 if f == "source.qch": |
|
214 fi = QFileInfo(docsPath.absolutePath() + QDir.separator() + f) |
|
215 namespace = QHelpEngineCore.namespaceName( |
|
216 fi.absoluteFilePath()) |
|
217 if not namespace: |
|
218 continue |
|
219 |
|
220 if dt.isValid() and \ |
|
221 namespace in engine.registeredDocumentations() and \ |
|
222 fi.lastModified().toString(Qt.ISODate) == \ |
|
223 dt.toString(Qt.ISODate) and \ |
|
224 qchFile == fi.absoluteFilePath(): |
|
225 return False |
|
226 |
|
227 if namespace in engine.registeredDocumentations(): |
|
228 engine.unregisterDocumentation(namespace) |
|
229 |
|
230 if not engine.registerDocumentation(fi.absoluteFilePath()): |
|
231 self.errorMessage.emit( |
|
232 self.tr( |
|
233 """<p>The file <b>{0}</b> could not be""" |
|
234 """ registered. <br/>Reason: {1}</p>""") |
|
235 .format(fi.absoluteFilePath, engine.error()) |
|
236 ) |
|
237 return False |
|
238 |
|
239 engine.setCustomValue( |
|
240 versionKey, |
|
241 fi.lastModified().toString(Qt.ISODate) + '|' + |
|
242 fi.absoluteFilePath()) |
|
243 return True |
|
244 |
|
245 return False |