' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
' This file can be replaced  in one of the future versions,
' so please if you want to modify it, make  a copy, do your
' modifications  in that copy and  change Scripts.ini  file 
' appropriately. 
' If you do not do this, you will lose  all your changes in
' this script when you install a new version of MediaMonkey
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Option Explicit     ' report undefined variables, ...

' function for quoting strings
Function QStr( astr)
  QStr = chr(34) & astr & chr(34)
End Function

' function for quoting strings converted to plain ASCII
Function QAStr( astr)
  QAStr = chr(34) & SDB.toASCII(astr) & chr(34)
End Function

Dim list      ' list of songs to be exported
Dim res       ' results of dialogs calls
Dim fullfile  ' fully specified output file name
Dim fso       ' FileSystemObject

' SDB variable is connected to MediaMonkey application object

Sub InitExport( ext, filter, iniDirValue)
  fullfile = ""

  ' Get a list of songs to be exported
  Set list = SDB.CurrentSongList

  If list.count=0 Then
    res = SDB.MessageBox( SDB.Localize("Select tracks to be exported, please."), mtError, Array(mbOk))
    Exit Sub
  End If

  ' Open inifile and get last used directory
  Dim iniF
  Set iniF = SDB.IniFile

  ' Create common dialog and ask where to save the file
  Dim dlg
  Set dlg = SDB.CommonDialog
  dlg.DefaultExt=ext
  dlg.Filter=filter
  dlg.Flags=cdlOFNOverwritePrompt + cdlOFNHideReadOnly + cdlOFNNoChangeDir
  dlg.InitDir = iniF.StringValue( "Scripts", iniDirValue)
  dlg.ShowSave

  if Not dlg.Ok Then
    Exit Sub   ' if cancel was pressed, exit
  End If

  ' Get the selected filename
  fullfile = dlg.FileName

  ' Connect to the FileSystemObject
  Set fso = SDB.Tools.FileSystem

  ' Write selected directory to the ini file
  iniF.StringValue( "Scripts", iniDirValue) = fullfile
End Sub

Function FormatStrTime (StrTimeValue)
  FormatStrTime = ""

  If Len (StrTimeValue) > 0 Then
    Dim BeginPosition, Position, TimePart, TimePartsCount
    BeginPosition = 1
    Position = InStr (BeginPosition, StrTimeValue, ":", vbTextCompare)
    TimePartsCount = 0

    Do While Position > 0
      TimePartsCount = TimePartsCount + 1
      BeginPosition = Position + 1
      Position = InStr (BeginPosition, StrTimeValue, ":", vbTextCompare)
    Loop

    For TimePart = 1 to 2 - TimePartsCount
      FormatStrTime = FormatStrTime + "00:"
    Next
  Else
    StrTimeValue = "00:00:00"
  End If

  FormatStrTime = FormatStrTime + StrTimeValue
  FormatStrTime = Replace (FormatStrTime, " ", "", 1, -1, vbTextCompare)
End Function

Sub FinishExport( ok)
  On Error Resume Next

  ' remove the output file if terminated
  if not Ok then
    fso.DeleteFile( fullfile)
  end if
End Sub



 ' escape XML string
Function MapXML( srcstring)
  srcstring = Replace( srcstring, "&", "&amp;")
  srcstring = Replace( srcstring, "<", "&lt;")
  srcstring = Replace( srcstring, ">", "&gt;")
  Dim i
  i=1
  While i<=Len(srcstring)
    If (AscW(Mid(srcstring, i, 1))>127) Then
      srcstring = Mid( srcstring, 1, i-1)+"&#"+CStr( AscW( Mid( srcstring, i, 1)))+";"+Mid( srcstring, i+1, Len(srcstring))
    End If
    i=i+1
  WEnd
  MapXML = srcstring
End Function

'==========================================================================================================
Sub VoxGenXML
  ' initialize export
  Call InitExport (".xml", "XML (*.xml)|*.xml|All files (*.*)|*.*", _
      "LastExportXMLDir")
  if fullfile="" then
    Exit Sub
  end if

  ' Create the output file
  Dim fout
  Set fout = fso.CreateTextFile( fullfile, True)

  ' Use progress to notify user about the current action
  Dim Progress
  Set Progress = SDB.Progress
  Dim ProgressString
  ProgressString = SDB.Localize("Exporting...")

  Dim i
  Dim Artists, Artist, MultArtists,counter
  Set Artists = list.Artists
  
  Dim Albums, Album
  Set Albums = list.Albums
  
  
  
  fout.WriteLine "<?xml version='1.0'?>" 
  fout.WriteLine "<root>" 

  Progress.MaxValue = list.count + Artists.Count

  Progress.Text = ProgressString & " (artists)"
  fout.WriteLine "   <Artists>"
  for i=0 to Artists.count-1
    Set Artist = Artists.Item(i)  
	
	if len(Artist.Name)>1 then
	MultArtists = Split(Artist.Name,"; ")
	for counter = 0 to UBound(MultArtists)  
		fout.WriteLine "      <Name>" & MapXML(MultArtists(counter)) & "</Name>"
	next
    end if
    
    Progress.Increase
    if Progress.Terminate then
      Exit For
    end if
  next
fout.WriteLine "</Artists>"

'
'------------------------------------------------------Export Albums
Progress.Text = ProgressString & " (albums)"
  fout.WriteLine "  <Albums>"
  for i=0 to Albums.count-1
    Set Album = Albums.Item(i)
	if len(Album.Name)>1 then
    fout.WriteLine "      <Albumname>" & MapXML(Album.Name) & "</Albumname>"
	end if
    Progress.Increase
    if Progress.Terminate then
      Exit For
    end if
  next
  fout.WriteLine "  </Albums>"


'------------------------------------------------------Export Songs
' Iterate through the list and export all songs
  Progress.Text = ProgressString & " (songs)"
  
  Progress.MaxValue = list.count
  Dim Song, Media

    fout.WriteLine "  <Songs>"

  for i=0 to list.count-1
    Set Song = list.Item(i)
    
		if len(Song.Title)>1 then
    fout.WriteLine "       <Title>" & MapXML(Song.Title) & "</Title>"
	end if


    
    
    Progress.Increase
    if Progress.Terminate then
      Exit For
    
	end if
  next
  
  fout.WriteLine "  </Songs>"





fout.WriteLine "</root>" 


  ' Close the output file and finish
  fout.Close

  ' Was it successfull?
  Dim ok
  if Progress.Terminate then
    ok = False
  else
    ok = True
  end if

  ' hide progress
  Set Progress = Nothing

  Call FinishExport( ok)
End Sub

