REM ***************************************************************************
REM **
REM **  IndeViewExport.bas - part of IndeView project: http://www.indeview.org
REM **
REM **
REM **  This macro exports the currently active presentation in OpenOffice
REM **  ------------------------------------------------------------------
REM **  and it creates an IndeView format version of it: this enables you
REM **  to display it using IndeView on PCs not having OpenOffice installed.
REM **
REM **  Copyright (C) 2003..2006 Michael & Karl-Heinz Zimmer.
REM **  All rights reserved.
REM **
REM **  This file is distributed under the terms of the
REM **  GNU Lesser General Public License version 2.1 as published by the
REM **  Free Software Foundation and appearing in the file LICENSE included
REM **  in the packaging of this file.
REM **
REM **  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
REM **  THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
REM **  PURPOSE.
REM **
REM **  See http://www.indeview.org for further information.
REM **
REM **  Contact info@indeview.org if any conditions of this
REM **  licensing are not clear to you.
REM **
REM **
REM **  Please consider sending
REM **  a little picture postcard to   Michael & Karl-Heinz Zimmer
REM **                                 Butzengasse 15
REM **                                 54343 Foehren
REM **                                 Germany
REM **
REM **  Your feedback will be very much appreciated: we would be glad about
REM **  receiving a visual representation of where on earth you are living
REM **  using our little module.
REM **
REM ***************************************************************************

Sub IndeViewExport
  DIM sROOT_DATA As String

  sROOT_DATA = "unknown"



  DIM oSimpleFileAccess as Object
  oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )
  DIM sTestFileURL as String
  sTestFileURL = ConvertToURL( sROOT_DATA ) + "/" + "BIN/WINDOWS/INDEVIEW.EXE"
  If oSimpleFileAccess.exists( sTestFileURL ) Then
    DIM oDoc As Object
    oDoc = ThisComponent
    If oDoc.SupportsService("com.sun.star.presentation.PresentationDocument") Then
      DIM oDir As Object
      oDir = createunoservice("com.sun.star.ui.dialogs.FolderPicker")
      With oDir
        .setDisplayDirectory( "file:///" )
        .setTitle("IndeView Export - select target directory")
        .setDescription("Select target directory")
      End With
      If oDir.execute() Then
        DIM sDir As String
        DIM sDataDir As String
        DIM sSysDataDir As String
        DIM sName As String
        DIM nNumPages As Integer
        DIM nPage As Integer
        DIM oPage As Object
        DIM oFilter As Object
        DIM aFilterData( 2 ) As new com.sun.star.beans.PropertyValue
        DIM aDescriptor( 2 ) As new com.sun.star.beans.PropertyValue
        DIM sFileNameExt As String
        DIM sFileName As String
        DIM sSysRunFile As String
        DIM nFile As Integer
        DIM sPageID As String
        DIM sSep As String

        DIM nSize As Integer
        DIM nSizeMax As Integer
        DIM aWidth(  6) As Integer
        DIM aHeight( 6) As Integer
        DIM aWidthID(6) As Integer
        nSizeMax  =  6

        aWidth(  0) = 640
        aWidthID(0) = 6
        aHeight( 0) = 480

        aWidth(  1) = 704
        aWidthID(1) = 7
        aHeight( 1) = 576

        aWidth(  2) = 800
        aWidthID(2) = 8
        aHeight( 2) = 600

        aWidth(  3) = 1024
        aWidthID(3) = 10
        aHeight( 3) = 768

        aWidth(  4) = 1152
        aWidthID(4) = 11
        aHeight( 4) = 864

        aWidth(  5) = 1280
        aWidthID(5) = 12
        aHeight( 5) = 1024

        aWidth(  6) = 1600
        aWidthID(6) = 16
        aHeight( 6) = 1200

        sDir = oDir.directory

        If DirCopyOK( ConvertToURL( sROOT_DATA ), ConvertToURL( sDir ), oSimpleFileAccess ) Then

          sSep = GetPathSeparator()

          sSysRunFile = ConvertFromUrl( sDir + "/INDEVIEW.RUN" )

          runFile = FreeFile
          Open sSysRunFile For Output Access Write As #runFile
            Print #runFile, "# IndeView presentation run file"
            Print #runFile, "#"
            Print #runFile, "# visit: http://www.indeview.org"
            Print #runFile, ""
            Print #runFile, "[COMMON]"
            Print #runFile, "Version=IndeView 1.0"
            Print #runFile, "sound=NONE"
            Print #runFile, "soundmode=REPEAT"
            Print #runFile, "start=Page0"

            aFilterData(0).Name = "Width"
            aFilterData(1).Name = "Height"
            aFilterData(2).Name = "Translucent"
            aFilterData(2).Value = false

            aDescriptor(0).Name = "FilterName"
            aDescriptor(0).Value = "PNG"
            sFileNameExt     =    ".PNG"
            aDescriptor(1).Name = "URL"
            aDescriptor(2).Name = "FilterData"

            oFilter = CreateUnoService("com.sun.star.drawing.GraphicExportFilter")
            nNumPages = oDoc.DrawPages.getCount()
            For nPage = 0 To nNumPages-1

              oPage = oDoc.DrawPages( nPage )
              sName = oPage.getName()
              sPageID = "Page" + LTrim(Str( nPage ))
              Print #runFile, ""
              Print #runFile, "[" + sPageID + "]"
              Print #runFile, "# " + sName
              Print #runFile, "info=DATA/"+ sPageID + ".INF"
              If nPage = nNumPages-1 Then
                Print #runFile, "next=NONE"
              Else
                Print #runFile, "next=Page" + LTrim(Str( nPage + 1 ))
              End If

              sDataDir = sDir + "/DATA"
              sSysDataDir = ConvertFromUrl( sDataDir )

              On Error GOTO mkdata_error
              MkDir sSysDataDir
              GOTO mkdata_ok
