summaryrefslogtreecommitdiff
path: root/wizards/source/importwizard/FilesModul.xba
blob: 1f207aa9ea6ef93f8564d4c70147041ec9b37ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="FilesModul" script:language="StarBasic">Option Explicit

Public AbsTemplateFound as Integer
Public AbsDocuFound as Integer
Public oLogDocument as Object
Public oLogTable as Object
Public bInsertRow as Boolean
Public sLogUrl as String
Public sCurPassWord as String



Function ReadApplicationDirectories(ApplIndex as Integer, FilesList(),bIsDocument as Boolean, sFiltername()) as Integer
Dim bCheckDocuType as Boolean
Dim FilterIndex as Integer
Dim bRecursive as Boolean
Dim sSourceDir as String
Dim bCheckRealType as Boolean
Dim a as Integer
Dim sFileContent() as String
Dim NewList() as String
Dim Index as Integer
Dim sLocExtension as String
	Index = Applications(ApplIndex,SBAPPLKEY)
	sLocExtension = &quot;&quot;
	If bIsDocument Then
		bCheckDocuType = Applications(ApplIndex,SBDOCCONVERT)
		bCheckRealType = False
		bRecursive = Applications(ApplIndex,SBDOCRECURSIVE)
		FilterIndex = Index
		sSourceDir = Applications(ApplIndex,SBDOCSOURCE)
	Else
		&apos; Templates
		bCheckDocuType = Applications(ApplIndex,SBTEMPLCONVERT)
		&apos; In SO the documenttype cannot be derived from the extension name
		bCheckRealType = WizardMode = SBXMLMODE
		If bCheckRealType Then
			&apos; Note: StarOffice-Math-Documents cannot be treated like templates
			bCheckRealType = Index &lt;&gt; 3
			If bCheckRealType Then
				sLocExtension = &quot;vor&quot;
			End If
			bIsDocument = Not bCheckRealType
		End If
		bRecursive = Applications(ApplIndex,SBTEMPLRECURSIVE)
    	FilterIndex = Index + MaxApplCount
		sSourceDir = Applications(ApplIndex,SBTEMPLSOURCE)
	End If
	If bCheckDocuType Then
		sFileContent() = GetMimeTypeList(sFilterName(FilterIndex))
		NewList() = ReadDirectories(sSourceDir, bRecursive, bCheckRealType, False, sFileContent(), sLocExtension)
		If Ubound(NewList()) &gt; -1 Then
			AddListtoFilesList(FilesList(), NewList(), ApplIndex)
			ImportDialog.LabelRetrieval.Label = sProgressPage_2 &amp;  &quot;  &quot; &amp; ReplaceString(sProgressPage_5, Str(Ubound(FilesList()) + 1) &amp; &quot; &quot;, &quot;%1&quot;)
		End If
	End If
	ReadApplicationDirectories() = Ubound(NewList(),1) + 1
End Function


Sub	ShowCurrentProgress(bIsDocument as Boolean, CurFound as Integer)
	If bIsDocument Then
		AbsDocuFound = AbsDocuFound + CurFound
		ImportDialog.LabelCurDocumentRetrieval.Label =  sProgressFound &amp; &quot; &quot; &amp; CStr(AbsDocuFound) &amp;  &quot; &quot; &amp; sProgressMoreDocs
	Else
		AbsTemplateFound = AbsTemplateFound + CurFound
		ImportDialog.LabelCurTemplateRetrieval.Label = sProgressFound &amp; &quot; &quot; &amp; CStr(AbsTemplateFound) &amp; &quot; &quot; &amp; sProgressMoreTemplates
  End If
End Sub


Sub ConvertAllDocuments(sFilterName())
Dim FileProperties(1) as new com.sun.star.beans.PropertyValue
Dim WriterWebProperties(0) as new com.sun.star.beans.PropertyValue
Dim OpenProperties(2) as new com.sun.star.beans.PropertyValue
Dim FilesList(0,2) as String
Dim sViewPath as String
Dim i as Integer
Dim FilterIndex as Integer
Dim sFullName as String
Dim sFileName as String
Dim oDocument as Object
Dim sExtension as String
Dim OldExtension as String
Dim CurFound as Integer
Dim TotFound as Integer
Dim TargetStemDir as String
Dim SourceStemDir as String
Dim TargetDir as String
Dim TargetFile as String
Dim CurFilterName as String
Dim ApplIndex as Integer
Dim Index as Integer
Dim bIsDocument as Boolean
Dim iOverWrite as Integer
Dim bDoSave as Boolean
Dim sCurFileExists as String
Dim oModel as Object
Dim MaxFileIndex as Integer
Dim FileCount as Integer
&apos;Dim sOldExtension as String
Dim bContainsBasicMacro as Boolean
Dim bIsPassWordProtected as Boolean
	bConversionIsRunnig = True
	AbsTemplateFound = 0
	AbsDocuFound = 0
	For i = 0 To ApplCount-1
		&apos;templates
		bIsDocument = False
		CurFound = ReadApplicationDirectories(i, FilesList(), bIsDocument, sFilterName())
		ShowCurrentProgress(bIsDocument, CurFound)
	Next i
	For i = 0 To ApplCount-1
		&apos;documents
		bIsDocument = True
		CurFound = ReadApplicationDirectories(i, FilesList(), bIsDocument, sFilterName())
		ShowCurrentProgress(bIsDocument, CurFound)
	Next i
	TotFound = AbsTemplateFound + AbsDocuFound
	CreateLogDocument(OpenProperties())
	If TotFound &gt; 0 Then
		InitializeProgressPage(ImportDialog)
		OpenProperties(0).Name = &quot;Hidden&quot;
		OpenProperties(0).Value = True
		OpenProperties(1).Name = &quot;AsTemplate&quot;
		OpenProperties(1).Name = False
		OpenProperties(2).Name = &quot;Silent&quot;
		OpenProperties(2).Name = True
		
		MaxFileIndex = Ubound(FilesList(),1)
		FileCount = 0
		For i = 0 To MaxFileIndex
			If bCancelTask Or RetValue = 0 Then
				bConversionIsRunnig = False
				Exit Sub
			End if
			bDoSave = True
			sFullName = FilesList(i,0)
			CurFiltername =	GetFilterName(FilesList(i,1), sFilterName(), sExtension, FilterIndex)
			ApplIndex = FilesList(i,2)
			sViewPath = CutPathView(sFullName, 60)
			ImportDialog.LabelCurDocument.Label = Str(i+1) &amp; &quot;/&quot; &amp; MaxFileIndex + 1 &amp; &quot;  (&quot; &amp; sViewPath &amp; &quot;)&quot;
&apos;		sOldExtension = GetFileNameExtension(sFullName, &quot;/&quot;)
&apos;		Select Case sOldExtension
&apos;			Case &quot;vor&quot;, &quot;dot&quot;, &quot;xlt&quot;, &quot;pot&quot;
&apos;				OpenProperties(1).Value = False
&apos;			Case Else
&apos;				OpenProperties(1).Value = False
&apos;		End Select
			oDocument = StarDesktop.LoadComponentFromURL(sFullName, &quot;_blank&quot;, 0, OpenProperties())
			bIsPassWordProtected = CheckPassWordProtection(oDocument)
			bContainsBasicMacro = CheckIfMacroExists(oDocument)
			If bSetFonts Then
				CheckScripts(oDocument, 1)
			End If

			If Not IsNull(oDocument) Then
				Select Case sExtension
					Case &quot;sxw&quot;, &quot;sxc&quot;, &quot;sxi&quot;, &quot;sxd&quot;, &quot;sxs&quot;, &quot;sxm&quot;
						SourceStemDir = RTrimStr(Applications(ApplIndex,SBDOCSOURCE), &quot;/&quot;)
						TargetStemDir = RTrimStr(Applications(ApplIndex,SBDOCTARGET), &quot;/&quot;)
					Case Else 								&apos; Templates and Helper-Applications remain
						SourceStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLSOURCE), &quot;/&quot;)
						TargetStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLTARGET), &quot;/&quot;)
				End Select

				TargetFile = ReplaceString(sFullname, TargetStemDir, SourceStemDir)
				sFileName = GetFileNameWithoutExtension(TargetFile, &quot;/&quot;)
				OldExtension = GetFileNameExtension(TargetFile)

				TargetFile = RTrimStr(TargetFile, OldExtension)
				TargetFile = TargetFile &amp; sExtension
				TargetDir = RTrimStr(TargetFile, sFileName &amp; &quot;.&quot; &amp; sExtension)
				If Not oUcb.Exists(TargetDir) Then
					CreateFolder(TargetDir)
				End If
				If oUcb.Exists(TargetFile) Then
					sCurFileExists = ReplaceString(sFileExists, ConvertFromUrl(TargetFile), &quot;&lt;1&gt;&quot;)
					sCurFileExists = ReplaceString(sCurFileExists, chr(13), &quot;&lt;CR&gt;&quot;)
					iOverWrite = Msgbox (sCurFileExists, 32 + 3, sTitle)
					Select Case iOverWrite
						Case 1	&apos; OK
							&apos; In the FileProperty-Bean this is already default
							bDoSave = True
						Case 2 	&apos; Abort
							CancelTask(False)
							bDoSave = False
						Case 7 	&apos; No
							bDoSave = False
					End Select
				End If
				If bDoSave Then
					InsertDocNamesToLogDocument(sFullName, TargetFile, bContainsBasicMacro)
					On Local Error Resume Next
					FileProperties(0).Name = &quot;FilterName&quot;
					FileProperties(0).Value = CurFilterName
					If bIsPassWordProtected Then
						FileProperties(1).Name = &quot;PassWord&quot;
						FileProperties(1).Value = sCurPassWord
					End If
					oDocument.StoreAsUrl(TargetFile,FileProperties())
					FileCount = FileCount + 1
					oDocument.Dispose()
					On Local Error Goto 0
				End If
			End If
		Next i
	End If
	AddLogStatistics(FileCount)
	oLogDocument.Dispose()
	If (ImportDialog.chkLogfile.State = 1) And (FileCount &gt; 0) Then
		ImportDialog.cmdShowLogFile.Enabled = True
	End If
	ImportDialog.cmdCancel.Label = sCloseButton
	ImportDialog.cmdGoOn.Label = sReady
	ImportDialog.cmdGoOn.Enabled = True
	bConversionIsRunnig = False
	Exit Sub
RTError:
	Msgbox sRTErrorDesc, 16, sRTErrorHeader
End Sub



Sub AddListtoFilesList(FirstList(), SecList(), ApplIndex as Integer)
Dim FirstStart as Integer, FirstEnd as Integer, i as Integer, s as Integer
	If FirstList(0,0) = &quot;&quot; Then
		FirstStart = Ubound(FirstList(),1)
	Else
		FirstStart = Ubound(FirstList(),1) + 1
	End If
	FirstEnd = FirstStart + Ubound(SecList(),1)
	ReDim Preserve FirstList(FirstEnd,2)
	s = 0
	For i = FirstStart To FirstEnd
		FirstList(i,0) = SecList(s,0)
		FirstList(i,1) = SecList(s,1)
		FirstList(i,2) = CStr(ApplIndex)
		s = s + 1
	Next i
End Sub


Function GetTargetTemplatePath(Index as Integer)
	Select Case WizardMode
		Case SBMICROSOFTMODE
			GetTargetTemplatePath() = SOTemplatePath &amp; &quot;/&quot; &amp; sTemplateGroupName
		Case SBXMLMODE
			If Index = 3 Then
				&apos; Helper Application
				GetTargetTemplatePath = SOWorkPath
			Else
				GetTargetTemplatePath = SOTemplatePath
			End If
	End Select
End Function


&apos; Retrieves the second value for a next to &apos;SearchString&apos; in
&apos; a two-dimensional string-Array
Function GetFilterName(sMimetypeorExtension as String, sFilterName(), sExtension as string, FilterIndex as Integer) as String
Dim i as Integer
Dim MaxIndex as Integer
Dim sLocFilterlist() as String
	For i = 0 To Ubound(sFiltername(),1)
		If Instr(1,sFilterName(i,0),sMimeTypeOrExtension) &lt;&gt; 0 Then
			sLocFilterList() = ArrayoutofString(sFiltername(i,0),&quot;|&quot;, MaxIndex)
			If MaxIndex = 0 Then
				sExtension = sFiltername(i,2)
				GetFilterName = sFilterName(i,1)
			Else
				Dim a as Integer
				Dim sLocExtensionList() as String
				a =	SearchArrayForPartString(sMimetypeOrExtension, sLocFilterList())
				sLocFilterList() = ArrayoutofString(sFiltername(i,1),&quot;|&quot;, MaxIndex)
				GetFilterName = sLocFilterList(a)
				sLocExtensionList() = ArrayoutofString(sFilterName(i,2), &quot;|&quot;, MaxIndex)
				sExtension = sLocExtensionList(a)
			End If
			Exit For
		End If
	Next
	FilterIndex = i
End Function


Function SearchArrayforPartString(SearchString as String, LocList()) as Integer
Dim i as integer
	For i = Lbound(LocList(),1) to Ubound(LocList(),1)
		If Instr(1,LocList(i), SearchString) &lt;&gt; 0 Then
			SearchArrayForPartString() = i
			Exit Function
		End if
	Next
	IndexinArray = -1
End Function


Function GetMimeTypeList(BigFiltername as STring)
Dim sBigList() as String
Dim sSmallList() as String
Dim sMimeTypeList()
Dim BigMaxIndex as Integer
Dim n as Integer
	sBigList() = ArrayoutofString(BigFilterName,&quot;|&quot;, BigMaxIndex)
	For n = 0 To BigMaxIndex
		sSmallList() = ArrayoutofString(sBigList(n),&quot;;&quot;)
		sMimeTypeList() = AddListToList(sMimeTypeList(), sSmallList())
	Next n
	GetMimetypeList() = sMimeTypeList()
End Function


Sub CreateLogDocument(HiddenProperties())
Dim OpenProperties(0) as new com.sun.star.beans.PropertyValue
Dim oTableCursor as Object
Dim oLogCursor as Object
Dim oLogRows as Object
Dim NoArgs()
Dim i as Integer
Dim bLogExists as Boolean
	If ImportDialog.chkLogfile.State = 1 Then
		i = 2
		OpenProperties(0).Name = &quot;Hidden&quot;
		OpenProperties(0).Value = True
		oLogDocument = StarDesktop.LoadComponentFromURL(&quot;private:factory/swriter&quot;, &quot;_blank&quot;, 4, OpenProperties())
		oLogCursor = oLogDocument.Text.CreateTextCursor
		oLogTable =  oLogDocument.CreateInstance(&quot;com.sun.star.text.TextTable&quot;)
		oLogTable.RepeatHeadline = true
		oLogCursor.Text.InsertTextContent(oLogCursor, oLogTable, True)
		oLogCursor = oLogTable.GetCellbyPosition(0,0).createTextCursor()
		oLogCursor.SetString(sSourceDocuments)
		oLogCursor = oLogTable.GetCellbyPosition(1,0).createTextCursor()
		oLogCursor.SetString(sTargetDocuments)
		bInsertRow = False
		sLogUrl = SOWorkPath &amp; &quot;/Logfile.sxw&quot;
		Do
			bLogExists = oUcb.Exists(sLogUrl)
			If bLogExists Then
				If i = 2 Then
					sLogUrl = ReplaceString(sLogUrl, &quot;/Logfile_2.sxw&quot;, &quot;/Logfile.sxw&quot;)
				Else
					sLogUrl = ReplaceString(sLogUrl, &quot;/Logfile_&quot; &amp; cStr(i) &amp; &quot;.sxw&quot;, &quot;/Logfile_&quot; &amp; cStr(i-1) &amp; &quot;.sxw&quot;)
				End If
				i = i + 1
			End If
		Loop Until Not bLogExists
		oLogDocument.StoreAsUrl(sLogUrl, NoArgs())
	EndIf
End Sub


