]> git.lyx.org Git - features.git/blob - development/Win32/packaging/installer/include/dictionaries.nsh
Consider inset strings in simple find/replaceAll (#12049)
[features.git] / development / Win32 / packaging / installer / include / dictionaries.nsh
1 /*
2 dictionaries.nsh
3
4 Handling of hunspell / MyThes dictionaries
5 */
6
7 # This script contains the following functions:
8 #
9 # - FindDictionaries (finds already installed dictionaries)
10 #
11 # - DownloadHunspellDictionaries and DownloadThesaurusDictionaries
12 #    (Downloads hunspell / MyThes dictionaries from a location that is
13 #     given in the file $INSTDIR\Resources\HunspellDictionaryNames.txt)
14 #
15 # - InstallHunspellDictionaries and InstallThesaurusDictionaries
16 #    (installs the selected hunspell / MyThes dictionaries except of
17 #     already existing ones), uses:
18 #    DownloadHunspellDictionaries or DownloadThesaurusDictionaries
19
20 # ---------------------------------------
21
22 Function FindDictionaries
23   # finds already installed dictionaries
24
25   # start with empty strings
26   StrCpy $FoundDict ""
27   StrCpy $FoundThes ""
28   
29   # read out the possible spell-checker filenames from the file 
30   FileOpen $R5 "$INSTDIR\Resources\HunspellDictionaryNames.txt" r
31   ${for} $5 1 73
32    # the file has 146 lines, but we only need to check for one of the 2 dictionary files per language
33    # therefore check only for every second line
34    FileRead $R5 $String # skip the .aff file
35    FileRead $R5 $String # $String is now the .dic filename
36    StrCpy $String $String -2 # remove the linebreak characters
37    StrCpy $R3 $String -4 # $R3 is now the dictionary language code
38    ${if} ${FileExists} "$INSTDIR\Resources\dicts\$String"
39     StrCpy $FoundDict "$R3 $FoundDict"
40    ${endif}
41   ${next}
42   FileClose $R5
43   
44   # read out the possible thesaurus filenames from the file     
45   FileOpen $R5 "$INSTDIR\Resources\ThesaurusDictionaryNames.txt" r
46   ${for} $5 1 28
47    # the file has 56 lines, but we only need to check for one of the 2 thesaurus files per language
48    # therefore check only for every second line
49    FileRead $R5 $String # $String is now the dictionary name
50    FileRead $R5 $String # $String is now the dictionary name
51    StrCpy $String $String -2 # remove the linebreak characters
52    StrCpy $R3 $String 5 3 # $R3 is now the dictionary language code
53    ${if} ${FileExists} "$INSTDIR\Resources\thes\$String"
54     StrCpy $FoundThes "$R3 $FoundThes"
55    ${endif}
56   ${next}
57   FileClose $R5
58
59 FunctionEnd
60
61 # ---------------------------------------
62
63 Function DownloadHunspellDictionaries
64  # Downloads hunspell dictionaries from a location that is given in the file
65  # $INSTDIR\Resources\HunspellDictionaryNames.txt
66  
67  # read out the locations from the file 
68  FileOpen $R5 "$INSTDIR\Resources\HunspellDictionaryNames.txt" r
69  ${For} $5 1 146 # the file has 146 lines
70  
71   FileRead $R5 $String # $String is now the dictionary name
72   StrCpy $R3 $String -6 # $R3 is now the dictionary language code
73   
74   ${if} $DictCode == $R3
75    StrCpy $String $String -2 # delete the linebreak characters at the end
76    # Download hunspell dictionaries
77    inetc::get /TIMEOUT=5000 "https://www.lyx.org/trac/export/HEAD/lyxsvn/dictionaries/trunk/dicts/$String" "$INSTDIR\Resources\dicts\$String" /END
78    Pop $R0
79    # if download failed
80    ${if} $R0 != "OK"
81     MessageBox MB_OK|MB_ICONEXCLAMATION "$(HunspellFailed)"
82     Goto abortinstall
83    ${endif}
84   ${endif} # end if $DictCode == $R3
85   
86  ${Next}
87  FileClose $R5
88
89  abortinstall:
90  Delete "$INSTDIR\$String"
91
92 FunctionEnd
93
94 #--------------------------------
95
96 Function DownloadThesaurusDictionaries
97  # Downloads thesaurus dictionaries from a location that is given in the file
98  # $INSTDIR\Resources\ThesaurusDictionaryNames.txt
99  
100  # read out the locations from the file 
101  FileOpen $R5 "$INSTDIR\Resources\ThesaurusDictionaryNames.txt" r
102  ${For} $5 1 56 # the file has 56 lines
103  
104   FileRead $R5 $String # $String is now the thesaurus name
105   StrCpy $R3 $String 5 3 # $R3 is now the thesaurus language code
106   
107   ${if} $ThesCode == $R3
108    StrCpy $String $String -2 # delete the linebreak characters at the end
109    # Download thesaurus files
110    inetc::get /TIMEOUT=5000 "https://www.lyx.org/trac/export/HEAD/lyxsvn/dictionaries/trunk/thes/$String" "$INSTDIR\Resources\thes\$String" /END
111    Pop $R0
112    # if download failed
113    ${if} $R0 != "OK"
114     MessageBox MB_OK|MB_ICONEXCLAMATION "$(ThesaurusFailed)"
115     Goto abortinstall
116    ${endif}
117   ${endif} # end if $ThesCode == $R3
118   
119  ${Next}
120  FileClose $R5
121
122  abortinstall:
123  Delete "$INSTDIR\$String"
124
125 FunctionEnd
126
127 #--------------------------------
128
129 Function InstallHunspellDictionaries
130  # installs the selected hunspell dictionaries except of already existing ones
131
132  ${Do}
133   # take the first code
134   StrCpy $Search ","
135   StrCpy $String $DictCodes
136   Call StrPoint
137   # we always have a "," for each code, so in case in case something
138   # went wrong, empty the code list to exit the loop
139   ${if} $Pointer == "-1"
140    StrCpy $DictCodes ""
141   ${endif}
142   ${if} $Pointer != "-1"
143    StrCpy $DictCode $DictCodes $Pointer
144    # remove the taken code from the list
145    IntOp $Pointer $Pointer + 1
146    StrCpy $DictCodes $DictCodes "" $Pointer
147    # don't dowload existing ones thus check if $DictCode is in $FoundDict
148    StrCpy $String $FoundDict
149    StrCpy $Search $DictCode
150    Call StrPoint # function from LyXUtils.nsh
151    ${if} $Pointer == "-1"
152     # download the dictionaries
153     Call DownloadHunspellDictionaries
154    ${endif}
155   ${endif}
156  ${LoopUntil} $DictCodes == ""
157  
158 FunctionEnd
159
160 #--------------------------------
161
162 Function InstallThesaurusDictionaries
163  # installs the selected thesaurus dictionaries except of already existing ones
164
165  ${Do}
166   # take the first code
167   StrCpy $Search ","
168   StrCpy $String $ThesCodes
169   Call StrPoint
170   # we always have a "," for each code, so in case in case something
171   # went wrong, empty the code list to exit the loop
172   ${if} $Pointer == "-1"
173    StrCpy $ThesCodes ""
174   ${endif}
175   ${if} $Pointer != "-1"
176    StrCpy $ThesCode $ThesCodes $Pointer
177    # remove the taken code from the list
178    IntOp $Pointer $Pointer + 1
179    StrCpy $ThesCodes $ThesCodes "" $Pointer
180    # don't dowload existing ones thus check if $ThesCode is in $FoundThes
181    StrCpy $String $FoundThes
182    StrCpy $Search $ThesCode
183    Call StrPoint # function from LyXUtils.nsh
184    ${if} $Pointer == "-1"
185     # download the dictionaries
186     Call DownloadThesaurusDictionaries
187    ${endif}
188   ${endif}
189  ${LoopUntil} $ThesCodes == ""
190  
191  # some dictionaries of language variants are identic
192  # therefore copy and rename an existing dictionary
193  CreateDirectory "$INSTDIR\Resources\backup"
194  ${if} ${FileExists} "$INSTDIR\Resources\thes\th_de_DE_v2.dat"
195   CopyFiles "$INSTDIR\Resources\thes\th_de_DE_v2.*" "$INSTDIR\Resources\backup"
196   Rename "$INSTDIR\Resources\backup\th_de_DE_v2.dat" "$INSTDIR\Resources\backup\th_de_AT_v2.dat"
197   Rename "$INSTDIR\Resources\backup\th_de_DE_v2.idx" "$INSTDIR\Resources\backup\th_de_AT_v2.idx"
198   CopyFiles "$INSTDIR\Resources\backup\th_de_AT_v2.*" "$INSTDIR\Resources\thes"
199  ${endif}
200  ${if} ${FileExists} "$INSTDIR\Resources\thes\th_en_US_v2.dat"
201   CopyFiles "$INSTDIR\Resources\thes\th_en_US_v2.*" "$INSTDIR\Resources\backup"
202   Rename "$INSTDIR\Resources\backup\th_en_US_v2.dat" "$INSTDIR\Resources\backup\th_en_AU_v2.dat"
203   Rename "$INSTDIR\Resources\backup\th_en_US_v2.idx" "$INSTDIR\Resources\backup\th_en_AU_v2.idx"
204   CopyFiles "$INSTDIR\Resources\backup\th_en_AU_v2.*" "$INSTDIR\Resources\thes"
205  ${endif}
206  RMDir "$INSTDIR\Resources\backup"
207  
208 FunctionEnd
209