]> git.lyx.org Git - lyx.git/blob - development/Win32/pdfview/pdfview.nsi
installer: uninstall fixes
[lyx.git] / development / Win32 / pdfview / pdfview.nsi
1 /*
2
3 Windows PDF view helper
4 Author: Joost Verburg
5
6 This will be installed as pdfview.exe.
7
8 The application will launch the default PDF viewer to display the PDF file,
9 but works around the file locking problems of Adobe Reader.
10
11 Source code of pdfopen/pdfclose is available at:
12 http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
13
14 */
15
16 !include LogicLib.nsh
17 !include FileFunc.nsh
18
19 #--------------------------------
20 # Settings
21
22 Caption "PDF Viewer"
23 OutFile pdfview.exe
24 Icon "..\packaging\icons\lyx.ico"
25 SilentInstall silent
26
27 #--------------------------------
28 # Windows Vista settings
29
30 RequestExecutionLevel user
31
32 #--------------------------------
33 # Constants
34
35 !define FALSE 0
36 !define TRUE 1
37
38 !define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
39 !define WAIT_TIMEOUT 0x00000102
40
41 #--------------------------------
42 # Variables
43
44 Var Character
45 Var RunAppReturn
46
47 Var OriginalFile
48 Var OriginalFileName
49 Var OriginalDir
50
51 Var PDFFile
52 Var ViewerFileName
53 Var Viewer
54 Var ViewerVersion
55 Var DDEName
56
57 Var ChangeNotification
58 Var WaitReturn
59 Var LockedFile
60
61 Var OriginalTimeHigh
62 Var OriginalTimeLow
63 Var CurrentTimeHigh
64 Var CurrentTimeLow
65
66 #--------------------------------
67 # Macros
68
69 !macro SystemCall STACK
70
71   # Call a Windows API function
72
73   Push `${STACK}`
74   CallInstDLL "$EXEDIR\System.dll" Call
75
76 !macroend
77
78 !macro HideConsole COMMAND_LINE
79
80   # Run an application and hide console output
81
82   Push `${COMMAND_LINE}`
83   CallInstDLL "$EXEDIR\Console.dll" Exec
84   Pop $RunAppReturn
85   
86   ${If} $RunAppReturn == "error"
87     MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
88   ${EndIf}
89
90 !macroend
91
92 #--------------------------------
93 # PDF viewing
94
95 Section "View PDF file"
96
97   InitPluginsDir # Temporary directory for PDF file
98
99   # Command line parameters
100   ${GetParameters} $OriginalFile
101
102   # Trim quotes
103   StrCpy $Character $OriginalFile 1
104   ${If} $Character == '"'
105     StrCpy $OriginalFile $OriginalFile "" 1
106   ${EndIf}
107   StrCpy $Character $OriginalFile 1 -1
108   ${If} $Character == '"'
109     StrCpy $OriginalFile $OriginalFile -1
110   ${EndIf}
111
112   GetFullPathName $OriginalFile $OriginalFile
113   ${GetFileName} $OriginalFile $OriginalFileName
114   ${GetParent} $OriginalFile $OriginalDir # tmpbuf
115   ${GetParent} $OriginalDir $OriginalDir # tmpdir
116
117   SetOutPath $TEMP # The LyX tmpbuf should not be locked
118
119   StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
120
121   # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
122   !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
123   Pop $ViewerFileName
124   ${GetFileName} $ViewerFileName $Viewer
125
126   ${If} $Viewer == ""
127     MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
128         Please install a PDF viewer such as Adobe Reader."
129     Quit        
130   ${EndIf}
131
132   ${If} $Viewer == "AcroRd32.exe"
133     ${OrIf} $Viewer == "Acrobat.exe"
134     
135     # Using Adobe viewer
136
137     GetDLLVersion $ViewerFileName $R0 $R1
138     IntOp $R2 $R0 >> 16
139     IntOp $R2 $R2 & 0x0000FFFF ; $R2 now contains major version
140     IntOp $R3 $R0 & 0x0000FFFF ; $R3 now contains minor version
141     IntOp $R4 $R1 >> 16
142     IntOp $R4 $R4 & 0x0000FFFF ; $R4 now contains release
143     IntOp $R5 $R1 & 0x0000FFFF ; $R5 now contains build
144     StrCpy $ViewerVersion "$R2"
145
146     ${If} $ViewerVersion < 10
147       StrCpy $DDEName "AcroView"
148     ${Else}
149       ${If} $Viewer == "AcroRd32.exe"
150         StrCpy $DDEName "AcroViewR$ViewerVersion"
151       ${ElseIf} $Viewer == "Acrobat.exe"
152         StrCpy $DDEName "AcroViewA$ViewerVersion"
153       ${EndIf}
154     ${EndIf}
155
156     # Close existing view
157     ${If} ${FileExists} $PDFFile
158       !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --reader "$ViewerFileName" --ddename "$DDEName" --file "$PDFFile"'
159     ${EndIf}
160     
161     # Copy PDF to temporary file to allow LyX to overwrite the original
162     CopyFiles /SILENT $OriginalFile $PDFFile
163     
164     # Open a new view
165     !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --reader "$ViewerFileName" --ddename "$DDEName" --back --file "$PDFFile"'
166     
167     # Monitor for updates of the original file
168     GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
169     !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
170       i 1, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
171     Pop $ChangeNotification
172     
173     ${Do}
174     
175       !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
176       Pop $WaitReturn
177            
178       # Check whether the lock is still active (if not, Adobe Reader is closed)
179       
180       FileOpen $LockedFile $PDFFile a
181       
182       ${If} $LockedFile != ""
183         # Quit this application
184         FileClose $LockedFile
185         Delete $PDFFile
186         !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
187         Quit
188       ${EndIf}
189       
190       ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
191         
192         # The LyX temporary directory has been updated
193         # Check whether it's the PDF file that has been updated
194           
195         GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
196         
197         ${If} $OriginalTimeHigh != $CurrentTimeHigh
198           ${OrIf} $OriginalTimeLow != $CurrentTimeLow
199           # PDF has been modified, update view
200           !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --reader "$ViewerFileName" --ddename "$DDEName" --file "$PDFFile"'
201           CopyFiles /SILENT $OriginalFile $PDFFile
202           !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --reader "$ViewerFileName" --ddename "$DDEName" --back --file "$PDFFile"'
203           
204           # Time of new file
205           StrCpy $OriginalTimeHigh $CurrentTimeHigh
206           StrCpy $OriginalTimeLow  $CurrentTimeLow
207         ${EndIf}
208         
209         #Monitor again
210         !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
211         
212       ${EndIf}
213     
214     ${Loop}
215     
216   ${Else}
217   
218     # Another PDF viewer like GSView is used
219     # No need for special actions, just forward to ShellExecute
220     ExecShell open $OriginalFile
221     
222   ${EndIf}
223     
224 SectionEnd