xref: /trunk/main/sc/source/ui/vba/vbaquerytable.cxx (revision 7a7771fb4d20b857459c676939e2cc2f4dec9635)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "vbaquerytable.hxx"
23 #include "helperdecl.hxx"
24 #include "document.hxx"
25 #include "docsh.hxx"
26 #include "address.hxx"
27 #include <tools/rtti.hxx>
28 #include <sfx2/linkmgr.hxx>
29 #include <sfx2/lnkbase.hxx>
30 #include "arealink.hxx"
31 #include "vbarange.hxx"
32 
33 using namespace com::sun::star;
34 using namespace com::sun::star::uno;
35 
ScVbaQueryTable(const css::uno::Sequence<css::uno::Any> &,const css::uno::Reference<css::uno::XComponentContext> & xContext)36 ScVbaQueryTable::ScVbaQueryTable( const css::uno::Sequence< css::uno::Any >& /*args*/,
37         const css::uno::Reference< css::uno::XComponentContext >& xContext )
38 :   ScVbaQueryTableImpl_BASE( xContext ),
39     m_pDocument( NULL ),
40     m_pParent( NULL )
41 {
42 }
43 
ScVbaQueryTable(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,ScDocument * pDocument,ScVbaRange * pParent)44 ScVbaQueryTable::ScVbaQueryTable( const css::uno::Reference< ov::XHelperInterface >& xParent,
45             const css::uno::Reference< css::uno::XComponentContext >& xContext,
46             ScDocument* pDocument,
47             ScVbaRange* pParent
48             )
49 :   ScVbaQueryTableImpl_BASE( xParent, xContext ),
50     m_pDocument( pDocument ),
51     m_pParent( pParent )
52 {
53 }
54 
~ScVbaQueryTable()55 ScVbaQueryTable::~ScVbaQueryTable()
56 {
57 }
58 
59 sal_Bool SAL_CALL
Refresh(const css::uno::Any & aBackgroundQuery)60 ScVbaQueryTable::Refresh( const css::uno::Any& aBackgroundQuery ) throw ( css::uno::RuntimeException )
61 {
62     if ( !m_pParent || !m_pDocument )
63         return sal_False;
64 
65     // Get parent Info
66     sal_Int32 nRow = m_pParent->getRow();
67     sal_Int32 nClm = m_pParent->getColumn();
68     sal_Int16 nTab = m_pParent->getWorksheet()->getIndex() - 1; // The vba index begins from 1.
69     ScAddress crrRngAddr( nClm, nRow, nTab );
70 
71     // Get link info
72     sfx2::LinkManager* pLinkMng = m_pDocument->GetLinkManager();
73     const sfx2::SvBaseLinks& rLinks = pLinkMng->GetLinks();
74     sal_uInt16 nCount = rLinks.Count();
75 
76     bool bBackground = false;
77     if ( aBackgroundQuery.hasValue() )
78         aBackgroundQuery >>= bBackground;
79 
80     bool bFoundLink = false;
81     for ( sal_uInt16 i = 0; i < nCount; ++i )
82     {
83         sfx2::SvBaseLink* pBase = *rLinks[i];
84         if ( pBase->ISA( ScAreaLink ) )
85         {
86             ScAreaLink* pAreaLink = static_cast< ScAreaLink* >( pBase );
87             const ScRange& destRange = pAreaLink->GetDestArea();
88             if ( destRange.In( crrRngAddr ) )
89             {
90                 pBase->Update();
91                 bFoundLink = true;
92             }
93         }
94     }
95 
96     return bFoundLink ? sal_True : sal_False;
97 }
98 
99 void SAL_CALL
Delete()100 ScVbaQueryTable::Delete() throw ( css::uno::RuntimeException )
101 {
102     if ( !m_pParent || !m_pDocument )
103         return;
104 
105     sal_Int32 nRow = m_pParent->getRow();
106     sal_Int32 nClm = m_pParent->getColumn();
107     sal_Int16 nTab = m_pParent->getWorksheet()->getIndex() - 1;
108     ScAddress crrRngAddr( nClm, nRow, nTab );
109 
110     sfx2::LinkManager* pLinkMng = m_pDocument->GetLinkManager();
111     const sfx2::SvBaseLinks& rLinks = pLinkMng->GetLinks();
112     sal_uInt16 nCount = rLinks.Count();
113 
114     for ( sal_uInt16 i = 0; i < nCount; ++i )
115     {
116         sfx2::SvBaseLink* pBase = *rLinks[i];
117         if ( pBase->ISA( ScAreaLink ) )
118         {
119             ScAreaLink* pAreaLink = static_cast< ScAreaLink* >( pBase );
120             const ScRange& destRange = pAreaLink->GetDestArea();
121             if ( destRange.In( crrRngAddr ) )
122             {
123                 pLinkMng->Remove( pBase );
124                 break;
125             }
126         }
127     }
128 }
129 
getServiceImplName()130 rtl::OUString& ScVbaQueryTable::getServiceImplName()
131 {
132     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaQueryTable") );
133     return sImplName;
134 }
135 
getServiceNames()136 css::uno::Sequence< rtl::OUString > ScVbaQueryTable::getServiceNames()
137 {
138     static css::uno::Sequence< rtl::OUString > aServiceNames;
139     if ( aServiceNames.getLength() == 0 )
140     {
141         aServiceNames.realloc( 1 );
142         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.QueryTable") );
143     }
144     return aServiceNames;
145 }
146