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 _FILTERHELPER_HXX_
25 #define _FILTERHELPER_HXX_
26 
27 // #ifndef _COM_SUN_STAR_UNO_TYPE_HXX_
28 // #include <com/sun/star/uno/Type.hxx>
29 // #endif
30 
31 // #ifndef INCLUDED_CPPU_UNOTYPE_HXX
32 // #include <cppu/unotype.hxx>
33 // #endif
34 
35 // #ifndef _TYPELIB_TYPECLASS_H_
36 // #include "typelib/typeclass.h"
37 // #endif
38 
39 // #ifndef _TYPELIB_TYPEDESCRIPTION_H_
40 // #include "typelib/typedescription.h"
41 // #endif
42 
43 #include <com/sun/star/beans/StringPair.hpp>
44 #include <com/sun/star/uno/Sequence.hxx>
45 
46 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HXX_
47 #include <com/sun/star/lang/IllegalArgumentException.hpp>
48 #endif
49 
50 #ifndef _COM_SUN_STAR_UNO_RUNTIMEEXCEPTION_HXX_
51 #include <com/sun/star/uno/RuntimeException.hpp>
52 #endif
53 
54 // #ifndef _RTL_USTRING_H_
55 // #include <rtl/ustring.hxx>
56 // #endif
57 
58 #include <list>
59 #include <vector>
60 
61 #include <premac.h>
62 #include <Cocoa/Cocoa.h>
63 #include <postmac.h>
64 
65 typedef ::com::sun::star::beans::StringPair UnoFilterEntry;
66 typedef ::com::sun::star::uno::Sequence< UnoFilterEntry >   UnoFilterList;  // can be transported more effectively
67 typedef ::std::list<NSString *> NSStringList;
68 typedef ::std::list<rtl::OUString> OUStringList;
69 
70 struct FilterEntry
71 {
72 protected:
73     rtl::OUString       m_sTitle;
74     OUStringList        m_sFilterSuffixList;
75     UnoFilterList       m_aSubFilters;
76 
77 public:
FilterEntryFilterEntry78     FilterEntry( const rtl::OUString& _rTitle, const OUStringList _rFilter )
79     : m_sTitle( _rTitle )
80     , m_sFilterSuffixList( _rFilter )
81     {
82     }
83 
84     FilterEntry( const rtl::OUString& _rTitle, const UnoFilterList& _rSubFilters );
85 
getTitleFilterEntry86     rtl::OUString       getTitle() const { return m_sTitle; }
getFilterSuffixListFilterEntry87     OUStringList    getFilterSuffixList() const { return m_sFilterSuffixList; }
88 
89     /// determines if the filter has sub filter (i.e., the filter is a filter group in real)
90     sal_Bool        hasSubFilters( ) const;
91 
92     /** retrieves the filters belonging to the entry
93         @return
94         the number of sub filters
95         */
96     sal_Int32       getSubFilters( UnoFilterList& _rSubFilterList );
97 
98     // helpers for iterating the sub filters
beginSubFiltersFilterEntry99     const UnoFilterEntry*   beginSubFilters() const { return m_aSubFilters.getConstArray(); }
endSubFiltersFilterEntry100     const UnoFilterEntry*   endSubFilters() const { return m_aSubFilters.getConstArray() + m_aSubFilters.getLength(); }
101 };
102 
103 typedef ::std::vector < FilterEntry >       FilterList;
104 
105 class FilterHelper {
106 
107 public:
108     FilterHelper();
109     virtual ~FilterHelper();
110 
111     //XFilterManager delegates
112     void SAL_CALL appendFilter( const ::rtl::OUString& aTitle, const ::rtl::OUString& aFilter )
113         throw( ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException );
114 
115     void SAL_CALL setCurrentFilter( const ::rtl::OUString& aTitle )
116         throw( ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException );
117 
118     ::rtl::OUString SAL_CALL getCurrentFilter(  )
119         throw( ::com::sun::star::uno::RuntimeException );
120 
121     //XFilterGroupManager delegates
122     void SAL_CALL appendFilterGroup( const ::rtl::OUString& sGroupTitle, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::StringPair >& aFilters )
123         throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
124 
125 
126     //accessor
127     FilterList* getFilterList();
128     NSStringList* getFilterNames();
129 
130     //misc
131     void SetCurFilter( const rtl::OUString& rFilter );
132     void SetFilterAtIndex(unsigned index);
133     OUStringList getCurrentFilterSuffixList();
134     int getCurrentFilterIndex();
135     void SetFilters();
136     sal_Bool filenameMatchesFilter(NSString * sFilename);
137 
138 private:
139     FilterList *m_pFilterList;
140     rtl::OUString m_aCurrentFilter;
141     NSStringList *m_pFilterNames;
142 
143     int implAddFilter( const rtl::OUString rFilter, const OUStringList rSuffixList);
144     int implAddFilterGroup( const rtl::OUString rFilter,
145                             const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::StringPair>& _rFilters );
146 
147     sal_Bool FilterNameExists( const rtl::OUString rTitle );
148     sal_Bool FilterNameExists( const UnoFilterList& _rGroupedFilters );
149 
150     void ensureFilterList( const ::rtl::OUString& _rInitialCurrentFilter );
151 
152     void fillSuffixList(OUStringList& aSuffixList, const ::rtl::OUString& suffixString);
153 
154 };
155 
156 #endif //_FILTERHELPER_HXX_
157