SystemDialog.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 traceback
  19. from .Desktop import Desktop
  20. from com.sun.star.ui.dialogs.TemplateDescription import \
  21. FILESAVE_AUTOEXTENSION, FILEOPEN_SIMPLE
  22. from com.sun.star.ui.dialogs.ExtendedFilePickerElementIds import \
  23. CHECKBOX_AUTOEXTENSION
  24. from com.sun.star.awt import WindowDescriptor
  25. from com.sun.star.awt.WindowClass import MODALTOP
  26. from com.sun.star.lang import IllegalArgumentException
  27. from com.sun.star.awt.VclWindowPeerAttribute import OK
  28. class SystemDialog(object):
  29. def __init__(self, xMSF, ServiceName, Type):
  30. try:
  31. self.xMSF = xMSF
  32. self.systemDialog = xMSF.createInstance(ServiceName)
  33. self.xStringSubstitution = self.createStringSubstitution(xMSF)
  34. # Add a name textbox to the filepicker
  35. if self.systemDialog is not None:
  36. if (hasattr(self.systemDialog, "initialize")):
  37. self.systemDialog.initialize((Type,))
  38. except Exception:
  39. traceback.print_exc()
  40. @classmethod
  41. def createStoreDialog(self, xmsf):
  42. return SystemDialog(
  43. xmsf, "com.sun.star.ui.dialogs.FilePicker",
  44. FILESAVE_AUTOEXTENSION)
  45. def subst(self, path):
  46. try:
  47. s = self.xStringSubstitution.substituteVariables(path, False)
  48. return s
  49. except Exception:
  50. traceback.print_exc()
  51. return path
  52. def callStoreDialog(self, displayDir, defaultName, sDocuType=None):
  53. if sDocuType is not None:
  54. self.addFilterToDialog(defaultName[-3:], sDocuType, True)
  55. self.sStorePath = None
  56. try:
  57. self.systemDialog.setValue(CHECKBOX_AUTOEXTENSION, 0, True)
  58. self.systemDialog.setDefaultName(defaultName)
  59. self.systemDialog.setDisplayDirectory(self.subst(displayDir))
  60. if self.execute(self.systemDialog):
  61. sPathList = self.systemDialog.getFiles()
  62. self.sStorePath = sPathList[0]
  63. except Exception:
  64. traceback.print_exc()
  65. return self.sStorePath
  66. def execute(self, execDialog):
  67. return execDialog.execute() == 1
  68. def addFilterToDialog(self, sExtension, filterName, setToDefault):
  69. try:
  70. #get the localized filtername
  71. uiName = self.getFilterUIName(filterName)
  72. pattern = "*." + sExtension
  73. #add the filter
  74. self.addFilter(uiName, pattern, setToDefault)
  75. except Exception:
  76. traceback.print_exc()
  77. def addFilter(self, uiName, pattern, setToDefault):
  78. try:
  79. self.systemDialog.appendFilter(uiName, pattern)
  80. if setToDefault:
  81. self.systemDialog.setCurrentFilter(uiName)
  82. except Exception:
  83. traceback.print_exc()
  84. '''
  85. note the result should go through conversion of the product name.
  86. @param filterName
  87. @return the UI localized name of the given filter name.
  88. '''
  89. def getFilterUIName(self, filterName):
  90. try:
  91. oFactory = self.xMSF.createInstance(
  92. "com.sun.star.document.FilterFactory")
  93. oObject = oFactory.getByName(filterName)
  94. xPropertyValue = list(oObject)
  95. for i in xPropertyValue:
  96. if i is not None and i.Name == "UIName":
  97. return str(i.Value).replace("%productname%", "LibreOffice")
  98. raise Exception(
  99. "UIName property not found for Filter " + filterName);
  100. except Exception:
  101. traceback.print_exc()
  102. return None
  103. @classmethod
  104. def showErrorBox(self, xMSF, sErrorMessage, AddTag=None, AddString=None):
  105. sErrorMessage = sErrorMessage.replace("%PRODUCTNAME", "LibreOffice" )
  106. sErrorMessage = sErrorMessage.replace(str(13), "<BR>")
  107. if AddTag and AddString:
  108. sErrorMessage = sErrorMessage.replace( AddString, AddTag)
  109. return self.showMessageBox(xMSF, "ErrorBox", OK, sErrorMessage)
  110. '''
  111. example:
  112. (xMSF, "ErrorBox", com.sun.star.awt.VclWindowPeerAttribute.OK, "message")
  113. @param windowServiceName one of the following strings:
  114. "ErrorBox", "WarningBox", "MessBox", "InfoBox", "QueryBox".
  115. There are other values possible, look
  116. under src/toolkit/source/awt/vcltoolkit.cxx
  117. @param windowAttribute see com.sun.star.awt.VclWindowPeerAttribute
  118. @return 0 = cancel, 1 = ok, 2 = yes, 3 = no(I'm not sure here)
  119. other values check for yourself ;-)
  120. '''
  121. @classmethod
  122. def showMessageBox(self, xMSF, windowServiceName, windowAttribute,
  123. MessageText, peer=None):
  124. if MessageText is None:
  125. return 0
  126. iMessage = 0
  127. try:
  128. # If the peer is null we try to get one from the desktop...
  129. if peer is None:
  130. xFrame = Desktop.getActiveFrame(xMSF)
  131. peer = xFrame.getComponentWindow()
  132. xToolkit = xMSF.createInstance("com.sun.star.awt.Toolkit")
  133. oDescriptor = WindowDescriptor()
  134. oDescriptor.WindowServiceName = windowServiceName
  135. oDescriptor.Parent = peer
  136. oDescriptor.Type = MODALTOP
  137. oDescriptor.WindowAttributes = windowAttribute
  138. xMsgPeer = xToolkit.createWindow(oDescriptor)
  139. xMsgPeer.MessageText = MessageText
  140. iMessage = xMsgPeer.execute()
  141. xMsgPeer.dispose()
  142. except Exception:
  143. traceback.print_exc()
  144. return iMessage
  145. @classmethod
  146. def createStringSubstitution(self, xMSF):
  147. xPathSubst = None
  148. try:
  149. xPathSubst = xMSF.createInstance(
  150. "com.sun.star.util.PathSubstitution")
  151. return xPathSubst
  152. except Exception:
  153. traceback.print_exc()
  154. return None