-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnDocLoadSetCurrentLocationInTitleBar.xba
134 lines (75 loc) · 4.41 KB
/
OnDocLoadSetCurrentLocationInTitleBar.xba
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
RO en attente de la fin de migration
<?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="OnDocLoadSetCurrentLocationInTitleBar" script:language="StarBasic" script:moduleType="normal">REM ***** BASIC *****
' TODO : try to migrate this script to javascript language in the case where it can giving the same functionnalities without storing the decode URL in a temporarily file
' Important : this code MUST be placed on ~/.config/libreoffice/4/user/basic/Standard/ macro
' equivalent topics :
' - https://ask.libreoffice.org/en/question/11292/how-to-display-full-file-path-in-window-title/
' - https://ask.libreoffice.org/en/question/44280/how-to-view-full-path-of-open-document/
Sub on_DocLoad(event As Object)
' Important : This Sub MUST be connected to the "Open Document" event of LibreOffice.
' https://ask.libreoffice.org/en/question/145481/struggling-to-auto-run-a-macro/
' The URL in LibreOffice : https://help.libreoffice.org/3.6/Basic/Basic_Glossary/fr#Notation_URL
' The events in LibreOffice : https://help.libreoffice.org/4.2/Basic/Event-Driven_Macros
' A big problem :
' A document "with space and accentué.odt" from "smb://nas.local/foo bar" network share
' when converted with ConvertFromURL() gives :
' "smb://nas.local/test/foo%20bar/with%20space%20and%20accentu%C3%A9.odt" which is hard to read.
' When I try to decode it with https://www.url-encode-decode.com/ it gives :
' "smb://nas.local/test/to to/with space and accentué.odt" which is easy to read.
' => solution : decode URL before display it
' TODO : find a native way to decode URL without storing the decoded URL in a temporarily file or implement a function to replace %xx (https://www.w3schools.com/tags/ref_urlencode.ASP)
' New document => nothing to do
If ThisComponent.Location = "" Then
Exit Sub
End If
Dim locationInTitleBar As String
Dim tempFileLocation As String
Dim currentController As Object
Dim frame As Object
tempFileLocation = Environ("HOME") & "/URL_converted"
' URL Decode with bash to temporarily file
' Based on : https://stackoverflow.com/questions/6250698/how-to-decode-url-encoded-string-in-shell#37840948
' Nota : I had just implemented the replace of % BUT not the replace of + (for space characters)
cmd = "bash -c " & _
Chr(34) & _
"url=" & ThisComponent.Location & _
" ; echo -e " & "${url//%/\\x}" & _
" > " & tempFileLocation & _
Chr(34) & _
""
Shell(cmd, 0, "", true)
' Get decoded URL from temporarily file
FileNo = Freefile
Open tempFileLocation For Input As #fileNo
While Not Eof(#fileNo)
Line Input #fileNo, locationInTitleBar
WEnd
Close #fileNo
Kill(tempFileLocation)
' Current location in title bar
' https://ask.libreoffice.org/en/question/147789/can-i-change-the-content-of-the-title-bar-from-a-macro/
currentController = ThisComponent.getCurrentController()
frame = currentController.getFrame()
frame.Title = ConvertFromURL(locationInTitleBar) & " - " & getDocumentType(thisComponent)
End Sub
' Based on https://wiki.openoffice.org/wiki/Currently_active_document
Function getDocumentType(component As Object) As String
If HasUnoInterfaces(component, "com.sun.star.lang.XServiceInfo") Then
If thisComponent.supportsService ("com.sun.star.text.GenericTextDocument") Then
getDocumentType = "LibreOffice Writer"
ElseIf thisComponent.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then
getDocumentType = "LibreOffice Calc"
ElseIf thisComponent.supportsService("com.sun.star.presentation.PresentationDocument") Then
getDocumentType = "LibreOffice Impress"
ElseIf thisComponent.supportsService("com.sun.star.drawing.GenericDrawingDocument") Then
getDocumentType = "LibreOffice Draw"
Else
getDocumentType = "Unknown Document type"
End If
Else
getDocumentType = "Not a document"
End If
End Function
</script:module>