Sub InsertDocNamesToLogDocument(SourceUrl as String, TargetUrl as String, bContainsBasicMacro as Boolean)
Dim oCell as Object
Dim oLogCursor as Object
Dim UrlList(1) as String
Dim LocFileName as String
Dim LocUrl as String
Dim i as Integer
Dim oCommentCursor as Object
	If ImportDialog.chkLogfile.State = 1 Then
		If bInsertRow Then
			oLogTable.Rows.InsertByIndex(oLogTable.Rows.Count,1)
		Else
			bInsertRow = True
		End If
		UrlList(0) = SourceUrl
		UrlList(1) = TargetUrl
		For i = 0 To 1
			oCell = oLogTable.GetCellbyPosition(i,oLogTable.Rows.Count-1)
			If i = 1 And bContainsBasicMacro Then
				oCommentCursor = oCell.createTextCursor()
				oCell.insertControlCharacter(oCommentCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
				oCell.insertString(oCommentCursor, sReeditMacro, false)								
			End If
			LocUrl = UrlList(i)
			oLogCursor = oCell.createTextCursor()
			oLogCursor.CollapseToStart()
			oLogCursor.HyperLinkURL = LocUrl
			oLogCursor.HyperLinkName = LocUrl
			oLogCursor.HyperLinkTarget = LocUrl
			LocFileName = FileNameOutOfPath(LocUrl)
			oCell.InsertString(oLogCursor, LocFileName,False)
		Next i 
		oLogDocument.Store()
	End If
End Sub


Sub AddLogStatistics(FileCount as Integer)
Dim oCell as Object
Dim oLogCursor as Object
Dim MaxRowIndex as Integer
Dim oTableCursor as Object
	If ImportDialog.chkLogfile.State = 1 Then
		MaxRowIndex = oLogTable.Rows.Count
		sLogSummary = ReplaceString(sLogSummary, FileCount, &quot;&lt;COUNT&gt;&quot;)
		oLogTable.Rows.InsertByIndex(MaxRowIndex, 1)
		oCell = oLogTable.GetCellbyPosition(0, MaxRowIndex)
		oLogCursor = oCell.createTextCursor()
		oCell.InsertString(oLogCursor, sLogSummary,False)
		oTableCursor = oLogTable.createCursorByCellName(oCell.CellName)
		oTableCursor.goRight(1, True)
		oTableCursor.mergeRange()
		oLogDocument.Store()
	End If
End Sub


&apos; This macro has to be reworked out again as it works with deprecated interfaces
&apos; It can be completed as soon as Bug #93295 will be fixed because right now it
&apos; is not possible to access BasicLibraries and DialogLibraries from outside the 
&apos; document
Function CheckIfMacroExists(oDocument as Object) as Boolean
Dim ModuleNames() as String
Dim MaxIndex as Integer
Dim oLibraryContainer as Object
&apos;	oLibraryContainer = oDocument.LibraryContainer
&apos;	If oLibraryContainer.hasElements Then
&apos;		ModuleNames = oDocument.LibraryContainer.ElementNames
&apos;		MaxIndex = Ubound(ModuleNames())
&apos;		For i = 0 To MaxIndex
&apos;			oLibrary = oLibraryContainer.getByName(ModuleNames(i))
&apos;			If Not IsNull(oLibrary.getDialogContainer) Then
&apos;				CheckIf MacroExists = True
&apos;				Exit Function
&apos;			End If
&apos;			If oLibrary.ModuleContainer.HasElements Then
&apos;				CheckIfMacroExists() = True
&apos;				Exit Function
&apos;			End If
&apos;		Next i
&apos;	End If
	CheckIfMacroExists() = False
End Function



Function CheckPassWordProtection(oDocument as Object)
Dim bIsPassWordProtected as Boolean
Dim i as Integer
Dim oArgs()
Dim MaxIndex as Integer
	bIsPassWordProtected = false
 	oArgs() = oDocument.getArgs()
 	MaxIndex = Ubound(oArgs())
	For i = 0 To MaxIndex
		If oArgs(i).Name = &quot;PassWord&quot; Then
			bIsPassWordProtected = True
			sCurPassWord = oArgs(i).Value
			Exit For
		End If
	Next i
	CheckPassWordProtection() = bIsPassWordProtected
End Function


Sub OpenLogDocument()
Dim NoArgs() as New com.sun.star.beans.PropertyValue
	OpenDocument(sLogUrl, NoArgs())
End Sub</script:module>