1*6d3a6a0bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d3a6a0bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d3a6a0bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d3a6a0bSAndrew Rist  * distributed with this work for additional information
6*6d3a6a0bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d3a6a0bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d3a6a0bSAndrew Rist  * "License"); you may not use this file except in compliance
9*6d3a6a0bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d3a6a0bSAndrew Rist  *
11*6d3a6a0bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d3a6a0bSAndrew Rist  *
13*6d3a6a0bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d3a6a0bSAndrew Rist  * software distributed under the License is distributed on an
15*6d3a6a0bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d3a6a0bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d3a6a0bSAndrew Rist  * specific language governing permissions and limitations
18*6d3a6a0bSAndrew Rist  * under the License.
19*6d3a6a0bSAndrew Rist  *
20*6d3a6a0bSAndrew Rist  *************************************************************/
21*6d3a6a0bSAndrew Rist 
22*6d3a6a0bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //------------------------------------------------------------------------
25cdf0e10cSrcweir // includes
26cdf0e10cSrcweir //------------------------------------------------------------------------
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
30cdf0e10cSrcweir #include "resourceprovider.hxx"
31cdf0e10cSrcweir #include <tools/resmgr.hxx>
32cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
33cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <svtools/svtools.hrc>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //------------------------------------------------------------
38cdf0e10cSrcweir // namespace directives
39cdf0e10cSrcweir //------------------------------------------------------------
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using rtl::OUString;
42cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
43cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //------------------------------------------------------------
46cdf0e10cSrcweir //
47cdf0e10cSrcweir //------------------------------------------------------------
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #define RES_NAME svt
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // because the label of a listbox is
52cdf0e10cSrcweir // a control itself (static text) we
53cdf0e10cSrcweir // have defined a control id for this
54cdf0e10cSrcweir // label which is the listbox control
55cdf0e10cSrcweir // id + 100
56cdf0e10cSrcweir #define LB_LABEL_OFFSET 100
57cdf0e10cSrcweir 
58cdf0e10cSrcweir const rtl::OUString TILDE = OUString::createFromAscii( "~" );
59cdf0e10cSrcweir const sal_Unicode TILDE_SIGN = L'~';
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #define FOLDERPICKER_TITLE            500
62cdf0e10cSrcweir #define FOLDER_PICKER_DEF_DESCRIPTION 501
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //------------------------------------------------------------
65cdf0e10cSrcweir // we have to translate control ids to resource ids
66cdf0e10cSrcweir //------------------------------------------------------------
67cdf0e10cSrcweir 
68cdf0e10cSrcweir struct _Entry
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     sal_Int32 ctrlId;
71cdf0e10cSrcweir     sal_Int16 resId;
72cdf0e10cSrcweir };
73cdf0e10cSrcweir 
74cdf0e10cSrcweir _Entry CtrlIdToResIdTable[] = {
75cdf0e10cSrcweir     { CHECKBOX_AUTOEXTENSION,                   STR_SVT_FILEPICKER_AUTO_EXTENSION },
76cdf0e10cSrcweir     { CHECKBOX_PASSWORD,                        STR_SVT_FILEPICKER_PASSWORD },
77cdf0e10cSrcweir     { CHECKBOX_FILTEROPTIONS,                   STR_SVT_FILEPICKER_FILTER_OPTIONS },
78cdf0e10cSrcweir     { CHECKBOX_READONLY,                        STR_SVT_FILEPICKER_READONLY },
79cdf0e10cSrcweir     { CHECKBOX_LINK,                            STR_SVT_FILEPICKER_INSERT_AS_LINK },
80cdf0e10cSrcweir     { CHECKBOX_PREVIEW,                         STR_SVT_FILEPICKER_SHOW_PREVIEW },
81cdf0e10cSrcweir     { PUSHBUTTON_PLAY,                          STR_SVT_FILEPICKER_PLAY },
82cdf0e10cSrcweir     { LISTBOX_VERSION + LB_LABEL_OFFSET,        STR_SVT_FILEPICKER_VERSION },
83cdf0e10cSrcweir     { LISTBOX_TEMPLATE + LB_LABEL_OFFSET,       STR_SVT_FILEPICKER_TEMPLATES },
84cdf0e10cSrcweir     { LISTBOX_IMAGE_TEMPLATE + LB_LABEL_OFFSET, STR_SVT_FILEPICKER_IMAGE_TEMPLATE },
85cdf0e10cSrcweir     { CHECKBOX_SELECTION,                       STR_SVT_FILEPICKER_SELECTION },
86cdf0e10cSrcweir     { FOLDERPICKER_TITLE,                       STR_SVT_FOLDERPICKER_DEFAULT_TITLE },
87cdf0e10cSrcweir     { FOLDER_PICKER_DEF_DESCRIPTION,            STR_SVT_FOLDERPICKER_DEFAULT_DESCRIPTION }
88cdf0e10cSrcweir };
89cdf0e10cSrcweir 
90cdf0e10cSrcweir const sal_Int32 SIZE_TABLE = sizeof( CtrlIdToResIdTable ) / sizeof( _Entry );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir //------------------------------------------------------------
93cdf0e10cSrcweir //
94cdf0e10cSrcweir //------------------------------------------------------------
95cdf0e10cSrcweir 
CtrlIdToResId(sal_Int32 aControlId)96cdf0e10cSrcweir sal_Int16 CtrlIdToResId( sal_Int32 aControlId )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     sal_Int16 aResId = -1;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < SIZE_TABLE; i++ )
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         if ( CtrlIdToResIdTable[i].ctrlId == aControlId )
103cdf0e10cSrcweir         {
104cdf0e10cSrcweir             aResId = CtrlIdToResIdTable[i].resId;
105cdf0e10cSrcweir             break;
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     return aResId;
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir //------------------------------------------------------------
113cdf0e10cSrcweir //
114cdf0e10cSrcweir //------------------------------------------------------------
115cdf0e10cSrcweir 
116cdf0e10cSrcweir class CResourceProvider_Impl
117cdf0e10cSrcweir {
118cdf0e10cSrcweir public:
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     //-------------------------------------
121cdf0e10cSrcweir     //
122cdf0e10cSrcweir     //-------------------------------------
123cdf0e10cSrcweir 
CResourceProvider_Impl()124cdf0e10cSrcweir     CResourceProvider_Impl( )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         m_ResMgr = CREATEVERSIONRESMGR( RES_NAME );
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     //-------------------------------------
130cdf0e10cSrcweir     //
131cdf0e10cSrcweir     //-------------------------------------
132cdf0e10cSrcweir 
~CResourceProvider_Impl()133cdf0e10cSrcweir     ~CResourceProvider_Impl( )
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         delete m_ResMgr;
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     //-------------------------------------
139cdf0e10cSrcweir     //
140cdf0e10cSrcweir     //-------------------------------------
141cdf0e10cSrcweir 
getResString(sal_Int16 aId)142cdf0e10cSrcweir     OUString getResString( sal_Int16 aId )
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         String   aResString;
145cdf0e10cSrcweir         OUString aResOUString;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         try
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             OSL_ASSERT( m_ResMgr );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir             // translate the control id to a resource id
152cdf0e10cSrcweir             sal_Int16 aResId = CtrlIdToResId( aId );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir             if ( aResId > -1 )
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir                 aResString = String( ResId( aResId, m_ResMgr ) );
157cdf0e10cSrcweir                 aResOUString = OUString( aResString );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                 // remove '~' signs, if there are two '~' signs
160cdf0e10cSrcweir                 // in a row we remove only one of them
161cdf0e10cSrcweir                 if ( aResOUString.indexOf( TILDE ) > -1 )
162cdf0e10cSrcweir                 {
163cdf0e10cSrcweir                     sal_Int32 nStrLen = aResOUString.getLength( );
164cdf0e10cSrcweir                     rtl::OUStringBuffer aBuffer( nStrLen );
165cdf0e10cSrcweir                     sal_Int32 i = 0;
166cdf0e10cSrcweir                     const sal_Unicode* pPos  = aResOUString.getStr( );
167cdf0e10cSrcweir                     const sal_Unicode* pNext = aResOUString.getStr( ) + 1;
168cdf0e10cSrcweir                     const sal_Unicode* pEnd  = aResOUString.getStr( ) + nStrLen;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                     while( pPos < pEnd )
171cdf0e10cSrcweir                     {
172cdf0e10cSrcweir                         // we insert the next character only if the current character
173cdf0e10cSrcweir                         // in not a '~' or the following character is also a '~'
174cdf0e10cSrcweir                         if ( (*pPos != TILDE_SIGN) ||
175cdf0e10cSrcweir                              ((*pPos == TILDE_SIGN) && (pNext < pEnd) && (*pNext == TILDE_SIGN)) )
176cdf0e10cSrcweir                         {
177cdf0e10cSrcweir                             aBuffer.insert( i, *pPos );
178cdf0e10cSrcweir                             i++;
179cdf0e10cSrcweir                         }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                         pPos++;
182cdf0e10cSrcweir                         pNext++;
183cdf0e10cSrcweir                     }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir                     aResOUString = aBuffer.makeStringAndClear( );
186cdf0e10cSrcweir                 }
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir         catch(...)
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         return aResOUString;
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir public:
197cdf0e10cSrcweir     ResMgr* m_ResMgr;
198cdf0e10cSrcweir };
199cdf0e10cSrcweir 
200cdf0e10cSrcweir //------------------------------------------------------------
201cdf0e10cSrcweir //
202cdf0e10cSrcweir //------------------------------------------------------------
203cdf0e10cSrcweir 
CResourceProvider()204cdf0e10cSrcweir CResourceProvider::CResourceProvider( ) :
205cdf0e10cSrcweir     m_pImpl( new CResourceProvider_Impl() )
206cdf0e10cSrcweir {
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir //------------------------------------------------------------
210cdf0e10cSrcweir //
211cdf0e10cSrcweir //------------------------------------------------------------
212cdf0e10cSrcweir 
~CResourceProvider()213cdf0e10cSrcweir CResourceProvider::~CResourceProvider( )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir     delete m_pImpl;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir //------------------------------------------------------------
219cdf0e10cSrcweir //
220cdf0e10cSrcweir //------------------------------------------------------------
221cdf0e10cSrcweir 
getResString(sal_Int32 aId)222cdf0e10cSrcweir OUString CResourceProvider::getResString( sal_Int32 aId )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir    return m_pImpl->getResString( aId );
225cdf0e10cSrcweir }
226