Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 4.85 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import uno
  2. import unohelper
  3.  
  4. from com.sun.star.uno import RuntimeException as _rtex
  5.  
  6. # equivalent to Basic, for file URL only
  7. from uno import systemPathToFileUrl as convertToURL
  8. from uno import fileUrlToSystemPath as convertFromURL
  9.  
  10. from com.sun.star.beans import PropertyValue
  11. from com.sun.star.task import XJob
  12.  
  13. implementation_name = "org.openoffice.extensions.indesko.IdentifyMe"
  14. implementation_services = ("com.sun.star.task.Job",)
  15.  
  16. #### usefull helpers ####
  17.  
  18. import sys,os
  19. from com.sun.star.connection import NoConnectException
  20. from com.sun.star.beans import PropertyValue
  21.  
  22. class OOoTools:
  23.     """helper tools for using pyUNO"""
  24.        
  25.     #----------------------------------------
  26.     #   Danny's stuff to make programming less convenient.
  27.     #   http://www.oooforum.org/forum/viewtopic.phtml?t=9115
  28.     #----------------------------------------
  29.  
  30.     def __init__(self, ctx):
  31.         self.oCoreReflection = None
  32.         self.desktop = None
  33.         self.ctx = ctx
  34.         self.desktop = self.getDesktop()
  35.         return
  36.  
  37.     def getServiceManager( self ):
  38.         """Get the ServiceManager from the running OpenOffice.org.
  39.         """
  40.         return self.ctx.ServiceManager
  41.    
  42.     def createUnoService( self, cClass ):
  43.         """A handy way to create a global objects within the running OOo.
  44.         """
  45.         oServiceManager = self.getServiceManager()
  46.         oObj = oServiceManager.createInstance( cClass )
  47.         return oObj
  48.    
  49.     def getDesktop( self ):
  50.         """An easy way to obtain the Desktop object from a running OOo.
  51.         """
  52.         if self.desktop == None:
  53.             self.desktop = self.createUnoService("com.sun.star.frame.Desktop")
  54.         return self.desktop
  55.  
  56.     def getCoreReflection( self ):
  57.         if self.oCoreReflection == None:
  58.             self.oCoreReflection = self.createUnoService(
  59.                                     "com.sun.star.reflection.CoreReflection" )
  60.         return self.oCoreReflection
  61.        
  62.     def createUnoStruct( self, cTypeName ):
  63.         """Create a UNO struct and return it.
  64.         """
  65.         oCoreReflection = self.getCoreReflection()
  66.  
  67.         # Get the IDL class for the type name
  68.         oXIdlClass = oCoreReflection.forName( cTypeName )
  69.  
  70.         # Create the struct.
  71.         oReturnValue, oStruct = oXIdlClass.createObject( None )
  72.  
  73.         return oStruct
  74.  
  75.     def makePropertyValue( self, cName=None, uValue=None,
  76.                                  nHandle=None, nState=None ):
  77.         """Create a com.sun.star.beans.PropertyValue struct and return it.
  78.         """
  79.         oPropertyValue = self.createUnoStruct(
  80.                                     "com.sun.star.beans.PropertyValue" )
  81.  
  82.         if cName != None:
  83.             oPropertyValue.Name = cName
  84.         if uValue != None:
  85.             oPropertyValue.Value = uValue
  86.         if nHandle != None:
  87.             oPropertyValue.Handle = nHandle
  88.         if nState != None:
  89.             oPropertyValue.State = nState
  90.  
  91.         return oPropertyValue
  92.  
  93.     def getConfigSetting( self, target, forUpdate):
  94.         # retourne le noeud de config demande
  95.         if forUpdate:
  96.             service = "com.sun.star.configuration.ConfigurationUpdateAccess"
  97.         else:
  98.             service = "com.sun.star.configuration.ConfigurationAccess"                
  99.         aConfigProvider = self.createUnoService("com.sun.star.configuration.ConfigurationProvider" )
  100.         # aParams(0).Name = "nodepath"
  101.         # aParams(0).Value = target
  102.         aSettings = aConfigProvider.createInstanceWithArguments(service, (makePropertyValue("nodepath",target)) )
  103.         return aSettings
  104.  
  105. class IdentifyMe(unohelper.Base, XJob):
  106.  
  107.     def __init__ (self, ctx):
  108.  
  109.         self.ctx = ctx
  110.         self.tools = OOoTools(ctx)
  111.         self.desktop = self.tools.getDesktop()
  112.         self.config = self.tools.getConfigSetting()
  113.  
  114.     def execute(self, args):
  115.         #get the firstname and lastname in the User Data
  116.         aSettings = config("/org.openoffice.UserProfile/Data", true)    
  117.         currentFirstname=aSettings.getbyname("givenname")
  118.         currentSurname=aSettings.getbyname("sn")
  119.         if (not currentFirstname and not currentSurname):
  120.             #if both firstname and surname are empty
  121.             #get the windows login
  122.             windowsLogin = os.getenv("USERNAME")
  123.             if windowsLogin:
  124.                 aSettings.replaceByName("sn", windowsLogin)
  125.                 aSettings.commitChanges()
  126.         return
  127.  
  128. g_TypeTable = {}
  129. # pythonloader looks for a static g_ImplementationHelper variable
  130. g_ImplementationHelper = unohelper.ImplementationHelper ()
  131.  
  132. # add the FormatFactory class to the implementation container,
  133. # which the loader uses to register/instantiate the component.
  134. g_ImplementationHelper.addImplementation (IdentifyMe,
  135.                     implementation_name,
  136.                     implementation_services)