Greetings programs,

I am having problems creating a chart in OOo Calc from my external C++
program. When I call the Reference<XTableCharts> member function
addNewByName, I get a segmentation fault deep down in several nested
calls within the shared object library liburp_uno.so. I am running
Fedora Core 9 x86-64 Linux, compiling with gcc version 4.3.2, and I have
OOo SDK 3.2.0. Here is a backtrace:

#5 <signal handler called>
#6 0x00007fffeb692882 in ?? () from
/opt/openoffice.org/ure/lib/liburp_uno.so
#7 0x00007fffeb69332e in ?? () from
/opt/openoffice.org/ure/lib/liburp_uno.so
#8 0x00007fffeb69adc6 in ?? () from
/opt/openoffice.org/ure/lib/liburp_uno.so
#9 0x00007fffeb69733d in ?? () from
/opt/openoffice.org/ure/lib/liburp_uno.so
#10 0x00007fffeb6a97f3 in ?? () from
/opt/openoffice.org/ure/lib/liburp_uno.so
#11 0x00007fffeb6a7fea in ?? () from
/opt/openoffice.org/ure/lib/liburp_uno.so
#12 0x00007fffed14028b in ?? () from
/opt/openoffice.org/ure/lib/libgcc3_uno.so
#13 0x00007fffed140a03 in ?? () from
/opt/openoffice.org/ure/lib/libgcc3_uno.so
#14 0x00007fffed141f1a in ?? () from
/opt/openoffice.org/ure/lib/libgcc3_uno.so
#15 0x00000000004a646b in PlotIPCOpenOffice::plot (this=0xc06160,
pd=0x1a502f0) at plotipcopenoffice.cc:356
#16 0x000000000041cf64 in GraphTool::plotData (this=0x775b20,
crntPlotSet=0x197c920) at graphtoolui.cc:3764

I have a few questions:

  1. Am I asking the right mailing list about this issue?
  2. I would like to download the source for this library and compile
     with debug symbols. What package do I need to get from CVS?
  3. I have attached a small test driver that replicates the seg fault
     on my machine. If you are able to view and/or run this test
     driver, could you please advise me if I am taking all the correct
     steps, or if there is something different that I need to do when
     calling addNewByName?

Thank you for your assistance!

Scott Thompson
#include <com/sun/star/awt/Point.hpp>
#include <com/sun/star/awt/Rectangle.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/chart/XAxisXSupplier.hpp>
#include <com/sun/star/chart/XAxisYSupplier.hpp>
#include <com/sun/star/chart/XChartDocument.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sheet/XCellRangeAddressable.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheets.hpp>
#include <com/sun/star/sheet/XSpreadsheetView.hpp>
#include <com/sun/star/table/CellRangeAddress.hpp>
#include <com/sun/star/table/XCell.hpp>
#include <com/sun/star/table/XCellRange.hpp>
#include <com/sun/star/table/XTableChart.hpp>
#include <com/sun/star/table/XTableCharts.hpp>
#include <com/sun/star/table/XTableChartsSupplier.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <cppuhelper/bootstrap.hxx>
#include <iostream>
#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/ustring.hxx>
#include <string.h>
#include <unistd.h>

using namespace com::sun::star::awt;
using namespace com::sun::star::beans;
using namespace com::sun::star::bridge;
using namespace com::sun::star::chart;
using namespace com::sun::star::container;
using namespace com::sun::star::document;
using namespace com::sun::star::frame;
using namespace com::sun::star::lang;
using namespace com::sun::star::sheet;
using namespace com::sun::star::table;
using namespace com::sun::star::uno;
using namespace cppu;
using namespace rtl;

//+ Class to manage an IPC connection with an OpenOffice.org spreadsheet
class PlotIPCOpenOffice {
	Reference<XComponent> d_xComponent;
					//- handle for a OpenOffice.org
					//  spreadsheet

//NOT IMPLEMENTED
	PlotIPCOpenOffice(const PlotIPCOpenOffice&);
	PlotIPCOpenOffice& operator=(const PlotIPCOpenOffice&);

public:
//CREATORS
	//+ Constructor
	PlotIPCOpenOffice() {};

	//+ Destructor
	~PlotIPCOpenOffice() {};

//MANIPULATORS
	//+ Start the IPC connection
	bool openIPC();

	//+ End the IPC connection
	bool closeIPC();

	//+ Plot some data.  Each plot is displayed on a new sheet.
	bool plot();
};

	bool
