1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
36 static ResMgr
* pResMgr
= 0;
37 static SvtSysLocale
* pSysLocale
= 0;
39 ResMgr
* GetResMgr( void )
42 pResMgr
= ResMgr::CreateResMgr("xmlsec");
46 const LocaleDataWrapper
& GetLocaleData( void )
49 pSysLocale
= new SvtSysLocale
;
50 return pSysLocale
->GetLocaleData();
53 DateTime
GetDateTime( const ::com::sun::star::util::DateTime
& _rDT
)
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
) );
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
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'));
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.
112 vector
< pair
< OUString
, OUString
> > parseDN(const OUString
& rRawString
)
114 vector
< pair
<OUString
, OUString
> > retVal
;
115 bool bInEscape
= false;
116 bool bInValue
= false;
118 sal_Int32 nTypeNameStart
= 0;
120 OUStringBuffer sbufValue
;
121 sal_Int32 length
= rRawString
.getLength();
123 for (sal_Int32 i
= 0; i
< length
; i
++)
125 sal_Unicode c
= rRawString
[i
];
131 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
132 sType
= sType
.trim();
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] == '"')
151 bInValue
= !bInValue
; //value is enclosed in " "
155 //This quote is escaped by a preceding quote and therefore is
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
168 OSL_ASSERT(!sType
.isEmpty());
169 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
171 //The next char is the start of the new type
172 nTypeNameStart
= i
+ 1;
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
189 if (sbufValue
.getLength())
191 OSL_ASSERT(!sType
.isEmpty());
192 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
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;
204 sal_Int32 nTypeNameStart
= 0;
206 OUStringBuffer sbufValue
;
207 sal_Int32 length
= rRawString
.getLength();
209 for (sal_Int32 i
= 0; i
< length
; i
++)
211 sal_Unicode c
= rRawString
[i
];
217 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
218 sType
= sType
.trim();
233 { // bInEscape is true
240 //an unescaped '"' is either at the beginning or end of the value
250 //This quote is escaped by a preceding quote and therefore is
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
263 OSL_ASSERT(!sType
.isEmpty());
264 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
266 //The next char is the start of the new type
267 nTypeNameStart
= i
+ 1;
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
287 if (sbufValue
.getLength())
289 OSL_ASSERT(!sType
.isEmpty());
290 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
297 OUString
GetContentPart( const OUString
& _rRawString
)
299 char const * aIDs
[] = { "CN", "OU", "O", "E", NULL
};
302 vector
< pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
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
;
315 if (!retVal
.isEmpty())
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();
326 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
327 char pBuffer
[ 3 ] = " ";
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 ];
338 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
339 aStr
.appendAscii( pBuffer
);
343 aStr
.appendAscii( _pSep
);
346 nBreak
= nBreakStart
;
351 return aStr
.makeStringAndClear();
354 long ShrinkToFitWidth( Control
& _rCtrl
, long _nOffs
)
356 long nWidth
= _rCtrl
.GetTextWidth( _rCtrl
.GetText() );
357 Size
aSize( _rCtrl
.GetSizePixel() );
359 aSize
.Width() = nWidth
;
360 _rCtrl
.SetSizePixel( aSize
);
364 void AlignAfterImage( const FixedImage
& _rImage
, Control
& _rCtrl
, long _nXOffset
)
366 Point
aPos( _rImage
.GetPosPixel() );
367 Size
aSize( _rImage
.GetSizePixel() );
373 n
+= aSize
.Height() / 2; // y-position is in the middle of the image
374 n
-= _rCtrl
.GetSizePixel().Height() / 2; // center Control
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: */