bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / miscdlgs / retypepassdlg.cxx
blob07f020b05f9080c1ae631995478de900286bbefd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "retypepassdlg.hxx"
22 #include "retypepassdlg.hrc"
23 #include "scresid.hxx"
24 #include "document.hxx"
25 #include "tabprotection.hxx"
27 #include <stdio.h>
29 #include <vcl/msgbox.hxx>
31 ScRetypePassDlg::ScRetypePassDlg(Window* pParent) :
32 ModalDialog(pParent, ScResId(RID_SCDLG_RETYPEPASS)),
34 maBtnOk (this, ScResId(BTN_OK)),
35 maBtnCancel (this, ScResId(BTN_CANCEL)),
36 maBtnHelp (this, ScResId(BTN_HELP)),
38 maTextDescription(this, ScResId(FT_DESC)),
39 maLineDocument(this, ScResId(FL_DOCUMENT)),
40 maTextDocStatus(this, ScResId(FT_DOCSTATUS)),
41 maBtnRetypeDoc(this, ScResId(BTN_RETYPE_DOC)),
43 maLineSheet(this, ScResId(FL_SHEET)),
44 maTextSheetName1(this, ScResId(FT_SHEETNAME1)),
45 maTextSheetStatus1(this, ScResId(FT_SHEETSTATUS1)),
46 maBtnRetypeSheet1(this, ScResId(BTN_RETYPE_SHEET1)),
48 maTextSheetName2(this, ScResId(FT_SHEETNAME2)),
49 maTextSheetStatus2(this, ScResId(FT_SHEETSTATUS2)),
50 maBtnRetypeSheet2(this, ScResId(BTN_RETYPE_SHEET2)),
52 maTextSheetName3(this, ScResId(FT_SHEETNAME3)),
53 maTextSheetStatus3(this, ScResId(FT_SHEETSTATUS3)),
54 maBtnRetypeSheet3(this, ScResId(BTN_RETYPE_SHEET3)),
56 maTextSheetName4(this, ScResId(FT_SHEETNAME4)),
57 maTextSheetStatus4(this, ScResId(FT_SHEETSTATUS4)),
58 maBtnRetypeSheet4(this, ScResId(BTN_RETYPE_SHEET4)),
60 maScrollBar (this, ScResId(SB_SCROLL)),
62 maTextNotProtected(ScResId(STR_NOT_PROTECTED)),
63 maTextNotPassProtected(ScResId(STR_NOT_PASS_PROTECTED)),
64 maTextHashBad(ScResId(STR_HASH_BAD)),
65 maTextHashGood(ScResId(STR_HASH_GOOD)),
66 maTextHashRegen(ScResId(STR_HASH_REGENERATED)),
68 mpDocItem(static_cast<ScDocProtection*>(NULL)),
69 mnCurScrollPos(0),
70 meDesiredHash(PASSHASH_SHA1)
72 Init();
75 ScRetypePassDlg::~ScRetypePassDlg()
79 short ScRetypePassDlg::Execute()
81 PopulateDialog();
82 CheckHashStatus();
83 return ModalDialog::Execute();
86 void ScRetypePassDlg::SetDataFromDocument(const ScDocument& rDoc)
88 const ScDocProtection* pDocProtect = rDoc.GetDocProtection();
89 if (pDocProtect && pDocProtect->isProtected())
90 mpDocItem.reset(new ScDocProtection(*pDocProtect));
92 SCTAB nTabCount = rDoc.GetTableCount();
93 maTableItems.reserve(nTabCount);
94 for (SCTAB i = 0; i < nTabCount; ++i)
96 TableItem aTabItem;
97 rDoc.GetName(i, aTabItem.maName);
99 const ScTableProtection* pTabProtect = rDoc.GetTabProtection(i);
100 if (pTabProtect && pTabProtect->isProtected())
101 aTabItem.mpProtect.reset(new ScTableProtection(*pTabProtect));
103 maTableItems.push_back(aTabItem);
107 void ScRetypePassDlg::SetDesiredHash(ScPasswordHash eHash)
109 meDesiredHash = eHash;
112 void ScRetypePassDlg::WriteNewDataToDocument(ScDocument& rDoc) const
114 if (mpDocItem.get())
115 rDoc.SetDocProtection(mpDocItem.get());
117 size_t nTabCount = static_cast<size_t>(rDoc.GetTableCount());
118 size_t n = maTableItems.size();
119 for (size_t i = 0; i < n; ++i)
121 if (i >= nTabCount)
122 break;
124 ScTableProtection* pTabProtect = maTableItems[i].mpProtect.get();
125 if (pTabProtect)
126 rDoc.SetTabProtection(static_cast<SCTAB>(i), pTabProtect);
130 void ScRetypePassDlg::Init()
132 Link aLink = LINK( this, ScRetypePassDlg, OKHdl );
133 maBtnOk.SetClickHdl(aLink);
135 aLink = LINK( this, ScRetypePassDlg, RetypeBtnHdl );
136 maBtnRetypeDoc.SetClickHdl(aLink);
137 maBtnRetypeSheet1.SetClickHdl(aLink);
138 maBtnRetypeSheet2.SetClickHdl(aLink);
139 maBtnRetypeSheet3.SetClickHdl(aLink);
140 maBtnRetypeSheet4.SetClickHdl(aLink);
142 maTextDocStatus.SetText(maTextNotProtected);
143 maTextSheetStatus1.SetText(maTextNotProtected);
144 maTextSheetStatus2.SetText(maTextNotProtected);
145 maTextSheetStatus3.SetText(maTextNotProtected);
146 maTextSheetStatus4.SetText(maTextNotProtected);
147 maBtnRetypeDoc.Disable();
149 // Make all sheet rows invisible.
151 maTextSheetName1.Show(false);
152 maTextSheetStatus1.Show(false);
153 maBtnRetypeSheet1.Show(false);
154 maBtnRetypeSheet1.Disable();
156 maTextSheetName2.Show(false);
157 maTextSheetStatus2.Show(false);
158 maBtnRetypeSheet2.Show(false);
159 maBtnRetypeSheet2.Disable();
161 maTextSheetName3.Show(false);
162 maTextSheetStatus3.Show(false);
163 maBtnRetypeSheet3.Show(false);
164 maBtnRetypeSheet3.Disable();
166 maTextSheetName4.Show(false);
167 maTextSheetStatus4.Show(false);
168 maBtnRetypeSheet4.Show(false);
169 maBtnRetypeSheet4.Disable();
171 maScrollBar.Show(false);
173 maScrollBar.SetEndScrollHdl( LINK( this, ScRetypePassDlg, ScrollHdl ) );
174 maScrollBar.SetScrollHdl( LINK( this, ScRetypePassDlg, ScrollHdl ) );
176 maScrollBar.SetPageSize(4);
177 maScrollBar.SetVisibleSize(4);
178 maScrollBar.SetLineSize(1);
181 void ScRetypePassDlg::PopulateDialog()
183 // Document protection first.
184 SetDocData();
186 // Sheet protection next. We're only interested in the first 4 sheets
187 // (or less).
188 size_t n = maTableItems.size();
189 for (size_t i = 0; i < n && i < 4; ++i)
190 SetTableData(i, static_cast< SCTAB >( i ));
192 if (n > 4)
194 maScrollBar.Show(true);
195 maScrollBar.SetRange(Range(0, n));
199 void ScRetypePassDlg::SetDocData()
201 bool bBtnEnabled = false;
202 if (mpDocItem.get() && mpDocItem->isProtected())
204 if (mpDocItem->isPasswordEmpty())
205 maTextDocStatus.SetText(maTextNotPassProtected);
206 else if (mpDocItem->hasPasswordHash(meDesiredHash))
207 maTextDocStatus.SetText(maTextHashGood);
208 else
210 // incompatible hash
211 maTextDocStatus.SetText(maTextHashBad);
212 bBtnEnabled = true;
215 maBtnRetypeDoc.Enable(bBtnEnabled);
218 void ScRetypePassDlg::SetTableData(size_t nRowPos, SCTAB nTab)
220 if (nRowPos >= 4)
221 return;
223 FixedText* pName = NULL;
224 FixedText* pStatus = NULL;
225 PushButton* pBtn = NULL;
226 switch (nRowPos)
228 case 0:
229 pName = &maTextSheetName1;
230 pStatus = &maTextSheetStatus1;
231 pBtn = &maBtnRetypeSheet1;
232 break;
233 case 1:
234 pName = &maTextSheetName2;
235 pStatus = &maTextSheetStatus2;
236 pBtn = &maBtnRetypeSheet2;
237 break;
238 case 2:
239 pName = &maTextSheetName3;
240 pStatus = &maTextSheetStatus3;
241 pBtn = &maBtnRetypeSheet3;
242 break;
243 case 3:
244 pName = &maTextSheetName4;
245 pStatus = &maTextSheetStatus4;
246 pBtn = &maBtnRetypeSheet4;
247 break;
248 default:
249 return;
252 bool bBtnEnabled = false;
253 pName->SetText(maTableItems[nTab].maName);
254 pName->Show(true);
255 const ScTableProtection* pTabProtect = maTableItems[nTab].mpProtect.get();
256 if (pTabProtect && pTabProtect->isProtected())
258 if (pTabProtect->isPasswordEmpty())
259 pStatus->SetText(maTextNotPassProtected);
260 else if (pTabProtect->hasPasswordHash(meDesiredHash))
261 pStatus->SetText(maTextHashGood);
262 else
264 // incompatible hash
265 pStatus->SetText(maTextHashBad);
266 bBtnEnabled = true;
269 else
270 pStatus->SetText(maTextNotProtected);
272 pStatus->Show(true);
273 pBtn->Show(true);
274 pBtn->Enable(bBtnEnabled);
277 void ScRetypePassDlg::ResetTableRows()
279 long nScrollPos = maScrollBar.GetThumbPos();
280 mnCurScrollPos = nScrollPos < 0 ? 0 : nScrollPos;
281 size_t nRowCount = maTableItems.size() - nScrollPos;
282 for (size_t i = 0; i < nRowCount; ++i)
283 SetTableData(i, static_cast< SCTAB >( i + nScrollPos ));
286 static bool lcl_IsInGoodStatus(ScPassHashProtectable* pProtected, ScPasswordHash eDesiredHash)
288 if (!pProtected || !pProtected->isProtected())
289 // Not protected.
290 return true;
292 if (pProtected->isPasswordEmpty())
293 return true;
295 if (pProtected->hasPasswordHash(eDesiredHash))
296 return true;
298 return false;
301 void ScRetypePassDlg::CheckHashStatus()
305 if (!lcl_IsInGoodStatus(mpDocItem.get(), meDesiredHash))
306 break;
308 bool bStatusGood = true;
309 size_t nTabCount = maTableItems.size();
310 for (size_t i = 0; i < nTabCount && bStatusGood; ++i)
312 if (!lcl_IsInGoodStatus(maTableItems[i].mpProtect.get(), meDesiredHash))
313 bStatusGood = false;
315 if (!bStatusGood)
316 break;
318 maBtnOk.Enable();
319 return;
321 while (false);
323 maBtnOk.Disable();
326 IMPL_LINK_NOARG(ScRetypePassDlg, OKHdl)
328 EndDialog(RET_OK);
329 return 0;
332 IMPL_LINK( ScRetypePassDlg, RetypeBtnHdl, PushButton*, pBtn )
334 ScPassHashProtectable* pProtected = NULL;
335 if (pBtn == &maBtnRetypeDoc)
337 // document protection.
338 pProtected = mpDocItem.get();
340 else
342 // sheet protection.
343 size_t nTabPos = mnCurScrollPos;
344 if (pBtn == &maBtnRetypeSheet2)
345 nTabPos += 1;
346 else if (pBtn == &maBtnRetypeSheet3)
347 nTabPos += 2;
348 else if (pBtn == &maBtnRetypeSheet4)
349 nTabPos += 3;
350 else if (pBtn != &maBtnRetypeSheet1)
351 // This should never happen !
352 return 0;
354 if (nTabPos >= maTableItems.size())
355 // Likewise, this should never happen !
356 return 0;
358 pProtected = maTableItems[nTabPos].mpProtect.get();
361 if (!pProtected)
362 // What the ... !?
363 return 0;
365 ScRetypePassInputDlg aDlg(this, pProtected);
366 if (aDlg.Execute() == RET_OK)
368 // OK is pressed. Update the protected item.
369 if (aDlg.IsRemovePassword())
371 // Remove password from this item.
372 pProtected->setPassword(String());
374 else
376 // Set a new password.
377 String aNewPass = aDlg.GetNewPassword();
378 pProtected->setPassword(aNewPass);
381 SetDocData();
382 ResetTableRows();
383 CheckHashStatus();
385 return 0;
388 IMPL_LINK_NOARG(ScRetypePassDlg, ScrollHdl)
390 ResetTableRows();
391 return 0;
394 // ============================================================================
396 ScRetypePassInputDlg::ScRetypePassInputDlg(Window* pParent, ScPassHashProtectable* pProtected) :
397 ModalDialog(pParent, ScResId(RID_SCDLG_RETYPEPASS_INPUT)),
399 maBtnOk (this, ScResId(BTN_OK)),
400 maBtnCancel (this, ScResId(BTN_CANCEL)),
401 maBtnHelp (this, ScResId(BTN_HELP)),
403 maBtnRetypePassword(this, ScResId(BTN_RETYPE_PASSWORD)),
405 maPassword1Text (this, ScResId(FT_PASSWORD1)),
406 maPassword1Edit (this, ScResId(ED_PASSWORD1)),
407 maPassword2Text (this, ScResId(FT_PASSWORD2)),
408 maPassword2Edit (this, ScResId(ED_PASSWORD2)),
409 maBtnMatchOldPass(this, ScResId(BTN_MATCH_OLD_PASSWORD)),
411 maBtnRemovePassword(this, ScResId(BTN_REMOVE_PASSWORD)),
413 mpProtected(pProtected)
415 Init();
418 ScRetypePassInputDlg::~ScRetypePassInputDlg()
422 short ScRetypePassInputDlg::Execute()
424 return ModalDialog::Execute();
427 bool ScRetypePassInputDlg::IsRemovePassword() const
429 return maBtnRemovePassword.IsChecked();
432 String ScRetypePassInputDlg::GetNewPassword() const
434 return maPassword1Edit.GetText();
437 void ScRetypePassInputDlg::Init()
439 Link aLink = LINK( this, ScRetypePassInputDlg, OKHdl );
440 maBtnOk.SetClickHdl(aLink);
441 aLink = LINK( this, ScRetypePassInputDlg, RadioBtnHdl );
442 maBtnRetypePassword.SetClickHdl(aLink);
443 maBtnRemovePassword.SetClickHdl(aLink);
444 aLink = LINK( this, ScRetypePassInputDlg, CheckBoxHdl );
445 maBtnMatchOldPass.SetClickHdl(aLink);
446 aLink = LINK( this, ScRetypePassInputDlg, PasswordModifyHdl );
447 maPassword1Edit.SetModifyHdl(aLink);
448 maPassword2Edit.SetModifyHdl(aLink);
450 maBtnOk.Disable();
451 maBtnRetypePassword.Check(true);
452 maBtnMatchOldPass.Check(true);
453 maPassword1Edit.GrabFocus();
456 void ScRetypePassInputDlg::CheckPasswordInput()
458 String aPass1 = maPassword1Edit.GetText();
459 String aPass2 = maPassword2Edit.GetText();
461 if (!aPass1.Len() || !aPass2.Len())
463 // Empty password is not allowed.
464 maBtnOk.Disable();
465 return;
468 if (!aPass1.Equals(aPass2))
470 // The two passwords differ.
471 maBtnOk.Disable();
472 return;
475 if (!maBtnMatchOldPass.IsChecked())
477 maBtnOk.Enable();
478 return;
481 if (!mpProtected)
483 // This should never happen!
484 maBtnOk.Disable();
485 return;
488 bool bPassGood = mpProtected->verifyPassword(aPass1);
489 maBtnOk.Enable(bPassGood);
492 IMPL_LINK_NOARG(ScRetypePassInputDlg, OKHdl)
494 EndDialog(RET_OK);
495 return 0;
498 IMPL_LINK( ScRetypePassInputDlg, RadioBtnHdl, RadioButton*, pBtn )
500 if (pBtn == &maBtnRetypePassword)
502 maBtnRemovePassword.Check(false);
503 maPassword1Text.Enable();
504 maPassword1Edit.Enable();
505 maPassword2Text.Enable();
506 maPassword2Edit.Enable();
507 maBtnMatchOldPass.Enable();
508 CheckPasswordInput();
510 else if (pBtn == &maBtnRemovePassword)
512 maBtnRetypePassword.Check(false);
513 maPassword1Text.Disable();
514 maPassword1Edit.Disable();
515 maPassword2Text.Disable();
516 maPassword2Edit.Disable();
517 maBtnMatchOldPass.Disable();
518 maBtnOk.Enable();
521 return 0;
524 IMPL_LINK_NOARG(ScRetypePassInputDlg, CheckBoxHdl)
526 CheckPasswordInput();
527 return 0;
530 IMPL_LINK_NOARG(ScRetypePassInputDlg, PasswordModifyHdl)
532 CheckPasswordInput();
533 return 0;
536 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */