]> git.lyx.org Git - lyx.git/blobdiff - development/Win32/pdfview/pdfview.nsi
quit when PDF file no longer exists
[lyx.git] / development / Win32 / pdfview / pdfview.nsi
index 5be4897a92cf1037e62172ea76ad588cc22d8aee..a30e9dfa84f677171964cafa2c189b211814c4b5 100644 (file)
-/*\r
-\r
-Windows PDF view helper\r
-Author: Joost Verburg\r
-\r
-This will be installed as pdfview.exe.\r
-\r
-The application will launch the default PDF viewer to display the PDF file,\r
-but works around the file locking problems of Adobe Reader.\r
-\r
-Source code of pdfopen/pdfclose is available at:\r
-http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/\r
-\r
-*/\r
-\r
-!include "LogicLib.nsh"\r
-!include "FileFunc.nsh"\r
-!insertmacro GetParameters\r
-!insertmacro GetFileName\r
-\r
-;--------------------------------\r
-;Settings\r
-\r
-OutFile pdfview.exe\r
-Icon "..\packaging\icons\lyx_32x32.ico"\r
-SilentInstall silent\r
-\r
-;--------------------------------\r
-;Constants\r
-\r
-!define FALSE 0\r
-!define TRUE 1\r
-\r
-;--------------------------------\r
-;Variables\r
-\r
-Var Dummy\r
-Var FileName\r
-Var Viewer\r
-Var OriginalTimeHigh\r
-Var OriginalTimeLow\r
-Var CurrentTimeHigh\r
-Var CurrentTimeLow\r
-\r
-;--------------------------------\r
-;Macros\r
-\r
-!macro SystemCall STACK\r
-\r
-  Push `${STACK}`\r
-  CallInstDLL "$EXEDIR\System.dll" Call\r
-\r
-!macroend\r
-\r
-!macro HideConsole COMMAND_LINE\r
-\r
-  Push `${COMMAND_LINE}`\r
-  CallInstDLL "$EXEDIR\nsExec.dll" Exec\r
-  Pop $Dummy\r
-  \r
-  ${if} $Dummy == "error"\r
-    MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $R0."\r
-  ${endif}\r
-\r
-!macroend\r
-\r
-;--------------------------------\r
-;PDF vieweing\r
-\r
-Section "View PDF file"\r
-\r
-  ;Command line parameters\r
-  Call GetParameters\r
-  Pop $FileName\r
-\r
-  ;Trim quotes\r
-  StrCpy $Dummy $FileName 1\r
-  ${if} $Dummy == '"'\r
-    StrCpy $FileName $FileName "" 1\r
-  ${endif}\r
-  StrCpy $Dummy $FileName 1 -1\r
-  ${if} $Dummy == '"'\r
-    StrCpy $FileName $FileName -1\r
-  ${endif}\r
-\r
-  GetFullPathName $FileName $FileName\r
-\r
-  ;Check whether the file will be opened with Adobe Reader or Adobe Acrobat\r
-  Push $FileName\r
-  !insertmacro SystemCall "shell32::FindExecutable(t s, t '', t .s)"\r
-  Call GetFileName\r
-  Pop $Viewer\r
-  \r
-  ${if} $Viewer == "AcroRd32.exe"\r
-    ${orif} $Viewer == "Acrobat.exe"\r
-    \r
-    ;Using Adobe viewer\r
-    \r
-    ;Close existing view\r
-    ${if} ${fileexists} $FileName~\r
-      !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$FileName~"'\r
-    ${endif}\r
-    \r
-    ;Copy PDF to temporary file to allow LyX to overwrite the original\r
-    CopyFiles /SILENT $FileName $FileName~\r
-    \r
-    ;Open a new view\r
-    !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$FileName~"'\r
-    \r
-    ;Monitor for updates of the original file\r
-    \r
-    GetFileTime $FileName $OriginalTimeHigh $OriginalTimeLow\r
-    \r
-    ${do}\r
-    \r
-      Sleep 500\r
-      \r
-      FileOpen $Dummy $FileName~ a\r
-      \r
-      ${if} $Dummy != ""\r
-        ;File no longer locked, reader closed\r
-        FileClose $Dummy\r
-        Delete $FileName~\r
-        Quit\r
-      ${endif}\r
-      \r
-      ${unless} ${fileexists} $FileName\r
-        ;Original no longer exists\r
-        Delete $FileName~\r
-        Quit\r
-      ${endif}\r
-      \r
-      GetFileTime $FileName $CurrentTimeHigh $CurrentTimeLow\r
-      \r
-      ${if} $OriginalTimeHigh != $CurrentTimeHigh\r
-        ${orif} $OriginalTimeLow != $CurrentTimeLow\r
-        \r
-        ;Original has been modified, update!\r
-        \r
-        StrCpy $OriginalTimeHigh $CurrentTimeHigh\r
-        StrCpy $OriginalTimeLow  $CurrentTimeLow\r
-        !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$FileName~"'\r
-        CopyFiles /SILENT $FileName $FileName~\r
-        !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$FileName~"'\r
-        \r
-      ${endif}\r
-    \r
-    ${loop}\r
-    \r
-  ${else}\r
-  \r
-    ;Another PDF viewer like GSView is used\r
-    ;No need for special actions, just forward to ShellExecute\r
-    ExecShell open $FileName\r
-    \r
-  ${endif}\r
-    \r
-SectionEnd\r
+/*
+
+Windows PDF view helper
+Author: Joost Verburg
+
+This will be installed as pdfview.exe.
+
+The application will launch the default PDF viewer to display the PDF file,
+but works around the file locking problems of Adobe Reader.
+
+Source code of pdfopen/pdfclose is available at:
+http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
+
+*/
+
+!include LogicLib.nsh
+!include FileFunc.nsh
+
+# Functions from FileFunc.nsh
+!insertmacro GetParameters
+!insertmacro GetFileName
+!insertmacro GetParent
+
+#--------------------------------
+# Settings
+
+Caption "PDF Viewer"
+OutFile pdfview.exe
+Icon "..\packaging\icons\lyx.ico"
+SilentInstall silent
+
+#--------------------------------
+# Windows Vista settings
+
+RequestExecutionLevel user
+
+#--------------------------------
+# Constants
+
+!define FALSE 0
+!define TRUE 1
+
+!define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
+!define WAIT_TIMEOUT 0x00000102
+
+#--------------------------------
+# Variables
+
+Var Character
+Var RunAppReturn
+
+Var OriginalFile
+Var OriginalFileName
+Var OriginalDir
+
+Var PDFFile
+Var Viewer
+
+Var ChangeNotification
+Var WaitReturn
+Var LockedFile
+
+Var OriginalTimeHigh
+Var OriginalTimeLow
+Var CurrentTimeHigh
+Var CurrentTimeLow
+
+#--------------------------------
+# Macros
+
+!macro SystemCall STACK
+
+  # Call a Windows API function
+
+  Push `${STACK}`
+  CallInstDLL "$EXEDIR\System.dll" Call
+
+!macroend
+
+!macro HideConsole COMMAND_LINE
+
+  # Run an application and hide console output
+
+  Push `${COMMAND_LINE}`
+  CallInstDLL "$EXEDIR\Console.dll" Exec
+  Pop $RunAppReturn
+  
+  ${If} $RunAppReturn == "error"
+    MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
+  ${EndIf}
+
+!macroend
+
+#--------------------------------
+# PDF viewing
+
+Section "View PDF file"
+
+  InitPluginsDir # Temporary directory for PDF file
+
+  # Command line parameters
+  ${GetParameters} $OriginalFile
+
+  # Trim quotes
+  StrCpy $Character $OriginalFile 1
+  ${If} $Character == '"'
+    StrCpy $OriginalFile $OriginalFile "" 1
+  ${EndIf}
+  StrCpy $Character $OriginalFile 1 -1
+  ${If} $Character == '"'
+    StrCpy $OriginalFile $OriginalFile -1
+  ${EndIf}
+
+  GetFullPathName $OriginalFile $OriginalFile
+  ${GetFileName} $OriginalFile $OriginalFileName
+  ${GetParent} $OriginalFile $OriginalDir
+
+  SetOutPath $TEMP # The LyX tmpbuf should not be locked
+
+  StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
+
+  # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
+  !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
+  Call GetFileName
+  Pop $Viewer
+
+  ${If} $Viewer == ""
+    MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
+        Please install a PDF viewer such as Adobe Reader."
+    Quit        
+  ${EndIf}
+
+  ${If} $Viewer == "AcroRd32.exe"
+    ${OrIf} $Viewer == "Acrobat.exe"
+    
+    # Using Adobe viewer
+    
+    # Close existing view
+    ${If} ${FileExists} $PDFFile
+      !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
+    ${EndIf}
+    
+    # Copy PDF to temporary file to allow LyX to overwrite the original
+    CopyFiles /SILENT $OriginalFile $PDFFile
+    
+    # Open a new view
+    !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
+    
+    # Monitor for updates of the original file
+    GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
+    !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
+      i 0, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
+    Pop $ChangeNotification
+    
+    ${Do}
+    
+      !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
+      Pop $WaitReturn
+      
+      # Check whether the PDF still exists (if not, LyX is being closed)
+      
+      ${IfNot} ${FileExists} $LockedFile
+        # Quit this application
+        !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
+        Quit
+      ${EndIf}
+      
+      # Check whether the lock is still active (if not, Adobe Reader being closed)
+      
+      FileOpen $LockedFile $PDFFile a
+      
+      ${If} $LockedFile != ""
+        # Quit this application
+        FileClose $LockedFile
+        Delete $PDFFile
+        !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
+        Quit
+      ${EndIf}
+      
+      ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
+        # The LyX tmpbuf has been updated
+        # Check whether it's the PDF file that has been updated
+          
+        GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
+        
+        ${if} $OriginalTimeHigh != $CurrentTimeHigh
+          ${orif} $OriginalTimeLow != $CurrentTimeLow
+          # PDF has been modified, update view
+          !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
+          CopyFiles /SILENT $OriginalFile $PDFFile
+          !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
+          
+          # Time of new file
+          StrCpy $OriginalTimeHigh $CurrentTimeHigh
+          StrCpy $OriginalTimeLow  $CurrentTimeLow
+        ${EndIf}
+        
+        #Monitor again
+        !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
+        
+      ${EndIf}
+    
+    ${Loop}
+    
+  ${Else}
+  
+    # Another PDF viewer like GSView is used
+    # No need for special actions, just forward to ShellExecute
+    ExecShell open $OriginalFile
+    
+  ${EndIf}
+    
+SectionEnd