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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 
27 #include "ChartDropTargetHelper.hxx"
28 #include "DiagramHelper.hxx"
29 #include "DataSourceHelper.hxx"
30 
31 #include <com/sun/star/chart2/XChartDocument.hpp>
32 #include <com/sun/star/chart2/data/XDataProvider.hpp>
33 #include <com/sun/star/container/XChild.hpp>
34 
35 #include <sot/formats.hxx>
36 #include <vector>
37 
38 using namespace ::com::sun::star;
39 
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::Sequence;
42 using ::rtl::OUString;
43 
44 namespace
45 {
46 
lcl_getStringsFromByteSequence(const Sequence<sal_Int8> & aByteSequence)47 ::std::vector< OUString > lcl_getStringsFromByteSequence(
48     const Sequence< sal_Int8 > & aByteSequence )
49 {
50     ::std::vector< OUString > aResult;
51     const sal_Int32 nLength = aByteSequence.getLength();
52     const sal_Char * pBytes( reinterpret_cast< const sal_Char* >( aByteSequence.getConstArray()));
53     sal_Int32 nStartPos = 0;
54     for( sal_Int32 nPos=0; nPos<nLength; ++nPos )
55     {
56         if( pBytes[nPos] == '\0' )
57         {
58             aResult.push_back( OUString( pBytes + nStartPos, (nPos - nStartPos), RTL_TEXTENCODING_ASCII_US ));
59             nStartPos = nPos + 1;
60         }
61     }
62     return aResult;
63 }
64 
65 } // anonymous namespace
66 
67 namespace chart
68 {
69 
ChartDropTargetHelper(const Reference<datatransfer::dnd::XDropTarget> & rxDropTarget,const Reference<chart2::XChartDocument> & xChartDocument)70 ChartDropTargetHelper::ChartDropTargetHelper(
71     const Reference< datatransfer::dnd::XDropTarget >& rxDropTarget,
72     const Reference< chart2::XChartDocument > & xChartDocument ) :
73         DropTargetHelper( rxDropTarget ),
74         m_xChartDocument( xChartDocument )
75 {}
76 
~ChartDropTargetHelper()77 ChartDropTargetHelper::~ChartDropTargetHelper()
78 {}
79 
satisfiesPrerequisites() const80 bool ChartDropTargetHelper::satisfiesPrerequisites() const
81 {
82     return  ( m_xChartDocument.is() &&
83               ! m_xChartDocument->hasInternalDataProvider());
84 }
85 
AcceptDrop(const AcceptDropEvent & rEvt)86 sal_Int8 ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent& rEvt )
87 {
88     sal_Int8 nResult = DND_ACTION_NONE;
89 
90     if( ( rEvt.mnAction == DND_ACTION_COPY ||
91           rEvt.mnAction == DND_ACTION_MOVE ) &&
92         satisfiesPrerequisites() &&
93         IsDropFormatSupported( SOT_FORMATSTR_ID_LINK ) )
94     {
95         // @todo: check if the data is suitable. Is this possible without XTransferable?
96         nResult = rEvt.mnAction;
97     }
98 
99     return nResult;
100 }
101 
ExecuteDrop(const ExecuteDropEvent & rEvt)102 sal_Int8 ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent& rEvt )
103 {
104     sal_Int8 nResult = DND_ACTION_NONE;
105 
106     if( ( rEvt.mnAction == DND_ACTION_COPY ||
107           rEvt.mnAction == DND_ACTION_MOVE ) &&
108         rEvt.maDropEvent.Transferable.is() &&
109         satisfiesPrerequisites())
110     {
111         TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
112         if( aDataHelper.HasFormat( SOT_FORMATSTR_ID_LINK ))
113         {
114             Sequence< sal_Int8 > aBytes;
115             if( aDataHelper.GetSequence( SOT_FORMATSTR_ID_LINK, aBytes ))
116             {
117                 ::std::vector< OUString > aStrings( lcl_getStringsFromByteSequence( aBytes ));
118                 if( aStrings.size() >= 3 &&
119                     aStrings[0].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "soffice" )))
120                 {
121                     OUString aDocName( aStrings[1] );
122                     OUString aRangeString( aStrings[2] );
123                     Reference< container::XChild > xChild( m_xChartDocument, uno::UNO_QUERY );
124                     if( xChild.is())
125                     {
126                         Reference< frame::XModel > xParentModel( xChild->getParent(), uno::UNO_QUERY );
127                         if( xParentModel.is() &&
128                             m_xChartDocument.is())
129                         {
130                             bool bDataComesFromParent = true;
131                             // @todo: get the title somehow and compare it to
132                             // aDocName if successful (the document is the
133                             // parent)
134                             if( bDataComesFromParent )
135                             {
136                                 Reference< chart2::XDiagram > xDiagram( m_xChartDocument->getFirstDiagram() );
137                                 Reference< chart2::data::XDataProvider > xDataProvider( m_xChartDocument->getDataProvider());
138                                 if( xDataProvider.is() && xDiagram.is() &&
139                                     DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument ))
140                                 {
141                                     Reference< chart2::data::XDataSource > xDataSource(
142                                         DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument ));
143                                     Sequence< beans::PropertyValue > aArguments(
144                                         xDataProvider->detectArguments( xDataSource ));
145 
146                                     OUString aOldRange;
147                                     beans::PropertyValue * pCellRange = 0;
148                                     for( sal_Int32 i=0; i<aArguments.getLength(); ++i )
149                                     {
150                                         if( aArguments[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("CellRangeRepresentation")))
151                                         {
152                                             pCellRange = (aArguments.getArray() + i);
153                                             aArguments[i].Value >>= aOldRange;
154                                             break;
155                                         }
156                                     }
157                                     if( pCellRange )
158                                     {
159                                         // copy means add ranges, move means replace
160                                         if( rEvt.mnAction == DND_ACTION_COPY )
161                                         {
162                                             // @todo: using implcit knowledge that ranges can be
163                                             // merged with ";". This should be done more general
164                                             pCellRange->Value <<= (aOldRange + OUString( sal_Unicode(';')) + aRangeString );
165                                         }
166                                         // move means replace range
167                                         else
168                                         {
169                                             pCellRange->Value <<= aRangeString;
170                                         }
171 
172                                         xDataSource.set( xDataProvider->createDataSource( aArguments ));
173                                         xDiagram->setDiagramData( xDataSource, aArguments );
174 
175                                         // always return copy state to avoid deletion of the dragged range
176                                         nResult = DND_ACTION_COPY;
177                                     }
178                                 }
179                             }
180                         }
181                     }
182                 }
183             }
184         }
185     }
186     return nResult;
187 }
188 
189 } //  namespace chart
190