Desktop.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 com.sun.star.frame.FrameSearchFlag import ALL
  21. from com.sun.star.util import URL
  22. from com.sun.star.i18n.KParseTokens import ANY_LETTER_OR_NUMBER, ASC_UNDERSCORE
  23. class Desktop(object):
  24. @classmethod
  25. def getDesktop(self, xMSF):
  26. xDesktop = None
  27. if xMSF is not None:
  28. try:
  29. xDesktop = xMSF.createInstance( "com.sun.star.frame.Desktop")
  30. except Exception:
  31. traceback.print_exc()
  32. else:
  33. print ("Can't create a desktop. null pointer !")
  34. return xDesktop
  35. @classmethod
  36. def getActiveFrame(self, xMSF):
  37. xDesktop = self.getDesktop(xMSF)
  38. return xDesktop.getActiveFrame()
  39. @classmethod
  40. def getDispatcher(self, xMSF, xFrame, _stargetframe, oURL):
  41. try:
  42. xDispatch = xFrame.queryDispatch(oURL, _stargetframe, ALL)
  43. return xDispatch
  44. except Exception:
  45. traceback.print_exc()
  46. return None
  47. @classmethod
  48. def connect(self, connectStr):
  49. localContext = uno.getComponentContext()
  50. resolver = localContext.ServiceManager.createInstanceWithContext(
  51. "com.sun.star.bridge.UnoUrlResolver", localContext)
  52. ctx = resolver.resolve( connectStr )
  53. orb = ctx.ServiceManager
  54. return orb
  55. @classmethod
  56. def getIncrementSuffix(self, xElementContainer, sElementName):
  57. bElementexists = True
  58. i = 1
  59. sIncSuffix = ""
  60. BaseName = sElementName
  61. while bElementexists:
  62. try:
  63. bElementexists = xElementContainer.hasByName(sElementName)
  64. except:
  65. bElementexists = xElementContainer.hasByHierarchicalName(
  66. sElementName)
  67. if bElementexists:
  68. i += 1
  69. sElementName = BaseName + str(i)
  70. if i > 1:
  71. sIncSuffix = str(i)
  72. return sIncSuffix
  73. '''
  74. Checks if the passed Element Name already exists in the ElementContainer.
  75. If yes it appends a suffix to make it unique
  76. @param xElementContainer
  77. @param sElementName
  78. @return a unique Name ready to be added to the container.
  79. '''
  80. @classmethod
  81. def getUniqueName(self, xElementContainer, sElementName):
  82. sIncSuffix = self.getIncrementSuffix(xElementContainer, sElementName)
  83. return sElementName + sIncSuffix