OfficeDocument.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. from unohelper import systemPathToFileUrl, absolutize
  21. from ..common.Desktop import Desktop
  22. from ..common.SystemDialog import SystemDialog
  23. from com.sun.star.awt import WindowDescriptor
  24. from com.sun.star.awt import Rectangle
  25. from com.sun.star.awt.WindowClass import TOP
  26. from com.sun.star.task import ErrorCodeIOException
  27. #Window Constants
  28. com_sun_star_awt_WindowAttribute_BORDER \
  29. = uno.getConstantByName( "com.sun.star.awt.WindowAttribute.BORDER" )
  30. com_sun_star_awt_WindowAttribute_SIZEABLE \
  31. = uno.getConstantByName( "com.sun.star.awt.WindowAttribute.SIZEABLE" )
  32. com_sun_star_awt_WindowAttribute_MOVEABLE \
  33. = uno.getConstantByName( "com.sun.star.awt.WindowAttribute.MOVEABLE" )
  34. com_sun_star_awt_VclWindowPeerAttribute_CLIPCHILDREN \
  35. = uno.getConstantByName(
  36. "com.sun.star.awt.VclWindowPeerAttribute.CLIPCHILDREN" )
  37. class OfficeDocument(object):
  38. '''Creates a new instance of OfficeDocument '''
  39. def __init__(self, _xMSF):
  40. self.xMSF = _xMSF
  41. @classmethod
  42. def attachEventCall(self, xComponent, EventName, EventType, EventURL):
  43. try:
  44. oEventProperties = list(range(2))
  45. oEventProperties[0] = uno.createUnoStruct(
  46. 'com.sun.star.beans.PropertyValue')
  47. oEventProperties[0].Name = "EventType"
  48. oEventProperties[0].Value = EventType
  49. # "Service", "StarBasic"
  50. oEventProperties[1] = uno.createUnoStruct(
  51. 'com.sun.star.beans.PropertyValue')
  52. oEventProperties[1].Name = "Script" #"URL";
  53. oEventProperties[1].Value = EventURL
  54. uno.invoke(xComponent.Events, "replaceByName",
  55. (EventName, uno.Any("[]com.sun.star.beans.PropertyValue",
  56. tuple(oEventProperties))))
  57. except Exception:
  58. traceback.print_exc()
  59. def dispose(self, xMSF, xComponent):
  60. try:
  61. if xComponent is not None:
  62. xFrame = xComponent.CurrentController.Frame
  63. if xComponent.isModified():
  64. xComponent.setModified(False)
  65. Desktop.dispatchURL(xMSF, ".uno:CloseDoc", xFrame)
  66. except Exception:
  67. traceback.print_exc()
  68. @classmethod
  69. def createNewFrame(self, xMSF, listener, FrameName="_blank"):
  70. xFrame = None
  71. if FrameName.lower() == "WIZARD_LIVE_PREVIEW".lower():
  72. xFrame = self.createNewPreviewFrame(xMSF, listener)
  73. else:
  74. xF = Desktop.getDesktop(xMSF)
  75. xFrame = xF.findFrame(FrameName, 0)
  76. if listener is not None:
  77. xFF = xF.getFrames()
  78. xFF.remove(xFrame)
  79. xF.addTerminateListener(listener)
  80. return xFrame
  81. @classmethod
  82. def createNewPreviewFrame(self, xMSF, listener):
  83. xToolkit = None
  84. try:
  85. xToolkit = xMSF.createInstance("com.sun.star.awt.Toolkit")
  86. except Exception:
  87. # TODO Auto-generated catch block
  88. traceback.print_exc()
  89. #describe the window and its properties
  90. aDescriptor = WindowDescriptor()
  91. aDescriptor.Type = TOP
  92. aDescriptor.WindowServiceName = "window"
  93. aDescriptor.ParentIndex = -1
  94. aDescriptor.Parent = None
  95. aDescriptor.Bounds = Rectangle(10, 10, 640, 480)
  96. #Set Window Attributes
  97. gnDefaultWindowAttributes = \
  98. com_sun_star_awt_WindowAttribute_BORDER + \
  99. com_sun_star_awt_WindowAttribute_MOVEABLE + \
  100. com_sun_star_awt_WindowAttribute_SIZEABLE + \
  101. com_sun_star_awt_VclWindowPeerAttribute_CLIPCHILDREN
  102. aDescriptor.WindowAttributes = gnDefaultWindowAttributes
  103. #create a new blank container window
  104. xPeer = None
  105. try:
  106. xPeer = xToolkit.createWindow(aDescriptor)
  107. except Exception:
  108. traceback.print_exc()
  109. #define some further properties of the frame window
  110. #if it's needed .-)
  111. #xPeer->setBackground(...);
  112. #create new empty frame and set window on it
  113. xFrame = None
  114. try:
  115. xFrame = xMSF.createInstance("com.sun.star.frame.Frame")
  116. except Exception:
  117. traceback.print_exc()
  118. xFrame.initialize(xPeer)
  119. #from now this frame is usable ...
  120. #and not part of the desktop tree.
  121. #You are alone with him .-)
  122. if listener is not None:
  123. Desktop.getDesktop(xMSF).addTerminateListener(listener)
  124. return xFrame
  125. @classmethod
  126. def load(self, xInterface, sURL, sFrame, xValues):
  127. xComponent = None
  128. try:
  129. if not sURL.startswith("file://"):
  130. sURL = systemPathToFileUrl(sURL)
  131. xComponent = xInterface.loadComponentFromURL(
  132. sURL, sFrame, 0, tuple(xValues))
  133. except Exception:
  134. traceback.print_exc()
  135. return xComponent
  136. @classmethod
  137. def store(self, xMSF, xComponent, StorePath, FilterName):
  138. try:
  139. if len(FilterName):
  140. oStoreProperties = list(range(2))
  141. oStoreProperties[0] = uno.createUnoStruct(
  142. 'com.sun.star.beans.PropertyValue')
  143. oStoreProperties[0].Name = "FilterName"
  144. oStoreProperties[0].Value = FilterName
  145. oStoreProperties[1] = uno.createUnoStruct(
  146. 'com.sun.star.beans.PropertyValue')
  147. oStoreProperties[1].Name = "InteractionHandler"
  148. oStoreProperties[1].Value = xMSF.createInstance(
  149. "com.sun.star.comp.uui.UUIInteractionHandler")
  150. else:
  151. oStoreProperties = list(range(0))
  152. StorePath = systemPathToFileUrl(StorePath)
  153. sPath = StorePath[:(StorePath.rfind("/") + 1)]
  154. sFile = StorePath[(StorePath.rfind("/") + 1):]
  155. xComponent.storeToURL(
  156. absolutize(sPath, sFile), tuple(oStoreProperties))
  157. return True
  158. except ErrorCodeIOException:
  159. #Throw this exception when trying to save a file
  160. #which is already opened in Libreoffice
  161. #TODO: handle it properly
  162. return True
  163. pass
  164. except Exception:
  165. traceback.print_exc()
  166. return False
  167. def close(self, xComponent):
  168. bState = False
  169. if xComponent is not None:
  170. try:
  171. xComponent.close(True)
  172. bState = True
  173. except Exception:
  174. print ("could not close doc")
  175. bState = False
  176. else:
  177. bState = True
  178. return bState
  179. def showMessageBox(
  180. self, xMSF, windowServiceName, windowAttribute, MessageText):
  181. return SystemDialog.showMessageBox(
  182. xMSF, windowServiceName, windowAttribute, MessageText)