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 <sfx2/securitypage.hxx>
23 #include <sfx2/sfxresid.hxx>
25 #include <sfx2/sfx.hrc>
26 #include <sfx2/sfxsids.hrc>
27 #include <sfx2/objsh.hxx>
28 #include <sfx2/viewsh.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <sfx2/passwd.hxx>
32 #include <vcl/button.hxx>
33 #include <vcl/edit.hxx>
34 #include <vcl/fixed.hxx>
35 #include <vcl/msgbox.hxx>
36 #include <svl/eitem.hxx>
37 #include <svl/poolitem.hxx>
38 #include <svl/intitem.hxx>
39 #include <svl/PasswordHelper.hxx>
40 #include <svtools/xwindowitem.hxx>
42 #include "../appl/app.hrc"
45 using namespace ::com::sun::star
;
51 enum RedliningMode
{ RL_NONE
, RL_WRITER
, RL_CALC
};
52 enum RedlineFunc
{ RF_ON
, RF_PROTECT
};
54 bool QueryState( sal_uInt16 _nSlot
, bool& _rValue
)
57 SfxViewShell
* pViewSh
= SfxViewShell::Current();
60 const SfxPoolItem
* pItem
;
61 SfxDispatcher
* pDisp
= pViewSh
->GetDispatcher();
62 SfxItemState nState
= pDisp
->QueryState( _nSlot
, pItem
);
63 bRet
= SfxItemState::DEFAULT
<= nState
;
65 _rValue
= ( static_cast< const SfxBoolItem
* >( pItem
) )->GetValue();
71 bool QueryRecordChangesProtectionState( RedliningMode _eMode
, bool& _rValue
)
74 if (_eMode
!= RL_NONE
)
76 sal_uInt16 nSlot
= _eMode
== RL_WRITER
? FN_REDLINE_PROTECT
: SID_CHG_PROTECT
;
77 bRet
= QueryState( nSlot
, _rValue
);
83 bool QueryRecordChangesState( RedliningMode _eMode
, bool& _rValue
)
86 if (_eMode
!= RL_NONE
)
88 sal_uInt16 nSlot
= _eMode
== RL_WRITER
? FN_REDLINE_ON
: FID_CHG_RECORD
;
89 bRet
= QueryState( nSlot
, _rValue
);
96 static bool lcl_GetPassword(
99 /*out*/OUString
&rPassword
)
102 ScopedVclPtrInstance
< SfxPasswordDialog
> aPasswdDlg(pParent
);
103 aPasswdDlg
->SetMinLen( 1 );
105 aPasswdDlg
->ShowExtras( SfxShowExtras::CONFIRM
);
106 if (RET_OK
== aPasswdDlg
->Execute() && !aPasswdDlg
->GetPassword().isEmpty())
108 rPassword
= aPasswdDlg
->GetPassword();
115 static bool lcl_IsPasswordCorrect( const OUString
&rPassword
)
119 SfxObjectShell
* pCurDocShell
= SfxObjectShell::Current();
120 uno::Sequence
< sal_Int8
> aPasswordHash
;
121 pCurDocShell
->GetProtectionHash( aPasswordHash
);
123 // check if supplied password was correct
124 uno::Sequence
< sal_Int8
> aNewPasswd( aPasswordHash
);
125 SvPasswordHelper::GetHashPassword( aNewPasswd
, rPassword
);
126 if (SvPasswordHelper::CompareHashPassword( aPasswordHash
, rPassword
))
127 bRes
= true; // password was correct
129 ScopedVclPtr
<InfoBox
>::Create( nullptr, SFX2_RESSTR(RID_SVXSTR_INCORRECT_PASSWORD
) )->Execute();
135 struct SfxSecurityPage_Impl
137 SfxSecurityPage
& m_rMyTabPage
;
139 VclPtr
<CheckBox
> m_pOpenReadonlyCB
;
140 VclPtr
<CheckBox
> m_pRecordChangesCB
; // for record changes
141 VclPtr
<PushButton
> m_pProtectPB
; // for record changes
142 VclPtr
<PushButton
> m_pUnProtectPB
; // for record changes
143 RedliningMode m_eRedlingMode
; // for record changes
145 bool m_bOrigPasswordIsConfirmed
;
146 bool m_bNewPasswordIsValid
;
147 OUString m_aNewPassword
;
149 OUString m_aEndRedliningWarning
;
150 bool m_bEndRedliningWarningDone
;
152 DECL_LINK( RecordChangesCBToggleHdl
, void* );
153 DECL_LINK( ChangeProtectionPBHdl
, void* );
155 SfxSecurityPage_Impl( SfxSecurityPage
&rDlg
, const SfxItemSet
&rItemSet
);
156 ~SfxSecurityPage_Impl();
158 bool FillItemSet_Impl( SfxItemSet
& );
159 void Reset_Impl( const SfxItemSet
& );
163 SfxSecurityPage_Impl::SfxSecurityPage_Impl( SfxSecurityPage
&rTabPage
, const SfxItemSet
& ) :
164 m_rMyTabPage (rTabPage
),
165 m_eRedlingMode ( RL_NONE
),
166 m_bOrigPasswordIsConfirmed ( false ),
167 m_bNewPasswordIsValid ( false ),
168 m_aEndRedliningWarning ( SFX2_RESSTR(RID_SVXSTR_END_REDLINING_WARNING
) ),
169 m_bEndRedliningWarningDone ( false )
171 rTabPage
.get(m_pOpenReadonlyCB
, "readonly");
172 rTabPage
.get(m_pRecordChangesCB
, "recordchanges");
173 rTabPage
.get(m_pProtectPB
, "protect");
174 rTabPage
.get(m_pUnProtectPB
, "unprotect");
175 m_pProtectPB
->Show();
176 m_pUnProtectPB
->Hide();
178 // force toggle hdl called before visual change of checkbox
179 m_pRecordChangesCB
->SetStyle( m_pRecordChangesCB
->GetStyle() | WB_EARLYTOGGLE
);
180 m_pRecordChangesCB
->SetToggleHdl( LINK( this, SfxSecurityPage_Impl
, RecordChangesCBToggleHdl
) );
181 m_pProtectPB
->SetClickHdl( LINK( this, SfxSecurityPage_Impl
, ChangeProtectionPBHdl
) );
182 m_pUnProtectPB
->SetClickHdl( LINK( this, SfxSecurityPage_Impl
, ChangeProtectionPBHdl
) );
186 SfxSecurityPage_Impl::~SfxSecurityPage_Impl()
191 bool SfxSecurityPage_Impl::FillItemSet_Impl( SfxItemSet
& )
193 bool bModified
= false;
195 SfxObjectShell
* pCurDocShell
= SfxObjectShell::Current();
196 if (pCurDocShell
&& !pCurDocShell
->IsReadOnly())
198 if (m_eRedlingMode
!= RL_NONE
)
200 const bool bDoRecordChanges
= m_pRecordChangesCB
->IsChecked();
201 const bool bDoChangeProtection
= m_pUnProtectPB
->IsVisible();
204 DBG_ASSERT( bDoRecordChanges
|| !bDoChangeProtection
, "no change recording should imply no change protection" );
205 DBG_ASSERT( bDoChangeProtection
|| !bDoRecordChanges
, "no change protection should imply no change recording" );
206 DBG_ASSERT( !bDoChangeProtection
|| !m_aNewPassword
.isEmpty(), "change protection should imply password length is > 0" );
207 DBG_ASSERT( bDoChangeProtection
|| m_aNewPassword
.isEmpty(), "no change protection should imply password length is 0" );
210 if (bDoRecordChanges
!= pCurDocShell
->IsChangeRecording())
212 pCurDocShell
->SetChangeRecording( bDoRecordChanges
);
216 // change record protection
217 if (m_bNewPasswordIsValid
&&
218 bDoChangeProtection
!= pCurDocShell
->HasChangeRecordProtection())
220 DBG_ASSERT( !bDoChangeProtection
|| bDoRecordChanges
,
221 "change protection requires record changes to be active!" );
222 pCurDocShell
->SetProtectionPassword( m_aNewPassword
);
228 const bool bDoOpenReadonly
= m_pOpenReadonlyCB
->IsChecked();
229 if (bDoOpenReadonly
!= pCurDocShell
->IsSecurityOptOpenReadOnly())
231 pCurDocShell
->SetSecurityOptOpenReadOnly( bDoOpenReadonly
);
240 void SfxSecurityPage_Impl::Reset_Impl( const SfxItemSet
& )
242 SfxObjectShell
* pCurDocShell
= SfxObjectShell::Current();
246 // no doc -> hide document settings
247 m_pOpenReadonlyCB
->Disable();
248 m_pRecordChangesCB
->Disable();
249 m_pProtectPB
->Show();
250 m_pProtectPB
->Disable();
251 m_pUnProtectPB
->Hide();
252 m_pUnProtectPB
->Disable();
256 bool bIsHTMLDoc
= false;
257 bool bProtect
= true, bUnProtect
= false;
258 SfxViewShell
* pViewSh
= SfxViewShell::Current();
261 const SfxPoolItem
* pItem
;
262 SfxDispatcher
* pDisp
= pViewSh
->GetDispatcher();
263 if (SfxItemState::DEFAULT
<= pDisp
->QueryState( SID_HTML_MODE
, pItem
))
265 sal_uInt16 nMode
= static_cast< const SfxUInt16Item
* >( pItem
)->GetValue();
266 bIsHTMLDoc
= ( ( nMode
& HTMLMODE_ON
) != 0 );
270 bool bIsReadonly
= pCurDocShell
->IsReadOnly();
273 m_pOpenReadonlyCB
->Check( pCurDocShell
->IsSecurityOptOpenReadOnly() );
274 m_pOpenReadonlyCB
->Enable( !bIsReadonly
);
277 m_pOpenReadonlyCB
->Disable();
280 if (QueryRecordChangesState( RL_WRITER
, bRecordChanges
) && !bIsHTMLDoc
)
281 m_eRedlingMode
= RL_WRITER
;
282 else if (QueryRecordChangesState( RL_CALC
, bRecordChanges
))
283 m_eRedlingMode
= RL_CALC
;
285 m_eRedlingMode
= RL_NONE
;
287 if (m_eRedlingMode
!= RL_NONE
)
289 bool bProtection(false);
290 QueryRecordChangesProtectionState( m_eRedlingMode
, bProtection
);
292 m_pProtectPB
->Enable( !bIsReadonly
);
293 m_pUnProtectPB
->Enable( !bIsReadonly
);
294 // set the right text
301 m_pRecordChangesCB
->Check( bRecordChanges
);
302 m_pRecordChangesCB
->Enable( /*!bProtection && */!bIsReadonly
);
304 m_bOrigPasswordIsConfirmed
= true; // default case if no password is set
305 uno::Sequence
< sal_Int8
> aPasswordHash
;
306 // check if password is available
307 if (pCurDocShell
->GetProtectionHash( aPasswordHash
) &&
308 aPasswordHash
.getLength() > 0)
309 m_bOrigPasswordIsConfirmed
= false; // password found, needs to be confirmed later on
313 // A Calc document that is shared will have 'm_eRedlingMode == RL_NONE'
314 // In shared documents change recording and protection must be disabled,
315 // similar to documents that do not support change recording at all.
316 m_pRecordChangesCB
->Check( false );
317 m_pRecordChangesCB
->Disable();
318 m_pProtectPB
->Check( false );
319 m_pUnProtectPB
->Check( false );
320 m_pProtectPB
->Disable();
321 m_pUnProtectPB
->Disable();
324 m_pProtectPB
->Show(bProtect
);
325 m_pUnProtectPB
->Show(bUnProtect
);
330 IMPL_LINK_NOARG(SfxSecurityPage_Impl
, RecordChangesCBToggleHdl
)
332 // when change recording gets disabled protection must be disabled as well
333 if (!m_pRecordChangesCB
->IsChecked()) // the new check state is already present, thus the '!'
335 bool bAlreadyDone
= false;
336 if (!m_bEndRedliningWarningDone
)
338 ScopedVclPtrInstance
<WarningBox
> aBox(m_rMyTabPage
.GetParent(), WinBits(WB_YES_NO
| WB_DEF_NO
),
339 m_aEndRedliningWarning
);
340 if (aBox
->Execute() != RET_YES
)
343 m_bEndRedliningWarningDone
= true;
346 const bool bNeedPasssword
= !m_bOrigPasswordIsConfirmed
347 && m_pProtectPB
->IsVisible();
348 if (!bAlreadyDone
&& bNeedPasssword
)
350 OUString aPasswordText
;
352 // dialog canceled or no password provided
353 if (!lcl_GetPassword( m_rMyTabPage
.GetParent(), false, aPasswordText
))
356 // ask for password and if dialog is canceled or no password provided return
357 if (lcl_IsPasswordCorrect( aPasswordText
))
358 m_bOrigPasswordIsConfirmed
= true;
364 m_pRecordChangesCB
->Check( true ); // restore original state
367 // remember required values to change protection and change recording in
368 // FillItemSet_Impl later on if password was correct.
369 m_bNewPasswordIsValid
= true;
370 m_aNewPassword
.clear();
371 m_pProtectPB
->Show();
372 m_pUnProtectPB
->Hide();
380 IMPL_LINK_NOARG(SfxSecurityPage_Impl
, ChangeProtectionPBHdl
)
382 if (m_eRedlingMode
== RL_NONE
)
385 // the push button text is always the opposite of the current state. Thus:
386 const bool bCurrentProtection
= m_pUnProtectPB
->IsVisible();
388 // ask user for password (if still necessary)
389 OUString aPasswordText
;
390 bool bNewProtection
= !bCurrentProtection
;
391 const bool bNeedPassword
= bNewProtection
|| !m_bOrigPasswordIsConfirmed
;
394 // ask for password and if dialog is canceled or no password provided return
395 if (!lcl_GetPassword( m_rMyTabPage
.GetParent(), bNewProtection
, aPasswordText
))
398 // provided password still needs to be checked?
399 if (!bNewProtection
&& !m_bOrigPasswordIsConfirmed
)
401 if (lcl_IsPasswordCorrect( aPasswordText
))
402 m_bOrigPasswordIsConfirmed
= true;
407 DBG_ASSERT( m_bOrigPasswordIsConfirmed
, "ooops... this should not have happened!" );
409 // remember required values to change protection and change recording in
410 // FillItemSet_Impl later on if password was correct.
411 m_bNewPasswordIsValid
= true;
412 m_aNewPassword
= bNewProtection
? aPasswordText
: OUString();
414 m_pRecordChangesCB
->Check( bNewProtection
);
416 m_pUnProtectPB
->Show(bNewProtection
);
417 m_pProtectPB
->Show(!bNewProtection
);
423 VclPtr
<SfxTabPage
> SfxSecurityPage::Create( vcl::Window
* pParent
, const SfxItemSet
* rItemSet
)
425 return VclPtr
<SfxSecurityPage
>::Create( pParent
, *rItemSet
);
429 SfxSecurityPage::SfxSecurityPage( vcl::Window
* pParent
, const SfxItemSet
& rItemSet
)
430 : SfxTabPage(pParent
, "SecurityInfoPage", "sfx/ui/securityinfopage.ui", &rItemSet
)
432 m_pImpl
.reset(new SfxSecurityPage_Impl( *this, rItemSet
));
437 bool SfxSecurityPage::FillItemSet( SfxItemSet
* rItemSet
)
439 bool bModified
= false;
440 DBG_ASSERT( m_pImpl
.get(), "implementation pointer is 0. Still in c-tor?" );
441 if (m_pImpl
.get() != 0)
442 bModified
= m_pImpl
->FillItemSet_Impl( *rItemSet
);
447 void SfxSecurityPage::Reset( const SfxItemSet
* rItemSet
)
449 DBG_ASSERT( m_pImpl
.get(), "implementation pointer is 0. Still in c-tor?" );
450 if (m_pImpl
.get() != 0)
451 m_pImpl
->Reset_Impl( *rItemSet
);
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */