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 #ifndef OOX_XLS_AUTOFILTERBUFFER_HXX
25 #define OOX_XLS_AUTOFILTERBUFFER_HXX
26 
27 #include <com/sun/star/table/CellRangeAddress.hpp>
28 #include "oox/helper/refvector.hxx"
29 #include "oox/xls/workbookhelper.hxx"
30 
31 namespace com { namespace sun { namespace star {
32     namespace sheet { struct TableFilterField2; }
33     namespace sheet { class XDatabaseRange; }
34     namespace sheet { class XSheetFilterDescriptor2; }
35 } } }
36 
37 namespace oox {
38 namespace xls {
39 
40 // ============================================================================
41 
42 /** Contains UNO API filter settings for a column in a filtered range. */
43 struct ApiFilterSettings
44 {
45     typedef ::std::vector< ::com::sun::star::sheet::TableFilterField2 > FilterFieldVector;
46 
47     FilterFieldVector   maFilterFields;     /// List of UNO API filter settings.
48     OptValue< bool >    mobNeedsRegExp;     /// If set, requires regular expressions to be enabled/disabled.
49 
50     explicit            ApiFilterSettings();
51 
52     void                appendField( bool bAnd, sal_Int32 nOperator, double fValue );
53     void                appendField( bool bAnd, sal_Int32 nOperator, const ::rtl::OUString& rValue );
54 };
55 
56 // ============================================================================
57 
58 /** Base class for specific filter settings for a column in a filtered range.
59  */
60 class FilterSettingsBase : public WorkbookHelper
61 {
62 public:
63     explicit            FilterSettingsBase( const WorkbookHelper& rHelper );
64 
65     /** Derived classes import filter settings from the passed attribute list. */
66     virtual void        importAttribs( sal_Int32 nElement, const AttributeList& rAttribs );
67     /** Derived classes import filter settings from the passed record. */
68     virtual void        importRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
69     /** Derived classes import filter settings from the FILTERCOLUMN record. */
70     virtual void        importBiffRecord( BiffInputStream& rStrm, sal_uInt16 nFlags );
71 
72     /** Derived classes return converted UNO API filter settings representing all filter settings. */
73     virtual ApiFilterSettings finalizeImport( sal_Int32 nMaxCount );
74 };
75 
76 typedef ::boost::shared_ptr< FilterSettingsBase > FilterSettingsRef;
77 
78 // ============================================================================
79 
80 /** Settings for a discrete filter, specifying a list of values to be shown in
81     the filtered range.
82  */
83 class DiscreteFilter : public FilterSettingsBase
84 {
85 public:
86     explicit            DiscreteFilter( const WorkbookHelper& rHelper );
87 
88     /** Imports filter settings from the filters and filter elements. */
89     virtual void        importAttribs( sal_Int32 nElement, const AttributeList& rAttribs );
90     /** Imports filter settings from the FILTERS and FILTER records. */
91     virtual void        importRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
92 
93     /** Returns converted UNO API filter settings representing all filter settings. */
94     virtual ApiFilterSettings finalizeImport( sal_Int32 nMaxCount );
95 
96 private:
97     typedef ::std::vector< ::rtl::OUString > FilterValueVector;
98 
99     FilterValueVector   maValues;
100     sal_Int32           mnCalendarType;
101     bool                mbShowBlank;
102 };
103 
104 // ============================================================================
105 
106 /** Settings for a top-10 filter. */
107 class Top10Filter : public FilterSettingsBase
108 {
109 public:
110     explicit            Top10Filter( const WorkbookHelper& rHelper );
111 
112     /** Imports filter settings from the filters and filter elements. */
113     virtual void        importAttribs( sal_Int32 nElement, const AttributeList& rAttribs );
114     /** Imports filter settings from the FILTERS and FILTER records. */
115     virtual void        importRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
116     /** Imports filter settings from the FILTERCOLUMN record. */
117     virtual void        importBiffRecord( BiffInputStream& rStrm, sal_uInt16 nFlags );
118 
119     /** Returns converted UNO API filter settings representing all filter settings. */
120     virtual ApiFilterSettings finalizeImport( sal_Int32 nMaxCount );
121 
122 private:
123     double              mfValue;        /// Number of items or percentage.
124     bool                mbTop;          /// True = show top (greatest) items/percentage.
125     bool                mbPercent;      /// True = percentage, false = number of items.
126 };
127 
128 // ============================================================================
129 
130 /** A filter criterion for a custom filter. */
131 struct FilterCriterionModel
132 {
133     ::com::sun::star::uno::Any maValue; /// Comparison operand.
134     sal_Int32           mnOperator;     /// Comparison operator.
135     sal_uInt8           mnDataType;     /// Operand data type (BIFF only).
136     sal_uInt8           mnStrLen;       /// Length of string operand (BIFF5-BIFF8 only).
137 
138     explicit            FilterCriterionModel();
139 
140     /** Sets the passed BIFF operator constant. */
141     void                setBiffOperator( sal_uInt8 nOperator );
142 
143     /** Imports the criterion model from the passed BIFF12 stream. */
144     void                readBiffData( SequenceInputStream& rStrm );
145     /** Imports the initial criterion data from the passed BIFF5/BIFF8 stream. */
146     void                readBiffData( BiffInputStream& rStrm );
147     /** Imports the trailing string data from the passed BIFF5/BIFF8 stream. */
148     void                readString( BiffInputStream& rStrm, BiffType eBiff, rtl_TextEncoding eTextEnc );
149 };
150 
151 // ----------------------------------------------------------------------------
152 
153 /** Settings for a custom filter, specifying one or two comparison operators
154     associated with some values.
155  */
156 class CustomFilter : public FilterSettingsBase
157 {
158 public:
159     explicit            CustomFilter( const WorkbookHelper& rHelper );
160 
161     /** Imports filter settings from the filters and filter elements. */
162     virtual void        importAttribs( sal_Int32 nElement, const AttributeList& rAttribs );
163     /** Imports filter settings from the FILTERS and FILTER records. */
164     virtual void        importRecord( sal_Int32 nRecId, SequenceInputStream& rStrm );
165     /** Imports filter settings from the FILTERCOLUMN record. */
166     virtual void        importBiffRecord( BiffInputStream& rStrm, sal_uInt16 nFlags );
167 
168     /** Returns converted UNO API filter settings representing all filter settings. */
169     virtual ApiFilterSettings finalizeImport( sal_Int32 nMaxCount );
170 
171 private:
172     /** Apeends the passed filter criteriom, if it contains valid settings. */
173     void                appendCriterion( const FilterCriterionModel& rCriterion );
174 
175 private:
176     typedef ::std::vector< FilterCriterionModel > FilterCriterionVector;
177 
178     FilterCriterionVector maCriteria;
179     bool                mbAnd;
180 };
181 
182 // ============================================================================
183 
184 /** A column in a filtered range. Contains an object with specific filter
185     settings for the cells in the column.
186  */
187 class FilterColumn : public WorkbookHelper
188 {
189 public:
190     explicit            FilterColumn( const WorkbookHelper& rHelper );
191 
192     /** Imports auto filter column settings from the filterColumn element. */
193     void                importFilterColumn( const AttributeList& rAttribs );
194     /** Imports auto filter column settings from the FILTERCOLUMN record. */
195     void                importFilterColumn( SequenceInputStream& rStrm );
196     /** Imports auto filter column settings from the FILTERCOLUMN record. */
197     void                importFilterColumn( BiffInputStream& rStrm );
198 
199     /** Creates and returns the specified filter settings object. */
200     template< typename FilterSettingsType >
createFilterSettings()201     inline FilterSettingsBase& createFilterSettings()
202         { mxSettings.reset( new FilterSettingsType( *this ) ); return *mxSettings; }
203 
204     /** Returns the index of the column in the filtered range this object is related to. */
getColumnId() const205     inline sal_Int32    getColumnId() const { return mnColId; }
206 
207     /** Returns converted UNO API filter settings representing all filter
208         settings of this column. */
209     ApiFilterSettings   finalizeImport( sal_Int32 nMaxCount );
210 
211 private:
212     FilterSettingsRef   mxSettings;
213     sal_Int32           mnColId;
214     bool                mbHiddenButton;
215     bool                mbShowButton;
216 };
217 
218 // ============================================================================
219 
220 class AutoFilter : public WorkbookHelper
221 {
222 public:
223     explicit            AutoFilter( const WorkbookHelper& rHelper );
224 
225     /** Imports auto filter settings from the autoFilter element. */
226     void                importAutoFilter( const AttributeList& rAttribs, sal_Int16 nSheet );
227     /** Imports auto filter settings from the AUTOFILTER record. */
228     void                importAutoFilter( SequenceInputStream& rStrm, sal_Int16 nSheet );
229 
230     /** Creates a new auto filter column and stores it internally. */
231     FilterColumn&       createFilterColumn();
232 
233     /** Applies the filter to the passed filter descriptor. */
234     void                finalizeImport( const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XSheetFilterDescriptor2 >& rxFilterDesc );
235 
236 private:
237     typedef RefVector< FilterColumn > FilterColumnVector;
238 
239     FilterColumnVector  maFilterColumns;
240     ::com::sun::star::table::CellRangeAddress maRange;
241 };
242 
243 // ============================================================================
244 
245 class AutoFilterBuffer : public WorkbookHelper
246 {
247 public:
248     explicit            AutoFilterBuffer( const WorkbookHelper& rHelper );
249 
250     /** Creates a new auto filter and stores it internally. */
251     AutoFilter&         createAutoFilter();
252 
253     /** Applies filter settings to a new database range object (used for sheet
254         autofilter or advanced filter as specified by built-in defined names). */
255     void                finalizeImport( sal_Int16 nSheet );
256 
257     /** Applies the filters to the passed database range object.
258         @return  True = this buffer contains valid auto filter settings. */
259     bool                finalizeImport( const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDatabaseRange >& rxDatabaseRange );
260 
261 private:
262     /** Returns the auto filter object used to perform auto filtering. */
263     AutoFilter*         getActiveAutoFilter();
264 
265 private:
266     typedef RefVector< AutoFilter > AutoFilterVector;
267     AutoFilterVector    maAutoFilters;
268 };
269 
270 // ============================================================================
271 
272 } // namespace xls
273 } // namespace oox
274 
275 #endif
276