mkdata_error:
              MsgBox( chr$(13) + "ERROR: Directory" + chr$(13) + chr$(13) + sSysDataDir + chr$(13) + chr$(13) + "could not be created!" + chr(13) , 0+48 , "IndeView export" )
              Exit Sub
mkdata_ok:
              
              pginfFile = FreeFile
              Open sSysDataDir + sSep + sPageID + ".INF" For Output Access Write As #pginfFile
                Print #pginfFile, "# IndeView page information file"
                Print #pginfFile, "#"
                Print #pginfFile, "# visit: http://www.indeview.org"
                Print #pginfFile, ""
                Print #pginfFile, "Name=" + sName
                Print #pginfFile, ""
                oFilter.setSourceDocument( oPage )
                For nSize = 0 To nSizeMax
                  If  10 > nPage Then
                    sFileName = "P00"
                  Else
                    If  100 > nPage Then
                      sFileName = "P0"
                    Else
                      sFileName = "P"
                    End If
                  End If
                  sFileName = sFileName + LTrim(Str( nPage )) + "W" + LTrim(Str( aWidthID( nSize ))) + sFileNameExt
                  aDescriptor(1).Value = sDataDir + "/" + sFileName
                  aFilterData(0).Value = aWidth(  nSize )
                  aFilterData(1).Value = aHeight( nSize )
                  aDescriptor(2).Value = aFilterData()
                  oFilter.filter( aDescriptor() )
                  Print #pginfFile, "File=" + sFileName + " " + LTrim(Str( aWidth( nSize ))) + "x" + LTrim(Str( aHeight( nSize )))
                Next
                Print #pginfFile, ""
                Print #pginfFile, "Notes="
                Print #pginfFile, ""
                Print #pginfFile, "TransitionEffect=NONE"
                Print #pginfFile, "TransitionSpeed=MEDIUM"
                Print #pginfFile, "TransitionSound=NONE"
                Print #pginfFile, "TransitionSoundMode=FULL"
                Print #pginfFile, ""
                Print #pginfFile, "ViewSound=NONE"
                Print #pginfFile, "ViewSoundLoop=1"
                Print #pginfFile, "ViewDuration=100"
                Print #pginfFile, "ViewDurationMode=ABSOLUTE"
                Print #pginfFile, ""
                Print #pginfFile, "ChangeToNext=USER"
              Close #pginfFile
            Next
          Close #runFile

        Else
          MsgBox( chr$(13) + "SORRY" + chr$(13) + "IndeViewExport.bas stopped due to file I/O error(s)." + chr(13) , 0+48 , "IndeView export" )
        End If
      End If
    Else
      MsgBox( chr$(13) + "SORRY" + chr$(13) + "This is not a presentation!" + chr(13) , 0+48 , "IndeView export" )
    End If
  Else
    MsgBox( chr$(13) + "ERROR" + chr$(13) + "IndeViewExport.bas macro setup error:" + chr(13) + chr(13) + sTestFileURL + " not found!" + chr(13), 0+48, "IndeView export" )
  End If
