1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"Protect" script:
language=
"StarBasic">REM ***** BASIC *****
23 Public PWIndex as Integer
26 Function UnprotectSheetsWithPassWord(oSheets as Object, bDoUnProtect as Boolean)
28 Dim MaxIndex as Integer
29 Dim iMsgResult as Integer
31 If bDocHasProtectedSheets Then
32 If Not bDoUnprotect Then
33 ' At First query if sheets shall generally be unprotected
34 iMsgResult = Msgbox(sMsgUNPROTECT,
36,sMsgDLGTITLE)
35 bDoUnProtect = iMsgResult =
6
38 MaxIndex = oSheets.Count-
1
40 bDocHasProtectedSheets = Not UnprotectSheet(oSheets(i))
41 If bDocHasProtectedSheets Then
47 ReDim UnProtectList() as String
49 ReDim Preserve UnProtectList(PWIndex) as String
52 Msgbox (sMsgSHEETSNOPROTECT,
64, sMsgDLGTITLE)
55 UnProtectSheetsWithPassword = bDocHasProtectedSheets
59 Function UnprotectSheet(oListSheet as Object)
60 Dim ListSheetName as String
61 Dim sStatustext as String
63 Dim bOneSheetIsUnprotected as Boolean
65 ListSheetName = oListSheet.Name
66 If oListSheet.IsProtected Then
67 oListSheet.Unprotect(
"")
68 If oListSheet.IsProtected Then
69 ' Sheet is protected by a Password
70 bOneSheetIsUnProtected = UnprotectSheetWithDialog(oListSheet, ListSheetName)
71 UnProtectSheet() = bOneSheetIsUnProtected
73 ' The Sheet could be unprotected without a password
74 AddSheettoUnprotectionlist(ListSheetName,
"")
75 UnprotectSheet() = True
78 UnprotectSheet() = True
83 Function UnprotectSheetWithDialog(oListSheet as Object, ListSheetName as String) as Boolean
84 Dim PWIsCorrect as Boolean
85 Dim QueryText as String
86 oDocument.CurrentController.SetActiveSheet(oListSheet)
87 QueryText = ReplaceString(sMsgPWPROTECT,
"'" & ListSheetName
& "'",
"%
1TableName%
1")
88 '"Please insert the password to unprotect the sheet
'" & ListSheetName
'"
90 ExecutePasswordDialog(QueryText)
91 If bCancelProtection Then
92 bCancelProtection = False
93 Msgbox (sMsgSHEETSNOPROTECT,
64, sMsgDLGTITLE)
94 UnprotectSheetWithDialog() = False
97 oListSheet.Unprotect(Password)
98 If oListSheet.IsProtected Then
100 Msgbox (sMsgWRONGPW,
64, sMsgDLGTITLE)
102 ' Sheet could be unprotected
103 AddSheettoUnprotectionlist(ListSheetName,Password)
106 Loop Until PWIsCorrect
107 UnprotectSheetWithDialog() = True
111 Sub ExecutePasswordDialog(QueryText as String)
114 .hlnPassword.Label = sMsgPASSWORD
115 .cmdCancel.Label = sMsgCANCEL
116 .cmdHelp.Label = sHELP
117 .cmdGoOn.Label = sMsgOK
118 .cmdGoOn.DefaultButton = True
120 DialogPassword.Execute
124 Password = PasswordModel.txtPassword.Text
125 DialogPassword.EndExecute
130 bCancelProtection = True
131 DialogPassword.EndExecute
135 ' Reprotects the previousliy protected sheets
136 ' The passwordinformation is stored in the List
'UnProtectList()
'
137 Sub ReprotectSheets()
139 Dim oProtectSheet as Object
140 Dim ProtectList() as String
141 Dim SheetName as String
142 Dim SheetPassword as String
143 If PWIndex
> -
1 Then
144 SetStatusLineText(sStsREPROTECT)
146 ProtectList() = ArrayOutOfString(UnProtectList(i),
";
")
147 SheetName = ProtectList(
0)
148 If Ubound(ProtectList())
> 0 Then
149 SheetPassWord = ProtectList(
1)
151 SheetPassword =
""
153 oProtectSheet = oSheets.GetbyName(SheetName)
154 If Not oProtectSheet.IsProtected Then
155 oProtectSheet.Protect(SheetPassWord)
158 SetStatusLineText(
"")
161 ReDim UnProtectList()
165 ' Add a Sheet to the list of sheets that finally have to be
167 Sub AddSheettoUnprotectionlist(ListSheetName as String, Password as String)
168 Dim MaxIndex as Integer
169 MaxIndex = Ubound(UnProtectList())
170 PWIndex = PWIndex +
1
171 If PWIndex
> MaxIndex Then
172 ReDim Preserve UnprotectList(MaxIndex + SBRANGEUBOUND)
174 UnprotectList(PWIndex) = ListSheetName
& ";
" & Password
178 Function CheckSheetProtection(oSheets as Object) as Boolean
179 Dim MaxIndex as Integer
181 Dim bProtectedSheets as Boolean
182 bProtectedSheets = False
183 MaxIndex = oSheets.Count-
1
184 For i =
0 To MaxIndex
185 bProtectedSheets = oSheets(i).IsProtected
186 If bProtectedSheets Then
187 CheckSheetProtection() = True
191 CheckSheetProtection() = False
192 End Function
</script:module>