'
' Graphical frontend to the M3U creator
'
' December 14, 2009 - Peter van Eerten - GPL
'
' Version 1.0: Initial release
' Version 1.1: Adapted for Cancel-button in FILEDIALOG
' Version 1.2: More improvements
' Version 1.3: Handle URI format correctly
' Version 1.4: Accept FLAC files
'---------------------------------------------------------------------------------------

OPTION COLLAPSE TRUE

INCLUDE "hug.bac", INIT, SHOW, HIDE, GRAB$, TEXT, FOCUS, WINDOW, FRAME, ATTACH, ENTRY, STOCK, \
    BUTTON, MARK, FILEDIALOG, MSGDIALOG, CALLBACK, DISPLAY, QUIT

'---------------------------------------------------------------------------------------

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, REPLACE$(txt$, "%20", " "))
    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 LEN(txt$) = 0 THEN
        SHOW(errdlg1)
    ELSE
        target$ = CONCAT$(MID$(txt$, INSTRREV(txt$, "/") + 1), ".m3u")

        IF FILETYPE(txt$) != 2 THEN
            SHOW(errdlg3)
        ELSE

            OPEN txt$ FOR DIRECTORY AS curdir

            REPEAT
                GETFILE item$ FROM curdir

                IF REGEX(item$, ".*\\.mp3") OR REGEX(item$, ".*\\.ogg") OR REGEX(item$, ".*\\.flac") THEN
                    list$ = CONCAT$(list$, NL$, item$)
                END IF
            UNTIL ISFALSE(LEN(item$))

            CLOSE DIRECTORY curdir

            SPLIT list$ BY NL$ TO nr$ SIZE dim

            IF dim = 0 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

'---------------------------------------------------------------------------------------

INIT
' 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