End Sub


REM  Note: This function requires the target directory to be existing!
REM  It copies all contents found in directory sourceURL into directory targetURL.
Function DirCopyOK( sSourceDirURL As String, sTargetDirURL As String, oSimpleFileAccess As Object ) As Boolean
  DIM aURLEntries()  As String
  DIM sURLEntry      As String
  DIM sSysEntry      As String
  DIM sSysName       As String
  DIM sSysTargetDir  As String
  DIM sSep           As String
  DIM iEntry         As Integer
  DIM maxEntry       As Integer
  Dim iSepPos        As Integer
  Dim sSourceDirURL2 As String
  Dim sTargetDirURL2 As String

  DirCopyOK = false

  sSep = GetPathSeparator()
  aURLEntries() = oSimpleFileAccess.getFolderContents( sSourceDirURL, TRUE )
  maxEntry = UBound( aURLEntries() )

  If -1 < maxEntry Then
    For iEntry = 0 To maxEntry
      sURLEntry = ""
      sURLEntry = aURLEntries( iEntry )
      sSysEntry = ConvertFromUrl( sURLEntry )
      sSysName  = GetLastToken(sSysEntry, sSep)

      If oSimpleFileAccess.isFolder( sURLEntry ) Then
        sSysTargetDir = ConvertFromUrl( sTargetDirURL ) + sSep + sSysName
        On Error GOTO mkd_error
        MkDir sSysTargetDir
        GOTO mkd_ok
mkd_error:
        MsgBox( chr$(13) + "ERROR: Directory" + chr$(13) + chr$(13) + sSysTargetDir + chr$(13) + chr$(13) + "could not be created!" + chr(13) , 0+48 , "IndeView export" )
        Exit Function
mkd_ok:
        sSourceDirURL2 = sSourceDirURL + "/" + sSysName
        sTargetDirURL2 = sTargetDirURL + "/" + sSysName
        If NOT DirCopyOK( sSourceDirURL2, sTargetDirURL2, oSimpleFileAccess ) Then
          MsgBox( chr$(13) + "ERROR: DirCopy stopped with error in" + chr$(13) + chr$(13) + sSysTargetDir + chr(13) , 0+48 , "IndeView export" )
          Exit Function
        End If
      Else
        On Error GOTO copy_error
        oSimpleFileAccess.copy( sSourceDirURL + "/" + sSysName, sTargetDirURL + "/" + sSysName )
        GOTO copy_ok
copy_error:
        MsgBox( chr$(13) + "ERROR: File" + chr$(13) + chr$(13) + sTargetDirURL + "/" + sSysName + chr$(13) + chr$(13) + "could not be stored!" + chr(13) , 0+48 , "IndeView export" )
        Exit Function
copy_ok:
      End If
    Next iEntry
   End If
  DirCopyOK = true
End Function



Function GetLastToken(BigString, Separator as String)
Dim i%, OldPos%, Pos%, SepLen%, BigLen%
Dim CurUbound as Integer
Dim StartUbound as Integer
  StartUbound = 50
  Dim LastTokenFound as String
  CurUbound = StartUbound
  OldPos = 1
  i = -1
  SepLen = Len(Separator)
  BigLen = Len(BigString)
  Do
    Pos = Instr(OldPos,BigString, Separator)
    i = i + 1
    If Pos = 0 Then
      LastTokenFound = Mid(BigString, OldPos, BigLen - OldPos + 1 )
    Else
      LastTokenFound = Mid(BigString, OldPos, Pos-OldPos )
      OldPos = Pos + SepLen
    End If
    If i = CurUbound Then
      CurUbound = CurUbound + StartUbound
    End If
  Loop until Pos = 0
  GetLastToken = LastTokenFound
End Function

