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