| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import gtk |
|---|
| 25 | import nautilus |
|---|
| 26 | import os |
|---|
| 27 | import urllib |
|---|
| 28 | from subprocess import Popen, PIPE, STDOUT |
|---|
| 29 | |
|---|
| 30 | def normalize(path): |
|---|
| 31 | path = path.replace(" ", "\\ ") |
|---|
| 32 | path = path.replace("(", "\\(") |
|---|
| 33 | path = path.replace(")", "\\)") |
|---|
| 34 | path = path.replace("'", "\\'") |
|---|
| 35 | path = path.replace("&", "\\&") |
|---|
| 36 | return path |
|---|
| 37 | |
|---|
| 38 | def log(txt): |
|---|
| 39 | return |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | def error_msg(txt): |
|---|
| 45 | d = gtk.MessageDialog(None, |
|---|
| 46 | gtk.DIALOG_MODAL | |
|---|
| 47 | gtk.DIALOG_DESTROY_WITH_PARENT, |
|---|
| 48 | gtk.MESSAGE_WARNING, |
|---|
| 49 | gtk.BUTTONS_OK, |
|---|
| 50 | txt) |
|---|
| 51 | d.run() |
|---|
| 52 | d.destroy() |
|---|
| 53 | return |
|---|
| 54 | |
|---|
| 55 | def info_msg(txt): |
|---|
| 56 | d = gtk.MessageDialog(None, |
|---|
| 57 | gtk.DIALOG_MODAL | |
|---|
| 58 | gtk.DIALOG_DESTROY_WITH_PARENT, |
|---|
| 59 | gtk.MESSAGE_INFO, |
|---|
| 60 | gtk.BUTTONS_OK, |
|---|
| 61 | txt) |
|---|
| 62 | d.run() |
|---|
| 63 | d.destroy() |
|---|
| 64 | return |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | class MultiseatUmountExtension(nautilus.MenuProvider): |
|---|
| 68 | def __init__(self): |
|---|
| 69 | self.file_names = [] |
|---|
| 70 | |
|---|
| 71 | def menu_activate_umount_multiseat(self, menu, data): |
|---|
| 72 | log("umount selected") |
|---|
| 73 | log(data) |
|---|
| 74 | umounted=False |
|---|
| 75 | |
|---|
| 76 | found=False |
|---|
| 77 | mountline='' |
|---|
| 78 | f=open('/proc/mounts', 'r') |
|---|
| 79 | for line in f.readlines(): |
|---|
| 80 | if line.startswith(data['Dev']): |
|---|
| 81 | mountline=line |
|---|
| 82 | found=True |
|---|
| 83 | log(line) |
|---|
| 84 | f.close() |
|---|
| 85 | if not found: |
|---|
| 86 | log("dispositivo no montado") |
|---|
| 87 | return error_msg("El dispositivo no está montado") |
|---|
| 88 | |
|---|
| 89 | cmd="/sbin/umount.multiseat %s"%data['Dev'] |
|---|
| 90 | p=Popen(cmd, shell=True, bufsize=0, |
|---|
| 91 | stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 92 | for _line in p.stdout.readlines(): |
|---|
| 93 | line=_line.strip() |
|---|
| 94 | log("umount.multiseat called, line=%s"%line) |
|---|
| 95 | if line == "no-mounted": |
|---|
| 96 | return error_msg("El dispositivo no está montado.") |
|---|
| 97 | elif line == "invalid-user" or \ |
|---|
| 98 | line.strip() == "not-yours" or \ |
|---|
| 99 | line.strip() == "no-uid": |
|---|
| 100 | return error_msg("El usuario no tiene permiso para desmontar ese dispositivo.") |
|---|
| 101 | elif line == "no-serial": |
|---|
| 102 | return error_msg("No se pudo leer el número de serie del dispositivo.") |
|---|
| 103 | elif line == "ok": |
|---|
| 104 | umounted=True |
|---|
| 105 | else: |
|---|
| 106 | return error_msg("Error desconocido:\n\n%s"%line) |
|---|
| 107 | |
|---|
| 108 | log("desmontaje correcto") |
|---|
| 109 | if umounted: |
|---|
| 110 | return info_msg("Dispositivo desmontado y listo para extraer.") |
|---|
| 111 | |
|---|
| 112 | def get_file_items(self, window, files): |
|---|
| 113 | """Called when the user selects a file in Nautilus.""" |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | if len(files) != 1: |
|---|
| 117 | return |
|---|
| 118 | |
|---|
| 119 | f = files[0] |
|---|
| 120 | log(f.get_uri()) |
|---|
| 121 | filepath = normalize(urllib.unquote(f.get_uri()[7:])) |
|---|
| 122 | name = os.path.basename(filepath) |
|---|
| 123 | |
|---|
| 124 | log(filepath) |
|---|
| 125 | log(name) |
|---|
| 126 | if not ".desktop" in name: |
|---|
| 127 | log("no .desktop file") |
|---|
| 128 | return |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | f = open(filepath, 'r') |
|---|
| 133 | data={} |
|---|
| 134 | for line in f.readlines(): |
|---|
| 135 | if "=" in line: |
|---|
| 136 | data[line.split('=')[0]]=line.strip().split('=')[1] |
|---|
| 137 | f.close() |
|---|
| 138 | log(data) |
|---|
| 139 | |
|---|
| 140 | if not data.has_key('X-multiseat-desktop'): |
|---|
| 141 | log("no key X-multiseat-desktop") |
|---|
| 142 | return |
|---|
| 143 | |
|---|
| 144 | umount_item = nautilus.MenuItem("NautilusPython::umount_multiseat_item", |
|---|
| 145 | "Desmontar dispositivo extraÃble multiseat", |
|---|
| 146 | "Desmontar dispositivo extraÃble multiseat", |
|---|
| 147 | "nautilus-mount-image") |
|---|
| 148 | |
|---|
| 149 | if os.path.isfile('/usr/share/icons/maxtoon/16x16/devices/usbpendrive_unmount.png'): |
|---|
| 150 | umount_item.set_property('icon', '/usr/share/icons/maxtoon/16x16/devices/usbpendrive_unmount.png') |
|---|
| 151 | elif os.path.isfile('/usr/share/icons/gnome/16x16/devices/usbpendrive_unmount.png'): |
|---|
| 152 | umount_item.set_property('icon', '/usr/share/icons/gnome/16x16/devices/usbpendrive_unmount.png') |
|---|
| 153 | else: |
|---|
| 154 | umount_item.set_property('icon', 'gtk-dialog-warning') |
|---|
| 155 | |
|---|
| 156 | umount_item.connect("activate", self.menu_activate_umount_multiseat, data) |
|---|
| 157 | log("found X-multiseat-desktop in %s"%filepath) |
|---|
| 158 | return umount_item, |
|---|
| 159 | |
|---|