Update ooo320-m1
[ooovba.git] / sc / source / ui / miscdlgs / retypepassdlg.cxx
blob3e27894429e340c11c46b2023ccca72b74765235
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: retypepassdlg.cxx,v $
10 * $Revision: 1.1.2.7 $
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_sc.hxx"
34 #include "retypepassdlg.hxx"
35 #include "retypepassdlg.hrc"
36 #include "scresid.hxx"
37 #include "document.hxx"
38 #include "tabprotection.hxx"
40 #include <stdio.h>
42 #include <vcl/msgbox.hxx>
44 ScRetypePassDlg::ScRetypePassDlg(Window* pParent) :
45 ModalDialog(pParent, ScResId(RID_SCDLG_RETYPEPASS)),
47 maBtnOk (this, ScResId(BTN_OK)),
48 maBtnCancel (this, ScResId(BTN_CANCEL)),
49 maBtnHelp (this, ScResId(BTN_HELP)),
51 maTextDescription(this, ScResId(FT_DESC)),
52 maLineDocument(this, ScResId(FL_DOCUMENT)),
53 maTextDocStatus(this, ScResId(FT_DOCSTATUS)),
54 maBtnRetypeDoc(this, ScResId(BTN_RETYPE_DOC)),
56 maLineSheet(this, ScResId(FL_SHEET)),
57 maTextSheetName1(this, ScResId(FT_SHEETNAME1)),
58 maTextSheetStatus1(this, ScResId(FT_SHEETSTATUS1)),
59 maBtnRetypeSheet1(this, ScResId(BTN_RETYPE_SHEET1)),
61 maTextSheetName2(this, ScResId(FT_SHEETNAME2)),
62 maTextSheetStatus2(this, ScResId(FT_SHEETSTATUS2)),
63 maBtnRetypeSheet2(this, ScResId(BTN_RETYPE_SHEET2)),
65 maTextSheetName3(this, ScResId(FT_SHEETNAME3)),
66 maTextSheetStatus3(this, ScResId(FT_SHEETSTATUS3)),
67 maBtnRetypeSheet3(this, ScResId(BTN_RETYPE_SHEET3)),
69 maTextSheetName4(this, ScResId(FT_SHEETNAME4)),
70 maTextSheetStatus4(this, ScResId(FT_SHEETSTATUS4)),
71 maBtnRetypeSheet4(this, ScResId(BTN_RETYPE_SHEET4)),
73 maScrollBar (this, ScResId(SB_SCROLL)),
75 maTextNotProtected(ScResId(STR_NOT_PROTECTED)),
76 maTextNotPassProtected(ScResId(STR_NOT_PASS_PROTECTED)),
77 maTextHashBad(ScResId(STR_HASH_BAD)),
78 maTextHashGood(ScResId(STR_HASH_GOOD)),
79 maTextHashRegen(ScResId(STR_HASH_REGENERATED)),
81 mpDocItem(static_cast<ScDocProtection*>(NULL)),
82 mnCurScrollPos(0),
83 meDesiredHash(PASSHASH_SHA1)
85 Init();
88 ScRetypePassDlg::~ScRetypePassDlg()
92 short ScRetypePassDlg::Execute()
94 PopulateDialog();
95 CheckHashStatus();
96 return ModalDialog::Execute();
99 void ScRetypePassDlg::SetDataFromDocument(const ScDocument& rDoc)
101 const ScDocProtection* pDocProtect = rDoc.GetDocProtection();
102 if (pDocProtect && pDocProtect->isProtected())
103 mpDocItem.reset(new ScDocProtection(*pDocProtect));
105 SCTAB nTabCount = rDoc.GetTableCount();
106 maTableItems.reserve(nTabCount);
107 for (SCTAB i = 0; i < nTabCount; ++i)
109 TableItem aTabItem;
110 rDoc.GetName(i, aTabItem.maName);
112 const ScTableProtection* pTabProtect = rDoc.GetTabProtection(i);
113 if (pTabProtect && pTabProtect->isProtected())
114 aTabItem.mpProtect.reset(new ScTableProtection(*pTabProtect));
116 maTableItems.push_back(aTabItem);
120 void ScRetypePassDlg::SetDesiredHash(ScPasswordHash eHash)
122 meDesiredHash = eHash;
125 void ScRetypePassDlg::WriteNewDataToDocument(ScDocument& rDoc) const
127 if (mpDocItem.get())
128 rDoc.SetDocProtection(mpDocItem.get());
130 size_t nTabCount = static_cast<size_t>(rDoc.GetTableCount());
131 size_t n = maTableItems.size();
132 for (size_t i = 0; i < n; ++i)
134 if (i >= nTabCount)
135 break;
137 ScTableProtection* pTabProtect = maTableItems[i].mpProtect.get();
138 if (pTabProtect)
139 rDoc.SetTabProtection(static_cast<SCTAB>(i), pTabProtect);
143 void ScRetypePassDlg::Init()
145 Link aLink = LINK( this, ScRetypePassDlg, OKHdl );
146 maBtnOk.SetClickHdl(aLink);
148 aLink = LINK( this, ScRetypePassDlg, RetypeBtnHdl );
149 maBtnRetypeDoc.SetClickHdl(aLink);
150 maBtnRetypeSheet1.SetClickHdl(aLink);
151 maBtnRetypeSheet2.SetClickHdl(aLink);
152 maBtnRetypeSheet3.SetClickHdl(aLink);
153 maBtnRetypeSheet4.SetClickHdl(aLink);
155 maTextDocStatus.SetText(maTextNotProtected);
156 maTextSheetStatus1.SetText(maTextNotProtected);
157 maTextSheetStatus2.SetText(maTextNotProtected);
158 maTextSheetStatus3.SetText(maTextNotProtected);
159 maTextSheetStatus4.SetText(maTextNotProtected);
160 maBtnRetypeDoc.Disable();
162 // Make all sheet rows invisible.
164 maTextSheetName1.Show(false);
165 maTextSheetStatus1.Show(false);
166 maBtnRetypeSheet1.Show(false);
167 maBtnRetypeSheet1.Disable();
169 maTextSheetName2.Show(false);
170 maTextSheetStatus2.Show(false);
171 maBtnRetypeSheet2.Show(false);
172 maBtnRetypeSheet2.Disable();
174 maTextSheetName3.Show(false);
175 maTextSheetStatus3.Show(false);
176 maBtnRetypeSheet3.Show(false);
177 maBtnRetypeSheet3.Disable();
179 maTextSheetName4.Show(false);
180 maTextSheetStatus4.Show(false);
181 maBtnRetypeSheet4.Show(false);
182 maBtnRetypeSheet4.Disable();
184 maScrollBar.Show(false);
186 maScrollBar.SetEndScrollHdl( LINK( this, ScRetypePassDlg, ScrollHdl ) );
187 maScrollBar.SetScrollHdl( LINK( this, ScRetypePassDlg, ScrollHdl ) );
189 maScrollBar.SetPageSize(4);
190 maScrollBar.SetVisibleSize(4);
191 maScrollBar.SetLineSize(1);
194 void ScRetypePassDlg::PopulateDialog()
196 // Document protection first.
197 SetDocData();
199 // Sheet protection next. We're only interested in the first 4 sheets
200 // (or less).
201 size_t n = maTableItems.size();
202 for (size_t i = 0; i < n && i < 4; ++i)
203 SetTableData(i, static_cast< SCTAB >( i ));
205 if (n > 4)
207 maScrollBar.Show(true);
208 maScrollBar.SetRange(Range(0, n));
212 void ScRetypePassDlg::SetDocData()
214 bool bBtnEnabled = false;
215 if (mpDocItem.get() && mpDocItem->isProtected())
217 if (mpDocItem->isPasswordEmpty())
218 maTextDocStatus.SetText(maTextNotPassProtected);
219 else if (mpDocItem->hasPasswordHash(meDesiredHash))
220 maTextDocStatus.SetText(maTextHashGood);
221 else
223 // incompatible hash
224 maTextDocStatus.SetText(maTextHashBad);
225 bBtnEnabled = true;
228 maBtnRetypeDoc.Enable(bBtnEnabled);
231 void ScRetypePassDlg::SetTableData(size_t nRowPos, SCTAB nTab)
233 if (nRowPos >= 4)
234 return;
236 FixedText* pName = NULL;
237 FixedText* pStatus = NULL;
238 PushButton* pBtn = NULL;
239 switch (nRowPos)
241 case 0:
242 pName = &maTextSheetName1;
243 pStatus = &maTextSheetStatus1;
244 pBtn = &maBtnRetypeSheet1;
245 break;
246 case 1:
247 pName = &maTextSheetName2;
248 pStatus = &maTextSheetStatus2;
249 pBtn = &maBtnRetypeSheet2;
250 break;
251 case 2:
252 pName = &maTextSheetName3;
253 pStatus = &maTextSheetStatus3;
254 pBtn = &maBtnRetypeSheet3;
255 break;
256 case 3:
257 pName = &maTextSheetName4;
258 pStatus = &maTextSheetStatus4;
259 pBtn = &maBtnRetypeSheet4;
260 break;
261 default:
262 return;
265 bool bBtnEnabled = false;
266 pName->SetText(maTableItems[nTab].maName);
267 pName->Show(true);
268 const ScTableProtection* pTabProtect = maTableItems[nTab].mpProtect.get();
269 if (pTabProtect && pTabProtect->isProtected())
271 if (pTabProtect->isPasswordEmpty())
272 pStatus->SetText(maTextNotPassProtected);
273 else if (pTabProtect->hasPasswordHash(meDesiredHash))
274 pStatus->SetText(maTextHashGood);
275 else
277 // incompatible hash
278 pStatus->SetText(maTextHashBad);
279 bBtnEnabled = true;
282 else
283 pStatus->SetText(maTextNotProtected);
285 pStatus->Show(true);
286 pBtn->Show(true);
287 pBtn->Enable(bBtnEnabled);
290 void ScRetypePassDlg::ResetTableRows()
292 long nScrollPos = maScrollBar.GetThumbPos();
293 mnCurScrollPos = nScrollPos < 0 ? 0 : nScrollPos;
294 size_t nRowCount = maTableItems.size() - nScrollPos;
295 for (size_t i = 0; i < nRowCount; ++i)
296 SetTableData(i, static_cast< SCTAB >( i + nScrollPos ));
299 bool lcl_IsInGoodStatus(ScPassHashProtectable* pProtected, ScPasswordHash eDesiredHash)
301 if (!pProtected || !pProtected->isProtected())
302 // Not protected.
303 return true;
305 if (pProtected->isPasswordEmpty())
306 return true;
308 if (pProtected->hasPasswordHash(eDesiredHash))
309 return true;
311 return false;
314 void ScRetypePassDlg::CheckHashStatus()
318 if (!lcl_IsInGoodStatus(mpDocItem.get(), meDesiredHash))
319 break;
321 bool bStatusGood = true;
322 size_t nTabCount = maTableItems.size();
323 for (size_t i = 0; i < nTabCount && bStatusGood; ++i)
325 if (!lcl_IsInGoodStatus(maTableItems[i].mpProtect.get(), meDesiredHash))
326 bStatusGood = false;
328 if (!bStatusGood)
329 break;
331 maBtnOk.Enable();
332 return;
334 while (false);
336 maBtnOk.Disable();
339 IMPL_LINK( ScRetypePassDlg, OKHdl, OKButton*, EMPTYARG )
341 EndDialog(RET_OK);
342 return 0;
345 IMPL_LINK( ScRetypePassDlg, RetypeBtnHdl, PushButton*, pBtn )
347 ScPassHashProtectable* pProtected = NULL;
348 if (pBtn == &maBtnRetypeDoc)
350 // document protection.
351 pProtected = mpDocItem.get();
353 else
355 // sheet protection.
356 size_t nTabPos = mnCurScrollPos;
357 if (pBtn == &maBtnRetypeSheet2)
358 nTabPos += 1;
359 else if (pBtn == &maBtnRetypeSheet3)
360 nTabPos += 2;
361 else if (pBtn == &maBtnRetypeSheet4)
362 nTabPos += 3;
363 else if (pBtn != &maBtnRetypeSheet1)
364 // This should never happen !
365 return 0;
367 if (nTabPos >= maTableItems.size())
368 // Likewise, this should never happen !
369 return 0;
371 pProtected = maTableItems[nTabPos].mpProtect.get();
374 if (!pProtected)
375 // What the ... !?
376 return 0;
378 ScRetypePassInputDlg aDlg(this, pProtected);
379 if (aDlg.Execute() == RET_OK)
381 // OK is pressed. Update the protected item.
382 if (aDlg.IsRemovePassword())
384 // Remove password from this item.
385 pProtected->setPassword(String());
387 else
389 // Set a new password.
390 String aNewPass = aDlg.GetNewPassword();
391 pProtected->setPassword(aNewPass);
394 SetDocData();
395 ResetTableRows();
396 CheckHashStatus();
398 return 0;
401 IMPL_LINK( ScRetypePassDlg, ScrollHdl, ScrollBar*, EMPTYARG )
403 ResetTableRows();
404 return 0;
407 // ============================================================================
409 ScRetypePassInputDlg::ScRetypePassInputDlg(Window* pParent, ScPassHashProtectable* pProtected) :
410 ModalDialog(pParent, ScResId(RID_SCDLG_RETYPEPASS_INPUT)),
412 maBtnOk (this, ScResId(BTN_OK)),
413 maBtnCancel (this, ScResId(BTN_CANCEL)),
414 maBtnHelp (this, ScResId(BTN_HELP)),
416 maBtnRetypePassword(this, ScResId(BTN_RETYPE_PASSWORD)),
418 maPassword1Text (this, ScResId(FT_PASSWORD1)),
419 maPassword1Edit (this, ScResId(ED_PASSWORD1)),
420 maPassword2Text (this, ScResId(FT_PASSWORD2)),
421 maPassword2Edit (this, ScResId(ED_PASSWORD2)),
422 maBtnMatchOldPass(this, ScResId(BTN_MATCH_OLD_PASSWORD)),
424 maBtnRemovePassword(this, ScResId(BTN_REMOVE_PASSWORD)),
426 mpProtected(pProtected)
428 Init();
431 ScRetypePassInputDlg::~ScRetypePassInputDlg()
435 short ScRetypePassInputDlg::Execute()
437 return ModalDialog::Execute();
440 bool ScRetypePassInputDlg::IsRemovePassword() const
442 return maBtnRemovePassword.IsChecked();
445 String ScRetypePassInputDlg::GetNewPassword() const
447 return maPassword1Edit.GetText();
450 void ScRetypePassInputDlg::Init()
452 Link aLink = LINK( this, ScRetypePassInputDlg, OKHdl );
453 maBtnOk.SetClickHdl(aLink);
454 aLink = LINK( this, ScRetypePassInputDlg, RadioBtnHdl );
455 maBtnRetypePassword.SetClickHdl(aLink);
456 maBtnRemovePassword.SetClickHdl(aLink);
457 aLink = LINK( this, ScRetypePassInputDlg, CheckBoxHdl );
458 maBtnMatchOldPass.SetClickHdl(aLink);
459 aLink = LINK( this, ScRetypePassInputDlg, PasswordModifyHdl );
460 maPassword1Edit.SetModifyHdl(aLink);
461 maPassword2Edit.SetModifyHdl(aLink);
463 maBtnOk.Disable();
464 maBtnRetypePassword.Check(true);
465 maBtnMatchOldPass.Check(true);
466 maPassword1Edit.GrabFocus();
469 void ScRetypePassInputDlg::CheckPasswordInput()
471 String aPass1 = maPassword1Edit.GetText();
472 String aPass2 = maPassword2Edit.GetText();
474 if (!aPass1.Len() || !aPass2.Len())
476 // Empty password is not allowed.
477 maBtnOk.Disable();
478 return;
481 if (!aPass1.Equals(aPass2))
483 // The two passwords differ.
484 maBtnOk.Disable();
485 return;
488 if (!maBtnMatchOldPass.IsChecked())
490 maBtnOk.Enable();
491 return;
494 if (!mpProtected)
496 // This should never happen!
497 maBtnOk.Disable();
498 return;
501 bool bPassGood = mpProtected->verifyPassword(aPass1);
502 maBtnOk.Enable(bPassGood);
505 IMPL_LINK( ScRetypePassInputDlg, OKHdl, OKButton*, EMPTYARG )
507 EndDialog(RET_OK);
508 return 0;
511 IMPL_LINK( ScRetypePassInputDlg, RadioBtnHdl, RadioButton*, pBtn )
513 if (pBtn == &maBtnRetypePassword)
515 maBtnRemovePassword.Check(false);
516 maPassword1Text.Enable();
517 maPassword1Edit.Enable();
518 maPassword2Text.Enable();
519 maPassword2Edit.Enable();
520 maBtnMatchOldPass.Enable();
521 CheckPasswordInput();
523 else if (pBtn == &maBtnRemovePassword)
525 maBtnRetypePassword.Check(false);
526 maPassword1Text.Disable();
527 maPassword1Edit.Disable();
528 maPassword2Text.Disable();
529 maPassword2Edit.Disable();
530 maBtnMatchOldPass.Disable();
531 maBtnOk.Enable();
534 return 0;
537 IMPL_LINK( ScRetypePassInputDlg, CheckBoxHdl, CheckBox*, EMPTYARG )
539 CheckPasswordInput();
540 return 0;
543 IMPL_LINK( ScRetypePassInputDlg, PasswordModifyHdl, Edit*, EMPTYARG )
545 CheckPasswordInput();
546 return 0;