From 69a4a8965ee774d0563136344e16b57941f13e0d Mon Sep 17 00:00:00 2001
From: Markus Mohrhard <markus.mohrhard@googlemail.com>
Date: Wed, 4 May 2011 14:19:07 +0200
Subject: [PATCH] add implementation for new uno interface XUnnamedDatabaseRanges

---
 sc/inc/datauno.hxx              |   29 ++++++++++
 sc/source/ui/unoobj/datauno.cxx |  112 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 134 insertions(+), 7 deletions(-)

diff --git a/sc/inc/datauno.hxx b/sc/inc/datauno.hxx
index 429ca3a..208816e 100644
--- a/sc/inc/datauno.hxx
+++ b/sc/inc/datauno.hxx
@@ -40,6 +40,7 @@
 #include <com/sun/star/sheet/XConsolidationDescriptor.hpp>
 #include <com/sun/star/sheet/XDatabaseRanges.hpp>
 #include <com/sun/star/sheet/XDatabaseRange.hpp>
+#include <com/sun/star/sheet/XUnnamedDatabaseRanges.hpp>
 #include <com/sun/star/sheet/XSubTotalDescriptor.hpp>
 #include <com/sun/star/sheet/XSubTotalField.hpp>
 #include <com/sun/star/lang/XServiceInfo.hpp>
@@ -501,6 +502,8 @@ private:
     String					aName;
     SfxItemPropertySet		aPropSet;
     XDBRefreshListenerArr_Impl aRefreshListeners;
+    bool                    bIsUnnamed;
+    SCTAB                   aTab;
 
 private:
     ScDBData*				GetDBData_Impl() const;
@@ -508,6 +511,7 @@ private:
 
 public:
                             ScDatabaseRangeObj(ScDocShell* pDocSh, const String& rNm);
+                            ScDatabaseRangeObj(ScDocShell* pDocSh, const SCTAB nTab);
     virtual					~ScDatabaseRangeObj();
 
     virtual void			Notify( SfxBroadcaster& rBC, const SfxHint& rHint );
@@ -662,6 +666,31 @@ public:
                                 throw(::com::sun::star::uno::RuntimeException);
 };
 
+class ScUnnamedDatabaseRangesObj : public cppu::WeakImplHelper1<
+                                com::sun::star::sheet::XUnnamedDatabaseRanges>,
+                            public SfxListener
+{
+private:
+    ScDocShell*				pDocShell;
+
+public:
+                            ScUnnamedDatabaseRangesObj(ScDocShell* pDocSh);
+    virtual					~ScUnnamedDatabaseRangesObj();
+
+    virtual void			Notify( SfxBroadcaster& rBC, const SfxHint& rHint );
+
+                            // XUnnamedDatabaseRanges
+    virtual void SAL_CALL addUnnamedDatabase( const ::com::sun::star::table::CellRangeAddress& aRange )
+                                throw(::com::sun::star::uno::RuntimeException);
+    virtual com::sun::star::uno::Any SAL_CALL getUnnamedDatabase( const sal_Int32 nTab )
+                                throw(::com::sun::star::uno::RuntimeException,
+                                ::com::sun::star::lang::IndexOutOfBoundsException,
+                                ::com::sun::star::container::NoSuchElementException );
+    virtual sal_Bool SAL_CALL hasByTable( sal_Int32 nTab )
+                                throw (::com::sun::star::uno::RuntimeException,
+                                ::com::sun::star::lang::IndexOutOfBoundsException);
+};
+
 
 
 #endif
diff --git a/sc/source/ui/unoobj/datauno.cxx b/sc/source/ui/unoobj/datauno.cxx
index edc7fea..fa21c82 100644
--- a/sc/source/ui/unoobj/datauno.cxx
+++ b/sc/source/ui/unoobj/datauno.cxx
@@ -57,6 +57,7 @@
 #include "docsh.hxx"
 #include "dbdocfun.hxx"
 #include "unonames.hxx"
+#include "globalnames.hxx"
 #include "globstr.hrc"
 #include "convuno.hxx"
 #include "hints.hxx"
@@ -1607,7 +1608,18 @@ void ScDataPilotFilterDescriptor::PutData( const ScQueryParam& rParam )
 ScDatabaseRangeObj::ScDatabaseRangeObj(ScDocShell* pDocSh, const String& rNm) :
     pDocShell( pDocSh ),
     aName( rNm ),
-    aPropSet( lcl_GetDBRangePropertyMap() )
+    aPropSet( lcl_GetDBRangePropertyMap() ),
+    bIsUnnamed(false)
+{
+    pDocShell->GetDocument()->AddUnoObject(*this);
+}
+
+ScDatabaseRangeObj::ScDatabaseRangeObj(ScDocShell* pDocSh, const SCTAB nTab) :
+    pDocShell( pDocSh ),
+    aName(RTL_CONSTASCII_USTRINGPARAM(STR_DB_LOCAL_NONAME)),
+    aPropSet( lcl_GetDBRangePropertyMap() ),
+    bIsUnnamed(true),
+    aTab( nTab )
 {
     pDocShell->GetDocument()->AddUnoObject(*this);
 }
