Files
libreoffice/configmgr/source/api2/translatechanges.cxx
2000-11-07 13:40:31 +00:00

330 lines
12 KiB
C++

/*************************************************************************
*
* $RCSfile: translatechanges.cxx,v $
*
* $Revision: 1.1 $
*
* last change: $Author: jb $ $Date: 2000-11-07 14:34:32 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#include "translatechanges.hxx"
#include "noderef.hxx"
#include "nodechange.hxx"
#include "nodechangeinfo.hxx"
#include "apifactory.hxx"
namespace configmgr
{
// ---------------------------------------------------------------------------------------------------
namespace css = ::com::sun::star;
namespace uno = css::uno;
namespace lang = css::lang;
namespace util = css::util;
namespace beans = css::beans;
namespace container = css::container;
// ---------------------------------------------------------------------------------------------------
namespace configuration
{
struct NodeChangeInfo;
class NodeChange;
class NodeChanges;
class Tree;
class NodeRef;
class NodeID;
}
// ---------------------------------------------------------------------------------------------------
namespace configapi
{
using configuration::Tree;
using configuration::Name;
using configuration::Path;
using configuration::AbsolutePath;
using configuration::RelativePath;
using configuration::NodeRef;
using configuration::NodeID;
using configuration::NodeChangeInfo;
using configuration::ExtendedNodeChangeInfo;
// ---------------------------------------------------------------------------------------------------
//interpreting NodeChanges
// resolve the relative path from a given base to the changed node
bool resolveChangeLocation(RelativePath& aPath, ExtendedNodeChangeInfo const& aChange, Tree const& aBaseTree)
{
AbsolutePath aOuterBasePath = aBaseTree.getContextPath();
AbsolutePath aChangeBaseTreePath = aChange.baseTree.getContextPath();
RelativePath aChangeBaseNodePath = aChange.baseTree.getLocalPath(aChange.baseNode);
// RelativePath aChangeLocalPath = aChange.accessor;
typedef Path::Iterator Iter;
Iter aChangeIt = aChangeBaseTreePath.begin(), aChangeEnd = aChangeBaseTreePath.end();
Iter aOuterIt = aOuterBasePath.begin(), aOuterEnd = aOuterBasePath.end();
// resolve the outer path against the base tree path
while (aOuterIt != aOuterEnd && aChangeIt != aChangeEnd)
{
if (*aOuterIt != *aChangeIt) return false; // mismatch
++aOuterIt;
++aChangeIt;
}
// now consider the base node path
if (aOuterIt == aOuterEnd)
{
aPath = RelativePath( Path::Components(aChangeIt, aChangeEnd) ).compose(aChangeBaseNodePath);
}
else //(aChangeIt == aChangeEnd)
{
aChangeIt = aChangeBaseNodePath.begin();
aChangeEnd = aChangeBaseNodePath.end();
// resolve the outer path against the base node path
while (aOuterIt != aOuterEnd && aChangeIt != aChangeEnd)
{
if (*aOuterIt != *aChangeIt) return false; // mismatch
++aOuterIt;
++aChangeIt;
}
if (aOuterIt == aOuterEnd)
{
aPath = RelativePath( Path::Components(aChangeIt, aChangeEnd) );
}
}
// now consider the existing change accessor
if (aOuterIt == aOuterEnd)
{
aPath = aPath.compose(aChange.accessor);
}
else //(aChangeIt == aChangeEnd) // shall we support actually going inside ?
{
aChangeIt = aChange.accessor.begin();
aChangeEnd = aChange.accessor.end();
// resolve the outer path against the change accessor
while (aOuterIt != aOuterEnd && aChangeIt != aChangeEnd)
{
if (*aOuterIt != *aChangeIt) return false; // mismatch
++aOuterIt;
++aChangeIt;
}
if (aOuterIt == aOuterEnd)
{
aPath = RelativePath( Path::Components(aChangeIt, aChangeEnd) );
}
}
return (aOuterIt == aOuterEnd); // resolved completely and assigned ??
}
// ---------------------------------------------------------------------------------------------------
// change path and base settings to start from the given base
bool rebaseChange(ExtendedNodeChangeInfo& aChange, Tree const& aBaseTree)
{
RelativePath aNewPath;
if (resolveChangeLocation(aNewPath,aChange,aBaseTree))
{
aChange.accessor = aNewPath;
aChange.baseTree = aBaseTree;
aChange.baseNode = aBaseTree.getRootNode();
return true;
}
else
return false;
}
// ---------------------------------------------------------------------------------------------------
// resolve non-uno elements to Uno Objects
bool resolveUnoObjects(UnoChange& aUnoChange, NodeChangeInfo const& aChange, Factory& rFactory)
{
if (aChange.isSetChange())
{
uno::Reference<uno::XInterface> aOldUnoObject = rFactory.findUnoElement(aChange.getOldElementNodeID());
uno::Reference<uno::XInterface> aNewUnoObject = rFactory.findUnoElement(aChange.getNewElementNodeID());
bool bFound = aNewUnoObject.is() || aOldUnoObject.is();
aUnoChange.newValue <<= aNewUnoObject;
aUnoChange.oldValue <<= aOldUnoObject;
return bFound;
}
else if (aChange.isValueChange())
{
aUnoChange.newValue = aChange.newValue;
aUnoChange.oldValue = aChange.oldValue;
return true;
}
else
{
return false;
}
}
// ---------------------------------------------------------------------------------------------------
// resolve non-uno elements to Uno Objects inplace
bool resolveToUno(NodeChangeInfo& aChange, Factory& rFactory)
{
struct UnoChange aUnoChange;
if (resolveUnoObjects(aUnoChange,aChange,rFactory))
{
aChange.newValue = aUnoChange.newValue;
aChange.oldValue = aUnoChange.oldValue;
return true;
}
else
return false;
}
// ---------------------------------------------------------------------------------------------------
// building events
/// find the sending api object
void fillEventSource(lang::EventObject& rEvent, Tree const& aTree, NodeRef const& aNode, Factory& rFactory)
{
rEvent.Source = rFactory.findUnoElement( NodeID(aTree,aNode) );
}
// ---------------------------------------------------------------------------------------------------
/// fill a change info from a NodeChangeInfo
void fillChange(util::ElementChange& rChange, ExtendedNodeChangeInfo const& aInfo, Tree const& aBaseTree, Factory& rFactory)
{
RelativePath aRelativePath;
if (!resolveChangeLocation(aRelativePath, aInfo, aBaseTree))
OSL_ENSURE(false, "WARNING: Change is not part of the given Tree");
UnoChange aUnoChange;
if (!resolveUnoObjects(aUnoChange, aInfo.change, rFactory))
OSL_ENSURE(false, "WARNING: Cannot find out old/new UNO objects involved in change");
rChange.Accessor <<= aRelativePath.toString();
rChange.Element = aUnoChange.newValue;
rChange.ReplacedElement = aUnoChange.oldValue;
}
// ---------------------------------------------------------------------------------------------------
/// fill a change info from a NodeChangeInfo (base,path and uno objects are assumed to be resolved already)
void fillChangeFromResolved(util::ElementChange& rChange, ExtendedNodeChangeInfo const& aInfo)
{
rChange.Accessor <<= aInfo.accessor.toString();
rChange.Element = aInfo.change.newValue;
rChange.ReplacedElement = aInfo.change.oldValue;
}
// ---------------------------------------------------------------------------------------------------
/// fill a event from a NodeChangeInfo
void fillEventData(container::ContainerEvent& rEvent, ExtendedNodeChangeInfo const& aInfo, Factory& rFactory)
{
UnoChange aUnoChange;
if (!resolveUnoObjects(aUnoChange, aInfo.change, rFactory))
OSL_ENSURE(false, "WARNING: Cannot find out old/new UNO objects involved in change");
rEvent.Accessor <<= aInfo.accessor.getLocalName().toString();
rEvent.Element = aUnoChange.newValue;
rEvent.ReplacedElement = aUnoChange.oldValue;
}
// ---------------------------------------------------------------------------------------------------
/// fill a event from a NodeChangeInfo (uno objects are assumed to be resolved already)
void fillEventDataFromResolved(container::ContainerEvent& rEvent, ExtendedNodeChangeInfo const& aInfo)
{
rEvent.Accessor <<= aInfo.accessor.getLocalName().toString();
rEvent.Element = aInfo.change.newValue;
rEvent.ReplacedElement = aInfo.change.oldValue;
}
// ---------------------------------------------------------------------------------------------------
/// fill a event from a NodeChangeInfo(uno objects are assumed to be resolved already)
bool fillEventData(beans::PropertyChangeEvent& rEvent, ExtendedNodeChangeInfo const& aInfo, Factory& rFactory, bool bMore)
{
if (!aInfo.isValueChange())
return false;
UnoChange aUnoChange;
if (!resolveUnoObjects(aUnoChange, aInfo.change, rFactory))
OSL_ENSURE(false, "WARNING: Cannot find out old/new UNO objects involved in change");
rEvent.PropertyName = aInfo.accessor.getLocalName().toString();
rEvent.NewValue = aUnoChange.newValue;
rEvent.OldValue = aUnoChange.oldValue;
rEvent.PropertyHandle = -1;
rEvent.Further = bMore;
return true;
}
// ---------------------------------------------------------------------------------------------------
/// fill a event from a NodeChangeInfo(uno objects are assumed to be resolved already)
bool fillEventDataFromResolved(beans::PropertyChangeEvent& rEvent, ExtendedNodeChangeInfo const& aInfo, bool bMore)
{
if (!aInfo.isValueChange())
return false;
rEvent.PropertyName = aInfo.accessor.getLocalName().toString();
rEvent.NewValue = aInfo.change.newValue;
rEvent.OldValue = aInfo.change.oldValue;
rEvent.PropertyHandle = -1;
rEvent.Further = bMore;
return true;
}
// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
}
// ---------------------------------------------------------------------------------------------------
}