Jump to content
Sign in to follow this  
BinaryBrother

Download Latest FireFox

Recommended Posts

BinaryBrother

Here is a mess I call code... I tried to organize it. Keep in mind I know I have long function names, but in a case like this optimization of function names isn't an issue... :)

I hope the topic isn't entirely misleading as I've posted my entire project... I'm nowhere near done, but the biggest feat was using RegExp to download the latest version of FireFox, which I'll list in a more usable way at the bottom.

Project Details:

Basically just a little script for fresh OS installs

Highlight:

FireFox auto-download

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=Configure x86.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Inet.au3>
#include <Array.au3>

; Resources
;   http://gskinner.com/RegExr/

;Set Globals
Global $CurrentVersion, $FireFoxLocalDIR, $FireFoxSaveDIR, $TempFile1

; Assign Globals
$CurrentVersion = "3.6.3"; Current version of the FireFox install in the AutoIt DIR
$FireFoxSaveDIR = @DesktopDir & "\FireFox Latest.exe"
$FireFoxLocalDIR = @ScriptDir & "\Firefox Setup 3.6.3.exe"
$Temp_LatestURL = _Generate_TMP_File()

;Run Program
If @OSVersion = "WIN_7" Or @OSVersion = "WIN_VISTA" Then
    _Set_UAC()
    _Fix_Aero()
    _Install_FireFox()
    _Clean_TMP()
    _Reboot()
ElseIf @OSVersion = "WIN_XP" Then
    _Install_FireFox()
    _Clean_TMP()
    _Reboot()
    ;Do XP Specific Stuff
EndIf

;Custom Functons
Func _Generate_TMP_File()
    $RandomFile = @TempDir & "\" & Random(20000, 120000, 1)
    While FileExists($RandomFile)
        $RandomFile = @TempDir & "\" & Random(20000, 120000, 1)
    WEnd
    Return $RandomFile
EndFunc   ;==>_Generate_TMP_File

Func _Set_UAC()
    If MsgBox(262144 + 4, "UAC", "Disable UAC?") = 6 Then
        ;Disable UAC
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System", "EnableLUA", "REG_DWORD", "0")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System", "ConsentPromptBehaviorAdmin", "REG_DWORD", "0")
    EndIf
EndFunc   ;==>_Set_UAC

Func _Fix_Aero()
    If MsgBox(262144 + 4, "Aero", "Try to enable Aero?") = 6 Then
        ShellExecute(@SystemDir & "\msdt.exe", "-id AeroDiagnostic")
        WinWait("Aero", "&Next")
        ;con
        While ControlGetText("Aero", "", "[CLASS:Button; INSTANCE:1]") = "&Next"
            ControlClick("Aero", "", "[CLASS:Button; INSTANCE:1]")
            ControlSetText("Aero", "", "[CLASS:Button; INSTANCE:1]", "&Done")
        WEnd
    EndIf
EndFunc   ;==>_Fix_Aero

Func _Install_FireFox()
    If MsgBox(262144 + 4, "FireFox", "Install FireFox?") = 6 Then
        If _InternetActive_And_FireFox_OutDated() And MsgBox(262144 + 4, "FireFox", "A Newer version of FireFox is out." & @CRLF & "Download new version?") = 6 Then
            _Download_And_Install_Latest_FireFox()
        Else
            _Install_Local_FireFox()
        EndIf
    EndIf
EndFunc   ;==>_Install_FireFox

Func _Install_Local_FireFox()
    TrayTip("FireFox", "Installing FireFox...", 5)
    ShellExecuteWait($FireFoxLocalDIR, "-ms")
    TrayTip("FireFox", "FireFox Installed!", 5)
EndFunc   ;==>_Install_Local_FireFox


Func _Reboot()
    MsgBox(0, "", "This would have been shutdown...")
EndFunc   ;==>_Reboot_Loop

Func _Clean_TMP()
    FileDelete($Temp_LatestURL)
EndFunc   ;==>_Clean_TMP


Func _InternetActive_And_FireFox_OutDated()
    If Not Ping("www.google.com", 250) And Not _INetGetSource("http://www.google.com") <> "" Then Return False

    $Source = _INetGetSource("http://www.mozilla.com/en-US/firefox/all.html")
    $LatestInfo = StringRegExp($Source, "(http://download\.mozilla\.org/\?product=firefox-([0-9]+\.[0-9]+\.[0-9]+)&amp;os=win&amp;lang=en-US)", 3)
    If Not IsArray($LatestInfo) Then Return False
    $LatestURL = $LatestInfo[0]
    $LatestVersion = $LatestInfo[1]
    If $LatestVersion <> $CurrentVersion Then
        FileWrite($Temp_LatestURL, $LatestURL)
        Return True
    Else
        Return False
    EndIf
EndFunc   ;==>_InternetActive_And_FireFox_OutDated

Func _Download_And_Install_Latest_FireFox()
    $LatestURL = FileRead($Temp_LatestURL)
    $Done = False
    $Size = InetGetSize($LatestURL, 1)
    $DHandle = InetGet($LatestURL, $FireFoxSaveDIR, 1, 1)
    While Not $Done
        $Info = InetGetInfo($DHandle, 0)
        $Done = InetGetInfo($DHandle, 2)
        $Percent = Ceiling(($Info / $Size) * 100)
        TrayTip("Downloading", $Percent, 5)
        Sleep(500)
    WEnd
    TrayTip("Installing", "Please Wait...", 5)
    ShellExecuteWait($FireFoxSaveDIR, "-ms")
    TrayTip("FireFox", "FireFox has been downloaded and installed.", 5)
EndFunc   ;==>_Download_And_Install_Latest_FireFox

------------------------------------------------------------------------------------------------------------------------------------------

Func _Download_And_Install_Latest_FireFox($Silent = False, $FireFoxSaveDIR = @DesktopDir & "\FireFox Latest.exe")
    $Source = _INetGetSource("http://www.mozilla.com/en-US/firefox/all.html")

    $LatestInfo = StringRegExp($Source, "(http://download\.mozilla\.org/\?product=firefox-([0-9]+\.[0-9]+\.[0-9]+)&amp;os=win&amp;lang=en-US)", 3)
    If Not IsArray($LatestInfo) Then Return False
    $LatestURL = $LatestInfo[0]
    $LatestVersion = $LatestInfo[1]; Maybe needed for compare of local copy?
    $Done = False
    $Size = InetGetSize($LatestURL, 1)
    $DHandle = InetGet($LatestURL, $FireFoxSaveDIR, 1, 1)
    While Not $Done
        $Info = InetGetInfo($DHandle, 0)
        $Done = InetGetInfo($DHandle, 2)
        $Percent = Ceiling(($Info / $Size) * 100)
        TrayTip("Downloading", $Percent, 5)
        Sleep(500)
    WEnd
    TrayTip("Installing", "Please Wait...", 5)
    If $Silent Then
        ShellExecuteWait($FireFoxSaveDIR, "-ms")
    Else
        ShellExecuteWait($FireFoxSaveDIR)
    EndIf
    TrayTip("FireFox", "FireFox has been downloaded and installed.", 5)
EndFunc

I think my RegExp looks pretty good, but I'd really appreciate a review from one of you RegExp guys. :idea:

As for a topic of Discussion, http://gskinner.com/RegExr/ is the most awesome thing I've ever seen...

Edited by BinaryBrother

Share this post


Link to post
Share on other sites
BinaryBrother

Keep in mind I know I have long function names, but in a case like this optimization of function names isn't an issue...

I already pointed out that in a case like this, it's a simple matter of preference. Optimization of that level is definitely not an issue with a script like this. :idea:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×