@@ -1641,12 +1653,19 @@ ScDBData* ScDatabaseRangeObj::GetDBData_Impl() const
     ScDBData* pRet = NULL;
     if (pDocShell)
     {
-        ScDBCollection* pNames = pDocShell->GetDocument()->GetDBCollection();
-        if (pNames)
+        if (bIsUnnamed)
         {
-            sal_uInt16 nPos = 0;
-            if (pNames->SearchName( aName, nPos ))
-                pRet = (*pNames)[nPos];
+            pRet = pDocShell->GetDocument()->GetAnonymousDBData(aTab);
+        }
+        else
+        {
+            ScDBCollection* pNames = pDocShell->GetDocument()->GetDBCollection();
+            if (pNames)
+            {
+                sal_uInt16 nPos = 0;
+                if (pNames->SearchName( aName, nPos ))
+                    pRet = (*pNames)[nPos];
+            }
         }
     }
     return pRet;
@@ -2079,7 +2098,7 @@ uno::Any SAL_CALL ScDatabaseRangeObj::getPropertyValue( const rtl::OUString& aPr
         {
             //	all database ranges except "unnamed" are user defined
             ScUnoHelpFunctions::SetBoolInAny( aRet,
-                        ( pData->GetName() != ScGlobal::GetRscString(STR_DB_NONAME) ) );
+                        ( pData->GetName() != String(RTL_CONSTASCII_USTRINGPARAM(STR_DB_LOCAL_NONAME))  ) );
         }
         else if ( aString.EqualsAscii( SC_UNO_LINKDISPBIT ) )
         {
@@ -2354,8 +2373,87 @@ sal_Bool SAL_CALL ScDatabaseRangesObj::hasByName( const rtl::OUString& aName )
 
 //------------------------------------------------------------------------
 
+ScUnnamedDatabaseRangesObj::ScUnnamedDatabaseRangesObj(ScDocShell* pDocSh) :
+    pDocShell( pDocSh )
+{
+    pDocShell->GetDocument()->AddUnoObject(*this);
+}
+
+ScUnnamedDatabaseRangesObj::~ScUnnamedDatabaseRangesObj()
+{
+    if (pDocShell)
+        pDocShell->GetDocument()->RemoveUnoObject(*this);
+}
+
+void ScUnnamedDatabaseRangesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
+{
+    //	Referenz-Update interessiert hier nicht
+
+    if ( rHint.ISA( SfxSimpleHint ) &&
+            ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
+    {
+        pDocShell = NULL;		// ungueltig geworden
+    }
+}
+
+// XUnnamedDatabaseRanges
+
+void ScUnnamedDatabaseRangesObj::addUnnamedDatabase( const ::com::sun::star::table::CellRangeAddress& aRange )
+                                throw(::com::sun::star::uno::RuntimeException)
+{
+    SolarMutexGuard aGuard;
+    bool bDone = false;
+    if (pDocShell)
+    {
+        ScDBDocFunc aFunc(*pDocShell);
 
+        String aString(RTL_CONSTASCII_USTRINGPARAM(STR_DB_LOCAL_NONAME));
+        ScRange aUnnamedRange( (SCCOL)aRange.StartColumn, (SCROW)aRange.StartRow, aRange.Sheet,
+                            (SCCOL)aRange.EndColumn,   (SCROW)aRange.EndRow,   aRange.Sheet );
+        bDone = aFunc.AddDBRange( aString, aUnnamedRange, sal_True );
+    }
+    if (!bDone)
+        throw uno::RuntimeException();		// no other exceptions specified
+}
 
+uno::Any ScUnnamedDatabaseRangesObj::getUnnamedDatabase( const sal_Int32 nTab )
+                                throw(uno::RuntimeException,
+                                    lang::IndexOutOfBoundsException,
+                                    container::NoSuchElementException)
+{
+    SolarMutexGuard aGuard;
+    if (pDocShell)
+    {
+        if (pDocShell->GetDocument()->GetTableCount() >= nTab)
+            throw lang::IndexOutOfBoundsException();
+        uno::Reference<sheet::XDatabaseRange> xRange( new ScDatabaseRangeObj(pDocShell, (SCTAB) nTab) );
+        if (xRange.is())
+            return uno::makeAny(xRange);
+        else
+            throw container::NoSuchElementException();
+    }
+    else
+        throw uno::RuntimeException();
+}
+
+sal_Bool ScUnnamedDatabaseRangesObj::hasByTable( sal_Int32 nTab )
+                                    throw (uno::RuntimeException,
+                                        lang::IndexOutOfBoundsException)
+{
+    SolarMutexGuard aGuard;
+    if (pDocShell)
+    {
+         if (pDocShell->GetDocument()->GetTableCount() >= nTab)
+            throw lang::IndexOutOfBoundsException();
+        if (pDocShell->GetDocument()->GetAnonymousDBData((SCTAB) nTab))
+            return true;
+        return false;
+    }
+    else
+        return false;
+}
+
+//------------------------------------------------------------------------
 
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
-- 
1.7.2.5

