update dev300-m58
[ooovba.git] / wizards / source / euro / Protect.xba
blob8a852e9c3e5c81e3e7bddb497831c313a985f1de
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Protect" script:language="StarBasic">REM ***** BASIC *****
4 Option Explicit
6 Public PWIndex as Integer
9 Function UnprotectSheetsWithPassWord(oSheets as Object, bDoUnProtect as Boolean)
10 Dim i as Integer
11 Dim MaxIndex as Integer
12 Dim iMsgResult as Integer
13 PWIndex = -1
14 If bDocHasProtectedSheets Then
15 If Not bDoUnprotect Then
16 &apos; At First query if sheets shall generally be unprotected
17 iMsgResult = Msgbox(sMsgUNPROTECT,36,sMsgDLGTITLE)
18 bDoUnProtect = iMsgResult = 6
19 End If
20 If bDoUnProtect Then
21 MaxIndex = oSheets.Count-1
22 For i = 0 To MaxIndex
23 bDocHasProtectedSheets = Not UnprotectSheet(oSheets(i))
24 If bDocHasProtectedSheets Then
25 ReprotectSheets()
26 Exit For
27 End If
28 Next i
29 If PWIndex = -1 Then
30 ReDim UnProtectList() as String
31 Else
32 ReDim Preserve UnProtectList(PWIndex) as String
33 End If
34 Else
35 Msgbox (sMsgSHEETSNOPROTECT, 64, sMsgDLGTITLE)
36 End If
37 End If
38 UnProtectSheetsWithPassword = bDocHasProtectedSheets
39 End Function
42 Function UnprotectSheet(oListSheet as Object)
43 Dim ListSheetName as String
44 Dim sStatustext as String
45 Dim i as Integer
46 Dim bOneSheetIsUnprotected as Boolean
47 i = -1
48 ListSheetName = oListSheet.Name
49 If oListSheet.IsProtected Then
50 oListSheet.Unprotect(&quot;&quot;)
51 If oListSheet.IsProtected Then
52 &apos; Sheet is protected by a Password
53 bOneSheetIsUnProtected = UnprotectSheetWithDialog(oListSheet, ListSheetName)
54 UnProtectSheet() = bOneSheetIsUnProtected
55 Else
56 &apos; The Sheet could be unprotected without a password
57 AddSheettoUnprotectionlist(ListSheetName,&quot;&quot;)
58 UnprotectSheet() = True
59 End If
60 Else
61 UnprotectSheet() = True
62 End If
63 End Function
66 Function UnprotectSheetWithDialog(oListSheet as Object, ListSheetName as String) as Boolean
67 Dim PWIsCorrect as Boolean
68 Dim QueryText as String
69 oDocument.CurrentController.SetActiveSheet(oListSheet)
70 QueryText = ReplaceString(sMsgPWPROTECT,&quot;&apos;&quot; &amp; ListSheetName &amp; &quot;&apos;&quot;, &quot;%1TableName%1&quot;)
71 &apos;&quot;Please insert the password to unprotect the sheet &apos;&quot; &amp; ListSheetName&apos;&quot;
73 ExecutePasswordDialog(QueryText)
74 If bCancelProtection Then
75 bCancelProtection = False
76 Msgbox (sMsgSHEETSNOPROTECT, 64, sMsgDLGTITLE)
77 UnprotectSheetWithDialog() = False
78 exit Function
79 End If
80 oListSheet.Unprotect(Password)
81 If oListSheet.IsProtected Then
82 PWIsCorrect = False
83 Msgbox (sMsgWRONGPW, 64, sMsgDLGTITLE)
84 Else
85 &apos; Sheet could be unprotected
86 AddSheettoUnprotectionlist(ListSheetName,Password)
87 PWIsCorrect = True
88 End If
89 Loop Until PWIsCorrect
90 UnprotectSheetWithDialog() = True
91 End Function
94 Sub ExecutePasswordDialog(QueryText as String)
95 With PasswordModel
96 .Title = QueryText
97 .hlnPassword.Label = sMsgPASSWORD
98 .cmdCancel.Label = sMsgCANCEL
99 .cmdHelp.Label = sHELP
100 .cmdGoOn.Label = sMsgOK
101 .cmdGoOn.DefaultButton = True
102 End With
103 DialogPassword.Execute
104 End Sub
106 Sub ReadPassword()
107 Password = PasswordModel.txtPassword.Text
108 DialogPassword.EndExecute
109 End Sub
112 Sub RejectPassword()
113 bCancelProtection = True
114 DialogPassword.EndExecute
115 End Sub
118 &apos; Reprotects the previousliy protected sheets
119 &apos; The passwordinformation is stored in the List &apos;UnProtectList()&apos;
120 Sub ReprotectSheets()
121 Dim i as Integer
122 Dim oProtectSheet as Object
123 Dim ProtectList() as String
124 Dim SheetName as String
125 Dim SheetPassword as String
126 If PWIndex &gt; -1 Then
127 SetStatusLineText(sStsREPROTECT)
128 For i = 0 To PWIndex
129 ProtectList() = ArrayOutOfString(UnProtectList(i),&quot;;&quot;)
130 SheetName = ProtectList(0)
131 If Ubound(ProtectList()) &gt; 0 Then
132 SheetPassWord = ProtectList(1)
133 Else
134 SheetPassword = &quot;&quot;
135 End If
136 oProtectSheet = oSheets.GetbyName(SheetName)
137 If Not oProtectSheet.IsProtected Then
138 oProtectSheet.Protect(SheetPassWord)
139 End If
140 Next i
141 SetStatusLineText(&quot;&quot;)
142 End If
143 PWIndex = -1
144 ReDim UnProtectList()
145 End Sub
148 &apos; Add a Sheet to the list of sheets that finally have to be
149 &apos; unprotected
150 Sub AddSheettoUnprotectionlist(ListSheetName as String, Password as String)
151 Dim MaxIndex as Integer
152 MaxIndex = Ubound(UnProtectList())
153 PWIndex = PWIndex + 1
154 If PWIndex &gt; MaxIndex Then
155 ReDim Preserve UnprotectList(MaxIndex + SBRANGEUBOUND)
156 End If
157 UnprotectList(PWIndex) = ListSheetName &amp; &quot;;&quot; &amp; Password
158 End Sub
161 Function CheckSheetProtection(oSheets as Object) as Boolean
162 Dim MaxIndex as Integer
163 Dim i as Integer
164 Dim bProtectedSheets as Boolean
165 bProtectedSheets = False
166 MaxIndex = oSheets.Count-1
167 For i = 0 To MaxIndex
168 bProtectedSheets = oSheets(i).IsProtected
169 If bProtectedSheets Then
170 CheckSheetProtection() = True
171 Exit Function
172 End If
173 Next i
174 CheckSheetProtection() = False
175 End Function</script:module>