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