xref: /aoo41x/main/framework/inc/queries.h (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_QUERIES_H_
29 #define __FRAMEWORK_QUERIES_H_
30 
31 //_________________________________________________________________________________________________________________
32 //	own includes
33 //_________________________________________________________________________________________________________________
34 
35 #include <general.h>
36 
37 //_________________________________________________________________________________________________________________
38 //	other includes
39 //_________________________________________________________________________________________________________________
40 #include <com/sun/star/uno/Sequence.hxx>
41 #include <rtl/ustring.hxx>
42 #include <rtl/ustrbuf.hxx>
43 
44 //_________________________________________________________________________________________________________________
45 //	namespace
46 //_________________________________________________________________________________________________________________
47 
48 namespace framework{
49 
50 //_________________________________________________________________________________________________________________
51 //	const
52 //_________________________________________________________________________________________________________________
53 
54 /*-************************************************************************************************************//**
55 	@short			These values describe our supported queries for type, filter ... properties.
56 					They are used by our FilterFactory or ouer TypeDetection to return
57 					subsets of our cached configuration.
58 *//*-*************************************************************************************************************/
59 #define BASE_QUERY_ALL                                  DECLARE_ASCII("_query_all"     )
60 #define BASE_QUERY_WRITER                               DECLARE_ASCII("_query_Writer"  )
61 #define BASE_QUERY_WEB                                  DECLARE_ASCII("_query_web"     )
62 #define BASE_QUERY_GLOBAL                               DECLARE_ASCII("_query_global"  )
63 #define BASE_QUERY_CHART                                DECLARE_ASCII("_query_chart"   )
64 #define BASE_QUERY_CALC                                 DECLARE_ASCII("_query_calc"    )
65 #define BASE_QUERY_IMPRESS                              DECLARE_ASCII("_query_impress" )
66 #define BASE_QUERY_DRAW                                 DECLARE_ASCII("_query_draw"    )
67 #define BASE_QUERY_MATH                                 DECLARE_ASCII("_query_math"    )
68 #define BASE_QUERY_GRAPHICS                             DECLARE_ASCII("_query_graphics")
69 
70 /*-************************************************************************************************************//**
71     @short          These parameters can be used in combination with BASE_QUERY_... defines to
72                     specialize it.
73                     use follow syntax to do so:     "<query>[:<param>[=<value>]]"
74                     e.g.:                           "_query_writer:default_first:use_order:sort_prop=uiname"
75 
76                     argument                        description                                     default
77                     -----------------------------------------------------------------------------------------------
78                     iflags=<mask>                   include filters by given mask                   0
79                     eflags=<mask>                   exclude filters by given mask                   0
80                     sort_prop=<[name,uiname]>       sort by internal name or uiname                 name
81                     descending                      sort descending                                 false
82                     use_order                       use order flag of filters for sorting           false
83                     default_first                   set default filter on top of return list        false
84                     case_sensitive                  compare "sort_prop" case sensitive              false
85 *//*-*************************************************************************************************************/
86 #define SEPERATOR_QUERYPARAM                            ((sal_Unicode)':')
87 #define SEPERATOR_QUERYPARAMVALUE                       ((sal_Unicode)'=')
88 
89 #define QUERYPARAM_IFLAGS                               DECLARE_ASCII("iflags"          )
90 #define QUERYPARAM_EFLAGS                               DECLARE_ASCII("eflags"          )
91 #define QUERYPARAM_SORT_PROP                            DECLARE_ASCII("sort_prop"       )
92 
93 #define QUERYPARAM_DESCENDING                           DECLARE_ASCII("descending"      )
94 #define QUERYPARAM_USE_ORDER                            DECLARE_ASCII("use_order"       )
95 #define QUERYPARAM_DEFAULT_FIRST                        DECLARE_ASCII("default_first"   )
96 #define QUERYPARAM_CASE_SENSITIVE                       DECLARE_ASCII("case_sensitive"  )
97 
98 #define QUERYPARAMVALUE_SORT_PROP_NAME                  DECLARE_ASCII("name"            )
99 #define QUERYPARAMVALUE_SORT_PROP_UINAME                DECLARE_ASCII("uiname"          )
100 
101 /*-************************************************************************************************************//**
102     @short          Helper class to support easy building of a query statements.
103 *//*-*************************************************************************************************************/
104 class QueryBuilder
105 {
106     public:
107         //---------------------------------------------------------------------------------------------------------
108         // start with empty query
109         //---------------------------------------------------------------------------------------------------------
110         QueryBuilder()
111         {
112             resetAll();
113         }
114 
115         //---------------------------------------------------------------------------------------------------------
116         // returns full query as copy of internal set values
117         //---------------------------------------------------------------------------------------------------------
118         ::rtl::OUString getQuery()
119         {
120             ::rtl::OUStringBuffer sCopy( m_sParams );
121             sCopy.insert( 0, m_sBase );
122             return sCopy.makeStringAndClear();
123         }
124 
125         //---------------------------------------------------------------------------------------------------------
126         // set new or change existing base query part
127         //---------------------------------------------------------------------------------------------------------
128         void setBase( const ::rtl::OUString& sBase )
129         {
130             m_sBase = sBase;
131         }
132 
133         //---------------------------------------------------------------------------------------------------------
134         // add new parameter (with optional value) to param list
135         //---------------------------------------------------------------------------------------------------------
136         void addParam( const ::rtl::OUString& sParam, const ::rtl::OUString& sValue = ::rtl::OUString() )
137         {
138             m_sParams.append( SEPERATOR_QUERYPARAM );
139             m_sParams.append( sParam               );
140             if( sValue.getLength() > 0 )
141             {
142                 m_sParams.append( SEPERATOR_QUERYPARAMVALUE );
143                 m_sParams.append( sValue                    );
144             }
145         }
146 
147         //---------------------------------------------------------------------------------------------------------
148         // forget all setted params and start with empty ones
149         // Attention: base of query isn't changed!
150         //---------------------------------------------------------------------------------------------------------
151         void resetParams()
152         {
153             m_sParams.makeStringAndClear();
154             m_sParams.ensureCapacity( 256 );
155         }
156 
157         //---------------------------------------------------------------------------------------------------------
158         // start with new empty query
159         //---------------------------------------------------------------------------------------------------------
160         void resetAll()
161         {
162             m_sBase = ::rtl::OUString();
163             resetParams();
164         }
165 
166     private:
167         ::rtl::OUString         m_sBase     ;
168         ::rtl::OUStringBuffer   m_sParams   ;
169 
170 };      // class QueryBuilder
171 
172 /*-************************************************************************************************************//**
173     @short          Helper class to analyze queries and split into his different parts (base, params and values).
174 *//*-*************************************************************************************************************/
175 class QueryAnalyzer
176 {
177     public:
178 
179         //---------------------------------------------------------------------------------------------------------
180         // it's will not perform to compare strings as query type ...
181         // so we convert it into these enum values.
182         // default = E_ALL!
183         //---------------------------------------------------------------------------------------------------------
184         enum EQuery
185         {
186             E_ALL       ,
187             E_WRITER    ,
188             E_WEB       ,
189             E_GLOBAL    ,
190             E_CHART     ,
191             E_CALC      ,
192             E_IMPRESS   ,
193             E_DRAW      ,
194             E_MATH      ,
195             E_GRAPHICS
196         };
197 
198         //---------------------------------------------------------------------------------------------------------
199         // these are valid values for param "sort_prop".
200         // other ones are not supported!
201         // default = E_NAME
202         //---------------------------------------------------------------------------------------------------------
203         enum ESortProp
204         {
205             E_NAME      ,
206             E_UINAME
207         };
208 
209         //---------------------------------------------------------------------------------------------------------
210         // analyze given query and split it into his different parts; <base>:<param1>:<param2=value>...
211         //---------------------------------------------------------------------------------------------------------
212         QueryAnalyzer( const ::rtl::OUString& sQuery )
213             // Don't forget to set default values for non given params!
214             :   m_eQuery        ( E_ALL      )   // return ALL filter ...
215             ,   m_nIFlags       ( 0          )   // which has set ANY flag ... (we remove all entries which match with these mask .. => 0!)
216             ,   m_nEFlags       ( 0          )   // (only used, if nIFlags==0 and himself!=0!)
217             ,   m_eSortProp     ( E_NAME     )   // sort it by internal name ...
218             ,   m_bDescending   ( sal_False  )   // in ascending order ...
219             ,   m_bCaseSensitive( sal_False  )   // ignore case ...
220             ,   m_bUseOrder     ( sal_False  )   // don't use order flag ...
221             ,   m_bDefaultFirst ( sal_False  )   // and don't handle default entries in special case!
222         {
223             // Translate old query format to new one first!
224             ::rtl::OUString sNewQuery( sQuery );
225             if( sQuery == DECLARE_ASCII("_filterquery_textdocument_withdefault") )
226                 sNewQuery=DECLARE_ASCII("_query_writer:default_first:use_order:sort_prop=uiname");
227             else
228             if( sQuery == DECLARE_ASCII("_filterquery_webdocument_withdefault") )
229                 sNewQuery=DECLARE_ASCII("_query_web:default_first:use_order:sort_prop=uiname");
230             else
231             if( sQuery == DECLARE_ASCII("_filterquery_globaldocument_withdefault") )
232                 sNewQuery=DECLARE_ASCII("_query_global:default_first:use_order:sort_prop=uiname");
233             else
234             if( sQuery == DECLARE_ASCII("_filterquery_chartdocument_withdefault") )
235                 sNewQuery=DECLARE_ASCII("_query_chart:default_first:use_order:sort_prop=uiname");
236             else
237             if( sQuery == DECLARE_ASCII("_filterquery_spreadsheetdocument_withdefault") )
238                 sNewQuery=DECLARE_ASCII("_query_calc:default_first:use_order:sort_prop=uiname");
239             else
240             if( sQuery == DECLARE_ASCII("_filterquery_presentationdocument_withdefault") )
241                 sNewQuery=DECLARE_ASCII("_query_impress:default_first:use_order:sort_prop=uiname");
242             else
243             if( sQuery == DECLARE_ASCII("_filterquery_drawingdocument_withdefault") )
244                 sNewQuery=DECLARE_ASCII("_query_draw:default_first:use_order:sort_prop=uiname");
245             else
246             if( sQuery == DECLARE_ASCII("_filterquery_formulaproperties_withdefault") )
247                 sNewQuery=DECLARE_ASCII("_query_math:default_first:use_order:sort_prop=uiname");
248             else
249             if( sQuery == DECLARE_ASCII("_filterquery_textdocument") )
250                 sNewQuery=DECLARE_ASCII("_query_writer:use_order:sort_prop=uiname");
251             else
252             if( sQuery == DECLARE_ASCII("_filterquery_webdocument") )
253                 sNewQuery=DECLARE_ASCII("_query_web:use_order:sort_prop=uiname");
254             else
255             if( sQuery == DECLARE_ASCII("_filterquery_globaldocument") )
256                 sNewQuery=DECLARE_ASCII("_query_global:use_order:sort_prop=uiname");
257             else
258             if( sQuery == DECLARE_ASCII("_filterquery_chartdocument") )
259                 sNewQuery=DECLARE_ASCII("_query_chart:use_order:sort_prop=uiname");
260             else
261             if( sQuery == DECLARE_ASCII("_filterquery_spreadsheetdocument") )
262                 sNewQuery=DECLARE_ASCII("_query_calc:use_order:sort_prop=uiname");
263             else
264             if( sQuery == DECLARE_ASCII("_filterquery_presentationdocument") )
265                 sNewQuery=DECLARE_ASCII("_query_impress:use_order:sort_prop=uiname");
266             else
267             if( sQuery == DECLARE_ASCII("_filterquery_drawingdocument") )
268                 sNewQuery=DECLARE_ASCII("_query_draw:use_order:sort_prop=uiname");
269             else
270             if( sQuery == DECLARE_ASCII("_filterquery_formulaproperties") )
271                 sNewQuery=DECLARE_ASCII("_query_math:use_order:sort_prop=uiname");
272 
273             // Analyze query ...
274             // Try to find base of it and safe it for faster access as enum value!
275             sal_Int32       nToken = 0;
276             ::rtl::OUString sParam    ;
277             ::rtl::OUString sBase  = sNewQuery.getToken( 0, SEPERATOR_QUERYPARAM, nToken );
278 
279             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_ALL      ) == sal_True ) m_eQuery = E_ALL      ;  else
280             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_WRITER   ) == sal_True ) m_eQuery = E_WRITER   ;  else
281             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_WEB      ) == sal_True ) m_eQuery = E_WEB      ;  else
282             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_GLOBAL   ) == sal_True ) m_eQuery = E_GLOBAL   ;  else
283             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_CHART    ) == sal_True ) m_eQuery = E_CHART    ;  else
284             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_CALC     ) == sal_True ) m_eQuery = E_CALC     ;  else
285             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_IMPRESS  ) == sal_True ) m_eQuery = E_IMPRESS  ;  else
286             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_DRAW     ) == sal_True ) m_eQuery = E_DRAW     ;  else
287             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_MATH     ) == sal_True ) m_eQuery = E_MATH     ;  else
288             if( sBase.equalsIgnoreAsciiCase( BASE_QUERY_GRAPHICS ) == sal_True ) m_eQuery = E_GRAPHICS ;
289 
290             // Try to get additional parameters ...
291             while( nToken >= 0 )
292             {
293                 sParam = sNewQuery.getToken( 0, SEPERATOR_QUERYPARAM, nToken );
294                 // "default_first"
295                 if( sParam.compareTo( QUERYPARAM_DEFAULT_FIRST, QUERYPARAM_DEFAULT_FIRST.getLength() ) == 0 )
296                 {
297                     m_bDefaultFirst = sal_True;
298                 }
299                 else
300                 // "use_order"
301                 if( sParam.compareTo( QUERYPARAM_USE_ORDER, QUERYPARAM_USE_ORDER.getLength() ) == 0 )
302                 {
303                     m_bUseOrder = sal_True;
304                 }
305                 else
306                 // "descending"
307                 if( sParam.compareTo( QUERYPARAM_DESCENDING, QUERYPARAM_DESCENDING.getLength() ) == 0 )
308                 {
309                     m_bDescending = sal_True;
310                 }
311                 else
312                 // "case_sensitive"
313                 if( sParam.compareTo( QUERYPARAM_CASE_SENSITIVE, QUERYPARAM_CASE_SENSITIVE.getLength() ) == 0 )
314                 {
315                     m_bCaseSensitive = sal_True;
316                 }
317                 else
318                 // "iflags=<mask>"
319                 if( sParam.compareTo( QUERYPARAM_IFLAGS, QUERYPARAM_IFLAGS.getLength() ) == 0 )
320                 {
321                     sal_Int32       nSubToken  = 0;
322                     ::rtl::OUString sParamName = sParam.getToken( 0, SEPERATOR_QUERYPARAMVALUE, nSubToken );
323                     if( nSubToken > 0 )
324                     {
325                         m_nIFlags = sParam.getToken( 0, SEPERATOR_QUERYPARAMVALUE, nSubToken ).toInt32();
326                     }
327                 }
328                 else
329                 // "eflags=<mask>"
330                 if( sParam.compareTo( QUERYPARAM_EFLAGS, QUERYPARAM_EFLAGS.getLength() ) == 0 )
331                 {
332                     sal_Int32       nSubToken  = 0;
333                     ::rtl::OUString sParamName = sParam.getToken( 0, SEPERATOR_QUERYPARAMVALUE, nSubToken );
334                     if( nSubToken > 0 )
335                     {
336                         m_nEFlags = sParam.getToken( 0, SEPERATOR_QUERYPARAMVALUE, nSubToken ).toInt32();
337                     }
338                 }
339                 else
340                 // "sort_prop=<[name,uiname]>"
341                 if( sParam.compareTo( QUERYPARAM_SORT_PROP, QUERYPARAM_SORT_PROP.getLength() ) == 0 )
342                 {
343                     sal_Int32       nSubToken  = 0;
344                     ::rtl::OUString sParamName = sParam.getToken( 0, SEPERATOR_QUERYPARAMVALUE, nSubToken );
345                     if( nSubToken > 0 )
346                     {
347                         ::rtl::OUString sParamValue = sParam.getToken( 0, SEPERATOR_QUERYPARAMVALUE, nSubToken );
348                         if( sParamValue.compareTo( QUERYPARAMVALUE_SORT_PROP_NAME, QUERYPARAMVALUE_SORT_PROP_NAME.getLength() ) == 0 )
349                             m_eSortProp = E_NAME;
350                         else
351                         if( sParamValue.compareTo( QUERYPARAMVALUE_SORT_PROP_UINAME, QUERYPARAMVALUE_SORT_PROP_UINAME.getLength() ) == 0 )
352                             m_eSortProp = E_UINAME;
353                     }
354                 }
355             }
356         }
357 
358         //---------------------------------------------------------------------------------------------------------
359         // return type of query. User can decide then, which action should be started.
360         // For faster work we converted query string into corresponding enum value!
361         //---------------------------------------------------------------------------------------------------------
362         EQuery getQueryType() const { return m_eQuery; }
363 
364         //---------------------------------------------------------------------------------------------------------
365         // access to additional parameter values
366         // Methods return default of realy set values!
367         //---------------------------------------------------------------------------------------------------------
368         sal_uInt32 getIFlags       () const { return m_nIFlags       ; }
369         sal_uInt32 getEFlags       () const { return m_nEFlags       ; }
370         ESortProp  getSortProp     () const { return m_eSortProp     ; }
371         sal_Bool   getDescending   () const { return m_bDescending   ; }
372         sal_Bool   getCaseSensitive() const { return m_bCaseSensitive; }
373         sal_Bool   getUseOrder     () const { return m_bUseOrder     ; }
374         sal_Bool   getDefaultFirst () const { return m_bDefaultFirst ; }
375 
376         //---------------------------------------------------------------------------------------------------------
377         // this method checks if given string match any supported query.
378         // (ignore additional parameters!)
379         //---------------------------------------------------------------------------------------------------------
380         static sal_Bool isQuery( const ::rtl::OUString& sQuery )
381         {
382             return(
383                     ( sQuery.compareToAscii( "_query_"      , 7  ) == 0 ) ||    // new style
384                     ( sQuery.compareToAscii( "_filterquery_", 13 ) == 0 )       // old style!
385                   );
386         }
387 
388     private:
389         EQuery      m_eQuery         ;
390         sal_uInt32  m_nIFlags        ;
391         sal_uInt32  m_nEFlags        ;
392         ESortProp   m_eSortProp      ;
393         sal_Bool    m_bDescending    ;
394         sal_Bool    m_bCaseSensitive ;
395         sal_Bool    m_bUseOrder      ;
396         sal_Bool    m_bDefaultFirst  ;
397 
398 };      // class QueryAnalyzer
399 
400 }		//	namespace framework
401 
402 #endif	//	#ifndef __FRAMEWORK_QUERIES_H_
403