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 //------------------------------------------------------------------------
25 // includes
26 //------------------------------------------------------------------------
27 
28 #include <osl/diagnose.h>
29 #include <rtl/ustrbuf.hxx>
30 #include <vos/mutex.hxx>
31 #include <vcl/svapp.hxx>
32 #include <tools/resmgr.hxx>
33 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
34 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
35 
36 #ifndef _SVTOOLS_SVTOOLS_HRC_
37 #include <svtools/svtools.hrc>
38 #endif
39 
40 #ifndef _SVTOOLS_FILEDLG2_HRC_
41 #include <svtools/filedlg2.hrc>
42 #endif
43 #include "NSString_OOoAdditions.hxx"
44 
45 #include "resourceprovider.hxx"
46 
47 //------------------------------------------------------------
48 // namespace directives
49 //------------------------------------------------------------
50 
51 using rtl::OUString;
52 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
53 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
54 
55 //------------------------------------------------------------
56 //
57 //------------------------------------------------------------
58 
59 static const char* RES_NAME = "fps_office";
60 static const char* OTHER_RES_NAME = "svt";
61 
62 //------------------------------------------------------------
63 // we have to translate control ids to resource ids
64 //------------------------------------------------------------
65 
66 struct _Entry
67 {
68     sal_Int32 ctrlId;
69     sal_Int16 resId;
70 };
71 
72 _Entry CtrlIdToResIdTable[] = {
73     { CHECKBOX_AUTOEXTENSION,                   STR_SVT_FILEPICKER_AUTO_EXTENSION },
74     { CHECKBOX_PASSWORD,                        STR_SVT_FILEPICKER_PASSWORD },
75     { CHECKBOX_FILTEROPTIONS,                   STR_SVT_FILEPICKER_FILTER_OPTIONS },
76     { CHECKBOX_READONLY,                        STR_SVT_FILEPICKER_READONLY },
77     { CHECKBOX_LINK,                            STR_SVT_FILEPICKER_INSERT_AS_LINK },
78     { CHECKBOX_PREVIEW,                         STR_SVT_FILEPICKER_SHOW_PREVIEW },
79     { PUSHBUTTON_PLAY,                          STR_SVT_FILEPICKER_PLAY },
80     { LISTBOX_VERSION_LABEL,                    STR_SVT_FILEPICKER_VERSION },
81     { LISTBOX_TEMPLATE_LABEL,                   STR_SVT_FILEPICKER_TEMPLATES },
82     { LISTBOX_IMAGE_TEMPLATE_LABEL,             STR_SVT_FILEPICKER_IMAGE_TEMPLATE },
83     { CHECKBOX_SELECTION,                       STR_SVT_FILEPICKER_SELECTION },
84     { FOLDERPICKER_TITLE,                       STR_SVT_FOLDERPICKER_DEFAULT_TITLE },
85     { FOLDER_PICKER_DEF_DESCRIPTION,            STR_SVT_FOLDERPICKER_DEFAULT_DESCRIPTION },
86     { FILE_PICKER_OVERWRITE,                    STR_SVT_ALREADYEXISTOVERWRITE },
87     { LISTBOX_FILTER_LABEL,                     STR_SVT_FILEPICKER_FILTER_TITLE}
88 };
89 
90 _Entry OtherCtrlIdToResIdTable[] = {
91     { FILE_PICKER_TITLE_OPEN,                   STR_FILEDLG_OPEN },
92     { FILE_PICKER_TITLE_SAVE,                   STR_FILEDLG_SAVE },
93     { FILE_PICKER_FILE_TYPE,                    STR_FILEDLG_TYPE }
94 };
95 
96 
97 const sal_Int32 SIZE_TABLE = sizeof( CtrlIdToResIdTable ) / sizeof( _Entry );
98 const sal_Int32 OTHER_SIZE_TABLE = sizeof( OtherCtrlIdToResIdTable ) / sizeof( _Entry );
99 
100 //------------------------------------------------------------
101 //
102 //------------------------------------------------------------
103 
CtrlIdToResId(sal_Int32 aControlId)104 sal_Int16 CtrlIdToResId( sal_Int32 aControlId )
105 {
106     sal_Int16 aResId = -1;
107 
108     for ( sal_Int32 i = 0; i < SIZE_TABLE; i++ )
109     {
110         if ( CtrlIdToResIdTable[i].ctrlId == aControlId )
111         {
112             aResId = CtrlIdToResIdTable[i].resId;
113             break;
114         }
115     }
116 
117     return aResId;
118 }
119 
OtherCtrlIdToResId(sal_Int32 aControlId)120 sal_Int16 OtherCtrlIdToResId( sal_Int32 aControlId )
121 {
122     sal_Int16 aResId = -1;
123 
124     for ( sal_Int32 i = 0; i < OTHER_SIZE_TABLE; i++ )
125     {
126         if ( OtherCtrlIdToResIdTable[i].ctrlId == aControlId )
127         {
128             aResId = OtherCtrlIdToResIdTable[i].resId;
129             break;
130         }
131     }
132 
133     return aResId;
134 }
135 
136 //------------------------------------------------------------
137 //
138 //------------------------------------------------------------
139 
140 class CResourceProvider_Impl
141 {
142 public:
143 
144     //-------------------------------------
145     //
146     //-------------------------------------
147 
CResourceProvider_Impl()148     CResourceProvider_Impl( )
149     {
150         m_ResMgr = ResMgr::CreateResMgr( RES_NAME );
151         m_OtherResMgr = ResMgr::CreateResMgr( OTHER_RES_NAME );
152     }
153 
154     //-------------------------------------
155     //
156     //-------------------------------------
157 
~CResourceProvider_Impl()158     ~CResourceProvider_Impl( )
159     {
160         delete m_ResMgr;
161         delete m_OtherResMgr;
162     }
163 
164     //-------------------------------------
165     //
166     //-------------------------------------
167 
getResString(sal_Int16 aId)168     NSString* getResString( sal_Int16 aId )
169     {
170         String   aResString;
171         OUString aResOUString;
172 
173         const ::vos::OGuard aGuard( Application::GetSolarMutex() );
174 
175         try
176         {
177             OSL_ASSERT( m_ResMgr && m_OtherResMgr );
178 
179             // translate the control id to a resource id
180             sal_Int16 aResId = CtrlIdToResId( aId );
181             if ( aResId > -1 )
182                 aResString = String( ResId( aResId, *m_ResMgr ) );
183             else
184             {
185                 aResId = OtherCtrlIdToResId( aId );
186                 if ( aResId > -1 ) {
187                     aResString = String( ResId( aResId, *m_OtherResMgr ) );
188                 }
189             }
190             if ( aResId > -1 )
191                 aResOUString = OUString( aResString );
192         }
193         catch(...)
194         {
195         }
196 
197         return [NSString stringWithOUString:aResOUString];
198     }
199 
200 public:
201     ResMgr* m_ResMgr;
202     ResMgr* m_OtherResMgr;
203 };
204 
205 //------------------------------------------------------------
206 //
207 //------------------------------------------------------------
208 
CResourceProvider()209 CResourceProvider::CResourceProvider( ) :
210     m_pImpl( new CResourceProvider_Impl() )
211 {
212 }
213 
214 //------------------------------------------------------------
215 //
216 //------------------------------------------------------------
217 
~CResourceProvider()218 CResourceProvider::~CResourceProvider( )
219 {
220     delete m_pImpl;
221 }
222 
223 //------------------------------------------------------------
224 //
225 //------------------------------------------------------------
226 
getResString(sal_Int32 aId)227 NSString* CResourceProvider::getResString( sal_Int32 aId )
228 {
229     NSString* sImmutable = m_pImpl->getResString( aId );
230     NSMutableString *sMutableString = [NSMutableString stringWithString:sImmutable];
231     [sMutableString replaceOccurrencesOfString:@"~" withString:@"" options:0 range:NSMakeRange(0, [sMutableString length])];
232 
233     NSString *result = [NSString stringWithString:sMutableString];
234 
235     return result;
236 }
237