merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / dialogs / resourcemanager.cxx
blobe41cf546f74fa1b447cc109f1a2792a03ecdd181
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "resourcemanager.hxx"
33 #include <vcl/svapp.hxx>
34 #include <vcl/fixed.hxx>
35 #include <svtools/stdctrl.hxx>
36 #include <svl/solar.hrc>
37 #include <unotools/syslocale.hxx>
38 #include <rtl/ustring.h>
39 #include <rtl/ustrbuf.h>
40 #include <vector>
42 using ::rtl::OUString;
43 using namespace std;
45 namespace XmlSec
47 static ResMgr* pResMgr = 0;
48 static SvtSysLocale* pSysLocale = 0;
50 ResMgr* GetResMgr( void )
52 if( !pResMgr )
54 ByteString aName( "xmlsec" );
55 // pResMgr = ResMgr::CreateResMgr( aName.GetBuffer(), Application::GetSettings().GetUILanguage() );
56 // LanguageType aLang( LANGUAGE_ENGLISH_US );
57 // pResMgr = ResMgr::CreateResMgr( aName.GetBuffer(), aLang );
58 // MT: Change to Locale
59 pResMgr = ResMgr::CreateResMgr( aName.GetBuffer() );
62 return pResMgr;
65 const LocaleDataWrapper& GetLocaleData( void )
67 if (!pSysLocale)
68 pSysLocale = new SvtSysLocale;
69 return pSysLocale->GetLocaleData();
72 DateTime GetDateTime( const ::com::sun::star::util::DateTime& _rDT )
74 return DateTime(
75 Date( _rDT.Day, _rDT.Month, _rDT.Year ),
76 Time( _rDT.Hours, _rDT.Minutes, _rDT.Seconds, _rDT.HundredthSeconds ) );
79 String GetDateTimeString( const ::com::sun::star::util::DateTime& _rDT )
81 // --> PB 2004-10-12 #i20172# String with date and time information
82 DateTime aDT( GetDateTime( _rDT ) );
83 const LocaleDataWrapper& rLoDa = GetLocaleData();
84 String sRet( rLoDa.getDate( aDT ) );
85 sRet += ' ';
86 sRet += rLoDa.getTime( aDT );
87 return sRet;
90 String GetDateTimeString( const rtl::OUString& _rDate, const rtl::OUString& _rTime )
92 String sDay( _rDate, 6, 2 );
93 String sMonth( _rDate, 4, 2 );
94 String sYear( _rDate, 0, 4 );
96 String sHour( _rTime, 0, 2 );
97 String sMin( _rTime, 4, 2 );
98 String sSec( _rTime, 6, 2 );
101 Date aDate( (USHORT)sDay.ToInt32(), (USHORT) sMonth.ToInt32(), (USHORT)sYear.ToInt32() );
102 Time aTime( sHour.ToInt32(), sMin.ToInt32(), sSec.ToInt32(), 0 );
103 const LocaleDataWrapper& rLoDa = GetLocaleData();
104 String aStr( rLoDa.getDate( aDate ) );
105 aStr.AppendAscii( " " );
106 aStr += rLoDa.getTime( aTime );
107 return aStr;
110 String GetDateString( const ::com::sun::star::util::DateTime& _rDT )
112 return GetLocaleData().getDate( GetDateTime( _rDT ) );
116 Creates two strings based on the distinguished name which are displayed in the
117 certificate details view. The first string contains only the values of the attribute
118 and valudes pairs, which are separated by commas. All escape characters ('"') are
119 removed.
120 The second string is for the details view at the bottom. It shows the attribute/value
121 pairs on different lines. All escape characters ('"') are removed.
123 pair< OUString, OUString> GetDNForCertDetailsView( const OUString & rRawString)
125 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(rRawString);
126 ::rtl::OUStringBuffer s1, s2;
127 OUString sEqual(RTL_CONSTASCII_USTRINGPARAM(" = "));
128 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
129 for (CIT i = vecAttrValueOfDN.begin(); i < vecAttrValueOfDN.end(); i ++)
131 if (i != vecAttrValueOfDN.begin())
133 s1.append(static_cast<sal_Unicode>(','));
134 s2.append(static_cast<sal_Unicode>('\n'));
136 s1.append(i->second);
137 s2.append(i->first);
138 s2.append(sEqual);
139 s2.append(i->second);
141 return make_pair(s1.makeStringAndClear(), s2.makeStringAndClear());
145 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
146 then the value will be enclosed in double quotes by the respective Windows or NSS function
147 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
148 they are escaped with a double quote. This function removes the escape characters.
150 #ifdef WNT
151 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
153 vector< pair<OUString, OUString> > retVal;
154 bool bInEscape = false;
155 bool bInValue = false;
156 bool bInType = true;
157 sal_Int32 nTypeNameStart = 0;
158 OUString sType;
159 ::rtl::OUStringBuffer sbufValue;
160 sal_Int32 length = rRawString.getLength();
162 for (sal_Int32 i = 0; i < length; i++)
164 sal_Unicode c = rRawString[i];
166 if (c == '=')
168 if (! bInValue)
170 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
171 sType = sType.trim();
172 bInType = false;
174 else
176 sbufValue.append(c);
179 else if (c == '"')
181 if (!bInEscape)
183 //If this is the quote is the first of the couple which enclose the
184 //whole value, because the value contains special characters
185 //then we just drop it. That is, this character must be followed by
186 //a character which is not '"'.
187 if ( i + 1 < length && rRawString[i+1] == '"')
188 bInEscape = true;
189 else
190 bInValue = !bInValue; //value is enclosed in " "
192 else
194 //This quote is escaped by a preceding quote and therefore is
195 //part of the value
196 sbufValue.append(c);
197 bInEscape = false;
200 else if (c == ',' || c == '+')
202 //The comma separate the attribute value pairs.
203 //If the comma is not part of a value (the value would then be enclosed in '"'),
204 //then we have reached the end of the value
205 if (!bInValue)
207 OSL_ASSERT(sType.getLength());
208 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
209 sType = OUString();
210 //The next char is the start of the new type
211 nTypeNameStart = i + 1;
212 bInType = true;
214 else
216 //The whole string is enclosed because it contains special characters.
217 //The enclosing '"' are not part of certificate but will be added by
218 //the function (Windows or NSS) which retrieves DN
219 sbufValue.append(c);
222 else
224 if (!bInType)
225 sbufValue.append(c);
228 if (sbufValue.getLength())
230 OSL_ASSERT(sType.getLength());
231 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
233 return retVal;
235 #else
236 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
238 vector< pair<OUString, OUString> > retVal;
239 //bInEscape == true means that the preceding character is an escape character
240 bool bInEscape = false;
241 bool bInValue = false;
242 bool bInType = true;
243 sal_Int32 nTypeNameStart = 0;
244 OUString sType;
245 ::rtl::OUStringBuffer sbufValue;
246 sal_Int32 length = rRawString.getLength();
248 for (sal_Int32 i = 0; i < length; i++)
250 sal_Unicode c = rRawString[i];
252 if (c == '=')
254 if (! bInValue)
256 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
257 sType = sType.trim();
258 bInType = false;
260 else
262 sbufValue.append(c);
265 else if (c == '\\')
267 if (!bInEscape)
269 bInEscape = true;
271 else
272 { // bInEscape is true
273 sbufValue.append(c);
274 bInEscape = false;
277 else if (c == '"')
279 //an unescaped '"' is either at the beginning or end of the value
280 if (!bInEscape)
282 if ( !bInValue)
283 bInValue = true;
284 else if (bInValue)
285 bInValue = false;
287 else
289 //This quote is escaped by a preceding quote and therefore is
290 //part of the value
291 sbufValue.append(c);
292 bInEscape = false;
295 else if (c == ',' || c == '+')
297 //The comma separate the attribute value pairs.
298 //If the comma is not part of a value (the value would then be enclosed in '"'),
299 //then we have reached the end of the value
300 if (!bInValue)
302 OSL_ASSERT(sType.getLength());
303 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
304 sType = OUString();
305 //The next char is the start of the new type
306 nTypeNameStart = i + 1;
307 bInType = true;
309 else
311 //The whole string is enclosed because it contains special characters.
312 //The enclosing '"' are not part of certificate but will be added by
313 //the function (Windows or NSS) which retrieves DN
314 sbufValue.append(c);
317 else
319 if (!bInType)
321 sbufValue.append(c);
322 bInEscape = false;
326 if (sbufValue.getLength())
328 OSL_ASSERT(sType.getLength());
329 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
331 return retVal;
334 #endif
336 String GetContentPart( const String& _rRawString )
338 char const * aIDs[] = { "CN", "OU", "O", "E", NULL };
339 OUString retVal;
340 int i = 0;
341 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
342 while ( aIDs[i] )
344 OUString sPartId = OUString::createFromAscii( aIDs[i++] );
345 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
346 for (CIT idn = vecAttrValueOfDN.begin(); idn != vecAttrValueOfDN.end(); idn++)
348 if (idn->first.equals(sPartId))
350 retVal = idn->second;
351 break;
354 if (retVal.getLength())
355 break;
357 return retVal;
360 String GetHexString( const ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, UINT16 _nLineBreak )
362 const sal_Int8* pSerNumSeq = _rSeq.getConstArray();
363 int nCnt = _rSeq.getLength();
364 String aStr;
365 const char pHexDigs[ 17 ] = "0123456789ABCDEF";
366 char pBuffer[ 3 ] = " ";
367 UINT8 nNum;
368 UINT16 nBreakStart = _nLineBreak? _nLineBreak : 1;
369 UINT16 nBreak = nBreakStart;
370 for( int i = 0 ; i < nCnt ; ++i )
372 nNum = UINT8( pSerNumSeq[ i ] );
374 //MM : exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
375 pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
376 nNum >>= 4;
377 pBuffer[ 0 ] = pHexDigs[ nNum ];
378 aStr.AppendAscii( pBuffer );
380 --nBreak;
381 if( nBreak )
382 aStr.AppendAscii( _pSep );
383 else
385 nBreak = nBreakStart;
386 aStr.AppendAscii( "\n" );
390 return aStr;
393 long ShrinkToFitWidth( Control& _rCtrl, long _nOffs )
395 long nWidth = _rCtrl.GetTextWidth( _rCtrl.GetText() );
396 Size aSize( _rCtrl.GetSizePixel() );
397 nWidth += _nOffs;
398 aSize.Width() = nWidth;
399 _rCtrl.SetSizePixel( aSize );
400 return nWidth;
403 void AlignAfterImage( const FixedImage& _rImage, Control& _rCtrl, long _nXOffset )
405 Point aPos( _rImage.GetPosPixel() );
406 Size aSize( _rImage.GetSizePixel() );
407 long n = aPos.X();
408 n += aSize.Width();
409 n += _nXOffset;
410 aPos.X() = n;
411 n = aPos.Y();
412 n += aSize.Height() / 2; // y-position is in the middle of the image
413 n -= _rCtrl.GetSizePixel().Height() / 2; // center Control
414 aPos.Y() = n;
415 _rCtrl.SetPosPixel( aPos );
418 void AlignAfterImage( const FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
420 AlignAfterImage( _rImage, static_cast< Control& >( _rFI ), _nXOffset );
421 ShrinkToFitWidth( _rFI );
424 void AlignAndFitImageAndControl( FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
426 _rImage.SetSizePixel( _rImage.GetImage().GetSizePixel() );
427 AlignAfterImage( _rImage, _rFI, _nXOffset );