Sub OnStartup()


 'add toolbar button
  Dim but : Set but = SDB.Objects("vcButton")
  If but Is Nothing Then
    Set but = SDB.UI.AddMenuItem(SDB.UI.Menu_TbStandard,0,0)
    but.Caption = "Update Play"
    but.IconIndex = 29
    but.UseScript = Script.ScriptPath
    but.OnClickFunc = "AutoIncPlayCount"
    but.Visible = true
    Set SDB.Objects("vcButton") = but
    
    
  End If


   Dim menuItem
   ' Add Increment Play Counter to the context menu of the track list
   ' first we make a separator
  SDB.UI.AddMenuItemSep SDB.UI.Menu_Pop_TrackList,0,0
   Set menuItem = sdb.UI.AddMenuItem( sdb.UI.Menu_Pop_TrackList, 0, 0)      ' Add to track list menu (1st section, 2nd item
   
   menuItem.Caption = "Export Tracks to VoxCommando"
   menuItem.OnClickFunc = "VoxGenXML"
   menuItem.UseScript = Script.ScriptPath
   menuItem.Hint = "this is a hint... but is it used?"
   menuItem.IconIndex = 29
   menuItem.Shortcut = "Alt-X"
End Sub




' 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 folder
Dim fso       ' FileSystemObject
dim InitDir

' 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

  InitDir = iniF.StringValue( "Scripts", iniDirValue)
  
  folder=SDB.SelectFolder(InitDir,"Select folder of playlists:")

  if folder="" Then
    Exit Sub   ' if cancel was pressed, exit
  End If

  ' Get the selected filename
  fullfile = folder+"mmArtists.xml"  

  ' Connect to the FileSystemObject
  Set fso = SDB.Tools.FileSystem

  ' Write selected directory to the ini file
  iniF.StringValue( "Scripts", iniDirValue) = folder
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;")
  srcstring = Replace( srcstring, "'", "''")
  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(TBB)
  ' initialize export
  ' we dont' use TBB it's send by the button so we have to accept it as param
  
  Call InitExport (".xml", "XML (*.xml)|*.xml|All files (*.*)|*.*", _
      "LastExportXMLDir")
  if fullfile="" then
    Exit Sub
  end if

  ' Create the output file
  Dim fout
  

  ' 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
  
  'open artist file for output
  
  fullfile = folder+"mmArtists.xml"
  Set fout = fso.CreateTextFile( fullfile, True)
  
  fout.WriteLine "<?xml version='1.0' encoding='utf-8'?>"
  fout.WriteLine "<!--A VoxCommando Payload file-->"
  fout.WriteLine "<PayloadsRoot>" 

  Progress.MaxValue = list.count + Artists.Count + Albums.Count
  'Progress.Text = ProgressString & " (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 "    <payload>"
			fout.WriteLine "        <value />"
			fout.WriteLine "        <phrase>" & MapXML(MultArtists(counter)) & "</phrase>"
			fout.WriteLine "        <subsetmatching>true</subsetmatching>"
			fout.WriteLine "    </payload>"
		next
    end if
    
    Progress.Increase
    if Progress.Terminate then
      Exit For
    end if
  next
fout.WriteLine "</PayloadsRoot>"
fout.Close

'
'------------------------------------------------------Export Albums

  'open albums file for output
  
	fullfile = folder+"mmAlbum.xml"
	Set fout = fso.CreateTextFile( fullfile, True)
	fout.WriteLine "<?xml version='1.0' encoding='utf-8'?>"
	fout.WriteLine "<!--A VoxCommando Payload file-->"
	fout.WriteLine "<PayloadsRoot>" 


	Progress.Text = ProgressString & " (albums)"
	
	for i=0 to Albums.count-1
		Set Album = Albums.Item(i)
		if len(Album.Name)>1 then
			fout.WriteLine "    <payload>"
			fout.WriteLine "        <value />"
			fout.WriteLine "        <phrase>"  & MapXML(Album.Name) & "</phrase>"
			fout.WriteLine "        <subsetmatching>true</subsetmatching>"
			
			fout.WriteLine "    </payload>"	
		end if
		Progress.Increase
		if Progress.Terminate then
			Exit For
		end if
	next
	fout.WriteLine "</PayloadsRoot>"
	fout.Close



'------------------------------------------------------Export Songs
' Iterate through the list and export all songs
	fullfile = folder+"mmSongs.xml"
	Set fout = fso.CreateTextFile( fullfile, True)
	fout.WriteLine "<?xml version='1.0' encoding='utf-8'?>"
	fout.WriteLine "<!--A VoxCommando Payload file-->"
	fout.WriteLine "<PayloadsRoot>" 
  
  
  Dim Song, Media    

	for i=0 to list.count-1
		Set Song = list.Item(i)    
		if len(Song.Title)>1 then	
			fout.WriteLine "    <payload>"
			fout.WriteLine "        <value />"
			fout.WriteLine "        <phrase>"  & MapXML(Song.Title) & "</phrase>"
			fout.WriteLine "        <subsetmatching>true</subsetmatching>"
			fout.WriteLine "    </payload>"	
		end if
		Progress.Increase
		if Progress.Terminate then
			Exit For    
		end if
	next
	fout.WriteLine "</PayloadsRoot>"
	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

