home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Bureautique / OpenOffice / Apache_OpenOffice_4.1.1_Win_x86_install_fr.exe / openoffice1.cab / Protect.xba < prev    next >
Extensible Markup Language  |  2014-02-25  |  6KB  |  196 lines

  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  *****
  24. Option Explicit
  25.  
  26. Public PWIndex as Integer
  27.  
  28.  
  29. Function UnprotectSheetsWithPassWord(oSheets as Object, bDoUnProtect as Boolean)
  30. Dim i as Integer
  31. Dim MaxIndex as Integer
  32. Dim 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
  59. End Function
  60.  
  61.  
  62. Function UnprotectSheet(oListSheet as Object)
  63. Dim ListSheetName as String
  64. Dim sStatustext as String
  65. Dim i as Integer
  66. Dim 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
  83. End Function
  84.  
  85.  
  86. Function UnprotectSheetWithDialog(oListSheet as Object, ListSheetName as String) as Boolean
  87. Dim PWIsCorrect as Boolean
  88. Dim 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
  111. End Function
  112.  
  113.  
  114. Sub    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
  124. End Sub
  125.  
  126. Sub ReadPassword()
  127.     Password = PasswordModel.txtPassword.Text
  128.     DialogPassword.EndExecute
  129. End Sub
  130.  
  131.  
  132. Sub RejectPassword()
  133.     bCancelProtection = True
  134.     DialogPassword.EndExecute
  135. End Sub
  136.  
  137.  
  138. ' Reprotects the previousliy protected sheets
  139. ' The passwordinformation is stored in the List 'UnProtectList()'
  140. Sub ReprotectSheets()
  141. Dim i as Integer
  142. Dim oProtectSheet as Object
  143. Dim ProtectList() as String
  144. Dim SheetName as String
  145. Dim 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()
  165. End Sub
  166.  
  167.  
  168. ' Add a Sheet to the list of sheets that finally have to be
  169. ' unprotected
  170. Sub AddSheettoUnprotectionlist(ListSheetName as String, Password as String)
  171. Dim 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
  178. End Sub
  179.  
  180.  
  181. Function CheckSheetProtection(oSheets as Object) as Boolean
  182. Dim MaxIndex as Integer
  183. Dim i as Integer
  184. Dim 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
  195. End Function</script:module>
  196.