home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / ndisgtk < prev    next >
Encoding:
Text File  |  2007-04-03  |  9.1 KB  |  310 lines

  1. #!/usr/bin/env python
  2.  
  3. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  4. #                                                                             #
  5. # ndisgtk - A GTK frontend for the ndiswrapper wireless driver tool           #
  6. #                                                                             #
  7. # Copyright (C) 2005, Sam Pohlenz <retrix@internode.on.net>                   #
  8. #                                                                             #
  9. # This program is free software; you can redistribute it and/or               #
  10. # modify it under the terms of the GNU General Public License                 #
  11. # as published by the Free Software Foundation; either version 2              #
  12. # of the License, or (at your option) any later version.                      #
  13. #                                                                             #
  14. # This program is distributed in the hope that it will be useful,             #
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of              #
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
  17. # GNU General Public License for more details.                                #
  18. #                                                                             #
  19. # You should have received a copy of the GNU General Public License           #
  20. # along with this program; if not, write to the Free Software                 #
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. #
  22. #                                                                             #
  23. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  24.  
  25. import sys
  26. import os
  27. import commands
  28. import re
  29.  
  30. # Attempt to load GTK bindings
  31. try:
  32.     import pygtk
  33.     pygtk.require("2.0")
  34.     import gtk
  35.     import gtk.glade
  36.     import gobject
  37. except ImportError:
  38.     print "Failed to load GTK bindings. Please check your Gnome installation."
  39.     sys.exit(1)
  40.  
  41. # Internationalization
  42. import locale
  43. import gettext
  44. locale.setlocale(locale.LC_ALL, "")
  45. gettext.bindtextdomain("ndisgtk", "/usr/share/locale")
  46. gettext.textdomain("ndisgtk")
  47. gettext.install("ndisgtk", "/usr/share/locale", unicode=1)
  48. gtk.glade.bindtextdomain("ndisgtk", "/usr/share/locale")
  49. gtk.glade.textdomain("ndisgtk")
  50.  
  51. # Data directory
  52. DATA_DIR = "/usr/share/ndisgtk"
  53.  
  54. def error_dialog(message, parent = None):
  55.     """
  56.     Displays an error message.
  57.     """
  58.     
  59.     dialog = gtk.MessageDialog(parent = parent, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, flags = gtk.DIALOG_MODAL)
  60.     dialog.set_markup(message)
  61.  
  62.     result = dialog.run()
  63.     dialog.destroy()
  64.  
  65. class NdisGTK:
  66.     """
  67.     Main application class.
  68.     """
  69.  
  70.     def __init__(self, kde=False):
  71.         """
  72.         Initializes the application.
  73.         """
  74.  
  75.         # Setup glade and signals
  76.         self.signals = { "gtk_main_quit": gtk.main_quit,
  77.                          "on_install_driver_clicked": self.install_driver_open,
  78.                          "on_remove_driver_clicked": self.remove_driver,
  79.                          "on_driver_list_cursor_changed": self.cursor_changed,
  80.                          "install_dialog_close": self.install_dialog_close,
  81.                          "install_button_clicked": self.install_driver,
  82.                          "network_button_clicked": self.config_network,
  83.                          "help_button_clicked": self.show_help,
  84.                          "drag_motion": self.drag_motion,
  85.                          "drag_data_received": self.drag_data_received }
  86.  
  87.         self.widgets = gtk.glade.XML(DATA_DIR + "/ndisgtk.glade", domain="ndisgtk")
  88.         self.widgets.signal_autoconnect(self.signals)
  89.  
  90.         # Get handle to window
  91.         self.window = self.widgets.get_widget("ndiswrapper_main")
  92.         
  93.         # Load icon
  94.         self.wifi_icon = gtk.gdk.pixbuf_new_from_file(DATA_DIR + "/ndisgtk.png")
  95.         self.window.set_icon_from_file(DATA_DIR + "/ndisgtk.png")
  96.  
  97.         # Get handle to 'Remove Driver' button
  98.         self.remove_driver = self.widgets.get_widget("remove_driver")
  99.  
  100.         # Get handle to 'Install Driver' dialog
  101.         self.install_dialog = self.widgets.get_widget("install_dialog")
  102.         self.install_dialog.set_transient_for(self.window)
  103.  
  104.         # Get handle to file chooser
  105.         self.file_chooser = self.widgets.get_widget("filechooser")
  106.  
  107.         # Enable drag-and-drop
  108.         self.window.drag_dest_set(gtk.DEST_DEFAULT_DROP, [("text/plain", 0, 80)], gtk.gdk.ACTION_COPY)
  109.  
  110.         # Setup driver list
  111.         self.setup_driver_list()
  112.         
  113.         # Use KDE network admin?
  114.         self.kde = kde
  115.  
  116.         gtk.main()
  117.     
  118.     
  119.     def setup_driver_list(self):
  120.         """
  121.         Sets up the driver list and list widget.
  122.         """
  123.  
  124.         # Initialize lists
  125.         self.driver_list = {}
  126.         self.driver_list_store = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
  127.         self.driver_list_widget = self.widgets.get_widget("driver_list")
  128.  
  129.         # Set up columns
  130.         first = gtk.TreeViewColumn("icon", gtk.CellRendererPixbuf(), pixbuf = 0)
  131.         second = gtk.TreeViewColumn("desc", gtk.CellRendererText(), markup = 1)
  132.  
  133.         self.driver_list_widget.append_column(first)
  134.         self.driver_list_widget.append_column(second)
  135.         
  136.         # Set list model for widget
  137.         self.driver_list_widget.set_model(self.driver_list_store)
  138.  
  139.         # Load the list of drivers
  140.         self.get_driver_list()
  141.  
  142.     def get_driver_list(self):
  143.         """
  144.         Gets the list of drivers from ndiswrapper.
  145.         """
  146.  
  147.         # Clear driver list
  148.         self.driver_list_store.clear()
  149.         self.driver_list = {}
  150.  
  151.         # Run the ndiswrapper list command
  152.         output = commands.getoutput("ndiswrapper -l")
  153.  
  154.         if "WARNING" in output:
  155.             error_dialog(_("Unable to see if hardware is present."), self.window)
  156.         
  157.         if "installed" in output:
  158.             # Drivers found
  159.             output = output.splitlines()
  160.             for i in range(1, len(output)):
  161.                 line = output[i]
  162.  
  163.                 if "hardware" in line: hardware_present = _("Yes")
  164.                 else: hardware_present = _("No")
  165.  
  166.                 # Get driver name
  167.                 p = re.compile(".*\\t")                        # match up to first tab
  168.                 driver_name = p.search(line).group()[:-1]    # strip trailing space
  169.                 
  170.                 # Add to list
  171.                 self.driver_list[i-1] = driver_name
  172.                 self.driver_list_store.append([self.wifi_icon,
  173.                     _("<b>%s</b>\nHardware present: %s") % (driver_name, hardware_present)])
  174.         else:
  175.             # No drivers installed
  176.             pass
  177.  
  178.     def drag_motion(self, window, context, x, y, time):
  179.         """
  180.         Called whenever a drag motion is made.
  181.         """
  182.  
  183.         context.drag_status(gtk.gdk.ACTION_COPY, time)
  184.         return True
  185.  
  186.     def drag_data_received(self, window, context, x, y, selection, info, timestamp):
  187.         """
  188.         Called when a file is dragged onto the main window.
  189.         """
  190.  
  191.         file = selection.get_text().strip()
  192.  
  193.         if file.startswith("file://"):
  194.             if file.endswith(".inf"):
  195.                 self.file_chooser.set_uri(file)
  196.                 self.install_driver_open()
  197.             else:
  198.                 error_dialog(_("Please drag an '.inf' file instead."), self.window)
  199.         
  200.         return True
  201.  
  202.     def show_help(self, *args):
  203.         """
  204.         Displays the help window.
  205.         Called when the 'Help' button is clicked.
  206.         """
  207.  
  208.         # TODO: Implement
  209.         error_dialog("Help", self.window)
  210.  
  211.     def config_network(self, *args):
  212.         """
  213.         Opens the network configuration tool.
  214.         """
  215.         
  216.         if self.kde:
  217.             os.system("kcmshell kcm_knetworkconfmodule &")
  218.         else:
  219.             os.system("network-admin &")
  220.  
  221.     def install_driver_open(self, *args):
  222.         """
  223.         Opens the install driver dialog.
  224.         """
  225.         
  226.         self.install_dialog.show()
  227.     
  228.     def install_dialog_close(self, *args):
  229.         """
  230.         Closes the install driver dialog.
  231.         """
  232.         
  233.         self.install_dialog.hide()
  234.         return True;
  235.  
  236.     def install_driver(self, *args):
  237.         """
  238.         Installs a selected wireless driver.
  239.         Called when the install dialog's 'Install Driver' button is clicked.
  240.         """
  241.         
  242.         inf_file = self.file_chooser.get_filename()
  243.  
  244.         if inf_file == None:
  245.             error_dialog(_("No file selected."), self.install_dialog)
  246.         elif not inf_file.lower().endswith(".inf"):
  247.             error_dialog(_("Not a valid driver .inf file."), self.install_dialog)
  248.         else:
  249.             # Attempt to install driver
  250.             output = commands.getoutput("ndiswrapper -i %s" % inf_file)
  251.             commands.getoutput("ndiswrapper -m")
  252.  
  253.             # Attempt to detect errors
  254.             if "already" in output:
  255.                 driver_name = output.split()[0]
  256.                 error_dialog(_("Driver <b>%s</b> is already installed.") % driver_name, self.install_dialog)
  257.             else:
  258.                 # Assume driver installed successfully. Set up and load module
  259.                 os.system("ndiswrapper -m")
  260.                 os.system("modprobe ndiswrapper")
  261.  
  262.                 self.get_driver_list()
  263.                 self.install_dialog_close()
  264.     
  265.     def remove_driver(self, *args):
  266.         """
  267.         Removes a driver after asking for confirmation.
  268.         Called when the 'Remove Driver' button is clicked.
  269.         """
  270.         
  271.         # Get the first selected driver
  272.         cursor = self.driver_list_widget.get_cursor()[0][0]
  273.         driver_name = self.driver_list[cursor]
  274.         
  275.         # Get confirmation
  276.         confirm = gtk.MessageDialog(type = gtk.MESSAGE_WARNING, buttons = gtk.BUTTONS_YES_NO)
  277.         confirm.set_markup(_("Are you sure you want to remove the <b>%s</b> driver?") % driver_name)
  278.         result = confirm.run()
  279.  
  280.         if result == gtk.RESPONSE_YES:
  281.             # Remove driver
  282.             output = commands.getoutput("ndiswrapper -e %s" % driver_name)
  283.  
  284.             # Reload driver list
  285.             self.get_driver_list()
  286.  
  287.         # Destroy the confirmation dialog
  288.         confirm.destroy()
  289.  
  290.     def cursor_changed(self, *args):
  291.         """
  292.         Called when the currently selected driver changes.
  293.         """
  294.  
  295.         # Allow the 'Remove Driver' button to be clicked
  296.         self.remove_driver.set_sensitive(True)
  297.  
  298.  
  299. if __name__ == '__main__':
  300.     # Check for root privileges
  301.     if os.getuid() != 0:
  302.         error_dialog(_("Root or sudo privileges required!"))
  303.         sys.exit(1)
  304.  
  305.     # Parse options and load GUI
  306.     if "--kde" in sys.argv:
  307.         NdisGTK(kde=True)
  308.     else:
  309.         NdisGTK()
  310.