1 | import urllib |
---|
2 | import gi |
---|
3 | gi.require("Caja","2.0") |
---|
4 | from gi.repository import Caja,GObject |
---|
5 | import pwd |
---|
6 | import getpass |
---|
7 | import grp |
---|
8 | import subprocess |
---|
9 | import os |
---|
10 | import os.path |
---|
11 | |
---|
12 | import locale |
---|
13 | import gettext |
---|
14 | |
---|
15 | #locale.textdomain("lliurex-homework-harvester") |
---|
16 | #gettext.textdomain("lliurex-homework-harvester") |
---|
17 | #_=locale.gettext |
---|
18 | |
---|
19 | def get_translation(line): |
---|
20 | |
---|
21 | try: |
---|
22 | |
---|
23 | trans={} |
---|
24 | trans["Set this folder to receive homeworks"]={} |
---|
25 | trans["Set this folder to receive homeworks"]["es"]="Marcar esta carpeta para recibir trabajos" |
---|
26 | trans["Set this folder to receive homeworks"]["qcv"]="Establix esta carpeta per a rebre els treballs" |
---|
27 | trans["Send file to teacher"]={} |
---|
28 | trans["Send file to teacher"]["es"]="Enviar fichero a profesor/a" |
---|
29 | trans["Send file to teacher"]["qcv"]="Envia el fitxer al professor o professor/a" |
---|
30 | |
---|
31 | lng=os.getenv("LANG") |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | if lng.find("ca")==0 or lng.find("qcv")==0: |
---|
36 | try: |
---|
37 | ret_line=trans[line]["qcv"] |
---|
38 | return ret_line |
---|
39 | except: |
---|
40 | return line |
---|
41 | |
---|
42 | if lng.find("es")==0: |
---|
43 | try: |
---|
44 | ret_line=trans[line]["es"] |
---|
45 | return ret_line |
---|
46 | except: |
---|
47 | return line |
---|
48 | else: |
---|
49 | return line |
---|
50 | |
---|
51 | except: |
---|
52 | return line |
---|
53 | |
---|
54 | |
---|
55 | #def get_translation |
---|
56 | |
---|
57 | |
---|
58 | _=get_translation |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | class LliurexHomeworkExtension(GObject.GObject,Caja.MenuProvider): |
---|
65 | |
---|
66 | def __init__(self): |
---|
67 | |
---|
68 | self.file_list=[] |
---|
69 | |
---|
70 | #def init |
---|
71 | |
---|
72 | def get_groups(self,user): |
---|
73 | |
---|
74 | groups=[] |
---|
75 | for g in grp.getgrall(): |
---|
76 | if user in g.gr_mem: |
---|
77 | groups.append(g.gr_name) |
---|
78 | return groups |
---|
79 | |
---|
80 | |
---|
81 | #def get_groups |
---|
82 | |
---|
83 | def check_ownership(self,user,path): |
---|
84 | |
---|
85 | info=os.stat(path) |
---|
86 | |
---|
87 | path_user_owner=info[4] |
---|
88 | path_group_owner=info[5] |
---|
89 | |
---|
90 | info=pwd.getpwnam(user) |
---|
91 | |
---|
92 | user_uid=info[2] |
---|
93 | group_uid=info[3] |
---|
94 | |
---|
95 | if path_user_owner==user_uid or path_group_owner==group_uid: |
---|
96 | return True |
---|
97 | else: |
---|
98 | return False |
---|
99 | |
---|
100 | #def check_ownership |
---|
101 | |
---|
102 | |
---|
103 | def teachers_cb(self,menu,path): |
---|
104 | |
---|
105 | command=[] |
---|
106 | command.append("/usr/bin/lliurex-homework-harvester") |
---|
107 | command.append(path) |
---|
108 | subprocess.Popen(command,stdout=subprocess.PIPE) |
---|
109 | |
---|
110 | #def teachers_cb |
---|
111 | |
---|
112 | def students_cb(self,menu,files): |
---|
113 | |
---|
114 | command=[] |
---|
115 | command.append("/usr/bin/lliurex-homework-sender") |
---|
116 | for file in files: |
---|
117 | command.append(file) |
---|
118 | subprocess.Popen(command,stdout=subprocess.PIPE) |
---|
119 | |
---|
120 | #def students_cb |
---|
121 | |
---|
122 | def get_file_items(self, window, files): |
---|
123 | |
---|
124 | file_list=[] |
---|
125 | for file in files: |
---|
126 | filename = urllib.unquote(file.get_uri()[7:]) |
---|
127 | file_list.append(str(filename)) |
---|
128 | |
---|
129 | user=getpass.getuser() |
---|
130 | group_list=self.get_groups(user) |
---|
131 | |
---|
132 | if "teachers" in group_list: |
---|
133 | |
---|
134 | #one item only |
---|
135 | if len(file_list)>1 or len(file_list)==0: |
---|
136 | return |
---|
137 | #folders only |
---|
138 | if not os.path.isdir(file_list[0]): |
---|
139 | return |
---|
140 | |
---|
141 | if not self.check_ownership(user,file_list[0]): |
---|
142 | return |
---|
143 | |
---|
144 | |
---|
145 | item = Caja.MenuItem(name='Caja::set_harverst_folder', |
---|
146 | label=_('Set this folder to receive homeworks')+"...", |
---|
147 | tip=_('Set this folder to receive homeworks'), |
---|
148 | icon='go-jump') |
---|
149 | item.connect('activate', self.teachers_cb, file_list[0]) |
---|
150 | |
---|
151 | |
---|
152 | return item, |
---|
153 | |
---|
154 | elif "students" in group_list: |
---|
155 | #STUDENT SIDE |
---|
156 | #files only |
---|
157 | |
---|
158 | for item in file_list: |
---|
159 | if not os.path.isfile(item): |
---|
160 | return |
---|
161 | |
---|
162 | |
---|
163 | item = Caja.MenuItem(name='Caja::send_homework', |
---|
164 | label=_('Send file to teacher')+"...", |
---|
165 | tip=_('Send file to teacher'), |
---|
166 | icon='go-jump') |
---|
167 | item.connect('activate', self.students_cb, file_list) |
---|
168 | return item, |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | else: |
---|
173 | return |
---|
174 | |
---|
175 | |
---|
176 | |
---|