1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> 3<!--*********************************************************** 4 * 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file 9 * to you under the Apache License, Version 2.0 (the 10 * "License"); you may not use this file except in compliance 11 * with the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, 16 * software distributed under the License is distributed on an 17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18 * KIND, either express or implied. See the License for the 19 * specific language governing permissions and limitations 20 * under the License. 21 * 22 ***********************************************************--> 23<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Protect" script:language="StarBasic">REM ***** BASIC ***** 24Option Explicit 25 26Public PWIndex as Integer 27 28 29Function UnprotectSheetsWithPassWord(oSheets as Object, bDoUnProtect as Boolean) 30Dim i as Integer 31Dim MaxIndex as Integer 32Dim iMsgResult as Integer 33 PWIndex = -1 34 If bDocHasProtectedSheets Then 35 If Not bDoUnprotect Then 36 ' At First query if sheets shall generally be unprotected 37 iMsgResult = Msgbox(sMsgUNPROTECT,36,sMsgDLGTITLE) 38 bDoUnProtect = iMsgResult = 6 39 End If 40 If bDoUnProtect Then 41 MaxIndex = oSheets.Count-1 42 For i = 0 To MaxIndex 43 bDocHasProtectedSheets = Not UnprotectSheet(oSheets(i)) 44 If bDocHasProtectedSheets Then 45 ReprotectSheets() 46 Exit For 47 End If 48 Next i 49 If PWIndex = -1 Then 50 ReDim UnProtectList() as String 51 Else 52 ReDim Preserve UnProtectList(PWIndex) as String 53 End If 54 Else 55 Msgbox (sMsgSHEETSNOPROTECT, 64, sMsgDLGTITLE) 56 End If 57 End If 58 UnProtectSheetsWithPassword = bDocHasProtectedSheets 59End Function 60 61 62Function UnprotectSheet(oListSheet as Object) 63Dim ListSheetName as String 64Dim sStatustext as String 65Dim i as Integer 66Dim bOneSheetIsUnprotected as Boolean 67 i = -1 68 ListSheetName = oListSheet.Name 69 If oListSheet.IsProtected Then 70 oListSheet.Unprotect("") 71 If oListSheet.IsProtected Then 72 ' Sheet is protected by a Password 73 bOneSheetIsUnProtected = UnprotectSheetWithDialog(oListSheet, ListSheetName) 74 UnProtectSheet() = bOneSheetIsUnProtected 75 Else 76 ' The Sheet could be unprotected without a password 77 AddSheettoUnprotectionlist(ListSheetName,"") 78 UnprotectSheet() = True 79 End If 80 Else 81 UnprotectSheet() = True 82 End If 83End Function 84 85 86Function UnprotectSheetWithDialog(oListSheet as Object, ListSheetName as String) as Boolean 87Dim PWIsCorrect as Boolean 88Dim QueryText as String 89 oDocument.CurrentController.SetActiveSheet(oListSheet) 90 QueryText = ReplaceString(sMsgPWPROTECT,"'" & ListSheetName & "'", "%1TableName%1") 91 '"Please insert the password to unprotect the sheet '" & ListSheetName'" 92 Do 93 ExecutePasswordDialog(QueryText) 94 If bCancelProtection Then 95 bCancelProtection = False 96 Msgbox (sMsgSHEETSNOPROTECT, 64, sMsgDLGTITLE) 97 UnprotectSheetWithDialog() = False 98 exit Function 99 End If 100 oListSheet.Unprotect(Password) 101 If oListSheet.IsProtected Then 102 PWIsCorrect = False 103 Msgbox (sMsgWRONGPW, 64, sMsgDLGTITLE) 104 Else 105 ' Sheet could be unprotected 106 AddSheettoUnprotectionlist(ListSheetName,Password) 107 PWIsCorrect = True 108 End If 109 Loop Until PWIsCorrect 110 UnprotectSheetWithDialog() = True 111End Function 112 113 114Sub ExecutePasswordDialog(QueryText as String) 115 With PasswordModel 116 .Title = QueryText 117 .hlnPassword.Label = sMsgPASSWORD 118 .cmdCancel.Label = sMsgCANCEL 119 .cmdHelp.Label = sHELP 120 .cmdGoOn.Label = sMsgOK 121 .cmdGoOn.DefaultButton = True 122 End With 123 DialogPassword.Execute 124End Sub 125 126Sub ReadPassword() 127 Password = PasswordModel.txtPassword.Text 128 DialogPassword.EndExecute 129End Sub 130 131 132Sub RejectPassword() 133 bCancelProtection = True 134 DialogPassword.EndExecute 135End Sub 136 137 138' Reprotects the previousliy protected sheets 139' The passwordinformation is stored in the List 'UnProtectList()' 140Sub ReprotectSheets() 141Dim i as Integer 142Dim oProtectSheet as Object 143Dim ProtectList() as String 144Dim SheetName as String 145Dim SheetPassword as String 146 If PWIndex > -1 Then 147 SetStatusLineText(sStsREPROTECT) 148 For i = 0 To PWIndex 149 ProtectList() = ArrayOutOfString(UnProtectList(i),";") 150 SheetName = ProtectList(0) 151 If Ubound(ProtectList()) > 0 Then 152 SheetPassWord = ProtectList(1) 153 Else 154 SheetPassword = "" 155 End If 156 oProtectSheet = oSheets.GetbyName(SheetName) 157 If Not oProtectSheet.IsProtected Then 158 oProtectSheet.Protect(SheetPassWord) 159 End If 160 Next i 161 SetStatusLineText("") 162 End If 163 PWIndex = -1 164 ReDim UnProtectList() 165End Sub 166 167 168' Add a Sheet to the list of sheets that finally have to be 169' unprotected 170Sub AddSheettoUnprotectionlist(ListSheetName as String, Password as String) 171Dim MaxIndex as Integer 172 MaxIndex = Ubound(UnProtectList()) 173 PWIndex = PWIndex + 1 174 If PWIndex > MaxIndex Then 175 ReDim Preserve UnprotectList(MaxIndex + SBRANGEUBOUND) 176 End If 177 UnprotectList(PWIndex) = ListSheetName & ";" & Password 178End Sub 179 180 181Function CheckSheetProtection(oSheets as Object) as Boolean 182Dim MaxIndex as Integer 183Dim i as Integer 184Dim bProtectedSheets as Boolean 185 bProtectedSheets = False 186 MaxIndex = oSheets.Count-1 187 For i = 0 To MaxIndex 188 bProtectedSheets = oSheets(i).IsProtected 189 If bProtectedSheets Then 190 CheckSheetProtection() = True 191 Exit Function 192 End If 193 Next i 194 CheckSheetProtection() = False 195End Function</script:module> 196