Bug 119519 - Basic: insert graphicobject via ThisComponent.createInstance("com.sun.star.text.GraphicObject") not working in 6.1.0.3
Summary: Basic: insert graphicobject via ThisComponent.createInstance("com.sun.star.te...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
6.1.0.3 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: imageHandling-regressions
  Show dependency treegraph
 
Reported: 2018-08-26 19:24 UTC by Kai Struck
Modified: 2018-08-30 12:36 UTC (History)
6 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kai Struck 2018-08-26 19:24:54 UTC
Description:
Tying to insert a linked graphicobject via ThisComponent.createInstance("com.sun.star.text.GraphicObject")
and 
oText.insertTextContent(oCursor, oGraph, False)
leads to an empty image frame (loading error)

Steps to Reproduce:
Customize the image url and run this Basic code:
Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("C:\Users\username\image.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 
 With oGraph
 .GraphicURL = sURL
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)
End Sub

Actual Results:
An empty image frame appears saying "Loading Error"

Expected Results:
The image should be inserted correctly and appear.


Reproducible: Always


User Profile Reset: No



Additional Info:
Comment 1 Drew Jensen 2018-08-26 20:36:04 UTC
Confirmed with Ubuntu 18.04 and 
Version: 6.1.1.0.0+
Build ID: 30c178dcb3301527ad92bbd245d1525ab77e314e

The macro works as expected in 6.0.5 and 6.2Alpha
Comment 2 Drew Jensen 2018-08-26 20:42:31 UTC
This could be related to a resolved issue from the changes to how images are handled.

See: https://bugs.documentfoundation.org/show_bug.cgi?id=117427
Comment 3 Drew Jensen 2018-08-26 20:44:55 UTC
Will add Tomaž Vajngerl to CC, perhaps he would know on that.
Comment 4 Xisco Faulí 2018-08-27 14:23:55 UTC
@Raal, is it the same case you investigated with Miklos in the Hamburg Hackfest?
Comment 5 Xisco Faulí 2018-08-27 15:17:55 UTC
Kai Struck, Could you please read the comments in bug 117427 to help you adapt your code?
Comment 6 Kai Struck 2018-08-28 09:52:54 UTC
Xisco Faulí,

Ok, after reading it and using the function getGraphFromUrlAsLink
for oGraph.Graphic I can make the above example code work: 


Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 oGraph.Graphic = getgraphfromurlaslink(sURL)  
 With oGraph
rem .GraphicURL = sURL
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)

End Sub

function getGraphFromUrlAsLink(sFileUrl as String ) as Object 
  oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") 
  Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue 
  oPropsIN(0).Name  = "URL" 
  oPropsIN(0).Value = converttoURL(sFileUrl)
  oPropsIN(1).Name  = "LoadAsLink" 
  oPropsIN(1).Value = TRUE
  getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) 
 end function


To get the URL of the image we now must use
oGraph.Graphic.OriginURL instead of oGraph.GraphicURL

If that means that all older code has to be adapted (possibly with a version check to make it compatible with older versions) then I'm not too happy about that.
Comment 7 Kai Struck 2018-08-28 10:10:01 UTC
With simple version check for compatibility for older Versions (just for info, still hoping not being forced to use it):
Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 
if Officeversion="LibreOffice6.1" then 
oGraph.Graphic = getgraphfromurlaslink(sURL) 
else
oGraph.GraphicURL = sURL
end if 

 With oGraph
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)
 
if Officeversion="LibreOffice6.1" then 
msgbox oGraph.Graphic.OriginURL
else
msgbox oGraph.GraphicURL
end if 

End Sub

function getGraphFromUrlAsLink(sFileUrl as String ) as Object 
  oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") 
  Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue 
  oPropsIN(0).Name  = "URL" 
  oPropsIN(0).Value = converttoURL(sFileUrl)
  oPropsIN(1).Name  = "LoadAsLink" 
  oPropsIN(1).Value = TRUE
  getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) 
 end function

function Officeversion
GlobalScope.BasicLibraries.loadLibrary("Tools")
Officeversion =  GetProductName()
end function
Comment 8 Kai Struck 2018-08-30 12:36:15 UTC
Hallo,
will the api changes go into any version higher than 6.0.x.x?
If so then I would use a version check like:

if val(replace(Officeversion,"LibreOffice",""))>6 then
oGraph.Graphic = getgraphfromurlaslink(sURL) 
else
oGraph.GraphicURL = sURL
end if 

function Officeversion
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
Officeversion =  GetProductName()
end function