Advertisement
deseven

qhosts

Mar 23rd, 2011
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; buttons
  2. ButtonNew =      CatchImage(0,?ButtonNew)
  3. ButtonDel =      CatchImage(2,?ButtonDel)
  4. ButtonRefresh =  CatchImage(3,?ButtonRefresh)
  5. ButtonHide =     CatchImage(4,?ButtonHide)
  6. DataSection
  7.   ButtonNew:     IncludeBinary "buttons\new.ico"
  8.   ButtonDel:     IncludeBinary "buttons\del.ico"
  9.   ButtonRefresh: IncludeBinary "buttons\refresh.ico"
  10.   ButtonHide:    IncludeBinary "buttons\hide.ico"
  11. EndDataSection
  12.  
  13. a$=Space(999) : GetModuleFileName_(0,@a$,999) : appname$=GetFilePart(a$)
  14. Global HostsPath.s = GetEnvironmentVariable("WINDIR") + "\system32\drivers\etc\hosts"
  15.  
  16. Procedure ParseHostsString(String.s)
  17.   Global IP.s = ""
  18.   Global Host.s = ""
  19.   If Left(String,1) = "#" And Not Left(String,2) = "#$"
  20.     ProcedureReturn #False
  21.   EndIf
  22.   If Len(String) > 8
  23.     For i = 1 To Len(String)
  24.       If Mid(String,i,1) = " " Or Mid(String,i,1) = Chr(9)
  25.       Else
  26.         For k = i To Len(String)
  27.           If Mid(String,k,1) = " " Or Mid(String,k,1) = Chr(9)
  28.             IP = Mid(String,i,k-i+1)
  29.             IP = ReplaceString(IP,Chr(9),"")
  30.             IP = ReplaceString(IP," ","")
  31.             For j = k To Len(String)
  32.               If Mid(String,j,1) = " " Or Mid(String,j,1) = Chr(9)
  33.               Else
  34.                 For l = j To Len(String)
  35.                   If Mid(String,l,1) = " " Or Mid(String,l,1) = Chr(9) Or l = Len(String)
  36.                     Host = Mid(String,j,l-j+1)
  37.                     Host = ReplaceString(Host,Chr(9),"")
  38.                     Host = ReplaceString(Host," ","")
  39.                     ProcedureReturn #True
  40.                     Break
  41.                   EndIf
  42.                 Next
  43.                 Break
  44.               EndIf
  45.             Next
  46.             Break
  47.           EndIf  
  48.         Next
  49.         Break  
  50.       EndIf
  51.     Next
  52.     ProcedureReturn #False
  53.   Else
  54.     ProcedureReturn #False
  55.   EndIf
  56. EndProcedure
  57.  
  58. Procedure LoadHosts()
  59.   ClearGadgetItems(10)
  60.   SetFileAttributes(HostsPath,#PB_FileSystem_Normal)
  61.   If ReadFile(0,HostsPath)
  62.     Global NewList Status.i()
  63.     While Eof(0) = 0
  64.       If ParseHostsString(ReadString(0))
  65.         If Left(IP,2) = "#$"
  66.           AddElement(Status())
  67.           Status() = 0
  68.           AddGadgetItem(10,ListIndex(Status()),Host + Chr(10) + ReplaceString(IP,"#$",""))
  69.         Else
  70.           AddElement(Status())
  71.           Status() = 1
  72.           AddGadgetItem(10,ListIndex(Status()),Host + Chr(10) + IP)
  73.           SetGadgetItemState(10,ListIndex(Status()),#PB_ListIcon_Checked)
  74.         EndIf
  75.       EndIf
  76.     Wend
  77.     CloseFile(0)
  78.     SetFileAttributes(HostsPath,#PB_FileSystem_ReadOnly)
  79.     ProcedureReturn #True
  80.   Else
  81.     ProcedureReturn #False
  82.   EndIf
  83. EndProcedure
  84.  
  85. Procedure SaveHosts()
  86.   SetFileAttributes(HostsPath,#PB_FileSystem_Normal)
  87.   If CreateFile(1,HostsPath)
  88.     ForEach Status()
  89.       If Status() = 1
  90.         WriteStringN(1,GetGadgetItemText(10,ListIndex(Status()),1) + Chr(9) + GetGadgetItemText(10,ListIndex(Status()),0))
  91.       Else
  92.         WriteStringN(1,"#$" + GetGadgetItemText(10,ListIndex(Status()),1) + Chr(9) + GetGadgetItemText(10,ListIndex(Status()),0))
  93.       EndIf
  94.     Next
  95.     CloseFile(1)
  96.     SetFileAttributes(HostsPath,#PB_FileSystem_ReadOnly)
  97.     ProcedureReturn #True
  98.   Else
  99.     ProcedureReturn #False
  100.   EndIf
  101. EndProcedure
  102.  
  103. Procedure AddEditHost()
  104.   If Len(GetGadgetText(21)) > 0 And Len(GetGadgetText(22)) > 7
  105.     ForEach(Status())
  106.       If GetGadgetItemText(10,ListIndex(Status()),0) = GetGadgetText(21)
  107.         RemoveGadgetItem(10,ListIndex(Status()))
  108.         AddGadgetItem(10,ListIndex(Status()),GetGadgetText(21) + Chr(10) + GetGadgetText(22))
  109.         If GetGadgetState(20) = #PB_Checkbox_Checked
  110.           SetGadgetItemState(10,ListIndex(Status()),#PB_ListIcon_Checked)
  111.           Status() = 1
  112.         Else
  113.           Status() = 0
  114.         EndIf
  115.         ProcedureReturn #True
  116.       EndIf
  117.     Next
  118.     LastElement(Status())
  119.     AddElement(Status())
  120.     AddGadgetItem(10,ListIndex(Status()),GetGadgetText(21) + Chr(10) + GetGadgetText(22))
  121.     If GetGadgetState(20) = #PB_Checkbox_Checked
  122.       SetGadgetItemState(10,ListIndex(Status()),#PB_ListIcon_Checked)
  123.       Status() = 1
  124.     Else
  125.       Status() = 0
  126.     EndIf
  127.     ProcedureReturn #True
  128.   EndIf
  129.   ProcedureReturn #False
  130. EndProcedure
  131.  
  132. Macro SetToolBarIconSize(ToolBar, IconSize)
  133.    
  134.    SendMessage_(ToolBarID(ToolBar), #TB_SETBITMAPSIZE, 0, (IconSize << 16) + IconSize)
  135.    ImageList_SetIconSize_(SendMessage_(ToolBarID(ToolBar), #TB_GETIMAGELIST, 0, 0) , IconSize , IconSize)
  136.    SendMessage_(ToolBarID(ToolBar), #TB_AUTOSIZE, 0, 0)
  137.    
  138. EndMacro
  139.  
  140. If OpenWindow(0,#PB_Ignore,#PB_Ignore,400,300,"QHosts",#PB_Window_Invisible|#PB_Window_ScreenCentered|#PB_Window_BorderLess)
  141.   Icon = ExtractIcon_(WindowID(0),appname$,0)
  142.   AddSysTrayIcon(100,WindowID(0),Icon)
  143.   SysTrayIconToolTip(100,"QHosts")
  144.   If CreatePopupMenu(0)
  145.     MenuItem(1,"QHosts")
  146.     DisableMenuItem(0,1,1)
  147.     MenuBar()
  148.     MenuItem(2,"Show")
  149.     MenuItem(3,"Exit")
  150.   EndIf
  151.   ButtonImageGadget(1,10,10,34,34,ButtonNew)
  152.   ButtonImageGadget(2,50,10,34,34,ButtonDel)
  153.   ButtonImageGadget(3,90,10,34,34,ButtonRefresh)
  154.   ButtonImageGadget(4,358,10,34,34,ButtonHide)
  155.   CheckBoxGadget(20,16,18,15,18,"")
  156.   StringGadget(21,33,18,181,18,"")
  157.   IPAddressGadget(22,224,18,100,18)
  158.   ButtonGadget(23,334,18,20,18,"+")
  159.   ButtonGadget(24,364,18,20,18,"X")
  160.   HideGadget(20,1) : HideGadget(21,1) : HideGadget(22,1) : HideGadget(23,1) : HideGadget(24,1)
  161.   ListIconGadget(10,10,52,380,235,"Host",250,#PB_ListIcon_MultiSelect|#PB_ListIcon_FullRowSelect|#PB_ListIcon_CheckBoxes|#PB_ListIcon_AlwaysShowSelection)
  162.   AddGadgetColumn(10,1,"IP",105)
  163.  
  164. Else
  165.   MessageRequester("Error!","Can't create window... [Code: 11]")
  166. EndIf
  167.  
  168. Repeat
  169.   ev = WaitWindowEvent()
  170.   If (ev = #PB_Event_SysTray And EventType() = #PB_EventType_LeftDoubleClick) Or (ev = #PB_Event_Menu And EventMenu() = 2)
  171.     HideWindow(0,1)
  172.     regn.i$ = CreateRoundRectRgn_(0,0,400,300,20,20)
  173.     SetWindowRgn_(WindowID(0),regn.i$,#True)
  174.     HideWindow(0,0)
  175.     SetActiveWindow(0)
  176.     If LoadHosts() = #False
  177.       MessageRequester("Error!","Can't load `hosts` file, check your permissions... [Code: 12]")
  178.     EndIf
  179.   EndIf
  180.   If ev = #PB_Event_SysTray And EventType() = #PB_EventType_RightClick
  181.     DisplayPopupMenu(0,WindowID(0))
  182.   EndIf
  183.   If ev = #PB_Event_Menu And EventMenu() = 3
  184.     ev = #PB_Event_CloseWindow
  185.   EndIf
  186.   If ev = #WM_LBUTTONDOWN
  187.     SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
  188.   EndIf
  189.   If EventGadget() = 10 And EventType() = #PB_EventType_LeftDoubleClick
  190.     HideGadget(1,1) : HideGadget(2,1) : HideGadget(3,1) : HideGadget(4,1)
  191.     HideGadget(20,0) : HideGadget(21,0) : HideGadget(22,0) : HideGadget(23,0) : HideGadget(24,0)
  192.     If GetGadgetState(10) > -1
  193.       SetGadgetText(21,GetGadgetItemText(10,GetGadgetState(10),0))
  194.       CurIP.s = GetGadgetItemText(10,GetGadgetState(10),1)
  195.       SetGadgetState(22,MakeIPAddress(Val(StringField(CurIP,1,".")),Val(StringField(CurIP,2,".")),Val(StringField(CurIP,3,".")),Val(StringField(CurIP,4,"."))))
  196.       SelectElement(Status(),GetGadgetState(10))
  197.       If Status() = 1
  198.         SetGadgetState(20,#PB_Checkbox_Checked)
  199.       Else
  200.         SetGadgetState(20,#PB_Checkbox_Unchecked)
  201.       EndIf
  202.     EndIf
  203.     SetActiveGadget(21)
  204.   EndIf
  205.   If EventGadget() = 24 And EventType() = #PB_EventType_LeftClick
  206.     HideGadget(20,1) : HideGadget(21,1) : HideGadget(22,1) : HideGadget(23,1) : HideGadget(24,1)
  207.     HideGadget(1,0) : HideGadget(2,0) : HideGadget(3,0) : HideGadget(4,0)
  208.     SetActiveGadget(10)
  209.   EndIf
  210.   If EventGadget() = 23 And EventType() = #PB_EventType_LeftClick
  211.     AddEditHost()
  212.     If SaveHosts() = #False
  213.       MessageRequester("Error!","Can't save `hosts` file, check your permissions... [Code: 13]")
  214.     EndIf
  215.     HideGadget(20,1) : HideGadget(21,1) : HideGadget(22,1) : HideGadget(23,1) : HideGadget(24,1)
  216.     HideGadget(1,0) : HideGadget(2,0) : HideGadget(3,0) : HideGadget(4,0)
  217.     SetActiveGadget(10)
  218.   EndIf
  219.   If EventGadget() = 1 And EventType() = #PB_EventType_LeftClick
  220.     HideGadget(1,1) : HideGadget(2,1) : HideGadget(3,1) : HideGadget(4,1)
  221.     HideGadget(20,0) : HideGadget(21,0) : HideGadget(22,0) : HideGadget(23,0) : HideGadget(24,0)
  222.     If GetGadgetState(10) > -1
  223.       SetGadgetText(21,GetGadgetItemText(10,GetGadgetState(10),0))
  224.       CurIP.s = GetGadgetItemText(10,GetGadgetState(10),1)
  225.       SetGadgetState(22,MakeIPAddress(Val(StringField(CurIP,1,".")),Val(StringField(CurIP,2,".")),Val(StringField(CurIP,3,".")),Val(StringField(CurIP,4,"."))))
  226.       SelectElement(Status(),GetGadgetState(10))
  227.       If Status() = 1
  228.         SetGadgetState(20,#PB_Checkbox_Checked)
  229.       Else
  230.         SetGadgetState(20,#PB_Checkbox_Unchecked)
  231.       EndIf
  232.     EndIf
  233.     SetActiveGadget(21)
  234.   EndIf  
  235.   If EventGadget() = 2 And EventType() = #PB_EventType_LeftClick
  236.    
  237.   EndIf
  238.   If EventGadget() = 3 And EventType() = #PB_EventType_LeftClick
  239.     FreeList(Status())
  240.     If LoadHosts() = #False
  241.       MessageRequester("Error!","Can't load `hosts` file, check your permissions... [Code: 12]")
  242.     EndIf
  243.   EndIf
  244.   If EventGadget() = 4 And EventType() = #PB_EventType_LeftClick
  245.     FreeList(Status())
  246.     HideWindow(0,1)
  247.   EndIf
  248.   If EventType() = #PB_EventType_LeftClick And EventGadget() = 10
  249.     ForEach Status()
  250.       If (GetGadgetItemState(10,ListIndex(Status())) & #PB_ListIcon_Checked And Status() = 1) Or (Not GetGadgetItemState(10,ListIndex(Status())) & #PB_ListIcon_Checked And Status() = 0)
  251.       Else
  252.         If Status() = 0
  253.           Status() = 1
  254.         Else
  255.           Status() = 0
  256.         EndIf
  257.         If SaveHosts() = #False
  258.           MessageRequester("Error!","Can't save `hosts` file, check your permissions... [Code: 13]")
  259.         EndIf
  260.       EndIf
  261.     Next
  262.   EndIf
  263. Until ev = #PB_Event_CloseWindow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement