File: ndisgtk

package info (click to toggle)
ndisgtk 0.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 372 kB
  • ctags: 34
  • sloc: python: 215; makefile: 87
file content (404 lines) | stat: -rwxr-xr-x 12,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#                                                                             #
# ndisgtk - A GTK frontend for the ndiswrapper wireless driver tool           #
#                                                                             #
# Copyright (C) 2005, Sam Pohlenz <retrix@internode.on.net>                   #
# Copyright (C) 2007-2008 Julian Andres Klode <jak@jak-linux.org>             #
#                                                                             #
# 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 2              #
# 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, write to the Free Software                 #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. #
#                                                                             #
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

import sys
import os
import subprocess
import re


def getoutput(*cmd):
	'''Like commands.getoutput, but uses subprocess'''
	myproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	value  =  myproc.stdout.read()
	retcode = myproc.wait()
	return value, retcode

# Attempt to load GTK bindings
try:
	import pygtk
	pygtk.require("2.0")
	import gtk
	import gtk.glade
	import gobject
except ImportError:
	print "Failed to load GTK bindings. Please check your Gnome installation."
	sys.exit(1)

# Internationalization
import locale
import gettext
locale.setlocale(locale.LC_ALL, "")
gettext.bindtextdomain("ndisgtk", "/usr/share/locale")
gettext.textdomain("ndisgtk")
gettext.install("ndisgtk", "/usr/share/locale", unicode=1)
gtk.glade.bindtextdomain("ndisgtk", "/usr/share/locale")
gtk.glade.textdomain("ndisgtk")

# Data directory
DATA_DIR = "/usr/share/ndisgtk"

def error_dialog(message, parent = None):
	"""
	Displays an error message.
	"""

	dialog = gtk.MessageDialog(parent = parent, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, flags = gtk.DIALOG_MODAL)
	dialog.set_markup(message)

	result = dialog.run()
	dialog.destroy()

class NdisGTK:
	"""
	Main application class.
	"""

	def __init__(self, kde=False):
		"""
		Initializes the application.
		"""

		# Setup glade and signals
		self.signals = { "gtk_main_quit": gtk.main_quit,
						 "on_install_driver_clicked": self.install_driver_open,
						 "on_remove_driver_clicked": self.remove_driver,
						 "on_driver_list_cursor_changed": self.cursor_changed,
						 "install_dialog_close": self.install_dialog_close,
						 "install_button_clicked": self.install_driver,
						 "network_button_clicked": self.config_network,
						 "help_button_clicked": self.show_help,
						 "drag_motion": self.drag_motion,
						 "drag_data_received": self.drag_data_received }

		self.widgets = gtk.glade.XML(DATA_DIR + "/ndisgtk.glade", domain="ndisgtk")
		self.widgets.signal_autoconnect(self.signals)

		# Get handle to window
		self.window = self.widgets.get_widget("ndiswrapper_main")

		# Load icon
		icon_theme = gtk.icon_theme_get_default()
		self.wifi_icon = icon_theme.load_icon('ndisgtk', 48, 0)
		self.wifi_error_icon = icon_theme.load_icon('ndisgtk-error', 48, 0)
		self.window.set_icon(self.wifi_icon)

		# Get handle to 'Remove Driver' button
		self.remove_driver = self.widgets.get_widget("remove_driver")

		# Get handle to 'Install Driver' dialog
		self.install_dialog = self.widgets.get_widget("install_dialog")
		self.install_dialog.set_transient_for(self.window)

		# Get handle to file chooser
		self.file_chooser = self.widgets.get_widget("filechooser")

		# Enable drag-and-drop
		self.window.drag_dest_set(gtk.DEST_DEFAULT_DROP, [("text/plain", 0, 80)], gtk.gdk.ACTION_COPY)

		# Setup driver list
		self.setup_driver_list()

		# Use KDE network admin?
		self.kde = kde

		gtk.main()

	def setup_driver_list(self):
		"""
		Sets up the driver list and list widget.
		"""

		# Initialize lists
		self.driver_list = []
		self.driver_list_store = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
		self.driver_list_widget = self.widgets.get_widget("driver_list")

		# Set up columns
		first = gtk.TreeViewColumn("icon", gtk.CellRendererPixbuf(), pixbuf = 0)
		second = gtk.TreeViewColumn("desc", gtk.CellRendererText(), markup = 1)

		self.driver_list_widget.append_column(first)
		self.driver_list_widget.append_column(second)

		# Set list model for widget
		self.driver_list_widget.set_model(self.driver_list_store)

		# Load the list of drivers
		self.get_driver_list()

	def get_driver_list(self):
		"""
		Gets the list of drivers from ndiswrapper.
		"""

		# Clear driver list
		self.driver_list_store.clear()
		self.driver_list = []

		# Run the ndiswrapper list command
		output,retcode = getoutput("ndiswrapper", "-l")

		# Ignore warnings from modprobe.
		output_lines = []
		for line in output.splitlines():
			if not line.startswith("WARNING: All config files need .conf"):
				output_lines.append(line)
		output = '\n'.join(output_lines)

		if "WARNING" in output:
			error_dialog(_("Unable to see if hardware is present."), self.window)

		if ": driver" in output or ": invalid" in output:
			# Newer ndiswrapper versions
			# Drivers found
			output = output.splitlines()

			for i in range(0, len(output)):
				line = output[i]
				if len(output) > i+1: line2 = output[i+1]
				else: line2=""

				if "present" in line2: hardware_present = _("Yes")
				else: hardware_present = _("No")

				# Get driver name
				p = re.compile(".*:")						# match up to first tab
				driver_name = p.search(line).group()[:-1].strip()	# strip trailing space

				# Add to list
				if "installed" in line:
					self.driver_list.append(driver_name)
					self.driver_list_store.append([self.wifi_icon,
						_("<b>%s</b>\nHardware present: %s") % (driver_name, hardware_present)])
				elif "invalid" in line:
					self.driver_list.append(driver_name)
					self.driver_list_store.append([self.wifi_error_icon,
						_("<b>%s</b>\nInvalid Driver!") % driver_name])

		elif "installed" in output or "Installed" in output or "invalid" in output:
			# Drivers found
			output = output.splitlines()
			for i in range(1, len(output)):
				line = output[i]

				if "hardware" in line: hardware_present = _("Yes")
				else: hardware_present = _("No")

				# Get driver name
				p = re.compile(".*\\t")						# match up to first tab
				driver_name = p.search(line).group()[:-1].strip()	# strip trailing space

				# Add to list

				if "installed" in line:
					self.driver_list.append(driver_name)
					self.driver_list_store.append([self.wifi_icon,
						_("<b>%s</b>\nHardware present: %s") % (driver_name, hardware_present)])
				elif "invalid" in line:
					self.driver_list.append(driver_name)
					self.driver_list_store.append([self.wifi_icon,
						_("<b>%s</b>\nInvalid Driver!") % driver_name])
		else:
			# No drivers installed
			pass

	def drag_motion(self, window, context, x, y, time):
		"""
		Called whenever a drag motion is made.
		"""

		context.drag_status(gtk.gdk.ACTION_COPY, time)
		return True

	def drag_data_received(self, window, context, x, y, selection, info, timestamp):
		"""
		Called when a file is dragged onto the main window.
		"""

		file = selection.get_text().strip()

		if file.startswith("file://"):
			if file.endswith(".inf"):
				self.file_chooser.set_uri(file)
				self.install_driver_open()
			else:
				error_dialog(_("Please drag an '.inf' file instead."), self.window)

		return True

	def show_help(self, *args):
		"""
		Displays the help window.
		Called when the 'Help' button is clicked.
		"""

		# TODO: Implement
		error_dialog("Help", self.window)

	def config_network(self, *args):
		"""
		Opens the network configuration tool.
		"""
		# Run the command under the normal user.
		uid = int(os.getenv("SUDO_UID", 0))
		if uid == 0:
			import pwd
			user = os.getenv("USERNAME", pwd.getpwuid(0).pw_name)
			uid = pwd.getpwnam(user).pw_uid

		if "XAUTHORITY" in os.environ:
			# Create a new Xauthority for running the configuration tool.
			import shutil, tempfile
			(xauth_fd, xauth_name) = tempfile.mkstemp('.ndisgtk', 'xauth-')
			os.close(xauth_fd)
			shutil.copyfile(os.getenv("XAUTHORITY"), xauth_name)
			os.chown(xauth_name, uid, -1)

		fork = os.fork()
		if fork == 0:
			# Set the new Xauthority, switch user and run the command
			os.environ["XAUTHORITY"] = xauth_name
			os.setuid(uid)
			try:
				if self.kde:
					subprocess.call(["kcmshell", "kcm_knetworkconfmodule"])
				else:
					subprocess.call(["nm-connection-editor"])				
			except Exception, e:
				try:
					os._exit(e.errno)
				except AttributeError:
					os._exit(1)
			os._exit(0)

		gobject.child_watch_add(fork, self.on_config_network_exited,
		                        xauth_name)
			
	def on_config_network_exited(self, pid, status, xauth_name=None):
		"""Called when the network configuration has been called"""
		errno = os.WEXITSTATUS(status)
		if errno == 2:
			error_dialog(_("Could not find a network configuration tool."))
		elif errno:
			print >> sys.stderr, "Configuration failed:", os.strerror(errno)
		os.unlink(xauth_name)

	def install_driver_open(self, *args):
		"""
		Opens the install driver dialog.
		"""

		self.install_dialog.show()

	def install_dialog_close(self, *args):
		"""
		Closes the install driver dialog.
		"""

		self.install_dialog.hide()
		return True;

	def install_driver(self, *args):
		"""
		Installs a selected wireless driver.
		Called when the install dialog's 'Install Driver' button is clicked.
		"""

		inf_file = self.file_chooser.get_filename()

		if inf_file == None:
			error_dialog(_("No file selected."), self.install_dialog)
		elif not inf_file.lower().endswith(".inf"):
			error_dialog(_("Not a valid driver .inf file."), self.install_dialog)
		else:
			# Attempt to install driver
			output, retcode = getoutput("ndiswrapper", "-i", inf_file)

			# Attempt to detect errors
			if "already" in output:
				#driver_name = output.split()[0]
				error_dialog(_("Driver is already installed."), self.install_dialog)
			elif retcode != 0:
				error_dialog(_("Error while installing.") , self.install_dialog)
			else:
				# Assume driver installed successfully. Set up and reload module
				subprocess.call(["ndiswrapper",  "-ma"])
				subprocess.call(["modprobe", "-r", "ndiswrapper"])
				out, ret = getoutput("modprobe", "ndiswrapper")
				if ret != 0:
					error_text = _("Module could not be loaded. Error was:\n\n<i>%s</i>\n")
					if "not found" in out:
						error_text += _("Is the ndiswrapper module installed?")
					error_dialog(error_text % out, self.install_dialog)

				self.get_driver_list()
				self.install_dialog_close()

	def remove_driver(self, *args):
		"""
		Removes a driver after asking for confirmation.
		Called when the 'Remove Driver' button is clicked.
		"""

		# Get the first selected driver
		cursor = self.driver_list_widget.get_cursor()[0][0]
		driver_name = self.driver_list[cursor]

		# Get confirmation
		confirm = gtk.MessageDialog(type = gtk.MESSAGE_WARNING, buttons = gtk.BUTTONS_YES_NO)
		confirm.set_markup(_("Are you sure you want to remove the <b>%s</b> driver?") % driver_name)
		result = confirm.run()

		if result == gtk.RESPONSE_YES:
			# Remove driver
			output,retcode = getoutput("ndiswrapper", "-e", driver_name)

			# Reload driver list
			self.get_driver_list()

		# Destroy the confirmation dialog
		confirm.destroy()

	def cursor_changed(self, *args):
		"""
		Called when the currently selected driver changes.
		"""

		# Allow the 'Remove Driver' button to be clicked
		self.remove_driver.set_sensitive(True)


if __name__ == '__main__':
	# Check for root privileges
	if os.getuid() != 0:
		error_dialog(_("Root or sudo privileges required!"))
		sys.exit(1)

	# Parse options and load GUI
	if "--kde" in sys.argv:
		NdisGTK(kde=True)
	else:
		NdisGTK()