' Gambas module file

' Copyright(C) 2011-2014, Michał Głowienka aka eloaders <eloaders@linux.pl>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.

Public Logs As New Logger
Public subsystem_device As String
Public subsystem_vendor As String
Public lspci As String
Private set_interface As String

Public Sub inits()
  Dim Avail_Net As Integer 
  Logs("Add Network Devices to ComboBox", Logger.Info)
  Finfosys.ComboBox2.Clear
  For Avail_Net = 1 To Dir("/sys/class/net/").Count
   Finfosys.ComboBox2.Add(Dir("/sys/class/net/")[Avail_Net - 1])
  Next
  Finfosys.ComboBox2.Index = 0
End

Public Sub get_informations_about_net_interface()
 SETNULL()
 Try subsystem_device = Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/device/device"), "\n", "")
 Try subsystem_vendor = Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/device/vendor"), "\n", "")
 Try Finfosys.Label157.Text = "Device address: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/address"), "\n", "")
 Try Finfosys.Label158.Text = "Device addr len: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/addr_len"), "\n", "")
 Try Finfosys.Label159.Text = "Assign type: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/addr_assign_type"), "\n", "")
 Try Finfosys.Label160.Text = "Broadcast: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/broadcast"), "\n", "")
 Try Finfosys.Label161.Text = "Carrier: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/carrier"), "\n", "")
 Try Finfosys.Label162.Text = "Dev id: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/dev_id"), "\n", "")
 Try Finfosys.Label163.Text = "Dormant: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/dormant"), "\n", "")
 Try Finfosys.Label164.Text = "Duplex: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/duplex"), "\n", "")
 Try Finfosys.Label165.Text = "Flags: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/flags"), "\n", "")
 Try Finfosys.Label166.Text = "Ifalias: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/ifalias"), "\n", "")
 Try Finfosys.Label167.Text = "Ifindex: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/ifindex"), "\n", "")
 Try Finfosys.Label168.Text = "Iflink: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/iflink"), "\n", "")
 Try Finfosys.Label169.Text = "Link mode: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/link_mode"), "\n", "")
 Try Finfosys.Label170.Text = "MTU: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/mtu"), "\n", "")
 Try Finfosys.Label171.Text = "Netdev group: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/netdev_group"), "\n", "")
 Try Finfosys.Label172.Text = "Operstate: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/operstate"), "\n", "")
 Try Finfosys.Label173.Text = "Speed: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/speed"), "\n", "")
 Try Finfosys.Label174.Text = "Tx queue len: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/tx_queue_len"), "\n", "")
 Try Finfosys.Label175.Text = "Type: " & Replace(File.Load("/sys/class/net/" & Finfosys.ComboBox2.Text & "/type"), "\n", "")
 
  If subsystem_device Like "*0x*" Then
  lspci = Mdecode._d(subsystem_vendor, subsystem_device)
  Else
     lspci = Null
  Endif

End

Public Function IP_INFO()
 Dim IPINFOIO_BUF As String
 Dim IPINFOIO_JSON As Collection
 Inc Application.Busy
 Try Exec ["curl", "ipinfo.io"] To IPINFOIO_BUF
 Try IPINFOIO_JSON = JSON.Decode(IPINFOIO_BUF)
 Dec Application.Busy
 'https://www.google.com/maps/search/54.6052,18.2355
 Try Finfosys.Label93.Text = IPINFOIO_JSON["ip"]
 Try Finfosys.Label233.Text = IPINFOIO_JSON["hostname"]
 Try Finfosys.Label234.Text = IPINFOIO_JSON["city"]
 Try Finfosys.Label235.Text = IPINFOIO_JSON["region"]
 Try Finfosys.Label236.Text = IPINFOIO_JSON["country"]
 Try Finfosys.Label237.Text = IPINFOIO_JSON["loc"]
 Try Finfosys.Label238.Text = IPINFOIO_JSON["org"]
End

Function SETNULL()
 
 Try Finfosys.Label157.Text = Null
 Try Finfosys.Label158.Text = Null
 Try Finfosys.Label159.Text = Null
 Try Finfosys.Label160.Text = Null
 Try Finfosys.Label161.Text = Null
 Try Finfosys.Label162.Text = Null
 Try Finfosys.Label163.Text = Null
 Try Finfosys.Label164.Text = Null
 Try Finfosys.Label165.Text = Null
 Try Finfosys.Label166.Text = Null
 Try Finfosys.Label167.Text = Null
 Try Finfosys.Label168.Text = Null
 Try Finfosys.Label169.Text = Null
 Try Finfosys.Label170.Text = Null
 Try Finfosys.Label171.Text = Null
 Try Finfosys.Label172.Text = Null
 Try Finfosys.Label173.Text = Null
 Try Finfosys.Label174.Text = Null
 Try Finfosys.Label175.Text = Null
 Try subsystem_device = Null
 Try subsystem_vendor = Null
 
End
