'
' Graphical frontend to the M3U creator - compile with HUG 0.8 or higher
'
' December 14, 2009 - Peter van Eerten - GPL - needs bacon 1.0 rc1 or higher
'
' Version 1.0: Initial release
' Version 1.1: Adapted for Cancel-button in FILEDIALOG
'---------------------------------------------------------------------------------------
SETENVIRON "LANG", "C"
INCLUDE "hug.bac"
'---------------------------------------------------------------------------------------
SUB Get_Directory
SHOW(dirdlg)
END SUB
'---------------------------------------------------------------------------------------
SUB Close_Dir_Dialog(NUMBER widget, int button)
LOCAL txt$
HIDE(widget)
IF button ISNOT GTK_RESPONSE_CANCEL THEN
txt$ = GRAB$(widget)
TEXT(in, txt$)
END IF
FOCUS(in)
END SUB
'---------------------------------------------------------------------------------------
SUB Close_Dialog(NUMBER widget)
HIDE(widget)
FOCUS(in)
END SUB
'---------------------------------------------------------------------------------------
SUB Create_M3u
LOCAL txt$, target$, item$, list$
LOCAL dim
LOCAL curdir TYPE DIR*
LOCAL ind TYPE FILE*
txt$ = GRAB$(in)
IF ISFALSE(LEN(txt$)) THEN
SHOW(errdlg1)
ELSE
target$ = CONCAT$(MID$(txt$, INSTRREV(txt$, "/") + 1), ".m3u")
IF FILETYPE(txt$) NE 2 THEN
SHOW(errdlg3)
ELSE
OPEN txt$ FOR DIRECTORY AS curdir
REPEAT
GETFILE item$ FROM curdir
IF REGEX(item$, ".*\\.mp3") OR REGEX(item$, ".*\\.ogg") THEN
list$ = CONCAT$(list$, NL$, item$)
END IF
UNTIL ISFALSE(LEN(item$))
CLOSE DIRECTORY curdir
SPLIT list$ BY NL$ TO nr$ SIZE dim
IF ISFALSE(dim) THEN
SHOW(errdlg2)
ELSE
SORT nr$
OPEN CONCAT$(txt$, "/", target$) FOR WRITING AS ind
FOR i = 0 TO dim - 1
WRITELN nr$[i] TO ind
NEXT
CLOSE FILE ind
SHOW(infodlg)
END IF
END IF
END IF
END SUB
'---------------------------------------------------------------------------------------
' Create main window
mainwin = WINDOW("M3U creator", 400, 190)
' Browse to directory
frame1 = FRAME(390, 80)
ATTACH(mainwin, frame1, 5, 5)
TEXT(frame1, "| Define directory |")
in = ENTRY("</Your/directory/here>", 270, 30)
ATTACH(mainwin, in, 15, 40)
browse = STOCK("gtk-open", 90, 40)
ATTACH(mainwin, browse, 295, 30)
' Action part
frame2 = FRAME(390, 80)
ATTACH(mainwin, frame2, 5, 95)
TEXT(frame2, "| Define action |")
create = BUTTON("_Create M3U", 90, 40)
ATTACH(mainwin, create, 15, 120)
about = MARK("Programmed with BaCon!", 170, 30)
ATTACH(mainwin, about, 115, 130)
quit = STOCK("gtk-quit", 90, 40)
ATTACH(mainwin, quit, 295, 120)
' Create dirbrowser
dirdlg = FILEDIALOG("Select directory...", "gtk-open", 120, 80, 2)
' Create error/information dialogs
errdlg1 = MSGDIALOG("No directory selected!", 250, 120, 3, 2)
errdlg2 = MSGDIALOG("No sound files found!", 250, 120, 3, 2)
errdlg3 = MSGDIALOG("Invalid directory selected!", 270, 120, 3, 2)
infodlg = MSGDIALOG("Creation of .m3u file ready.", 270, 120, 0, 2)
FOCUS(in)
' Define the callbacks
CALLBACK(quit, QUIT)
CALLBACK(browse, Get_Directory)
CALLBACK(dirdlg, Close_Dir_Dialog)
CALLBACK(errdlg1, Close_Dialog)
CALLBACK(errdlg2, Close_Dialog)
CALLBACK(errdlg3, Close_Dialog)
CALLBACK(infodlg, Close_Dialog)
CALLBACK(create, Create_M3u)
' Endless GTK loop
DISPLAY