merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / source / dialogs / resourcemanager.cxx
blobbd0694de41667225db39f0ab23fb2605bc115303
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: resourcemanager.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include "resourcemanager.hxx"
36 #include <vcl/svapp.hxx>
37 #include <vcl/fixed.hxx>
38 #include <svtools/stdctrl.hxx>
39 #include <svtools/solar.hrc>
40 #include <svtools/syslocale.hxx>
41 #include <rtl/ustring.h>
42 #include <rtl/ustrbuf.h>
43 #include <vector>
45 using ::rtl::OUString;
46 using namespace std;
48 namespace XmlSec
50 static ResMgr* pResMgr = 0;
51 static SvtSysLocale* pSysLocale = 0;
53 ResMgr* GetResMgr( void )
55 if( !pResMgr )
57 ByteString aName( "xmlsec" );
58 // pResMgr = ResMgr::CreateResMgr( aName.GetBuffer(), Application::GetSettings().GetUILanguage() );
59 // LanguageType aLang( LANGUAGE_ENGLISH_US );
60 // pResMgr = ResMgr::CreateResMgr( aName.GetBuffer(), aLang );
61 // MT: Change to Locale
62 pResMgr = ResMgr::CreateResMgr( aName.GetBuffer() );
65 return pResMgr;
68 const LocaleDataWrapper& GetLocaleData( void )
70 if (!pSysLocale)
71 pSysLocale = new SvtSysLocale;
72 return pSysLocale->GetLocaleData();
75 DateTime GetDateTime( const ::com::sun::star::util::DateTime& _rDT )
77 return DateTime(
78 Date( _rDT.Day, _rDT.Month, _rDT.Year ),
79 Time( _rDT.Hours, _rDT.Minutes, _rDT.Seconds, _rDT.HundredthSeconds ) );
82 String GetDateTimeString( const ::com::sun::star::util::DateTime& _rDT )
84 // --> PB 2004-10-12 #i20172# String with date and time information
85 DateTime aDT( GetDateTime( _rDT ) );
86 const LocaleDataWrapper& rLoDa = GetLocaleData();
87 String sRet( rLoDa.getDate( aDT ) );
88 sRet += ' ';
89 sRet += rLoDa.getTime( aDT );
90 return sRet;
93 String GetDateTimeString( const rtl::OUString& _rDate, const rtl::OUString& _rTime )
95 String sDay( _rDate, 6, 2 );
96 String sMonth( _rDate, 4, 2 );
97 String sYear( _rDate, 0, 4 );
99 String sHour( _rTime, 0, 2 );
100 String sMin( _rTime, 4, 2 );
101 String sSec( _rTime, 6, 2 );
104 Date aDate( (USHORT)sDay.ToInt32(), (USHORT) sMonth.ToInt32(), (USHORT)sYear.ToInt32() );
105 Time aTime( sHour.ToInt32(), sMin.ToInt32(), sSec.ToInt32(), 0 );
106 const LocaleDataWrapper& rLoDa = GetLocaleData();
107 String aStr( rLoDa.getDate( aDate ) );
108 aStr.AppendAscii( " " );
109 aStr += rLoDa.getTime( aTime );
110 return aStr;
113 String GetDateString( const ::com::sun::star::util::DateTime& _rDT )
115 return GetLocaleData().getDate( GetDateTime( _rDT ) );
119 Creates two strings based on the distinguished name which are displayed in the
120 certificate details view. The first string contains only the values of the attribute
121 and valudes pairs, which are separated by commas. All escape characters ('"') are
122 removed.
123 The second string is for the details view at the bottom. It shows the attribute/value
124 pairs on different lines. All escape characters ('"') are removed.
126 pair< OUString, OUString> GetDNForCertDetailsView( const OUString & rRawString)
128 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(rRawString);
129 ::rtl::OUStringBuffer s1, s2;
130 OUString sEqual(RTL_CONSTASCII_USTRINGPARAM(" = "));
131 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
132 for (CIT i = vecAttrValueOfDN.begin(); i < vecAttrValueOfDN.end(); i ++)
134 if (i != vecAttrValueOfDN.begin())
136 s1.append(static_cast<sal_Unicode>(','));
137 s2.append(static_cast<sal_Unicode>('\n'));
139 s1.append(i->second);
140 s2.append(i->first);
141 s2.append(sEqual);
142 s2.append(i->second);
144 return make_pair(s1.makeStringAndClear(), s2.makeStringAndClear());
148 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
149 then the value will be enclosed in double quotes by the respective Windows or NSS function
150 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
151 they are escaped with a double quote. This function removes the escape characters.
153 #ifdef WNT
154 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
156 vector< pair<OUString, OUString> > retVal;
157 bool bInEscape = false;
158 bool bInValue = false;
159 bool bInType = true;
160 sal_Int32 nTypeNameStart = 0;
161 OUString sType;
162 ::rtl::OUStringBuffer sbufValue;
163 sal_Int32 length = rRawString.getLength();
165 for (sal_Int32 i = 0; i < length; i++)
167 sal_Unicode c = rRawString[i];
169 if (c == '=')
171 if (! bInValue)
173 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
174 sType = sType.trim();
175 bInType = false;
177 else
179 sbufValue.append(c);
182 else if (c == '"')
184 if (!bInEscape)
186 //If this is the quote is the first of the couple which enclose the
187 //whole value, because the value contains special characters
188 //then we just drop it. That is, this character must be followed by
189 //a character which is not '"'.
190 if ( i + 1 < length && rRawString[i+1] == '"')
191 bInEscape = true;
192 else
193 bInValue = !bInValue; //value is enclosed in " "
195 else
197 //This quote is escaped by a preceding quote and therefore is
198 //part of the value
199 sbufValue.append(c);
200 bInEscape = false;
203 else if (c == ',')
205 //The comma separate the attribute value pairs.
206 //If the comma is not part of a value (the value would then be enclosed in '"'),
207 //then we have reached the end of the value
208 if (!bInValue)
210 OSL_ASSERT(sType.getLength());
211 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
212 sType = OUString();
213 //The next char is the start of the new type
214 nTypeNameStart = i + 1;
215 bInType = true;
217 else
219 //The whole string is enclosed because it contains special characters.
220 //The enclosing '"' are not part of certificate but will be added by
221 //the function (Windows or NSS) which retrieves DN
222 sbufValue.append(c);
225 else
227 if (!bInType)
228 sbufValue.append(c);
231 if (sbufValue.getLength())
233 OSL_ASSERT(sType.getLength());
234 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
236 return retVal;
238 #else
239 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
241 vector< pair<OUString, OUString> > retVal;
242 //bInEscape == true means that the preceding character is an escape character
243 bool bInEscape = false;
244 bool bInValue = false;
245 bool bInType = true;
246 sal_Int32 nTypeNameStart = 0;
247 OUString sType;
248 ::rtl::OUStringBuffer sbufValue;
249 sal_Int32 length = rRawString.getLength();
251 for (sal_Int32 i = 0; i < length; i++)
253 sal_Unicode c = rRawString[i];
255 if (c == '=')
257 if (! bInValue)
259 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
260 sType = sType.trim();
261 bInType = false;
263 else
265 sbufValue.append(c);
268 else if (c == '\\')
270 if (!bInEscape)
272 bInEscape = true;
274 else
275 { // bInEscape is true
276 sbufValue.append(c);
277 bInEscape = false;
280 else if (c == '"')
282 //an unescaped '"' is either at the beginning or end of the value
283 if (!bInEscape)
285 if ( !bInValue)
286 bInValue = true;
287 else if (bInValue)
288 bInValue = false;
290 else
292 //This quote is escaped by a preceding quote and therefore is
293 //part of the value
294 sbufValue.append(c);
295 bInEscape = false;
298 else if (c == ',')
300 //The comma separate the attribute value pairs.
301 //If the comma is not part of a value (the value would then be enclosed in '"'),
302 //then we have reached the end of the value
303 if (!bInValue)
305 OSL_ASSERT(sType.getLength());
306 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
307 sType = OUString();
308 //The next char is the start of the new type
309 nTypeNameStart = i + 1;
310 bInType = true;
312 else
314 //The whole string is enclosed because it contains special characters.
315 //The enclosing '"' are not part of certificate but will be added by
316 //the function (Windows or NSS) which retrieves DN
317 sbufValue.append(c);
320 else
322 if (!bInType)
324 sbufValue.append(c);
325 bInEscape = false;
329 if (sbufValue.getLength())
331 OSL_ASSERT(sType.getLength());
332 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
334 return retVal;
337 #endif
339 String GetContentPart( const String& _rRawString )
341 char const * aIDs[] = { "CN", "OU", "O", "E", NULL };
342 OUString retVal;
343 int i = 0;
344 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
345 while ( aIDs[i] )
347 OUString sPartId = OUString::createFromAscii( aIDs[i++] );
348 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
349 for (CIT idn = vecAttrValueOfDN.begin(); idn != vecAttrValueOfDN.end(); idn++)
351 if (idn->first.equals(sPartId))
353 retVal = idn->second;
354 break;
357 if (retVal.getLength())
358 break;
360 return retVal;
363 String GetHexString( const ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, UINT16 _nLineBreak )
365 const sal_Int8* pSerNumSeq = _rSeq.getConstArray();
366 int nCnt = _rSeq.getLength();
367 String aStr;
368 const char pHexDigs[ 17 ] = "0123456789ABCDEF";
369 char pBuffer[ 3 ] = " ";
370 UINT8 nNum;
371 UINT16 nBreakStart = _nLineBreak? _nLineBreak : 1;
372 UINT16 nBreak = nBreakStart;
373 for( int i = 0 ; i < nCnt ; ++i )
375 nNum = UINT8( pSerNumSeq[ i ] );
377 //MM : exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
378 pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
379 nNum >>= 4;
380 pBuffer[ 0 ] = pHexDigs[ nNum ];
381 aStr.AppendAscii( pBuffer );
383 --nBreak;
384 if( nBreak )
385 aStr.AppendAscii( _pSep );
386 else
388 nBreak = nBreakStart;
389 aStr.AppendAscii( "\n" );
393 return aStr;
396 long ShrinkToFitWidth( Control& _rCtrl, long _nOffs )
398 long nWidth = _rCtrl.GetTextWidth( _rCtrl.GetText() );
399 Size aSize( _rCtrl.GetSizePixel() );
400 nWidth += _nOffs;
401 aSize.Width() = nWidth;
402 _rCtrl.SetSizePixel( aSize );
403 return nWidth;
406 void AlignAfterImage( const FixedImage& _rImage, Control& _rCtrl, long _nXOffset )
408 Point aPos( _rImage.GetPosPixel() );
409 Size aSize( _rImage.GetSizePixel() );
410 long n = aPos.X();
411 n += aSize.Width();
412 n += _nXOffset;
413 aPos.X() = n;
414 n = aPos.Y();
415 n += aSize.Height() / 2; // y-position is in the middle of the image
416 n -= _rCtrl.GetSizePixel().Height() / 2; // center Control
417 aPos.Y() = n;
418 _rCtrl.SetPosPixel( aPos );
421 void AlignAfterImage( const FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
423 AlignAfterImage( _rImage, static_cast< Control& >( _rFI ), _nXOffset );
424 ShrinkToFitWidth( _rFI );
427 void AlignAndFitImageAndControl( FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
429 _rImage.SetSizePixel( _rImage.GetImage().GetSizePixel() );
430 AlignAfterImage( _rImage, _rFI, _nXOffset );