1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sfx2/securitypage.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "securitypage.hrc"
30cdf0e10cSrcweir #include "sfxresid.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <sfx2/sfx.hrc>
33cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
34cdf0e10cSrcweir #include <sfx2/objsh.hxx>
35cdf0e10cSrcweir #include <sfx2/viewsh.hxx>
36cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
37cdf0e10cSrcweir #include <sfx2/passwd.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <vcl/button.hxx>
40cdf0e10cSrcweir #include <vcl/edit.hxx>
41cdf0e10cSrcweir #include <vcl/fixed.hxx>
42cdf0e10cSrcweir #include <vcl/msgbox.hxx>
43cdf0e10cSrcweir #include <svl/eitem.hxx>
44cdf0e10cSrcweir #include <svl/poolitem.hxx>
45cdf0e10cSrcweir #include <svl/intitem.hxx>
46cdf0e10cSrcweir #include <svl/PasswordHelper.hxx>
47cdf0e10cSrcweir #include <svtools/xwindowitem.hxx>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir namespace
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     enum RedliningMode  { RL_NONE, RL_WRITER, RL_CALC };
58cdf0e10cSrcweir     enum RedlineFunc    { RF_ON, RF_PROTECT };
59cdf0e10cSrcweir 
60cdf0e10cSrcweir /*
61cdf0e10cSrcweir     bool QueryIsEnabled( sal_uInt16 _nSlot )
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         bool bRes = false;
64cdf0e10cSrcweir         SfxViewShell* pViewSh = SfxViewShell::Current();
65cdf0e10cSrcweir         if (pViewSh)
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir             const SfxPoolItem* pItem;
68cdf0e10cSrcweir             SfxDispatcher* pDisp = pViewSh->GetDispatcher();
69cdf0e10cSrcweir             SfxItemState eState = pDisp->QueryState( _nSlot, pItem );
70cdf0e10cSrcweir             bRes = (eState & SFX_ITEM_DISABLED) == 0;
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         return bRes;
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir */
75cdf0e10cSrcweir 
QueryState(sal_uInt16 _nSlot,bool & _rValue)76cdf0e10cSrcweir     bool QueryState( sal_uInt16 _nSlot, bool& _rValue )
77cdf0e10cSrcweir     {
78cdf0e10cSrcweir         bool bRet = false;
79cdf0e10cSrcweir         SfxViewShell* pViewSh = SfxViewShell::Current();
80cdf0e10cSrcweir         if (pViewSh)
81cdf0e10cSrcweir         {
82cdf0e10cSrcweir             const SfxPoolItem* pItem;
83cdf0e10cSrcweir             SfxDispatcher* pDisp = pViewSh->GetDispatcher();
84cdf0e10cSrcweir             SfxItemState nState = pDisp->QueryState( _nSlot, pItem );
85cdf0e10cSrcweir             bRet = SFX_ITEM_AVAILABLE <= nState;
86cdf0e10cSrcweir             if (bRet)
87cdf0e10cSrcweir                 _rValue = ( static_cast< const SfxBoolItem* >( pItem ) )->GetValue();
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         return bRet;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
QueryRecordChangesProtectionState(RedliningMode _eMode,bool & _rValue)93cdf0e10cSrcweir     bool QueryRecordChangesProtectionState( RedliningMode _eMode, bool& _rValue )
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         bool bRet = false;
96cdf0e10cSrcweir         if (_eMode != RL_NONE)
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             sal_uInt16 nSlot = _eMode == RL_WRITER ? FN_REDLINE_PROTECT : SID_CHG_PROTECT;
99cdf0e10cSrcweir             bRet = QueryState( nSlot, _rValue );
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir         return bRet;
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 
QueryRecordChangesState(RedliningMode _eMode,bool & _rValue)105cdf0e10cSrcweir     bool QueryRecordChangesState( RedliningMode _eMode, bool& _rValue )
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         bool bRet = false;
108cdf0e10cSrcweir         if (_eMode != RL_NONE)
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             sal_uInt16 nSlot = _eMode == RL_WRITER ? FN_REDLINE_ON : FID_CHG_RECORD;
111cdf0e10cSrcweir             bRet = QueryState( nSlot, _rValue );
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         return bRet;
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
lcl_GetPassword(Window * pParent,bool bProtect,String & rPassword)121cdf0e10cSrcweir static short lcl_GetPassword(
122cdf0e10cSrcweir     Window *pParent,
123cdf0e10cSrcweir     bool bProtect,
124cdf0e10cSrcweir     /*out*/String &rPassword )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     bool bRes = false;
127cdf0e10cSrcweir     SfxPasswordDialog aPasswdDlg( pParent );
128cdf0e10cSrcweir     const String aTitle( SfxResId( bProtect ? RID_SFX_PROTECT_RECORDS : RID_SFX_UNPROTECT_RECORDS ) );
129cdf0e10cSrcweir     aPasswdDlg.SetText( aTitle );
130cdf0e10cSrcweir     aPasswdDlg.SetMinLen( 1 );
131cdf0e10cSrcweir     if (bProtect)
132cdf0e10cSrcweir         aPasswdDlg.ShowExtras( SHOWEXTRAS_CONFIRM );
133cdf0e10cSrcweir     if (RET_OK == aPasswdDlg.Execute() && aPasswdDlg.GetPassword().Len() > 0)
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         rPassword = aPasswdDlg.GetPassword();
136cdf0e10cSrcweir         bRes = true;
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir     return bRes;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 
lcl_IsPasswordCorrect(const String & rPassword)142cdf0e10cSrcweir static bool lcl_IsPasswordCorrect( const String &rPassword )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     bool bRes = false;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     SfxObjectShell* pCurDocShell = SfxObjectShell::Current();
147cdf0e10cSrcweir     uno::Sequence< sal_Int8 >   aPasswordHash;
148cdf0e10cSrcweir     pCurDocShell->GetProtectionHash( aPasswordHash );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     // check if supplied password was correct
151cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aNewPasswd( aPasswordHash );
152cdf0e10cSrcweir     SvPasswordHelper::GetHashPassword( aNewPasswd, rPassword );
153cdf0e10cSrcweir     if (SvPasswordHelper::CompareHashPassword( aPasswordHash, rPassword ))
154cdf0e10cSrcweir         bRes = true;    // password was correct
155cdf0e10cSrcweir     else
156cdf0e10cSrcweir         InfoBox( NULL, String( SfxResId( RID_SFX_INCORRECT_PASSWORD ) ) ).Execute();
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     return bRes;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165cdf0e10cSrcweir struct SfxSecurityPage_Impl
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     SfxSecurityPage &   m_rMyTabPage;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     FixedLine           m_aNewPasswordToOpenFL;
170cdf0e10cSrcweir     FixedText           m_aNewPasswordToOpenFT;
171cdf0e10cSrcweir     Edit                m_aNewPasswordToOpenED;
172cdf0e10cSrcweir     FixedText           m_aConfirmPasswordToOpenFT;
173cdf0e10cSrcweir     Edit                m_aConfirmPasswordToOpenED;
174cdf0e10cSrcweir     FixedText           m_aNewPasswordInfoFT;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     FixedLine           m_aNewPasswordToModifyFL;
177cdf0e10cSrcweir     FixedText           m_aNewPasswordToModifyFT;
178cdf0e10cSrcweir     Edit                m_aNewPasswordToModifyED;
179cdf0e10cSrcweir     FixedText           m_aConfirmPasswordToModifyFT;
180cdf0e10cSrcweir     Edit                m_aConfirmPasswordToModifyED;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     FixedLine           m_aOptionsFL;
183cdf0e10cSrcweir     CheckBox            m_aOpenReadonlyCB;
184cdf0e10cSrcweir     CheckBox            m_aRecordChangesCB;         // for record changes
185cdf0e10cSrcweir     PushButton          m_aChangeProtectionPB;      // for record changes
186cdf0e10cSrcweir     String              m_aProtectSTR;              // for record changes
187cdf0e10cSrcweir     String              m_aUnProtectSTR;            // for record changes
188cdf0e10cSrcweir     RedliningMode       m_eRedlingMode;             // for record changes
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     bool                m_bOrigPasswordIsConfirmed;
191cdf0e10cSrcweir     bool                m_bNewPasswordIsValid;
192cdf0e10cSrcweir     String              m_aNewPassword;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     String              m_aEndRedliningWarning;
195cdf0e10cSrcweir     bool                m_bEndRedliningWarningDone;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     DECL_LINK( RecordChangesCBToggleHdl, void* );
198cdf0e10cSrcweir     DECL_LINK( ChangeProtectionPBHdl, void* );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     SfxSecurityPage_Impl( SfxSecurityPage &rDlg, const SfxItemSet &rItemSet );
201cdf0e10cSrcweir     ~SfxSecurityPage_Impl();
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     sal_Bool    FillItemSet_Impl( SfxItemSet & );
204cdf0e10cSrcweir     void    Reset_Impl( const SfxItemSet & );
205cdf0e10cSrcweir };
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 
SfxSecurityPage_Impl(SfxSecurityPage & rTabPage,const SfxItemSet &)208cdf0e10cSrcweir SfxSecurityPage_Impl::SfxSecurityPage_Impl( SfxSecurityPage &rTabPage, const SfxItemSet & ) :
209cdf0e10cSrcweir     m_rMyTabPage                    (rTabPage),
210cdf0e10cSrcweir     m_aNewPasswordToOpenFL          (&rTabPage, SfxResId( PASSWORD_TO_OPEN_FL ) ),
211cdf0e10cSrcweir     m_aNewPasswordToOpenFT          (&rTabPage, SfxResId( PASSWORD_TO_OPEN_FT ) ),
212cdf0e10cSrcweir     m_aNewPasswordToOpenED          (&rTabPage, SfxResId( PASSWORD_TO_OPEN_ED ) ),
213cdf0e10cSrcweir     m_aConfirmPasswordToOpenFT      (&rTabPage, SfxResId( CONFIRM_PASSWORD_TO_OPEN_FT ) ),
214cdf0e10cSrcweir     m_aConfirmPasswordToOpenED      (&rTabPage, SfxResId( CONFIRM_PASSWORD_TO_OPEN_ED ) ),
215cdf0e10cSrcweir     m_aNewPasswordInfoFT            (&rTabPage, SfxResId( PASSWORD_INFO_FT ) ),
216cdf0e10cSrcweir     m_aNewPasswordToModifyFL        (&rTabPage, SfxResId( PASSWORD_TO_MODIFY_FL ) ),
217cdf0e10cSrcweir     m_aNewPasswordToModifyFT        (&rTabPage, SfxResId( PASSWORD_TO_MODIFY_FT ) ),
218cdf0e10cSrcweir     m_aNewPasswordToModifyED        (&rTabPage, SfxResId( PASSWORD_TO_MODIFY_ED ) ),
219cdf0e10cSrcweir     m_aConfirmPasswordToModifyFT    (&rTabPage, SfxResId( CONFIRM_PASSWORD_TO_MODIFY_FT ) ),
220cdf0e10cSrcweir     m_aConfirmPasswordToModifyED    (&rTabPage, SfxResId( CONFIRM_PASSWORD_TO_MODIFY_ED ) ),
221cdf0e10cSrcweir     m_aOptionsFL                    (&rTabPage, SfxResId( OPTIONS_FL ) ),
222cdf0e10cSrcweir     m_aOpenReadonlyCB               (&rTabPage, SfxResId( OPEN_READONLY_CB ) ),
223cdf0e10cSrcweir     m_aRecordChangesCB              (&rTabPage, SfxResId( RECORD_CHANGES_CB ) ),
224cdf0e10cSrcweir     m_aChangeProtectionPB           (&rTabPage, SfxResId( CHANGE_PROTECTION_PB ) ),
225cdf0e10cSrcweir     m_aProtectSTR                   ( SfxResId( STR_PROTECT ) ),
226cdf0e10cSrcweir     m_aUnProtectSTR                 ( SfxResId( STR_UNPROTECT ) ),
227cdf0e10cSrcweir     m_eRedlingMode                  ( RL_NONE ),
228cdf0e10cSrcweir     m_bOrigPasswordIsConfirmed      ( false ),
229cdf0e10cSrcweir     m_bNewPasswordIsValid           ( false ),
230cdf0e10cSrcweir     m_aEndRedliningWarning          ( SfxResId( STR_END_REDLINING_WARNING ) ),
231cdf0e10cSrcweir     m_bEndRedliningWarningDone      ( false )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     m_aChangeProtectionPB.SetText( m_aProtectSTR );
234cdf0e10cSrcweir     // adjust button width if necessary
235cdf0e10cSrcweir     long nBtnTextWidth = 0;
236cdf0e10cSrcweir     long nTemp = m_aChangeProtectionPB.GetCtrlTextWidth( m_aChangeProtectionPB.GetText() );
237cdf0e10cSrcweir     if (nTemp > nBtnTextWidth)
238cdf0e10cSrcweir         nBtnTextWidth = nTemp;
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     // force toggle hdl called before visual change of checkbox
241cdf0e10cSrcweir     m_aRecordChangesCB.SetStyle( m_aRecordChangesCB.GetStyle() | WB_EARLYTOGGLE );
242cdf0e10cSrcweir     m_aRecordChangesCB.SetToggleHdl( LINK( this, SfxSecurityPage_Impl, RecordChangesCBToggleHdl ) );
243cdf0e10cSrcweir     m_aChangeProtectionPB.SetClickHdl( LINK( this, SfxSecurityPage_Impl, ChangeProtectionPBHdl ) );
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     // #i112277: for the time being (OOO 3.3) the following options should not
247cdf0e10cSrcweir     // be available. In the long run however it is planned to implement the yet
248cdf0e10cSrcweir     // missing functionality. Thus now we hide them and move the remaining ones up.
249cdf0e10cSrcweir     m_aNewPasswordToOpenFL.Hide();
250cdf0e10cSrcweir     m_aNewPasswordToOpenFT.Hide();
251cdf0e10cSrcweir     m_aNewPasswordToOpenED.Hide();
252cdf0e10cSrcweir     m_aConfirmPasswordToOpenFT.Hide();
253cdf0e10cSrcweir     m_aConfirmPasswordToOpenED.Hide();
254cdf0e10cSrcweir     m_aNewPasswordInfoFT.Hide();
255cdf0e10cSrcweir     m_aNewPasswordToModifyFL.Hide();
256cdf0e10cSrcweir     m_aNewPasswordToModifyFT.Hide();
257cdf0e10cSrcweir     m_aNewPasswordToModifyED.Hide();
258cdf0e10cSrcweir     m_aConfirmPasswordToModifyFT.Hide();
259cdf0e10cSrcweir     m_aConfirmPasswordToModifyED.Hide();
260cdf0e10cSrcweir     const long nDelta = m_aOptionsFL.GetPosPixel().Y() - m_aNewPasswordToOpenFL.GetPosPixel().Y();
261cdf0e10cSrcweir     Point aPos;
262cdf0e10cSrcweir     aPos = m_aOptionsFL.GetPosPixel();
263cdf0e10cSrcweir     aPos.Y() -= nDelta;
264cdf0e10cSrcweir     m_aOptionsFL.SetPosPixel( aPos );
265cdf0e10cSrcweir     aPos = m_aOpenReadonlyCB.GetPosPixel();
266cdf0e10cSrcweir     aPos.Y() -= nDelta;
267cdf0e10cSrcweir     m_aOpenReadonlyCB.SetPosPixel( aPos );
268cdf0e10cSrcweir     aPos = m_aRecordChangesCB.GetPosPixel();
269cdf0e10cSrcweir     aPos.Y() -= nDelta;
270cdf0e10cSrcweir     m_aRecordChangesCB.SetPosPixel( aPos );
271cdf0e10cSrcweir     aPos = m_aChangeProtectionPB.GetPosPixel();
272cdf0e10cSrcweir     aPos.Y() -= nDelta;
273cdf0e10cSrcweir     m_aChangeProtectionPB.SetPosPixel( aPos );
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 
~SfxSecurityPage_Impl()277cdf0e10cSrcweir SfxSecurityPage_Impl::~SfxSecurityPage_Impl()
278cdf0e10cSrcweir {
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 
FillItemSet_Impl(SfxItemSet &)282cdf0e10cSrcweir sal_Bool SfxSecurityPage_Impl::FillItemSet_Impl( SfxItemSet & )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir     bool bModified = false;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     SfxObjectShell* pCurDocShell = SfxObjectShell::Current();
287cdf0e10cSrcweir     if (pCurDocShell&& !pCurDocShell->IsReadOnly())
288cdf0e10cSrcweir     {
289cdf0e10cSrcweir         if (m_eRedlingMode != RL_NONE )
290cdf0e10cSrcweir         {
291cdf0e10cSrcweir             const bool bDoRecordChanges     = m_aRecordChangesCB.IsChecked();
292cdf0e10cSrcweir             const bool bDoChangeProtection  = m_aChangeProtectionPB.GetText() != m_aProtectSTR;
293cdf0e10cSrcweir 
294cdf0e10cSrcweir             // sanity checks
295cdf0e10cSrcweir             DBG_ASSERT( bDoRecordChanges || !bDoChangeProtection, "no change recording should imply no change protection" );
296cdf0e10cSrcweir             DBG_ASSERT( bDoChangeProtection || !bDoRecordChanges, "no change protection should imply no change recording" );
297cdf0e10cSrcweir             DBG_ASSERT( !bDoChangeProtection || m_aNewPassword.Len() > 0, "change protection should imply password length is > 0" );
298cdf0e10cSrcweir             DBG_ASSERT( bDoChangeProtection || m_aNewPassword.Len() == 0, "no change protection should imply password length is 0" );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir             // change recording
301cdf0e10cSrcweir             if (bDoRecordChanges != pCurDocShell->IsChangeRecording())
302cdf0e10cSrcweir             {
303cdf0e10cSrcweir                 pCurDocShell->SetChangeRecording( bDoRecordChanges );
304cdf0e10cSrcweir                 bModified = true;
305cdf0e10cSrcweir             }
306cdf0e10cSrcweir 
307cdf0e10cSrcweir             // change record protection
308cdf0e10cSrcweir             if (m_bNewPasswordIsValid &&
309cdf0e10cSrcweir                 bDoChangeProtection != pCurDocShell->HasChangeRecordProtection())
310cdf0e10cSrcweir             {
311cdf0e10cSrcweir                 DBG_ASSERT( !bDoChangeProtection || bDoRecordChanges,
312cdf0e10cSrcweir                         "change protection requires record changes to be active!" );
313cdf0e10cSrcweir                 pCurDocShell->SetProtectionPassword( m_aNewPassword );
314cdf0e10cSrcweir                 bModified = true;
315cdf0e10cSrcweir             }
316cdf0e10cSrcweir         }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir         // open read-only?
319cdf0e10cSrcweir         const sal_Bool bDoOpenReadonly = m_aOpenReadonlyCB.IsChecked();
320cdf0e10cSrcweir         if (pCurDocShell->HasSecurityOptOpenReadOnly() &&
321cdf0e10cSrcweir             bDoOpenReadonly != pCurDocShell->IsSecurityOptOpenReadOnly())
322cdf0e10cSrcweir         {
323cdf0e10cSrcweir             pCurDocShell->SetSecurityOptOpenReadOnly( bDoOpenReadonly );
324cdf0e10cSrcweir             bModified = true;
325cdf0e10cSrcweir         }
326cdf0e10cSrcweir     }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     return bModified;
329cdf0e10cSrcweir }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 
Reset_Impl(const SfxItemSet &)332cdf0e10cSrcweir void SfxSecurityPage_Impl::Reset_Impl( const SfxItemSet & )
333cdf0e10cSrcweir {
334cdf0e10cSrcweir     SfxObjectShell* pCurDocShell = SfxObjectShell::Current();
335cdf0e10cSrcweir 
336cdf0e10cSrcweir     String sNewText = m_aProtectSTR;
337cdf0e10cSrcweir     if (!pCurDocShell)
338cdf0e10cSrcweir     {
339cdf0e10cSrcweir         // no doc -> hide document settings
340cdf0e10cSrcweir         m_aOpenReadonlyCB.Disable();
341cdf0e10cSrcweir         m_aRecordChangesCB.Disable();
342cdf0e10cSrcweir         m_aChangeProtectionPB.Disable();
343cdf0e10cSrcweir     }
344cdf0e10cSrcweir     else
345cdf0e10cSrcweir     {
346cdf0e10cSrcweir         bool bIsHTMLDoc = false;
347cdf0e10cSrcweir         SfxViewShell* pViewSh = SfxViewShell::Current();
348cdf0e10cSrcweir         if (pViewSh)
349cdf0e10cSrcweir         {
350cdf0e10cSrcweir             const SfxPoolItem* pItem;
351cdf0e10cSrcweir             SfxDispatcher* pDisp = pViewSh->GetDispatcher();
352cdf0e10cSrcweir             if (SFX_ITEM_AVAILABLE <= pDisp->QueryState( SID_HTML_MODE, pItem ))
353cdf0e10cSrcweir             {
354cdf0e10cSrcweir                 sal_uInt16 nMode = static_cast< const SfxUInt16Item* >( pItem )->GetValue();
355cdf0e10cSrcweir                 bIsHTMLDoc = ( ( nMode & HTMLMODE_ON ) != 0 );
356cdf0e10cSrcweir             }
357cdf0e10cSrcweir         }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir         sal_Bool bIsReadonly = pCurDocShell->IsReadOnly();
360cdf0e10cSrcweir         if (pCurDocShell->HasSecurityOptOpenReadOnly() && !bIsHTMLDoc)
361cdf0e10cSrcweir         {
362cdf0e10cSrcweir             m_aOpenReadonlyCB.Check( pCurDocShell->IsSecurityOptOpenReadOnly() );
363cdf0e10cSrcweir             m_aOpenReadonlyCB.Enable( !bIsReadonly );
364cdf0e10cSrcweir         }
365cdf0e10cSrcweir         else
366cdf0e10cSrcweir             m_aOpenReadonlyCB.Disable();
367cdf0e10cSrcweir 
368cdf0e10cSrcweir         bool bRecordChanges;
369cdf0e10cSrcweir         if (QueryRecordChangesState( RL_WRITER, bRecordChanges ) && !bIsHTMLDoc)
370cdf0e10cSrcweir             m_eRedlingMode = RL_WRITER;
371cdf0e10cSrcweir         else if (QueryRecordChangesState( RL_CALC, bRecordChanges ))
372cdf0e10cSrcweir             m_eRedlingMode = RL_CALC;
373cdf0e10cSrcweir         else
374cdf0e10cSrcweir             m_eRedlingMode = RL_NONE;
375cdf0e10cSrcweir 
376cdf0e10cSrcweir         if (m_eRedlingMode != RL_NONE)
377cdf0e10cSrcweir         {
378cdf0e10cSrcweir             bool bProtection;
379cdf0e10cSrcweir             QueryRecordChangesProtectionState( m_eRedlingMode, bProtection );
380cdf0e10cSrcweir 
381cdf0e10cSrcweir             m_aChangeProtectionPB.Enable( !bIsReadonly );
382cdf0e10cSrcweir             // set the right text
383cdf0e10cSrcweir             if (bProtection)
384cdf0e10cSrcweir                 sNewText = m_aUnProtectSTR;
385cdf0e10cSrcweir 
386cdf0e10cSrcweir             m_aRecordChangesCB.Check( bRecordChanges );
387cdf0e10cSrcweir             m_aRecordChangesCB.Enable( /*!bProtection && */!bIsReadonly );
388cdf0e10cSrcweir 
389cdf0e10cSrcweir             m_bOrigPasswordIsConfirmed = true;   // default case if no password is set
390cdf0e10cSrcweir             uno::Sequence< sal_Int8 > aPasswordHash;
391cdf0e10cSrcweir             // check if password is available
392cdf0e10cSrcweir             if (pCurDocShell->GetProtectionHash( aPasswordHash ) &&
393cdf0e10cSrcweir                 aPasswordHash.getLength() > 0)
394cdf0e10cSrcweir                 m_bOrigPasswordIsConfirmed = false;  // password found, needs to be confirmed later on
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir         else
397cdf0e10cSrcweir         {
398cdf0e10cSrcweir             // A Calc document that is shared will have 'm_eRedlingMode == RL_NONE'
399cdf0e10cSrcweir             // In shared documents change recording and protection must be disabled,
400cdf0e10cSrcweir             // similar to documents that do not support change recording at all.
401cdf0e10cSrcweir             m_aRecordChangesCB.Check( sal_False );
402cdf0e10cSrcweir             m_aRecordChangesCB.Disable();
403cdf0e10cSrcweir             m_aChangeProtectionPB.Check( sal_False );
404cdf0e10cSrcweir             m_aChangeProtectionPB.Disable();
405cdf0e10cSrcweir         }
406cdf0e10cSrcweir     }
407cdf0e10cSrcweir 
408cdf0e10cSrcweir     m_aChangeProtectionPB.SetText( sNewText );
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 
IMPL_LINK(SfxSecurityPage_Impl,RecordChangesCBToggleHdl,void *,EMPTYARG)412cdf0e10cSrcweir IMPL_LINK( SfxSecurityPage_Impl, RecordChangesCBToggleHdl, void*, EMPTYARG )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir     // when change recording gets disabled protection must be disabled as well
415cdf0e10cSrcweir     if (!m_aRecordChangesCB.IsChecked())    // the new check state is already present, thus the '!'
416cdf0e10cSrcweir     {
417cdf0e10cSrcweir         bool bAlreadyDone = false;
418cdf0e10cSrcweir         if (!m_bEndRedliningWarningDone)
419cdf0e10cSrcweir         {
420cdf0e10cSrcweir 		    WarningBox aBox( m_rMyTabPage.GetParent(), WinBits(WB_YES_NO | WB_DEF_NO),
421cdf0e10cSrcweir                     m_aEndRedliningWarning );
422cdf0e10cSrcweir             if (aBox.Execute() != RET_YES)
423cdf0e10cSrcweir                 bAlreadyDone = true;
424cdf0e10cSrcweir             else
425cdf0e10cSrcweir                 m_bEndRedliningWarningDone = true;
426cdf0e10cSrcweir         }
427cdf0e10cSrcweir 
428cdf0e10cSrcweir         const bool bNeedPasssword = !m_bOrigPasswordIsConfirmed
429cdf0e10cSrcweir                 && m_aChangeProtectionPB.GetText() != m_aProtectSTR;
430cdf0e10cSrcweir         if (!bAlreadyDone && bNeedPasssword)
431cdf0e10cSrcweir         {
432cdf0e10cSrcweir             String aPasswordText;
433cdf0e10cSrcweir 
434cdf0e10cSrcweir             // dialog canceled or no password provided
435cdf0e10cSrcweir             if (!lcl_GetPassword( m_rMyTabPage.GetParent(), false, aPasswordText ))
436cdf0e10cSrcweir                 bAlreadyDone = true;
437cdf0e10cSrcweir 
438cdf0e10cSrcweir             // ask for password and if dialog is canceled or no password provided return
439cdf0e10cSrcweir             if (lcl_IsPasswordCorrect( aPasswordText ))
440cdf0e10cSrcweir                 m_bOrigPasswordIsConfirmed = true;
441cdf0e10cSrcweir             else
442cdf0e10cSrcweir                 bAlreadyDone = true;
443cdf0e10cSrcweir         }
444cdf0e10cSrcweir 
445cdf0e10cSrcweir         if (bAlreadyDone)
446cdf0e10cSrcweir             m_aRecordChangesCB.Check( true );     // restore original state
447cdf0e10cSrcweir         else
448cdf0e10cSrcweir         {
449cdf0e10cSrcweir             // remember required values to change protection and change recording in
450cdf0e10cSrcweir             // FillItemSet_Impl later on if password was correct.
451cdf0e10cSrcweir             m_bNewPasswordIsValid = true;
452cdf0e10cSrcweir             m_aNewPassword = String();
453cdf0e10cSrcweir 
454cdf0e10cSrcweir             m_aChangeProtectionPB.SetText( m_aProtectSTR );
455cdf0e10cSrcweir         }
456cdf0e10cSrcweir     }
457cdf0e10cSrcweir 
458cdf0e10cSrcweir     return 0;
459cdf0e10cSrcweir }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir 
IMPL_LINK(SfxSecurityPage_Impl,ChangeProtectionPBHdl,void *,EMPTYARG)462cdf0e10cSrcweir IMPL_LINK( SfxSecurityPage_Impl, ChangeProtectionPBHdl, void*, EMPTYARG )
463cdf0e10cSrcweir {
464cdf0e10cSrcweir     if (m_eRedlingMode == RL_NONE)
465cdf0e10cSrcweir         return 0;
466cdf0e10cSrcweir 
467cdf0e10cSrcweir     // the push button text is always the opposite of the current state. Thus:
468cdf0e10cSrcweir     const bool bCurrentProtection = m_aChangeProtectionPB.GetText() != m_aProtectSTR;
469cdf0e10cSrcweir 
470cdf0e10cSrcweir     // ask user for password (if still necessary)
471cdf0e10cSrcweir     String aPasswordText;
472cdf0e10cSrcweir     bool bNewProtection = !bCurrentProtection;
473cdf0e10cSrcweir     const bool bNeedPassword = bNewProtection || !m_bOrigPasswordIsConfirmed;
474cdf0e10cSrcweir     if (bNeedPassword)
475cdf0e10cSrcweir     {
476cdf0e10cSrcweir         // ask for password and if dialog is canceled or no password provided return
477cdf0e10cSrcweir         if (!lcl_GetPassword( m_rMyTabPage.GetParent(), bNewProtection, aPasswordText ))
478cdf0e10cSrcweir             return 0;
479cdf0e10cSrcweir 
480cdf0e10cSrcweir         // provided password still needs to be checked?
481cdf0e10cSrcweir         if (!bNewProtection && !m_bOrigPasswordIsConfirmed)
482cdf0e10cSrcweir         {
483cdf0e10cSrcweir             if (lcl_IsPasswordCorrect( aPasswordText ))
484cdf0e10cSrcweir                 m_bOrigPasswordIsConfirmed = true;
485cdf0e10cSrcweir             else
486cdf0e10cSrcweir                 return 0;
487cdf0e10cSrcweir         }
488cdf0e10cSrcweir     }
489cdf0e10cSrcweir     DBG_ASSERT( m_bOrigPasswordIsConfirmed, "ooops... this should not have happened!" );
490cdf0e10cSrcweir 
491cdf0e10cSrcweir     // remember required values to change protection and change recording in
492cdf0e10cSrcweir     // FillItemSet_Impl later on if password was correct.
493cdf0e10cSrcweir     m_bNewPasswordIsValid = true;
494cdf0e10cSrcweir     m_aNewPassword = bNewProtection? aPasswordText : String();
495cdf0e10cSrcweir 
496cdf0e10cSrcweir //    // RecordChangesCB is enabled if protection is off
497cdf0e10cSrcweir //    m_aRecordChangesCB.Enable( !bNewProtection );
498cdf0e10cSrcweir     m_aRecordChangesCB.Check( bNewProtection );
499cdf0e10cSrcweir     // toggle text of button "Protect" <-> "Unprotect"
500cdf0e10cSrcweir     m_aChangeProtectionPB.SetText( bNewProtection ? m_aUnProtectSTR : m_aProtectSTR );
501cdf0e10cSrcweir 
502cdf0e10cSrcweir     return 0;
503cdf0e10cSrcweir }
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 
506cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 
Create(Window * pParent,const SfxItemSet & rItemSet)509cdf0e10cSrcweir SfxTabPage* SfxSecurityPage::Create( Window * pParent, const SfxItemSet & rItemSet )
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     return new SfxSecurityPage( pParent, rItemSet );
512cdf0e10cSrcweir }
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 
SfxSecurityPage(Window * pParent,const SfxItemSet & rItemSet)515cdf0e10cSrcweir SfxSecurityPage::SfxSecurityPage( Window* pParent, const SfxItemSet& rItemSet ) :
516cdf0e10cSrcweir     SfxTabPage( pParent, SfxResId( TP_DOCINFOSECURITY ), rItemSet )
517cdf0e10cSrcweir {
518cdf0e10cSrcweir     m_pImpl = std::auto_ptr< SfxSecurityPage_Impl >(new SfxSecurityPage_Impl( *this, rItemSet ));
519cdf0e10cSrcweir 
520cdf0e10cSrcweir     FreeResource();
521cdf0e10cSrcweir }
522cdf0e10cSrcweir 
523cdf0e10cSrcweir 
~SfxSecurityPage()524cdf0e10cSrcweir SfxSecurityPage::~SfxSecurityPage()
525cdf0e10cSrcweir {
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 
FillItemSet(SfxItemSet & rItemSet)529cdf0e10cSrcweir sal_Bool SfxSecurityPage::FillItemSet( SfxItemSet & rItemSet )
530cdf0e10cSrcweir {
531cdf0e10cSrcweir     bool bModified = false;
532cdf0e10cSrcweir     DBG_ASSERT( m_pImpl.get(), "implementation pointer is 0. Still in c-tor?" );
533cdf0e10cSrcweir     if (m_pImpl.get() != 0)
534cdf0e10cSrcweir         bModified =  m_pImpl->FillItemSet_Impl( rItemSet );
535cdf0e10cSrcweir     return bModified;
536cdf0e10cSrcweir }
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 
Reset(const SfxItemSet & rItemSet)539cdf0e10cSrcweir void SfxSecurityPage::Reset( const SfxItemSet & rItemSet )
540cdf0e10cSrcweir {
541cdf0e10cSrcweir     DBG_ASSERT( m_pImpl.get(), "implementation pointer is 0. Still in c-tor?" );
542cdf0e10cSrcweir     if (m_pImpl.get() != 0)
543cdf0e10cSrcweir         m_pImpl->Reset_Impl( rItemSet );
544cdf0e10cSrcweir }
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 
547cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 
550