update credits
[LibreOffice.git] / xmlsecurity / source / dialogs / resourcemanager.cxx
blob2611960df02875ff327ec942c3820523325c8288
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "resourcemanager.hxx"
23 #include <vcl/svapp.hxx>
24 #include <vcl/fixed.hxx>
25 #include <svtools/stdctrl.hxx>
26 #include <svl/solar.hrc>
27 #include <unotools/syslocale.hxx>
28 #include <rtl/ustring.h>
29 #include <rtl/ustrbuf.h>
30 #include <vector>
32 using namespace std;
34 namespace XmlSec
36 static ResMgr* pResMgr = 0;
37 static SvtSysLocale* pSysLocale = 0;
39 ResMgr* GetResMgr( void )
41 if (!pResMgr)
42 pResMgr = ResMgr::CreateResMgr("xmlsec");
43 return pResMgr;
46 const LocaleDataWrapper& GetLocaleData( void )
48 if (!pSysLocale)
49 pSysLocale = new SvtSysLocale;
50 return pSysLocale->GetLocaleData();
53 DateTime GetDateTime( const ::com::sun::star::util::DateTime& _rDT )
55 return DateTime(
56 Date( _rDT.Day, _rDT.Month, _rDT.Year ),
57 Time( _rDT.Hours, _rDT.Minutes, _rDT.Seconds, _rDT.NanoSeconds ) );
60 OUString GetDateTimeString( const ::com::sun::star::util::DateTime& _rDT )
62 // String with date and time information (#i20172#)
63 DateTime aDT( GetDateTime( _rDT ) );
64 const LocaleDataWrapper& rLoDa = GetLocaleData();
65 OUStringBuffer sRet( rLoDa.getDate( aDT ) );
66 sRet.append( ' ' );
67 sRet.append( rLoDa.getTime( aDT ) );
68 return sRet.makeStringAndClear();
71 OUString GetDateString( const ::com::sun::star::util::DateTime& _rDT )
73 return GetLocaleData().getDate( GetDateTime( _rDT ) );
77 Creates two strings based on the distinguished name which are displayed in the
78 certificate details view. The first string contains only the values of the attribute
79 and valudes pairs, which are separated by commas. All escape characters ('"') are
80 removed.
81 The second string is for the details view at the bottom. It shows the attribute/value
82 pairs on different lines. All escape characters ('"') are removed.
84 pair< OUString, OUString> GetDNForCertDetailsView( const OUString & rRawString)
86 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(rRawString);
87 OUStringBuffer s1, s2;
88 OUString sEqual(" = ");
89 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
90 for (CIT i = vecAttrValueOfDN.begin(); i < vecAttrValueOfDN.end(); ++i)
92 if (i != vecAttrValueOfDN.begin())
94 s1.append(static_cast<sal_Unicode>(','));
95 s2.append(static_cast<sal_Unicode>('\n'));
97 s1.append(i->second);
98 s2.append(i->first);
99 s2.append(sEqual);
100 s2.append(i->second);
102 return make_pair(s1.makeStringAndClear(), s2.makeStringAndClear());
106 Whenever the attribute value contains special characters, such as '"' or ',' (without '')
107 then the value will be enclosed in double quotes by the respective Windows or NSS function
108 which we use to retrieve, for example, the subject name. If double quotes appear in the value then
109 they are escaped with a double quote. This function removes the escape characters.
111 #ifdef WNT
112 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
114 vector< pair<OUString, OUString> > retVal;
115 bool bInEscape = false;
116 bool bInValue = false;
117 bool bInType = true;
118 sal_Int32 nTypeNameStart = 0;
119 OUString sType;
120 OUStringBuffer sbufValue;
121 sal_Int32 length = rRawString.getLength();
123 for (sal_Int32 i = 0; i < length; i++)
125 sal_Unicode c = rRawString[i];
127 if (c == '=')
129 if (! bInValue)
131 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
132 sType = sType.trim();
133 bInType = false;
135 else
137 sbufValue.append(c);
140 else if (c == '"')
142 if (!bInEscape)
144 //If this is the quote is the first of the couple which enclose the
145 //whole value, because the value contains special characters
146 //then we just drop it. That is, this character must be followed by
147 //a character which is not '"'.
148 if ( i + 1 < length && rRawString[i+1] == '"')
149 bInEscape = true;
150 else
151 bInValue = !bInValue; //value is enclosed in " "
153 else
155 //This quote is escaped by a preceding quote and therefore is
156 //part of the value
157 sbufValue.append(c);
158 bInEscape = false;
161 else if (c == ',' || c == '+')
163 //The comma separate the attribute value pairs.
164 //If the comma is not part of a value (the value would then be enclosed in '"'),
165 //then we have reached the end of the value
166 if (!bInValue)
168 OSL_ASSERT(!sType.isEmpty());
169 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
170 sType = OUString();
171 //The next char is the start of the new type
172 nTypeNameStart = i + 1;
173 bInType = true;
175 else
177 //The whole string is enclosed because it contains special characters.
178 //The enclosing '"' are not part of certificate but will be added by
179 //the function (Windows or NSS) which retrieves DN
180 sbufValue.append(c);
183 else
185 if (!bInType)
186 sbufValue.append(c);
189 if (sbufValue.getLength())
191 OSL_ASSERT(!sType.isEmpty());
192 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
194 return retVal;
196 #else
197 vector< pair< OUString, OUString> > parseDN(const OUString& rRawString)
199 vector< pair<OUString, OUString> > retVal;
200 //bInEscape == true means that the preceding character is an escape character
201 bool bInEscape = false;
202 bool bInValue = false;
203 bool bInType = true;
204 sal_Int32 nTypeNameStart = 0;
205 OUString sType;
206 OUStringBuffer sbufValue;
207 sal_Int32 length = rRawString.getLength();
209 for (sal_Int32 i = 0; i < length; i++)
211 sal_Unicode c = rRawString[i];
213 if (c == '=')
215 if (! bInValue)
217 sType = rRawString.copy(nTypeNameStart, i - nTypeNameStart);
218 sType = sType.trim();
219 bInType = false;
221 else
223 sbufValue.append(c);
226 else if (c == '\\')
228 if (!bInEscape)
230 bInEscape = true;
232 else
233 { // bInEscape is true
234 sbufValue.append(c);
235 bInEscape = false;
238 else if (c == '"')
240 //an unescaped '"' is either at the beginning or end of the value
241 if (!bInEscape)
243 if ( !bInValue)
244 bInValue = true;
245 else if (bInValue)
246 bInValue = false;
248 else
250 //This quote is escaped by a preceding quote and therefore is
251 //part of the value
252 sbufValue.append(c);
253 bInEscape = false;
256 else if (c == ',' || c == '+')
258 //The comma separate the attribute value pairs.
259 //If the comma is not part of a value (the value would then be enclosed in '"'),
260 //then we have reached the end of the value
261 if (!bInValue)
263 OSL_ASSERT(!sType.isEmpty());
264 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
265 sType = OUString();
266 //The next char is the start of the new type
267 nTypeNameStart = i + 1;
268 bInType = true;
270 else
272 //The whole string is enclosed because it contains special characters.
273 //The enclosing '"' are not part of certificate but will be added by
274 //the function (Windows or NSS) which retrieves DN
275 sbufValue.append(c);
278 else
280 if (!bInType)
282 sbufValue.append(c);
283 bInEscape = false;
287 if (sbufValue.getLength())
289 OSL_ASSERT(!sType.isEmpty());
290 retVal.push_back(make_pair(sType, sbufValue.makeStringAndClear()));
292 return retVal;
295 #endif
297 OUString GetContentPart( const OUString& _rRawString )
299 char const * aIDs[] = { "CN", "OU", "O", "E", NULL };
300 OUString retVal;
301 int i = 0;
302 vector< pair< OUString, OUString > > vecAttrValueOfDN = parseDN(_rRawString);
303 while ( aIDs[i] )
305 OUString sPartId = OUString::createFromAscii( aIDs[i++] );
306 typedef vector< pair < OUString, OUString > >::const_iterator CIT;
307 for (CIT idn = vecAttrValueOfDN.begin(); idn != vecAttrValueOfDN.end(); ++idn)
309 if (idn->first.equals(sPartId))
311 retVal = idn->second;
312 break;
315 if (!retVal.isEmpty())
316 break;
318 return retVal;
321 OUString GetHexString( const ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, const char* _pSep, sal_uInt16 _nLineBreak )
323 const sal_Int8* pSerNumSeq = _rSeq.getConstArray();
324 int nCnt = _rSeq.getLength();
325 OUStringBuffer aStr;
326 const char pHexDigs[ 17 ] = "0123456789ABCDEF";
327 char pBuffer[ 3 ] = " ";
328 sal_uInt8 nNum;
329 sal_uInt16 nBreakStart = _nLineBreak? _nLineBreak : 1;
330 sal_uInt16 nBreak = nBreakStart;
331 for( int i = 0 ; i < nCnt ; ++i )
333 nNum = sal_uInt8( pSerNumSeq[ i ] );
335 // exchange the buffer[0] and buffer[1], which make it consistent with Mozilla and Windows
336 pBuffer[ 1 ] = pHexDigs[ nNum & 0x0F ];
337 nNum >>= 4;
338 pBuffer[ 0 ] = pHexDigs[ nNum ];
339 aStr.appendAscii( pBuffer );
341 --nBreak;
342 if( nBreak )
343 aStr.appendAscii( _pSep );
344 else
346 nBreak = nBreakStart;
347 aStr.append( '\n' );
351 return aStr.makeStringAndClear();
354 long ShrinkToFitWidth( Control& _rCtrl, long _nOffs )
356 long nWidth = _rCtrl.GetTextWidth( _rCtrl.GetText() );
357 Size aSize( _rCtrl.GetSizePixel() );
358 nWidth += _nOffs;
359 aSize.Width() = nWidth;
360 _rCtrl.SetSizePixel( aSize );
361 return nWidth;
364 void AlignAfterImage( const FixedImage& _rImage, Control& _rCtrl, long _nXOffset )
366 Point aPos( _rImage.GetPosPixel() );
367 Size aSize( _rImage.GetSizePixel() );
368 long n = aPos.X();
369 n += aSize.Width();
370 n += _nXOffset;
371 aPos.X() = n;
372 n = aPos.Y();
373 n += aSize.Height() / 2; // y-position is in the middle of the image
374 n -= _rCtrl.GetSizePixel().Height() / 2; // center Control
375 aPos.Y() = n;
376 _rCtrl.SetPosPixel( aPos );
379 void AlignAfterImage( const FixedImage& _rImage, FixedInfo& _rFI, long _nXOffset )
381 AlignAfterImage( _rImage, static_cast< Control& >( _rFI ), _nXOffset );
382 ShrinkToFitWidth( _rFI );
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */