File "VLCPortable Converter.bas"

Path: /VLC Shortcut Play Folder/VLCPortable Converter.bas
File size: 10.96 KB
MIME-type:
Charset: utf-8

#COMPILER PBWIN 9
#COMPILE EXE
#REGISTER NONE
#DIM ALL
#RESOURCE "VPC.PBR"

$VER = "1.1"

'------------------------------------------------------------------------------
' Include files
'------------------------------------------------------------------------------
%USEMACROS = 1

#INCLUDE ONCE "Win32API.inc"
#INCLUDE ONCE "CommCtrl.inc"
#INCLUDE ONCE "InitCtrl.inc"
#INCLUDE ONCE "SavePos.inc"
#INCLUDE ONCE "DragnDrop.inc"
#INCLUDE ONCE "ReadShortcut.inc"
#INCLUDE ONCE "CreateShortcut.inc"
#INCLUDE ONCE "Registry.inc"

'------------------------------------------------------------------------------
' Initial Declares - eliminate unnecessary macros in COMMCTRL.INC
'------------------------------------------------------------------------------
%NOANIMATE         = 1  ' Animate control
%NOBUTTON          = 1  ' Button
%NOCOMBO           = 1  ' Combo box
%NOCOMBOEX         = 1  ' ComboBoxEx
%NODATETIMEPICK    = 1  ' Date/time picker
%NODRAGLIST        = 1  ' Drag list control
%NOEDIT            = 1  ' Edit control
%NOFLATSBAPIS      = 1  ' Flat scroll bar
%NOHEADER          = 1  ' Header control
%NOHOTKEY          = 1  ' HotKey control
%NOIMAGELIST       = 1  ' Image APIs
%NOIPADDRESS       = 1  ' IP Address edit control
%NOLIST            = 1  ' List box control
%NOMENUHELP        = 1  ' Menu help
%NOMONTHCAL        = 1  ' MonthCal
%NOMUI             = 1  ' MUI
%NONATIVEFONTCTL   = 1  ' Native Font control
%NOPAGESCROLLER    = 1  ' Pager
%NOPROGRESS        = 1  ' Progress control
%NOREBAR           = 1  ' Rebar control
%NOSTATUSBAR       = 1  ' Status bar
%NOTABCONTROL      = 1  ' Tab control
%NOTOOLBAR         = 1  ' Tool bar
%NOTOOLTIPS        = 1  ' Tool tips
%NOTRACKBAR        = 1  ' Track bar
%NOTRACKMOUSEEVENT = 1  ' Track Mouse Event
%NOTREEVIEW        = 1  ' TreeView
%NOUPDOWN          = 1  ' Up Down arrow control

'------------------------------------------------------------------------------
' Equates
'------------------------------------------------------------------------------
%IDC_LABEL1    = %WM_USER + 2110 ' control ids
%IDC_BTNPATH   = %WM_USER + 2120
%IDC_BTNCONV   = %WM_USER + 2121
%IDC_TXT1      = %WM_USER + 2140
%IDC_TXT2      = %WM_USER + 2141

'------------------------------------------------------------------------------
' Global variables
'------------------------------------------------------------------------------
GLOBAL ghDlg       AS DWORD   ' main dialog's handle
GLOBAL ghIcon      AS DWORD   ' main dialog's icon handle
GLOBAL gStartPath  AS STRING  ' path of video folder
GLOBAL VlcPorPath AS STRING
GLOBAL VlcLocPath AS STRING

'------------------------------------------------------------------------------
' Util functions
'------------------------------------------------------------------------------
SUB SetNewPath()
    LOCAL i AS LONG

    ' Set new start path
    gStartPath = RTRIM$(gStartPath, ANY "\/") + "\"
    i = CountEligibleShortcuts()

    IF i = 0 THEN
        CONTROL SET TEXT  ghDlg, %IDC_LABEL1, _
        "No VLC shortcuts found in this folder..."
        CONTROL SET COLOR ghDlg, %IDC_LABEL1, %RED, -1
        CONTROL DISABLE   ghDlg, %IDC_BTNCONV

    ELSE
        CONTROL SET TEXT  ghDlg, %IDC_LABEL1, _
        "VLCPortable found. Ready to convert " + FORMAT$(i) + " shortcuts."
        CONTROL SET COLOR ghDlg, %IDC_LABEL1, -1, -1
        CONTROL SET TEXT  ghDlg, %IDC_TXT1, LEFT$(gStartPath, -1)
        CONTROL ENABLE    ghDlg, %IDC_BTNCONV
    END IF
END SUB

'------------------------------------------------------------------------------
FUNCTION EXISTS(BYVAL fileOrFolder AS STRING) AS LONG
    LOCAL Dummy&
    Dummy& = GETATTR(fileOrFolder)
    FUNCTION = (ERRCLEAR = 0)
END FUNCTION

'------------------------------------------------------------------------------
SUB GetVlcPaths()
    LOCAL r AS STRING

    ' Get path for VLCPortable
    VlcPorPath = DIR$(EXE.PATH$ + "VlcPortable*", ONLY %SUBDIR) : DIR$ CLOSE
    IF VlcPorPath <> "" THEN
        VlcPorPath = EXE.PATH$ + VlcPorPath + "\"
        r = DIR$(VlcPorPath + "VLC*.exe") : DIR$ CLOSE
        IF r <> "" THEN VlcPorPath += r ELSE VlcPorPath = ""
    END IF

    ' Get path for local VLC
    VlcLocPath = GETREGVALUE(%HKEY_LOCAL_MACHINE, "SOFTWARE\VideoLAN\VLC", "")

END SUB

'------------------------------------------------------------------------------
FUNCTION CountEligibleShortcuts() AS LONG
    LOCAL Link AS LinkType
    LOCAL e AS STRING
    LOCAL n AS LONG

    e = DIR$(gStartPath + "*.lnk")
    WHILE e <> ""
        Link.zLinkName   = (e)
        Link.zLinkFolder = (gStartPath)
        LinkQuery(Link)
        IF ISTRUE INSTR(Link.zExeName, "vlc.exe") THEN INCR n
        e = DIR$(NEXT)
    WEND
    DIR$ CLOSE

    FUNCTION = n
END FUNCTION

'------------------------------------------------------------------------------
SUB ConvertAllShortcuts()
    LOCAL  Link AS LinkType
    LOCAL  e AS STRING

    IF VlcPorPath = "" THEN EXIT SUB

    e = DIR$(gStartPath + "*.lnk")
    WHILE e <> ""
        Link.zLinkName   = (e)
        Link.zLinkFolder = (gStartPath)
        LinkQuery(Link)
        IF ISTRUE INSTR(Link.zExeName, "vlc.exe") THEN
            ' Overwrite shortcut
            CreateShortcut _
              gStartPath + e,                     _ ' 1. the link file to be created
              VlcPorPath,                         _ ' 2. the file/document where the shortcut should point to
              Link.zArguments,                    _ ' 3. command-line parameters
              Link.zWorkDir,                      _ ' 4. the folder where the executable file should start in
              %SW_SHOW,                           _ ' 5. %SW_SHOW, %SW_HIDE etc.
              VlcPorPath,                         _ ' 6. icon file or executable file containing an icon
              0,                                  _ ' 7. icon index in the aforementioned file
              ("(c) mougino.free.fr 2025")          ' 8. any comment (stored in the shortcut)
        END IF
        e = DIR$(NEXT)
    WEND
    DIR$ CLOSE

END SUB

'------------------------------------------------------------------------------
' Main callback
'------------------------------------------------------------------------------
CALLBACK FUNCTION DlgProc () AS LONG
    LOCAL t AS STRING
    LOCAL r AS STRING
    LOCAL i AS LONG

    ' Callback handlers
    CB_SAVEPOS
    CB_DRAGNDROP

    SELECT CASE CB.MSG

    CASE %WM_SETCURSOR ' change cursor to link-hand when hovering over icons
        i = GetDlgCtrlId(CB.WPARAM)
        IF i = %IDC_TXT2 THEN
            SetCursor LoadCursor(%NULL, BYVAL %IDC_HAND)
            SetWindowLong CB.HNDL, %dwl_msgresult, 1
            FUNCTION = 1
        END IF

    CASE %WM_INITDIALOG
        IF VlcPorPath = "" THEN
            CONTROL DISABLE ghDlg, %IDC_BTNPATH
            CONTROL SET TEXT CB.HNDL, %IDC_LABEL1, _
            "No VLCPortable* folder found near this program."
            CONTROL SET COLOR CB.HNDL, %IDC_LABEL1, %RED, -1
        END IF

    CASE %WM_COMMAND
        IF CB.CTLMSG <> %BN_CLICKED THEN EXIT SELECT

        SELECT CASE CB.CTL

        CASE %IDCANCEL
            DIALOG END CB.HNDL

        CASE %IDC_TXT2
            IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                ShellExecute %NULL, "open", "http://mougino.free.fr/freeware", "", "", %SW_SHOW
            END IF

        CASE %IDC_BTNPATH
            ' Change path of video folder
            DISPLAY BROWSE CB.HNDL,,, "", "", %BIF_RETURNONLYFSDIRS _
                OR %BIF_DONTGOBELOWDOMAIN OR %BIF_NONEWFOLDERBUTTON _
                TO gStartPath
            IF LEN(gStartPath) THEN
                SetNewPath()
            END IF

        CASE %IDC_BTNCONV
            ' Convert all shortcuts to Portable VLC
            i = CountEligibleShortcuts()
            IF i <> 0 THEN
                ConvertAllShortcuts()
                CONTROL SET TEXT CB.HNDL, %IDC_LABEL1, _
                FORMAT$(i) + " shortcuts successfully converted!"
                CONTROL SET COLOR CB.HNDL, %IDC_LABEL1, -1, -1
            ELSE
                CONTROL SET TEXT CB.HNDL, %IDC_LABEL1, _
                "No VLC shortcuts found in this folder..."
                CONTROL SET COLOR CB.HNDL, %IDC_LABEL1, %RED, -1
            END IF
            CONTROL DISABLE CB.HNDL, %IDC_BTNCONV

        END SELECT

    CASE %WM_DESTROY

    END SELECT

END FUNCTION


'------------------------------------------------------------------------------
' Main entry point for the application
'------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
    LOCAL r AS STRING

    ' Get VLC paths (Portable near program and Local from registry)
    GetVlcPaths()

    ' Initialize the common control library with standard Windows classes
    InitComCtl32

    ' Create dialog
    DIALOG NEW %HWND_DESKTOP, EXE.NAME$ + $SPC + $VER,,, 258, 40, _
        %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO ghDlg

    DIALOG SET ICON      ghDlg, "ICO1"

    CONTROL ADD LABEL,   ghDlg, -1,          "Folder :",        5,  6,  35, 12
    CONTROL ADD TEXTBOX, ghDlg, %IDC_TXT1, "",                  40,  4, 150, 12

    r = "VLCPortable found. Browse for a folder or drag'n drop one here ^"
    CONTROL ADD LABEL,   ghDlg, %IDC_LABEL1, r,                 5, 22, 200, 10, _
      %SS_PATHELLIPSIS OR %WS_CHILD OR %WS_VISIBLE, %WS_EX_LEFT OR %WS_EX_LTRREADING

    CONTROL ADD BUTTON,  ghDlg, %IDC_BTNPATH, "...",            190,  4, 12, 12
    CONTROL ADD BUTTON,  ghDlg, %IDC_BTNCONV, "&Convert",       212,  6, 40, 18, %WS_TABSTOP OR %BS_DEFAULT
    CONTROL DISABLE      ghDlg, %IDC_BTNCONV

    CONTROL ADD LABEL, ghDlg,  %IDC_TXT2,  "[?]", 246, 29, 8, 12, %SS_NOTIFY
    CONTROL SET COLOR  ghDlg,  %IDC_TXT2,  %BLUE, -1

    ' Show dialog
    DIALOG SHOW MODAL ghDlg CALL DlgProc

END FUNCTION


'------------------------------------------------------------------------------
' File dropped management
'------------------------------------------------------------------------------
SUB FileDropped(BYVAL myfile AS STRING)
    LOCAL hSearch AS DWORD                      ' Search handle
    LOCAL WFD     AS WIN32_FIND_DATA            ' FindFirstFile structure
    LOCAL imglst  AS STRING                     ' Path to existing image list
    LOCAL xt      AS STRING                     ' File extension
    LOCAL r       AS STRING                     ' Cmd result
    LOCAL i       AS LONG                       ' Enumerator

    IF VlcPorPath = "" THEN EXIT SUB

    hSearch = FindFirstFile((myfile), WFD)      ' Get search handle
    FindClose hSearch

    IF hSearch <> %INVALID_HANDLE_VALUE THEN

        IF (WFD.dwFileAttributes AND _
            %FILE_ATTRIBUTE_DIRECTORY) _        ' If it's a directory
          = %FILE_ATTRIBUTE_DIRECTORY THEN
            gStartPath = myfile                 ' Set the path
            SetNewPath()
        END IF

    END IF

END SUB