1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: resourcemanager.cxx,v $
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>
45 using ::rtl::OUString
;
50 static ResMgr
* pResMgr
= 0;
51 static SvtSysLocale
* pSysLocale
= 0;
53 ResMgr
* GetResMgr( void )
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() );
68 const LocaleDataWrapper
& GetLocaleData( void )
71 pSysLocale
= new SvtSysLocale
;
72 return pSysLocale
->GetLocaleData();
75 DateTime
GetDateTime( const ::com::sun::star::util::DateTime
& _rDT
)
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
) );
89 sRet
+= rLoDa
.getTime( aDT
);
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
);
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
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
);
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.
154 vector
< pair
< OUString
, OUString
> > parseDN(const OUString
& rRawString
)
156 vector
< pair
<OUString
, OUString
> > retVal
;
157 bool bInEscape
= false;
158 bool bInValue
= false;
160 sal_Int32 nTypeNameStart
= 0;
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
];
173 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
174 sType
= sType
.trim();
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] == '"')
193 bInValue
= !bInValue
; //value is enclosed in " "
197 //This quote is escaped by a preceding quote and therefore is
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
210 OSL_ASSERT(sType
.getLength());
211 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
213 //The next char is the start of the new type
214 nTypeNameStart
= i
+ 1;
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
231 if (sbufValue
.getLength())
233 OSL_ASSERT(sType
.getLength());
234 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
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;
246 sal_Int32 nTypeNameStart
= 0;
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
];
259 sType
= rRawString
.copy(nTypeNameStart
, i
- nTypeNameStart
);
260 sType
= sType
.trim();
275 { // bInEscape is true
282 //an unescaped '"' is either at the beginning or end of the value
292 //This quote is escaped by a preceding quote and therefore is
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
305 OSL_ASSERT(sType
.getLength());
306 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
308 //The next char is the start of the new type
309 nTypeNameStart
= i
+ 1;
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
329 if (sbufValue
.getLength())
331 OSL_ASSERT(sType
.getLength());
332 retVal
.push_back(make_pair(sType
, sbufValue
.makeStringAndClear()));
339 String
GetContentPart( const String
& _rRawString
)
341 char const * aIDs
[] = { "CN", "OU", "O", "E", NULL
};
344 vector
< pair
< OUString
, OUString
> > vecAttrValueOfDN
= parseDN(_rRawString
);
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
;
357 if (retVal
.getLength())
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();
368 const char pHexDigs
[ 17 ] = "0123456789ABCDEF";
369 char pBuffer
[ 3 ] = " ";
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 ];
380 pBuffer
[ 0 ] = pHexDigs
[ nNum
];
381 aStr
.AppendAscii( pBuffer
);
385 aStr
.AppendAscii( _pSep
);
388 nBreak
= nBreakStart
;
389 aStr
.AppendAscii( "\n" );
396 long ShrinkToFitWidth( Control
& _rCtrl
, long _nOffs
)
398 long nWidth
= _rCtrl
.GetTextWidth( _rCtrl
.GetText() );
399 Size
aSize( _rCtrl
.GetSizePixel() );
401 aSize
.Width() = nWidth
;
402 _rCtrl
.SetSizePixel( aSize
);
406 void AlignAfterImage( const FixedImage
& _rImage
, Control
& _rCtrl
, long _nXOffset
)
408 Point
aPos( _rImage
.GetPosPixel() );
409 Size
aSize( _rImage
.GetSizePixel() );
415 n
+= aSize
.Height() / 2; // y-position is in the middle of the image
416 n
-= _rCtrl
.GetSizePixel().Height() / 2; // center Control
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
);