PlotIPCOpenOffice::openIPC()
{
	try {
		// Get the remote office component context
		Reference<XComponentContext> xContext(bootstrap());
		if (! xContext.is())
		{
			std::cerr << "No component context!\n";
			return false;
		}

		// Get the remote office service manager
		Reference<XMultiComponentFactory> xServiceManager(xContext->getServiceManager());
		if (! xServiceManager.is())
		{
			std::cerr << "No service manager!\n";
			return false;
		}

		// Get an instance of the remote office desktop UNO service
		// and query the XComponentLoader interface
		OUString service(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop"));
		Reference<XInterface> xInterface(
			xServiceManager->createInstanceWithContext(service, xContext));
		Reference<XComponentLoader> xComponentLoader(
			xInterface, UNO_QUERY_THROW);

		// Open a spreadsheet document
		OUString url(RTL_CONSTASCII_USTRINGPARAM("private:factory/scalc"));
		OUString frameName(RTL_CONSTASCII_USTRINGPARAM("_blank"));
		Reference<XComponent> xComponent =
			xComponentLoader->loadComponentFromURL(url, frameName, 0,
			Sequence<PropertyValue>());
			d_xComponent = xComponent;
		if (! d_xComponent.is())
		{
			std::cerr << "Opening spreadsheet document failed!\n";
			return false;
		}
	}
	catch (cppu::BootstrapException& e)
	{
		std::cerr << "Caught BootstrapException: "
		     << OUStringToOString(e.getMessage(), RTL_TEXTENCODING_ASCII_US).getStr()
		     << '\n';
		return false;
	}
	catch (com::sun::star::uno::Exception& e)
	{
		std::cerr << "Caught UNO exception: "
		     << OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr()
		     << '\n';
		return false;
	}
	return true;
}

	bool
PlotIPCOpenOffice::closeIPC()

{
	bool closed = true;
	try {
		d_xComponent->dispose();
	}
	catch(com::sun::star::uno::Exception &e) {
		closed = false;
	}

	return closed;
}

	bool
PlotIPCOpenOffice::plot()
{
	std::string tmp = "Plot";
	OUString sheetName = OUString::createFromAscii(tmp.c_str());

	// Insert a new sheet into the spreadsheet
	Reference<XSpreadsheetDocument> xSheetDocument (d_xComponent, UNO_QUERY);
	Reference<XSpreadsheets> xSheets = xSheetDocument->getSheets();
	xSheets->insertNewByName(sheetName, 0);

	// Grab a handle to the new sheet
	Reference<XIndexAccess> xIndex (xSheets, UNO_QUERY);
	Any any = xIndex->getByIndex(0);
	Reference<XSpreadsheet> xSheet;
	any >>= xSheet;

	// Make the new sheet the active sheet
	Reference<XModel> xSpreadsheetModel(d_xComponent, UNO_QUERY);
	Reference<XController> xSpreadsheetController = xSpreadsheetModel->getCurrentController();
	Reference<XSpreadsheetView> xSpreadsheetView(xSpreadsheetController, UNO_QUERY);
	xSpreadsheetView->setActiveSheet(xSheet);


	// Insert the data into the spreadsheet
	xCellRange->getCellByPosition(1, 1)->setValue(10);
	xCellRange->getCellByPosition(2, 1)->setValue(10);
	xCellRange->getCellByPosition(1, 2)->setValue(20);
	xCellRange->getCellByPosition(2, 2)->setValue(20);

	// Get a reference to the desired cells
	Reference<XCellRange> xCellRange(xSheet, UNO_QUERY);
	Reference<XCellRange> oRange;
	oRange = xCellRange->getCellRangeByPosition(1, 1, 2, 2);

	Rectangle oRectangle;
	oRectangle.X = 8000;
	oRectangle.Y = 3000;
	oRectangle.Width = 15000;
	oRectangle.Height = 10000;

	Reference<XCellRangeAddressable> oRangeAddr(oRange, UNO_QUERY);
	CellRangeAddress oAddr = oRangeAddr->getRangeAddress();
	Sequence<CellRangeAddress> sAddr(1);
	CellRangeAddress* seqArray = sAddr.getArray();
	seqArray[0] = oAddr;

	Reference<XTableChartsSupplier> aSheet(xSheet, UNO_QUERY);
	Reference<CellRangeAddress> aAddresses;
	OUString chartName = sheetName;
	Reference<XTableCharts> aChartCollection = aSheet->getCharts();
	Reference<XNameAccess> aChartCollectionNA(aChartCollection, UNO_QUERY);

	if ((aChartCollectionNA != NULL) && (!aChartCollectionNA->hasByName(chartName))) {
		// Add a chart
		sal_Bool columnHeaders = sal_False;
		sal_Bool rowHeaders = sal_False;
		aChartCollection->addNewByName(chartName, oRectangle, sAddr, columnHeaders, rowHeaders);

		try {
			// Get a reference to the chart
			Reference<XTableChart> aTableChart(aChartCollectionNA->getByName(chartName), UNO_QUERY);
			Reference<XEmbeddedObjectSupplier> xEmbeddedObjectSupplier(aTableChart, UNO_QUERY);
			Reference<XInterface> xInterface = xEmbeddedObjectSupplier->getEmbeddedObject();
			Reference<XChartDocument> xChartDoc(xInterface, UNO_QUERY);

			// Suspend display updates for the chart.
			xChartDoc->lockControllers();

			// Change the chart type to the specified type
			Reference<XMultiServiceFactory> xMultiServiceFactory(xChartDoc, UNO_QUERY);
			OUString chartType;
			chartType = OUString::createFromAscii("com.sun.star.chart.XYDiagram");
			Reference<XInterface> newChart = xMultiServiceFactory->createInstance(chartType);
			Reference<XDiagram> xDiagram(newChart, UNO_QUERY);
			xChartDoc->setDiagram(xDiagram);
		}
		catch (com::sun::star::container::NoSuchElementException &e) {
			std::cerr << "Couldn't find chart with name "
					<< chartName
					<< ": "
					<< OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr()
					<< '\n';
		}
	}

	return true;
}

int main( )
{
	PlotIPCOpenOffice * returnipc = new PlotIPCOpenOffice();

	returnipc->openIPC();
	returnipc->plot();
	returnipc->closeIPC();

	return 0;
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to