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