TextFieldHandler.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. import time
  20. from com.sun.star.util import DateTime
  21. from com.sun.star.uno import RuntimeException
  22. from com.sun.star.beans import UnknownPropertyException
  23. class TextFieldHandler(object):
  24. xTextFieldsSupplierAux = None
  25. arrayTextFields = []
  26. dictTextFields = {}
  27. def __init__(self, xMSF, xTextDocument):
  28. self.xMSFDoc = xMSF
  29. self.xTextFieldsSupplier = xTextDocument
  30. if TextFieldHandler.xTextFieldsSupplierAux is not \
  31. self.xTextFieldsSupplier:
  32. self.__getTextFields()
  33. TextFieldHandler.xTextFieldsSupplierAux = self.xTextFieldsSupplier
  34. def refreshTextFields(self):
  35. xUp = self.xTextFieldsSupplier.TextFields
  36. xUp.refresh()
  37. def __getTextFields(self):
  38. try:
  39. if self.xTextFieldsSupplier.TextFields.hasElements():
  40. xEnum = \
  41. self.xTextFieldsSupplier.TextFields.createEnumeration()
  42. while xEnum.hasMoreElements():
  43. oTextField = xEnum.nextElement()
  44. TextFieldHandler.arrayTextFields.append(oTextField)
  45. xPropertySet = oTextField.TextFieldMaster
  46. if xPropertySet.Name:
  47. TextFieldHandler.dictTextFields[xPropertySet.Name] = \
  48. oTextField
  49. except Exception:
  50. traceback.print_exc()
  51. def changeUserFieldContent(self, _FieldName, _FieldContent):
  52. try:
  53. DependentTextFields = \
  54. TextFieldHandler.dictTextFields[_FieldName]
  55. except KeyError:
  56. return None
  57. try:
  58. if hasattr(DependentTextFields, "TextFieldMaster"):
  59. DependentTextFields.TextFieldMaster.Content = _FieldContent
  60. self.refreshTextFields()
  61. except UnknownPropertyException:
  62. pass
  63. def updateDocInfoFields(self):
  64. try:
  65. for i in TextFieldHandler.arrayTextFields:
  66. if i.supportsService(
  67. "com.sun.star.text.TextField.ExtendedUser"):
  68. i.update()
  69. if i.supportsService(
  70. "com.sun.star.text.TextField.User"):
  71. i.update()
  72. except Exception:
  73. traceback.print_exc()
  74. def updateDateFields(self):
  75. try:
  76. now = time.localtime(time.time())
  77. dt = DateTime()
  78. dt.Day = time.strftime("%d", now)
  79. dt.Year = time.strftime("%Y", now)
  80. dt.Month = time.strftime("%m", now)
  81. dt.Month += 1
  82. for i in TextFieldHandler.arrayTextFields:
  83. if i.supportsService(
  84. "com.sun.star.text.TextField.DateTime"):
  85. try:
  86. i.IsFixed = False
  87. i.DateTimeValue = dt
  88. except RuntimeException:
  89. pass
  90. except Exception:
  91. traceback.print_exc()
  92. def removeUserFieldByContent(self):
  93. #Remove userfield when its text is empty
  94. xDependentTextFields = TextFieldHandler.arrayTextFields
  95. for i in xDependentTextFields:
  96. try:
  97. if not i.TextFieldMaster.Content:
  98. i.dispose()
  99. except Exception:
  100. #TextField doesn't even have the attribute Content,
  101. #so it's empty
  102. i.dispose()