sync master with lastest vba changes
[ooovba.git] / sc / source / ui / miscdlgs / retypepassdlg.cxx
blob899ae0d747dc1872cb1286e30c6127e9f836009e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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 #include "retypepassdlg.hxx"
32 #include "retypepassdlg.hrc"
33 #include "scresid.hxx"
34 #include "document.hxx"
35 #include "tabprotection.hxx"
37 #include <stdio.h>
39 #include <vcl/msgbox.hxx>
41 ScRetypePassDlg::ScRetypePassDlg(Window* pParent) :
42 ModalDialog(pParent, ScResId(RID_SCDLG_RETYPEPASS)),
44 maBtnOk (this, ScResId(BTN_OK)),
45 maBtnCancel (this, ScResId(BTN_CANCEL)),
46 maBtnHelp (this, ScResId(BTN_HELP)),
48 maTextDescription(this, ScResId(FT_DESC)),
49 maLineDocument(this, ScResId(FL_DOCUMENT)),
50 maTextDocStatus(this, ScResId(FT_DOCSTATUS)),
51 maBtnRetypeDoc(this, ScResId(BTN_RETYPE_DOC)),
53 maLineSheet(this, ScResId(FL_SHEET)),
54 maTextSheetName1(this, ScResId(FT_SHEETNAME1)),
55 maTextSheetStatus1(this, ScResId(FT_SHEETSTATUS1)),
56 maBtnRetypeSheet1(this, ScResId(BTN_RETYPE_SHEET1)),
58 maTextSheetName2(this, ScResId(FT_SHEETNAME2)),
59 maTextSheetStatus2(this, ScResId(FT_SHEETSTATUS2)),
60 maBtnRetypeSheet2(this, ScResId(BTN_RETYPE_SHEET2)),
62 maTextSheetName3(this, ScResId(FT_SHEETNAME3)),
63 maTextSheetStatus3(this, ScResId(FT_SHEETSTATUS3)),
64 maBtnRetypeSheet3(this, ScResId(BTN_RETYPE_SHEET3)),
66 maTextSheetName4(this, ScResId(FT_SHEETNAME4)),
67 maTextSheetStatus4(this, ScResId(FT_SHEETSTATUS4)),
68 maBtnRetypeSheet4(this, ScResId(BTN_RETYPE_SHEET4)),
70 maScrollBar (this, ScResId(SB_SCROLL)),
72 maTextNotProtected(ScResId(STR_NOT_PROTECTED)),
73 maTextNotPassProtected(ScResId(STR_NOT_PASS_PROTECTED)),
74 maTextHashBad(ScResId(STR_HASH_BAD)),
75 maTextHashGood(ScResId(STR_HASH_GOOD)),
76 maTextHashRegen(ScResId(STR_HASH_REGENERATED)),
78 mpDocItem(static_cast<ScDocProtection*>(NULL)),
79 mnCurScrollPos(0),
80 meDesiredHash(PASSHASH_SHA1)
82 Init();
85 ScRetypePassDlg::~ScRetypePassDlg()
89 short ScRetypePassDlg::Execute()
91 PopulateDialog();
92 CheckHashStatus();
93 return ModalDialog::Execute();
96 void ScRetypePassDlg::SetData(const ScDocument& rDoc)
98 const ScDocProtection* pDocProtect = rDoc.GetDocProtection();
99 if (pDocProtect && pDocProtect->isProtected())
100 mpDocItem.reset(new ScDocProtection(*pDocProtect));
102 SCTAB nTabCount = rDoc.GetTableCount();
103 maTableItems.reserve(nTabCount);
104 for (SCTAB i = 0; i < nTabCount; ++i)
106 TableItem aTabItem;
107 rDoc.GetName(i, aTabItem.maName);
109 const ScTableProtection* pTabProtect = rDoc.GetTabProtection(i);
110 if (pTabProtect && pTabProtect->isProtected())
111 aTabItem.mpProtect.reset(new ScTableProtection(*pTabProtect));
113 maTableItems.push_back(aTabItem);
117 void ScRetypePassDlg::SetDesiredHash(ScPasswordHash eHash)
119 meDesiredHash = eHash;
122 void ScRetypePassDlg::WriteNewDataToDocument(ScDocument& rDoc) const
124 if (mpDocItem.get())
125 rDoc.SetDocProtection(mpDocItem.get());
127 size_t nTabCount = rDoc.GetTableCount();
128 size_t n = maTableItems.size();
129 for (size_t i = 0; i < n; ++i)
131 if (i >= nTabCount)
132 break;
134 ScTableProtection* pTabProtect = maTableItems[i].mpProtect.get();
135 if (pTabProtect)
136 rDoc.SetTabProtection(static_cast<SCTAB>(i), pTabProtect);
140 void ScRetypePassDlg::Init()
142 Link aLink = LINK( this, ScRetypePassDlg, OKHdl );
143 maBtnOk.SetClickHdl(aLink);
145 aLink = LINK( this, ScRetypePassDlg, RetypeBtnHdl );
146 maBtnRetypeDoc.SetClickHdl(aLink);
147 maBtnRetypeSheet1.SetClickHdl(aLink);
148 maBtnRetypeSheet2.SetClickHdl(aLink);
149 maBtnRetypeSheet3.SetClickHdl(aLink);
150 maBtnRetypeSheet4.SetClickHdl(aLink);
152 maTextDocStatus.SetText(maTextNotProtected);
153 maTextSheetStatus1.SetText(maTextNotProtected);
154 maTextSheetStatus2.SetText(maTextNotProtected);
155 maTextSheetStatus3.SetText(maTextNotProtected);
156 maTextSheetStatus4.SetText(maTextNotProtected);
157 maBtnRetypeDoc.Disable();
159 // Make all sheet rows invisible.
161 maTextSheetName1.Show(false);
162 maTextSheetStatus1.Show(false);
163 maBtnRetypeSheet1.Show(false);
164 maBtnRetypeSheet1.Disable();
166 maTextSheetName2.Show(false);
167 maTextSheetStatus2.Show(false);
168 maBtnRetypeSheet2.Show(false);
169 maBtnRetypeSheet2.Disable();
171 maTextSheetName3.Show(false);
172 maTextSheetStatus3.Show(false);
173 maBtnRetypeSheet3.Show(false);
174 maBtnRetypeSheet3.Disable();
176 maTextSheetName4.Show(false);
177 maTextSheetStatus4.Show(false);
178 maBtnRetypeSheet4.Show(false);
179 maBtnRetypeSheet4.Disable();
181 maScrollBar.Show(false);
183 maScrollBar.SetEndScrollHdl( LINK( this, ScRetypePassDlg, ScrollHdl ) );
184 maScrollBar.SetScrollHdl( LINK( this, ScRetypePassDlg, ScrollHdl ) );
186 maScrollBar.SetPageSize(4);
187 maScrollBar.SetVisibleSize(4);
188 maScrollBar.SetLineSize(1);
191 void ScRetypePassDlg::PopulateDialog()
193 // Document protection first.
194 SetDocData();
196 // Sheet protection next. We're only interested in the first 4 sheets
197 // (or less).
198 size_t n = maTableItems.size();
199 for (size_t i = 0; i < n && i < 4; ++i)
200 SetTableData(i, i);
202 if (n > 4)
204 maScrollBar.Show(true);
205 maScrollBar.SetRange(Range(0, n));
209 void ScRetypePassDlg::SetDocData()
211 bool bBtnEnabled = false;
212 if (mpDocItem.get() && mpDocItem->isProtected())
214 if (mpDocItem->isPasswordEmpty())
215 maTextDocStatus.SetText(maTextNotPassProtected);
216 else if (mpDocItem->hasPasswordHash(meDesiredHash))
217 maTextDocStatus.SetText(maTextHashGood);
218 else
220 // incompatible hash
221 maTextDocStatus.SetText(maTextHashBad);
222 bBtnEnabled = true;
225 maBtnRetypeDoc.Enable(bBtnEnabled);
228 void ScRetypePassDlg::SetTableData(sal_uInt8 nRowPos, SCTAB nTab)
230 if (nRowPos >= 4)
231 return;
233 FixedText* pName = NULL;
234 FixedText* pStatus = NULL;
235 PushButton* pBtn = NULL;
236 switch (nRowPos)
238 case 0:
239 pName = &maTextSheetName1;
240 pStatus = &maTextSheetStatus1;
241 pBtn = &maBtnRetypeSheet1;
242 break;
243 case 1:
244 pName = &maTextSheetName2;
245 pStatus = &maTextSheetStatus2;
246 pBtn = &maBtnRetypeSheet2;
247 break;
248 case 2:
249 pName = &maTextSheetName3;
250 pStatus = &maTextSheetStatus3;
251 pBtn = &maBtnRetypeSheet3;
252 break;
253 case 3:
254 pName = &maTextSheetName4;
255 pStatus = &maTextSheetStatus4;
256 pBtn = &maBtnRetypeSheet4;
257 break;
258 default:
259 return;
262 bool bBtnEnabled = false;
263 pName->SetText(maTableItems[nTab].maName);
264 pName->Show(true);
265 const ScTableProtection* pTabProtect = maTableItems[nTab].mpProtect.get();
266 if (pTabProtect && pTabProtect->isProtected())
268 if (pTabProtect->isPasswordEmpty())
269 pStatus->SetText(maTextNotPassProtected);
270 else if (pTabProtect->hasPasswordHash(meDesiredHash))
271 pStatus->SetText(maTextHashGood);
272 else
274 // incompatible hash
275 pStatus->SetText(maTextHashBad);
276 bBtnEnabled = true;
279 else
280 pStatus->SetText(maTextNotProtected);
282 pStatus->Show(true);
283 pBtn->Show(true);
284 pBtn->Enable(bBtnEnabled);
287 void ScRetypePassDlg::ResetTableRows()
289 long nScrollPos = maScrollBar.GetThumbPos();
290 mnCurScrollPos = nScrollPos < 0 ? 0 : nScrollPos;
291 size_t nRowCount = maTableItems.size() - nScrollPos;
292 for (size_t i = 0; i < nRowCount; ++i)
293 SetTableData(i, i + nScrollPos);
296 bool lcl_IsInGoodStatus(ScPassHashProtectable* pProtected, ScPasswordHash eDesiredHash)
298 if (!pProtected || !pProtected->isProtected())
299 // Not protected.
300 return true;
302 if (pProtected->isPasswordEmpty())
303 return true;
305 if (pProtected->hasPasswordHash(eDesiredHash))
306 return true;
308 return false;
311 void ScRetypePassDlg::CheckHashStatus()
315 if (!lcl_IsInGoodStatus(mpDocItem.get(), meDesiredHash))
316 break;
318 bool bStatusGood = true;
319 size_t nTabCount = maTableItems.size();
320 for (size_t i = 0; i < nTabCount && bStatusGood; ++i)
322 if (!lcl_IsInGoodStatus(maTableItems[i].mpProtect.get(), meDesiredHash))
323 bStatusGood = false;
325 if (!bStatusGood)
326 break;
328 maBtnOk.Enable();
329 return;
331 while (false);
333 maBtnOk.Disable();
336 IMPL_LINK( ScRetypePassDlg, OKHdl, OKButton*, EMPTYARG )
338 EndDialog(RET_OK);
339 return 0;
342 IMPL_LINK( ScRetypePassDlg, RetypeBtnHdl, PushButton*, pBtn )
344 ScPassHashProtectable* pProtected = NULL;
345 if (pBtn == &maBtnRetypeDoc)
347 // document protection.
348 pProtected = mpDocItem.get();
350 else
352 // sheet protection.
353 size_t nTabPos = mnCurScrollPos;
354 if (pBtn == &maBtnRetypeSheet2)
355 nTabPos += 1;
356 else if (pBtn == &maBtnRetypeSheet3)
357 nTabPos += 2;
358 else if (pBtn == &maBtnRetypeSheet4)
359 nTabPos += 3;
360 else if (pBtn != &maBtnRetypeSheet1)
361 // This should never happen !
362 return 0;
364 if (nTabPos >= maTableItems.size())
365 // Likewise, this should never happen !
366 return 0;
368 pProtected = maTableItems[nTabPos].mpProtect.get();
371 if (!pProtected)
372 // What the ... !?
373 return 0;
375 ScRetypePassInputDlg aDlg(this, pProtected);
376 if (aDlg.Execute() == RET_OK)
378 // OK is pressed. Update the protected item.
379 if (aDlg.IsRemovePassword())
381 // Remove password from this item.
382 pProtected->setPassword(String());
384 else
386 // Set a new password.
387 String aNewPass = aDlg.GetNewPassword();
388 pProtected->setPassword(aNewPass);
391 SetDocData();
392 ResetTableRows();
393 CheckHashStatus();
395 return 0;
398 IMPL_LINK( ScRetypePassDlg, ScrollHdl, ScrollBar*, EMPTYARG )
400 ResetTableRows();
401 return 0;
404 // ============================================================================
406 ScRetypePassInputDlg::ScRetypePassInputDlg(Window* pParent, ScPassHashProtectable* pProtected) :
407 ModalDialog(pParent, ScResId(RID_SCDLG_RETYPEPASS_INPUT)),
409 maBtnOk (this, ScResId(BTN_OK)),
410 maBtnCancel (this, ScResId(BTN_CANCEL)),
411 maBtnHelp (this, ScResId(BTN_HELP)),
413 maBtnRetypePassword(this, ScResId(BTN_RETYPE_PASSWORD)),
415 maPassword1Text (this, ScResId(FT_PASSWORD1)),
416 maPassword1Edit (this, ScResId(ED_PASSWORD1)),
417 maPassword2Text (this, ScResId(FT_PASSWORD2)),
418 maPassword2Edit (this, ScResId(ED_PASSWORD2)),
419 maBtnMatchOldPass(this, ScResId(BTN_MATCH_OLD_PASSWORD)),
421 maBtnRemovePassword(this, ScResId(BTN_REMOVE_PASSWORD)),
423 mpProtected(pProtected)
425 Init();
428 ScRetypePassInputDlg::~ScRetypePassInputDlg()
432 short ScRetypePassInputDlg::Execute()
434 return ModalDialog::Execute();
437 bool ScRetypePassInputDlg::IsRemovePassword() const
439 return maBtnRemovePassword.IsChecked();
442 String ScRetypePassInputDlg::GetNewPassword() const
444 return maPassword1Edit.GetText();
447 void ScRetypePassInputDlg::Init()
449 Link aLink = LINK( this, ScRetypePassInputDlg, OKHdl );
450 maBtnOk.SetClickHdl(aLink);
451 aLink = LINK( this, ScRetypePassInputDlg, RadioBtnHdl );
452 maBtnRetypePassword.SetClickHdl(aLink);
453 maBtnRemovePassword.SetClickHdl(aLink);
454 aLink = LINK( this, ScRetypePassInputDlg, CheckBoxHdl );
455 maBtnMatchOldPass.SetClickHdl(aLink);
456 aLink = LINK( this, ScRetypePassInputDlg, PasswordModifyHdl );
457 maPassword1Edit.SetModifyHdl(aLink);
458 maPassword2Edit.SetModifyHdl(aLink);
460 maBtnOk.Disable();
461 maBtnRetypePassword.Check(true);
462 maBtnMatchOldPass.Check(true);
463 maPassword1Edit.GrabFocus();
466 void ScRetypePassInputDlg::CheckPasswordInput()
468 String aPass1 = maPassword1Edit.GetText();
469 String aPass2 = maPassword2Edit.GetText();
471 if (!aPass1.Len() || !aPass2.Len())
473 // Empty password is not allowed.
474 maBtnOk.Disable();
475 return;
478 if (!aPass1.Equals(aPass2))
480 // The two passwords differ.
481 maBtnOk.Disable();
482 return;
485 if (!maBtnMatchOldPass.IsChecked())
487 maBtnOk.Enable();
488 return;
491 if (!mpProtected)
493 // This should never happen!
494 maBtnOk.Disable();
495 return;
498 bool bPassGood = mpProtected->verifyPassword(aPass1);
499 maBtnOk.Enable(bPassGood);
502 IMPL_LINK( ScRetypePassInputDlg, OKHdl, OKButton*, EMPTYARG )
504 EndDialog(RET_OK);
505 return 0;
508 IMPL_LINK( ScRetypePassInputDlg, RadioBtnHdl, RadioButton*, pBtn )
510 if (pBtn == &maBtnRetypePassword)
512 maBtnRemovePassword.Check(false);
513 maPassword1Text.Enable();
514 maPassword1Edit.Enable();
515 maPassword2Text.Enable();
516 maPassword2Edit.Enable();
517 maBtnMatchOldPass.Enable();
518 CheckPasswordInput();
520 else if (pBtn == &maBtnRemovePassword)
522 maBtnRetypePassword.Check(false);
523 maPassword1Text.Disable();
524 maPassword1Edit.Disable();
525 maPassword2Text.Disable();
526 maPassword2Edit.Disable();
527 maBtnMatchOldPass.Disable();
528 maBtnOk.Enable();
531 return 0;
534 IMPL_LINK( ScRetypePassInputDlg, CheckBoxHdl, CheckBox*, EMPTYARG )
536 CheckPasswordInput();
537 return 0;
540 IMPL_LINK( ScRetypePassInputDlg, PasswordModifyHdl, Edit*, EMPTYARG )
542 CheckPasswordInput();
543 return 0;