TextDocument.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. #
  18. import uno
  19. import traceback
  20. import time
  21. from datetime import date as dateTimeObject
  22. from .TextFieldHandler import TextFieldHandler
  23. from ..document.OfficeDocument import OfficeDocument
  24. from ..common.Desktop import Desktop
  25. from ..common.Configuration import Configuration
  26. from ..common.NumberFormatter import NumberFormatter
  27. from com.sun.star.i18n.NumberFormatIndex import DATE_SYS_DDMMYY
  28. from com.sun.star.view.DocumentZoomType import ENTIRE_PAGE
  29. from com.sun.star.beans.PropertyState import DIRECT_VALUE
  30. class TextDocument(object):
  31. def __init__(self, xMSF,listener=None,bShowStatusIndicator=None,
  32. FrameName=None,_sPreviewURL=None,_moduleIdentifier=None,
  33. _textDocument=None, xArgs=None):
  34. self.xMSF = xMSF
  35. self.xTextDocument = None
  36. if listener is not None:
  37. if FrameName is not None:
  38. '''creates an instance of TextDocument
  39. and creates a named frame.
  40. No document is actually loaded into this frame.'''
  41. self.xFrame = OfficeDocument.createNewFrame(
  42. xMSF, listener, FrameName)
  43. return
  44. elif _sPreviewURL is not None:
  45. '''creates an instance of TextDocument by
  46. loading a given URL as preview'''
  47. self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
  48. self.xTextDocument = self.loadAsPreview(_sPreviewURL, True)
  49. elif xArgs is not None:
  50. '''creates an instance of TextDocument
  51. and creates a frame and loads a document'''
  52. self.xDesktop = Desktop.getDesktop(xMSF);
  53. self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
  54. self.xTextDocument = OfficeDocument.load(
  55. self.xFrame, URL, "_self", xArgs);
  56. self.xWindowPeer = self.xFrame.getComponentWindow()
  57. self.m_xDocProps = self.xTextDocument.DocumentProperties
  58. CharLocale = self.xTextDocument.CharLocale
  59. return
  60. else:
  61. '''creates an instance of TextDocument from
  62. the desktop's current frame'''
  63. self.xDesktop = Desktop.getDesktop(xMSF);
  64. self.xFrame = self.xDesktop.getActiveFrame()
  65. self.xTextDocument = self.xFrame.getController().Model
  66. elif _moduleIdentifier is not None:
  67. try:
  68. '''create the empty document, and set its module identifier'''
  69. self.xTextDocument = xMSF.createInstance(
  70. "com.sun.star.text.TextDocument")
  71. self.xTextDocument.initNew()
  72. self.xTextDocument.setIdentifier(
  73. _moduleIdentifier.Identifier)
  74. # load the document into a blank frame
  75. xDesktop = Desktop.getDesktop(xMSF)
  76. loadArgs = list(range(1))
  77. loadArgs[0] = "Model"
  78. loadArgs[0] = -1
  79. loadArgs[0] = self.xTextDocument
  80. loadArgs[0] = DIRECT_VALUE
  81. xDesktop.loadComponentFromURL(
  82. "private:object", "_blank", 0, loadArgs)
  83. # remember some things for later usage
  84. self.xFrame = self.xTextDocument.CurrentController.Frame
  85. except Exception:
  86. traceback.print_exc()
  87. elif _textDocument is not None:
  88. '''creates an instance of TextDocument
  89. from a given XTextDocument'''
  90. self.xFrame = _textDocument.CurrentController.Frame
  91. self.xTextDocument = _textDocument
  92. if bShowStatusIndicator:
  93. self.showStatusIndicator()
  94. self.init()
  95. def init(self):
  96. self.xWindowPeer = self.xFrame.getComponentWindow()
  97. self.m_xDocProps = self.xTextDocument.DocumentProperties
  98. self.CharLocale = self.xTextDocument.CharLocale
  99. self.xText = self.xTextDocument.Text
  100. def showStatusIndicator(self):
  101. self.xProgressBar = self.xFrame.createStatusIndicator()
  102. self.xProgressBar.start("", 100)
  103. self.xProgressBar.setValue(5)
  104. def loadAsPreview(self, sDefaultTemplate, asTemplate):
  105. loadValues = list(range(3))
  106. # open document in the Preview mode
  107. loadValues[0] = uno.createUnoStruct(
  108. 'com.sun.star.beans.PropertyValue')
  109. loadValues[0].Name = "ReadOnly"
  110. loadValues[0].Value = True
  111. loadValues[1] = uno.createUnoStruct(
  112. 'com.sun.star.beans.PropertyValue')
  113. loadValues[1].Name = "AsTemplate"
  114. if asTemplate:
  115. loadValues[1].Value = True
  116. else:
  117. loadValues[1].Value = False
  118. loadValues[2] = uno.createUnoStruct(
  119. 'com.sun.star.beans.PropertyValue')
  120. loadValues[2].Name = "Preview"
  121. loadValues[2].Value = True
  122. self.xTextDocument = OfficeDocument.load(
  123. self.xFrame, sDefaultTemplate, "_self", loadValues)
  124. self.DocSize = self.getPageSize()
  125. try:
  126. self.xTextDocument.CurrentController.ViewSettings.ZoomType = ENTIRE_PAGE
  127. except Exception:
  128. traceback.print_exc()
  129. myFieldHandler = TextFieldHandler(self.xMSF, self.xTextDocument)
  130. myFieldHandler.updateDocInfoFields()
  131. return self.xTextDocument
  132. def getPageSize(self):
  133. try:
  134. xNameAccess = self.xTextDocument.StyleFamilies
  135. xPageStyleCollection = xNameAccess.getByName("PageStyles")
  136. xPageStyle = xPageStyleCollection.getByName("First Page")
  137. return xPageStyle.Size
  138. except Exception:
  139. traceback.print_exc()
  140. return None
  141. '''creates an instance of TextDocument and creates a
  142. frame and loads a document'''
  143. def createTextCursor(self, oCursorContainer):
  144. xTextCursor = oCursorContainer.createTextCursor()
  145. return xTextCursor
  146. def refresh(self):
  147. self.xTextDocument.refresh()
  148. '''
  149. This method sets the Author of a Wizard-generated template correctly
  150. and adds an explanatory sentence to the template description.
  151. @param WizardName The name of the Wizard.
  152. @param TemplateDescription The old Description which is being
  153. appended with another sentence.
  154. @return void.
  155. '''
  156. def setWizardTemplateDocInfo(self, WizardName, TemplateDescription):
  157. try:
  158. xNA = Configuration.getConfigurationRoot(
  159. self.xMSF, "/org.openoffice.UserProfile/Data", False)
  160. gn = xNA.getByName("givenname")
  161. sn = xNA.getByName("sn")
  162. fullname = str(gn) + " " + str(sn)
  163. now = time.localtime(time.time())
  164. year = time.strftime("%Y", now)
  165. month = time.strftime("%m", now)
  166. day = time.strftime("%d", now)
  167. dateObject = dateTimeObject(int(year), int(month), int(day))
  168. du = self.DateUtils(self.xMSF, self.xTextDocument)
  169. ff = du.getFormat(DATE_SYS_DDMMYY)
  170. myDate = du.format(ff, dateObject)
  171. xDocProps2 = self.xTextDocument.DocumentProperties
  172. xDocProps2.Author = fullname
  173. xDocProps2.ModifiedBy = fullname
  174. description = xDocProps2.Description
  175. description = description + " " + TemplateDescription
  176. description = description.replace("<wizard_name>", WizardName)
  177. description = description.replace("<current_date>", myDate)
  178. xDocProps2.Description = description
  179. except Exception:
  180. traceback.print_exc()
  181. @classmethod
  182. def getFrameByName(self, sFrameName, xTD):
  183. if xTD.TextFrames.hasByName(sFrameName):
  184. return xTD.TextFrames.getByName(sFrameName)
  185. return None
  186. def searchFillInItems(self, typeSearch):
  187. sd = self.xTextDocument.createSearchDescriptor()
  188. if typeSearch == 0:
  189. sd.setSearchString("<[^>]+>")
  190. elif typeSearch == 1:
  191. sd.setSearchString("#[^#]+#")
  192. sd.setPropertyValue("SearchRegularExpression", True)
  193. sd.setPropertyValue("SearchWords", True)
  194. auxList = []
  195. allItems = self.xTextDocument.findAll(sd)
  196. for i in list(range(allItems.Count)):
  197. auxList.append(allItems.getByIndex(i))
  198. return auxList
  199. class DateUtils(object):
  200. def __init__(self, xmsf, document):
  201. self.formatSupplier = document
  202. formatSettings = self.formatSupplier.getNumberFormatSettings()
  203. date = formatSettings.NullDate
  204. self.calendar = dateTimeObject(date.Year, date.Month, date.Day)
  205. self.formatter = NumberFormatter.createNumberFormatter(xmsf,
  206. self.formatSupplier)
  207. '''
  208. @param format a constant of the enumeration NumberFormatIndex
  209. @return
  210. '''
  211. def getFormat(self, format):
  212. return NumberFormatter.getNumberFormatterKey(
  213. self.formatSupplier, format)
  214. '''
  215. @param date a VCL date in form of 20041231
  216. @return a document relative date
  217. '''
  218. def format(self, formatIndex, date):
  219. difference = date - self.calendar
  220. return self.formatter.convertNumberToString(formatIndex,
  221. difference.days)