1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <vcl/status.hxx>
22 #include <vcl/builder.hxx>
23 #include <vcl/menu.hxx>
24 #include <vcl/image.hxx>
25 #include <vcl/commandevent.hxx>
26 #include <vcl/event.hxx>
27 #include <sfx2/signaturestate.hxx>
28 #include <sfx2/app.hxx>
29 #include <sfx2/module.hxx>
30 #include <sfx2/dispatch.hxx>
31 #include <sfx2/objsh.hxx>
32 #include <sfx2/sfxsids.hrc>
33 
34 #include <svl/intitem.hxx>
35 
36 #include <svl/eitem.hxx>
37 
38 #include <svx/strings.hrc>
39 #include <svx/dialmgr.hxx>
40 #include <svx/xmlsecctrl.hxx>
41 #include <tools/urlobj.hxx>
42 #include <bitmaps.hlst>
43 #include <sal/log.hxx>
44 
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 
47 SFX_IMPL_STATUSBAR_CONTROL( XmlSecStatusBarControl, SfxUInt16Item );
48 
49 struct XmlSecStatusBarControl::XmlSecStatusBarControl_Impl
50 {
51     SignatureState  mnState;
52     Image       maImage;
53     Image       maImageBroken;
54     Image       maImageNotValidated;
55 };
56 
57 
XmlSecStatusBarControl(sal_uInt16 _nSlotId,sal_uInt16 _nId,StatusBar & _rStb)58 XmlSecStatusBarControl::XmlSecStatusBarControl( sal_uInt16 _nSlotId,  sal_uInt16 _nId, StatusBar& _rStb )
59     :SfxStatusBarControl( _nSlotId, _nId, _rStb )
60     ,mpImpl( new XmlSecStatusBarControl_Impl )
61 {
62     mpImpl->mnState = SignatureState::UNKNOWN;
63 
64     mpImpl->maImage             = Image(StockImage::Yes, RID_SVXBMP_SIGNET);
65     mpImpl->maImageBroken       = Image(StockImage::Yes, RID_SVXBMP_SIGNET_BROKEN);
66     mpImpl->maImageNotValidated = Image(StockImage::Yes, RID_SVXBMP_SIGNET_NOTVALIDATED);
67 }
68 
~XmlSecStatusBarControl()69 XmlSecStatusBarControl::~XmlSecStatusBarControl()
70 {
71 }
72 
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem * pState)73 void XmlSecStatusBarControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
74 {
75     if( SfxItemState::DEFAULT != eState )
76     {
77         mpImpl->mnState = SignatureState::UNKNOWN;
78     }
79     else if( auto pUint16Item = dynamic_cast< const SfxUInt16Item* >(pState) )
80     {
81         mpImpl->mnState = static_cast<SignatureState>(pUint16Item->GetValue());
82     }
83     else
84     {
85         SAL_WARN( "svx.stbcrtls", "+XmlSecStatusBarControl::StateChanged(): invalid item type" );
86         mpImpl->mnState = SignatureState::UNKNOWN;
87     }
88 
89     GetStatusBar().SetItemData( GetId(), nullptr ); // necessary ?
90 
91     GetStatusBar().SetItemText( GetId(), "" );    // necessary ?
92 
93     const char* pResId = RID_SVXSTR_XMLSEC_NO_SIG;
94     if ( mpImpl->mnState == SignatureState::OK )
95         pResId = RID_SVXSTR_XMLSEC_SIG_OK;
96     else if ( mpImpl->mnState == SignatureState::BROKEN )
97         pResId = RID_SVXSTR_XMLSEC_SIG_NOT_OK;
98     else if ( mpImpl->mnState == SignatureState::NOTVALIDATED )
99         pResId = RID_SVXSTR_XMLSEC_SIG_OK_NO_VERIFY;
100     else if ( mpImpl->mnState == SignatureState::PARTIAL_OK )
101         pResId = RID_SVXSTR_XMLSEC_SIG_CERT_OK_PARTIAL_SIG;
102 
103     GetStatusBar().SetQuickHelpText(GetId(), SvxResId(pResId));
104 }
105 
Command(const CommandEvent & rCEvt)106 void XmlSecStatusBarControl::Command( const CommandEvent& rCEvt )
107 {
108     if( rCEvt.GetCommand() == CommandEventId::ContextMenu )
109     {
110         VclBuilder aBuilder(nullptr, VclBuilderContainer::getUIRootDir(), "svx/ui/xmlsecstatmenu.ui", "");
111         VclPtr<PopupMenu> aPopupMenu(aBuilder.get_menu("menu"));
112         if (aPopupMenu->Execute(&GetStatusBar(), rCEvt.GetMousePosPixel()))
113         {
114             css::uno::Any a;
115             SfxUInt16Item aState( GetSlotId(), 0 );
116             INetURLObject aObj( m_aCommandURL );
117 
118             css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
119             aArgs[0].Name  = aObj.GetURLPath();
120             aState.QueryValue( a );
121             aArgs[0].Value = a;
122 
123             execute( aArgs );
124         }
125     }
126     else
127         SfxStatusBarControl::Command( rCEvt );
128 }
129 
Paint(const UserDrawEvent & rUsrEvt)130 void XmlSecStatusBarControl::Paint( const UserDrawEvent& rUsrEvt )
131 {
132     vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
133 
134     tools::Rectangle           aRect = rUsrEvt.GetRect();
135     Color               aOldLineColor = pDev->GetLineColor();
136     Color               aOldFillColor = pDev->GetFillColor();
137 
138     pDev->SetLineColor();
139     pDev->SetFillColor( pDev->GetBackground().GetColor() );
140 
141     long yOffset = (aRect.GetHeight() - mpImpl->maImage.GetSizePixel().Height()) / 2;
142 
143     if( mpImpl->mnState == SignatureState::OK )
144     {
145         aRect.AdjustTop(yOffset );
146         pDev->DrawImage( aRect.TopLeft(), mpImpl->maImage );
147     }
148     else if( mpImpl->mnState == SignatureState::BROKEN )
149     {
150         aRect.AdjustTop(yOffset );
151         pDev->DrawImage( aRect.TopLeft(), mpImpl->maImageBroken );
152     }
153     else if( mpImpl->mnState == SignatureState::NOTVALIDATED
154         || mpImpl->mnState == SignatureState::PARTIAL_OK)
155     {
156         aRect.AdjustTop(yOffset );
157         pDev->DrawImage( aRect.TopLeft(), mpImpl->maImageNotValidated );
158     }
159     else
160         pDev->DrawRect( aRect );
161 
162     pDev->SetLineColor( aOldLineColor );
163     pDev->SetFillColor( aOldFillColor );
164 }
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
167