URL Tool

15 Dec

All links at one place

#include <GUIConstants.au3>
#include <file.au3>
#include <Process.au3>

Global $Handle = ""
$ListItems = ""
$CountLines = ""
$File = ""
$OldFile = ""
$OpenedFile = ""

GUICreate("URLs", 500, 500)

$FileMenu = GUICtrlCreateMenu ("&File")
$NewID = GUICtrlCreateMenuitem ("New",$FileMenu)
$OpenID = GUICtrlCreateMenuitem ("Open",$FileMenu)

$ListBoxID=GUICtrlCreateList ("", 10,35,480,415)
$AddID  = GUICtrlCreateButton("Add", 10, 5, 50, 20)
$DeleteID = GUICtrlCreateButton("Delete", 70, 5, 50, 20)
$EditLinkID = GUICtrlCreateButton("Edit Link", 130, 5, 70, 20)
$EditMemoID = GUICtrlCreateButton("Edit Memo", 210, 5, 70, 20)
$MoveID = GUICtrlCreateButton("Move", 290, 5, 70, 20)
$SurfID = GUICtrlCreateButton("Surf", 370, 5, 50, 20)

GUISetState ()

ButtonActive($GUI_DISABLE)
Do
 $msg = GUIGetMsg()
 Select
  Case $msg= $OpenID
   $Run = 0
   $OldFile = $OpenedFile
   $message = "Open What File?"
   $OpenedFile = FileOpenDialog($message, @ScriptDir & "\List\", "All (*.lst)")

   If @error Then
        MsgBox(4096,"","No file chosen")
    $OpenedFile = $OldFile
   Else
        $OpenedFile = StringReplace($OpenedFile, "|", @CRLF)
    $Run = 1
   EndIf

   If $Run = 1 Then
    RefreshListBox()
    ButtonActive($GUI_ENABLE)
   EndIf
   GUICtrlCreateLabel ("",  15, 450, 480, 100)

  Case $msg= $NewID
   $NewFile = InputBox("Add New File", "Type File Name", "", "", 200, 100)
   $OpenedFile = @ScriptDir & "\List\" & $NewFile & ".lst"
   FileWriteLine($OpenedFile, "")
   $NewDesFile = @ScriptDir & "\List\" & $NewFile & ".lst.des"
   FileWriteLine($NewDesFile, "")

   ButtonActive($GUI_ENABLE)
   GUICtrlSetData($ListBoxID,"")
   GUICtrlCreateLabel ("",  15, 450, 480, 100)

  Case $msg= $AddID
   $AddItem = InputBox("Add", "Type The Link", "", "", 200, 100)
   FileWrite($OpenedFile, $AddItem & @LF)

   $File = $OpenedFile & ".des"
   $AddDes = InputBox("Add", "Type The Memo", "", "", 200, 100)
   If $AddDes = "" Then
    FileWrite($File, $AddItem & @LF)
   Else
    FileWrite($File, $AddDes & @LF)
   EndIf

   RefreshListBox()
  
  Case $msg= $DeleteID
   $File = $OpenedFile & ".des"
   $CountLines = _FileCountLines($File)
   $SelectedList = GUICtrlRead($ListBoxID)
   DeleteDesLine ($File, $SelectedList)

   $CountLines = _FileCountLines($OpenedFile)
   $Handle = FileOpen($OpenedFile, 0)
   If $Handle = -1 Then
        MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
   EndIf
   $SelectedList = GUICtrlRead($ListBoxID)
   $Index = FindLineIndex($SelectedList)
   FileClose($Handle)
   DeleteTheLine ($Index)

   $CountLines = _FileCountLines($File)
   RefreshListBox()

   GUICtrlCreateLabel ("",  15, 450, 480, 100)

  Case $msg= $EditLinkID
   $File = $OpenedFile & ".des"
   $CountLines = _FileCountLines($File)
   $Handle = FileOpen($File, 0)
   If $Handle = -1 Then
        MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $File)
   EndIf
   $SelectedList = GUICtrlRead($ListBoxID)
   $Index = FindLineIndex($SelectedList)
   FileClose($Handle)

   $CountLines = _FileCountLines($OpenedFile)
   $Handle = FileOpen($OpenedFile, 0)
   If $Handle = -1 Then
        MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
   EndIf
   $Link=FindLineMessage($Index)
   FileClose($Handle)

   $NewLink = InputBox("New Link", "Type New Link", $Link, "", 200, 100)
   ReplaceTheLine ($OpenedFile, $Index, $NewLink)
  
   GUICtrlCreateLabel ($NewLink,  15, 450, 480, 100)

  Case $msg= $EditMemoID
   $File = $OpenedFile & ".des"
   $CountLines = _FileCountLines($File)
   $SelectedList = GUICtrlRead($ListBoxID)
   $NewDes = InputBox("New Label", "Type Messages", $SelectedList, "", 200, 100)
   ReplaceTheLine ($File, $Index, $NewDes)

   RefreshListBox()

  Case $msg= $MoveID
   $message = "Move To Which File?"
   $MoveFile = FileOpenDialog($message, @ScriptDir & "\List\", "All (*.lst)")
   If $MoveFile = $OpenedFile Then
    MsgBox(0, "???", "Do Not Select Same File!")
   Else
    ;get memo
    $MoveDes = GUICtrlRead($ListBoxID)

    ;get link
    $CountLines = _FileCountLines($OpenedFile)
    $Handle = FileOpen($OpenedFile, 0)
    If $Handle = -1 Then
         MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
    EndIf
    $SelectedList = GUICtrlRead($ListBoxID)
    $Index = FindLineIndex($SelectedList)
    FileClose($Handle)

    $CountLines = _FileCountLines($OpenedFile)
    $Handle = FileOpen($OpenedFile, 0)
    If $Handle = -1 Then
         MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
    EndIf
    $MoveItem=FindLineMessage($Index)
    FileClose($Handle)

    FileWrite($MoveFile, $MoveItem & @LF)
    $File = $MoveFile & ".des"
    FileWrite($File, $MoveDes & @LF)

    ;same delete function
    $File = $OpenedFile & ".des"
    $CountLines = _FileCountLines($File)
    $SelectedList = GUICtrlRead($ListBoxID)
    DeleteDesLine ($File, $SelectedList)

    $CountLines = _FileCountLines($OpenedFile)
    $Handle = FileOpen($OpenedFile, 0)
    If $Handle = -1 Then
         MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
    EndIf
    $SelectedList = GUICtrlRead($ListBoxID)
    $Index = FindLineIndex($SelectedList)
    FileClose($Handle)
    DeleteTheLine ($Index)

    $CountLines = _FileCountLines($File)
    RefreshListBox()

    GUICtrlCreateLabel ("",  15, 450, 480, 100)

   EndIf

  Case $msg= $SurfID
   $File = $OpenedFile & ".des"
   $CountLines = _FileCountLines($File)
   $Handle = FileOpen($File, 0)
   If $Handle = -1 Then
        MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $File)
   EndIf
   $SelectedList = GUICtrlRead($ListBoxID)
   $Index = FindLineIndex($SelectedList)
   FileClose($Handle)

   $CountLines = _FileCountLines($OpenedFile)
   $Handle = FileOpen($OpenedFile, 0)
   If $Handle = -1 Then
        MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
   EndIf
   $Link=FindLineMessage($Index)
   FileClose($Handle)
 
   $rc = _RunDos("start " & $Link)

  Case $msg= $ListBoxID
   $CountLines = _FileCountLines($File)

   $Handle = FileOpen($File, 0)
   If $Handle = -1 Then
        MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
   EndIf

   $SelectedList = GUICtrlRead($ListBoxID)
   $Index = FindLineIndex($SelectedList)
   FileClose($Handle)

   $Handle = FileOpen($OpenedFile, 0)
   If $Handle = -1 Then
       MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $OpenedFile)
   EndIf
   $LineRead = FileReadLine($Handle, $Index)
   FileClose($Handle)
   GUICtrlCreateLabel ($LineRead,  15, 450, 480, 100)
 
 EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func FindLineIndex($x)
    For $i = 1 to $CountLines Step 1
     $NextLine = FileReadLine($Handle)
        If @Error Then  ;The end of the file has been reached.
         Done()
        EndIf
    If $NextLine == $x Then
  ;MsgBox(0x10, 'Error', $i)
  $Index = $i
    EndIf
    Next
    Return $Index
EndFunc

Func FindLineMessage($x)
    For $i = 1 to $CountLines Step 1
     $NextLine = FileReadLine($Handle)
        If @Error Then  ;The end of the file has been reached.
         Done()
        EndIf
    If $i == $x Then 
  $Memo = $NextLine        
    EndIf
    Next
    Return $Memo
EndFunc

Func DeleteTheLine ($x)
    FileCopy($OpenedFile, "Temp.txt")
    FileDelete($OpenedFile)
    For $i = 1 to $CountLines Step 1
    $Text = FileReadLine("Temp.txt", $i)
    If $i <> $x Then
        FileWriteLine($OpenedFile, $Text)
    EndIf
    Next
    FileDelete("Temp.txt")
EndFunc

Func DeleteDesLine ($File, $x)
    FileCopy($File, "Temp.txt")
    FileDelete($File)
    For $i = 1 to $CountLines Step 1
    $Text = FileReadLine("Temp.txt", $i)
    If $Text <> $x Then
        FileWriteLine($File, $Text)
    EndIf
    Next
    FileDelete("Temp.txt")
EndFunc

Func ReplaceTheLine ($File, $x, $NewDes)
    FileCopy($File, "Temp.txt")
    FileDelete($File)
    For $i = 1 to $CountLines Step 1
    $Text = FileReadLine("Temp.txt", $i)
    If $i == $x Then
        FileWriteLine($File, $NewDes)
 Else
     FileWriteLine($File, $Text)
    EndIf
    Next
    FileDelete("Temp.txt")
EndFunc

Func Done()
    FileClose($Handle)
    $myLoop = 0
EndFunc

Func ButtonActive($ENABLE_DISABLE)
 GUICtrlSetState ($AddID, $ENABLE_DISABLE)
 GUICtrlSetState ($DeleteID, $ENABLE_DISABLE)
 GUICtrlSetState ($EditLinkID, $ENABLE_DISABLE)
 GUICtrlSetState ($EditMemoID, $ENABLE_DISABLE)
 GUICtrlSetState ($MoveID, $ENABLE_DISABLE)
 GUICtrlSetState ($SurfID, $ENABLE_DISABLE)
EndFunc

Func RefreshListBox()
 $myLoop = 1
 $File = $OpenedFile & ".des"
 $Handle = FileOpen($File, 0)
 If $Handle = -1 Then
      MsgBox(0x10, 'Error', 'Could not open the file:' & @LF & $File)
      $myLoop = 0
 EndIf

 GUICtrlSetData($ListBoxID,"")
 $ListItems = ""
 while $myLoop == 1
      $NextLine = FileReadLine($Handle)
      If @Error Then ;The end of the file has been reached.
   GUICtrlSetData($ListBoxID, $ListItems)
   GUISetState ()
       $myLoop = 0
      EndIf
     $ListItems = $ListItems & $NextLine & "|"
 WEnd
     FileClose($Handle)
EndFunc

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: