root/trunk/max-multiseat-storage/nautilus-umount-multiseat.py

Revision 870, 5.6 KB (checked in by mario.izquierdo, 16 months ago)

max-multiseat-storage (6.0.max6)

  • multiseat-udisks.py:
    • getSeatID() search busnum/devnum path from (end -8 folders) instead of first 7.
    • getSeatID() if devname is disk without partitions use '-7'
  • Add pylintrc and check/fix/clean the 2 python files
  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2# -*- coding: UTF-8 -*-
3##########################################################################
4#
5# Multiseat Nautilus umount USB storage devices
6# Copyright 2011, Mario Izquierdo, mariodebian at gmail
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2, or (at your option)
11# any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21# 02111-1307, USA.
22###########################################################################
23
24import gtk
25import nautilus
26import os
27import urllib
28from subprocess import Popen, PIPE, STDOUT
29
30def 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
38def log(txt):
39    return
40    #f = open('/tmp/nautilus-umount-multiseat.log', 'a')
41    #f.write(str(txt) + "\n")
42    #f.close()
43
44def 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
55def 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
67class 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        # read /proc/mounts for device
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        # umount.multiseat a C app with bit SUID
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        # show a message if ok (umount OK)
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        # This script will only accept one file
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        # read desktop file and search
131        # X-multiseat-desktop=true
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
Note: See TracBrowser for help on using the browser.