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