1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import gi |
---|
4 | gi.require_version('Gtk', '3.0') |
---|
5 | from gi.repository import Gtk, Pango, GdkPixbuf, Gdk, Gio, GObject, GLib |
---|
6 | gi.require_version('AppIndicator3', '0.1') |
---|
7 | from gi.repository import AppIndicator3 as appindicator |
---|
8 | |
---|
9 | import os |
---|
10 | from os import listdir |
---|
11 | from os.path import isfile, isfile,isdir,join |
---|
12 | import threading |
---|
13 | import subprocess |
---|
14 | import sys |
---|
15 | import gettext |
---|
16 | import json |
---|
17 | import random |
---|
18 | import notify2 |
---|
19 | import time |
---|
20 | import datetime |
---|
21 | import copy |
---|
22 | |
---|
23 | |
---|
24 | import pyinotify |
---|
25 | from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent |
---|
26 | |
---|
27 | import signal |
---|
28 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
29 | |
---|
30 | import lliurex.lliurexgdrive |
---|
31 | |
---|
32 | import gettext |
---|
33 | gettext.textdomain('lliurex-gdrive') |
---|
34 | _ = gettext.gettext |
---|
35 | |
---|
36 | LOCK_INDICATOR="~/.config/lliurex-google-drive-profiles/lgdIndicator.lock" |
---|
37 | LOCK_GUI="~/.config/lliurex-google-drive-profiles/lgdGUI.lock" |
---|
38 | RSRC="/usr/share/lliurex-gdrive/" |
---|
39 | MOUNT_ON_IMAGE=RSRC+"rsrc/mount_on.png" |
---|
40 | MOUNT_OFF_IMAGE=RSRC+"rsrc/mount_off.png" |
---|
41 | MOUNT_WARNING_IMAGE=RSRC+"rsrc/mount_warning.png" |
---|
42 | SP1=RSRC+"rsrc/sp1.png" |
---|
43 | SP2=RSRC+"rsrc/sp2.png" |
---|
44 | SP3=RSRC+"rsrc/sp3.png" |
---|
45 | SP4=RSRC+"rsrc/sp4.png" |
---|
46 | SP5=RSRC+"rsrc/sp5.png" |
---|
47 | SP6=RSRC+"rsrc/sp6.png" |
---|
48 | SP7=RSRC+"rsrc/sp7.png" |
---|
49 | SP8=RSRC+"rsrc/sp8.png" |
---|
50 | |
---|
51 | TIME_CHECK_STATUS=900000 |
---|
52 | |
---|
53 | class LliurexGdriveIndicator: |
---|
54 | |
---|
55 | WATCH_DIR=os.path.expanduser("~/.config/lliurex-google-drive-profiles/") |
---|
56 | |
---|
57 | def __init__(self,icon_name): |
---|
58 | |
---|
59 | self.lock_gui=os.path.expanduser(LOCK_GUI) |
---|
60 | self.lock_indicator=os.path.expanduser(LOCK_INDICATOR) |
---|
61 | |
---|
62 | self.LliurexGoogleDriveManager=lliurex.lliurexgdrive.LliurexGoogleDriveManager() |
---|
63 | self.app_indicator=appindicator.Indicator.new("lliurex-gdrive",icon_name,appindicator.IndicatorCategory.APPLICATION_STATUS) |
---|
64 | self.app_indicator.set_status(appindicator.IndicatorStatus.ACTIVE) |
---|
65 | self.menu = Gtk.Menu() |
---|
66 | |
---|
67 | self.menu.add_events(Gdk.EventMask.ALL_EVENTS_MASK) |
---|
68 | self.app_indicator.set_menu(self.menu) |
---|
69 | |
---|
70 | self.profiles_info=self.LliurexGoogleDriveManager.profiles_config.copy() |
---|
71 | self.current_status={} |
---|
72 | self.blacklist=['.Trash','.Trash-1000'] |
---|
73 | self.init_files=[] |
---|
74 | self.update_files=[] |
---|
75 | self.local_changes={} |
---|
76 | self.notifications_list=[] |
---|
77 | |
---|
78 | |
---|
79 | self.createLockToken() |
---|
80 | self.check_initial_connection() |
---|
81 | self.populate_menu() |
---|
82 | self.start_inotify() |
---|
83 | self.sync_threads={} |
---|
84 | self.result_sync_threads={} |
---|
85 | self.profile_clicked=[] |
---|
86 | self.sp_cont={} |
---|
87 | self.sp_img={} |
---|
88 | self.checking_status=False |
---|
89 | |
---|
90 | |
---|
91 | GLib.timeout_add(TIME_CHECK_STATUS,self.check_status) |
---|
92 | |
---|
93 | #def __init__ |
---|
94 | |
---|
95 | def check_initial_connection(self): |
---|
96 | |
---|
97 | self.initial_connection=self.LliurexGoogleDriveManager.check_google_connection() |
---|
98 | return |
---|
99 | |
---|
100 | #def check_initial_connection |
---|
101 | |
---|
102 | def generate_sync_threads(self,profile): |
---|
103 | |
---|
104 | id=int(random.random()*1000) |
---|
105 | t=threading.Thread(target=self.check_sync,args=(id,profile)) |
---|
106 | t.daemon=True |
---|
107 | t.start() |
---|
108 | self.sync_threads[id]={} |
---|
109 | self.sync_threads[id]["thread"]=t |
---|
110 | self.result_sync_threads[id]={} |
---|
111 | return id |
---|
112 | |
---|
113 | #def generate_sync_threads |
---|
114 | |
---|
115 | def init_connect_thread(self): |
---|
116 | |
---|
117 | self.check_connect_t=threading.Thread(target=self.check_connect) |
---|
118 | self.check_connect_t.daemon=True |
---|
119 | self.check_connect_t.start() |
---|
120 | |
---|
121 | #def init_connect_thread |
---|
122 | |
---|
123 | def createLockToken(self): |
---|
124 | |
---|
125 | #self.lockpath=os.path(LOCK_INDICATOR) |
---|
126 | if not os.path.exists(self.lock_indicator): |
---|
127 | f=open(self.lock_indicator,'w') |
---|
128 | f.close |
---|
129 | |
---|
130 | #def createLockToken |
---|
131 | |
---|
132 | def start_inotify(self): |
---|
133 | |
---|
134 | inotify_t=threading.Thread(target=self._inotify) |
---|
135 | inotify_t.daemon=True |
---|
136 | inotify_t.start() |
---|
137 | |
---|
138 | #def start_inotif |
---|
139 | |
---|
140 | |
---|
141 | def _inotify(self): |
---|
142 | |
---|
143 | |
---|
144 | wm=WatchManager() |
---|
145 | mask= pyinotify.IN_CLOSE_WRITE |
---|
146 | |
---|
147 | |
---|
148 | class Process_handler(ProcessEvent): |
---|
149 | |
---|
150 | def __init__(self,main): |
---|
151 | |
---|
152 | self.main=main |
---|
153 | |
---|
154 | |
---|
155 | def process_IN_CLOSE_WRITE(self,event): |
---|
156 | |
---|
157 | self.main.update_info() |
---|
158 | self.main.initial_connection=True |
---|
159 | |
---|
160 | if os.path.expanduser(event.pathname)==os.path.join(LliurexGdriveIndicator.WATCH_DIR,"configProfiles"): |
---|
161 | |
---|
162 | for profile in self.main.update_profiles: |
---|
163 | mountpoint_new=self.main.update_profiles[profile]["mountpoint"] |
---|
164 | gdriveFolder_new=self.main.update_profiles[profile]["gdrive_folder"] |
---|
165 | if not profile in self.main.profiles_info: |
---|
166 | self.main.add_menu_item(profile,mountpoint_new) |
---|
167 | wdd=wm.add_watch(mountpoint_new,mask,rec=True) |
---|
168 | else: |
---|
169 | mountpoint_old=self.main.profiles_info[profile]["mountpoint"] |
---|
170 | gdriveFolder_old=self.main.profiles_info[profile]["gdrive_folder"] |
---|
171 | if (mountpoint_new != mountpoint_old) or (gdriveFolder_new != gdriveFolder_old): |
---|
172 | ''' |
---|
173 | try: |
---|
174 | ide=wm.get_wd(mountpoint_old) |
---|
175 | if id !=None: |
---|
176 | wdd=wm.rm_watch(ide,rec=False) |
---|
177 | except Exception as e: |
---|
178 | print "ERROR" |
---|
179 | print str(e) |
---|
180 | pass |
---|
181 | ''' |
---|
182 | self.main.remove_folder_info(profile) |
---|
183 | self.main.init_folders_info(profile,mountpoint_new) |
---|
184 | wdd=wm.add_watch(mountpoint_new,mask,rec=True) |
---|
185 | |
---|
186 | profile_rm="" |
---|
187 | for item in self.main.menu.get_children(): |
---|
188 | if item.profile !="": |
---|
189 | if not item.profile in self.main.update_profiles: |
---|
190 | self.main.menu.remove(item) |
---|
191 | profile_rm=item.profile |
---|
192 | |
---|
193 | if profile_rm !="": |
---|
194 | mountpoint=self.main.profiles_info[profile_rm]["mountpoint"] |
---|
195 | ''' |
---|
196 | try: |
---|
197 | ide=wm.get_wd(mountpoint) |
---|
198 | wdd=wm.rm_watch(ide,rec=False) |
---|
199 | except Exception as e: |
---|
200 | print str(e) |
---|
201 | pass |
---|
202 | ''' |
---|
203 | |
---|
204 | self.main.remove_folder_info(profile_rm) |
---|
205 | |
---|
206 | |
---|
207 | self.main.profiles_info=self.main.update_profiles.copy() |
---|
208 | self.main.menu.show_all() |
---|
209 | |
---|
210 | else: |
---|
211 | if 'ountToken' in os.path.basename(event.pathname): |
---|
212 | tmp_profile=os.path.basename(event.pathname).split("__")[0] |
---|
213 | tmp_profile=tmp_profile.decode("utf-8") |
---|
214 | for item in self.main.profiles_info: |
---|
215 | if item==tmp_profile: |
---|
216 | tmp_mountpoint=self.main.profiles_info[item]["mountpoint"] |
---|
217 | ''' |
---|
218 | try: |
---|
219 | ide=wm.get_wd(tmp_mountpoint) |
---|
220 | if id !=None: |
---|
221 | wdd=wm.rm_watch(ide,rec=False) |
---|
222 | except Exception as e: |
---|
223 | print str(e) |
---|
224 | pass |
---|
225 | ''' |
---|
226 | self.main.remove_folder_info(item) |
---|
227 | if 'MountToken' in os.path.basename(event.pathname): |
---|
228 | self.main.init_folders_info(item,tmp_mountpoint) |
---|
229 | wdd=wm.add_watch(tmp_mountpoint,mask,rec=True) |
---|
230 | |
---|
231 | else: |
---|
232 | time.sleep(2) |
---|
233 | t=join(event.path,event.name) |
---|
234 | if isfile(t): |
---|
235 | |
---|
236 | ''' |
---|
237 | for item in self.main.profiles_info: |
---|
238 | tmp_mount=self.main.profiles_info[item]["mountpoint"] |
---|
239 | |
---|
240 | if event.path in tmp_mount: |
---|
241 | cmd="google-drive-ocamlfuse -cc -label %s"%item |
---|
242 | os.system(cmd) |
---|
243 | ''' |
---|
244 | |
---|
245 | try: |
---|
246 | |
---|
247 | state=os.stat(t) |
---|
248 | |
---|
249 | except Exception as e: |
---|
250 | print str(e) |
---|
251 | t=t.split('trashinfo') |
---|
252 | t=t[0]+'trashinfo' |
---|
253 | state=os.stat(t) |
---|
254 | |
---|
255 | t=t.decode("utf-8") |
---|
256 | access=datetime.datetime.fromtimestamp(state.st_mtime) |
---|
257 | self.main.local_changes[t]=access |
---|
258 | |
---|
259 | |
---|
260 | notifier=Notifier(wm,Process_handler(self)) |
---|
261 | wdd=wm.add_watch(LliurexGdriveIndicator.WATCH_DIR,mask,rec=True) |
---|
262 | |
---|
263 | for profile in self.profiles_info: |
---|
264 | path=os.path.expanduser(self.profiles_info[profile]["mountpoint"]) |
---|
265 | wdd=wm.add_watch(path,mask,rec=True) |
---|
266 | |
---|
267 | |
---|
268 | while True: |
---|
269 | try: |
---|
270 | |
---|
271 | notifier.process_events() |
---|
272 | |
---|
273 | if notifier.check_events(): |
---|
274 | notifier.read_events() |
---|
275 | |
---|
276 | except Exception as e: |
---|
277 | print str(e) |
---|
278 | notifier.stop() |
---|
279 | |
---|
280 | return False |
---|
281 | |
---|
282 | |
---|
283 | #def _inotify |
---|
284 | |
---|
285 | def update_info(self): |
---|
286 | |
---|
287 | f=open(self.LliurexGoogleDriveManager.config_file) |
---|
288 | |
---|
289 | try: |
---|
290 | self.update_profiles=json.load(f) |
---|
291 | except: |
---|
292 | self.update_profiles={} |
---|
293 | |
---|
294 | f.close() |
---|
295 | |
---|
296 | #def update_info |
---|
297 | |
---|
298 | def add_menu_item(self,profile,mountpoint): |
---|
299 | |
---|
300 | if self.initial_connection: |
---|
301 | status_info=self.LliurexGoogleDriveManager.check_mountpoint_status(mountpoint) |
---|
302 | info=self.item_status_info(status_info) |
---|
303 | self.current_status[profile]=status_info["status"] |
---|
304 | else: |
---|
305 | status_info={} |
---|
306 | status_info['status']=None |
---|
307 | status_info['size']=0 |
---|
308 | status_info['used']=0 |
---|
309 | info=self.item_status_info(status_info) |
---|
310 | self.current_status[profile]=None |
---|
311 | |
---|
312 | label_item=profile |
---|
313 | item=Gtk.ImageMenuItem() |
---|
314 | item.set_label(label_item) |
---|
315 | item.set_image(info["img"]) |
---|
316 | item.set_tooltip_text(info["tooltip"]) |
---|
317 | item.profile=profile |
---|
318 | item.size=False |
---|
319 | item.status=True |
---|
320 | item.set_always_show_image(True) |
---|
321 | item.connect("activate",self.item_clicked,profile) |
---|
322 | self.menu.insert(item,0) |
---|
323 | |
---|
324 | |
---|
325 | item=Gtk.MenuItem() |
---|
326 | label_item=info["used_size"] |
---|
327 | item.set_label(label_item) |
---|
328 | item.profile=profile |
---|
329 | item.size=True |
---|
330 | item.status=False |
---|
331 | self.menu.insert(item,1) |
---|
332 | |
---|
333 | item=Gtk.MenuItem() |
---|
334 | label_item=_("Open folder") |
---|
335 | item.set_label(label_item) |
---|
336 | item.connect("activate",self.open_folder,profile) |
---|
337 | item.profile=profile |
---|
338 | item.size=False |
---|
339 | item.status=False |
---|
340 | self.menu.insert(item,2) |
---|
341 | |
---|
342 | item=Gtk.MenuItem() |
---|
343 | label_item=_("Update cache") |
---|
344 | item.set_label(label_item) |
---|
345 | item.set_tooltip_text(_("Click if do not see all the files in the Google Drive account")) |
---|
346 | item.connect("activate",self.refresh_cache_clicked,profile) |
---|
347 | item.profile=profile |
---|
348 | item.size=False |
---|
349 | item.status=False |
---|
350 | self.menu.insert(item,3) |
---|
351 | |
---|
352 | item=Gtk.SeparatorMenuItem() |
---|
353 | item.profile=profile |
---|
354 | item.size=False |
---|
355 | item.status=False |
---|
356 | self.menu.insert(item,4) |
---|
357 | self.init_folders_info(profile,mountpoint) |
---|
358 | |
---|
359 | |
---|
360 | #def add_menu_item |
---|
361 | |
---|
362 | |
---|
363 | def populate_menu(self,empty=True): |
---|
364 | |
---|
365 | |
---|
366 | if empty: |
---|
367 | for c in self.menu.get_children(): |
---|
368 | self.menu.remove(c) |
---|
369 | |
---|
370 | |
---|
371 | for profile in self.profiles_info: |
---|
372 | mountpoint=self.profiles_info[profile]["mountpoint"] |
---|
373 | self.add_menu_item(profile,mountpoint) |
---|
374 | |
---|
375 | |
---|
376 | item=Gtk.ImageMenuItem() |
---|
377 | label_item=_("Open Lliurex GDrive") |
---|
378 | item.set_label(label_item) |
---|
379 | img=Gtk.Image() |
---|
380 | img.set_from_stock(Gtk.STOCK_PREFERENCES,Gtk.IconSize.MENU) |
---|
381 | item.set_image(img) |
---|
382 | item.set_always_show_image(True) |
---|
383 | item.connect("activate",self.open_gui) |
---|
384 | item.profile="" |
---|
385 | item.size=False |
---|
386 | item.status=False |
---|
387 | self.menu.append(item) |
---|
388 | |
---|
389 | item=Gtk.ImageMenuItem() |
---|
390 | img=Gtk.Image() |
---|
391 | img.set_from_stock(Gtk.STOCK_CLOSE,Gtk.IconSize.MENU) |
---|
392 | item.set_image(img) |
---|
393 | item.set_always_show_image(True) |
---|
394 | label_item=_("Close") |
---|
395 | item.set_label(label_item) |
---|
396 | item.connect("activate",self.quit) |
---|
397 | item.profile="" |
---|
398 | item.size=False |
---|
399 | item.status=False |
---|
400 | self.menu.append(item) |
---|
401 | |
---|
402 | |
---|
403 | self.menu.show_all() |
---|
404 | |
---|
405 | |
---|
406 | #def populate_menu |
---|
407 | |
---|
408 | def init_folders_info(self,profile,mountpoint): |
---|
409 | |
---|
410 | self.folders_info={} |
---|
411 | tmp={} |
---|
412 | self.folders_info['profile']=profile |
---|
413 | tmp['profile']=profile |
---|
414 | tmp['notifications']=[] |
---|
415 | self.notifications_list.append(tmp) |
---|
416 | self.files=set() |
---|
417 | self.controlDirectory(mountpoint) |
---|
418 | self.folders_info['files']=self.files |
---|
419 | self.init_files.append(self.folders_info) |
---|
420 | |
---|
421 | #def init_folders_info |
---|
422 | |
---|
423 | |
---|
424 | def controlDirectory(self,path): |
---|
425 | |
---|
426 | |
---|
427 | for item in listdir(path): |
---|
428 | if item not in self.blacklist: |
---|
429 | |
---|
430 | t=join(path,item) |
---|
431 | |
---|
432 | if isfile(t): |
---|
433 | |
---|
434 | compartido=False |
---|
435 | state=os.stat(t) |
---|
436 | access=datetime.datetime.fromtimestamp(state.st_mtime) |
---|
437 | tmp=t+":__"+str(access) |
---|
438 | |
---|
439 | self.files.add(tmp) |
---|
440 | else: |
---|
441 | if isdir(t): |
---|
442 | self.controlDirectory(t) |
---|
443 | |
---|
444 | #def controlDirectory |
---|
445 | |
---|
446 | def remove_folder_info(self,profile): |
---|
447 | |
---|
448 | i=0 |
---|
449 | for item in self.init_files: |
---|
450 | if profile ==item["profile"]: |
---|
451 | self.init_files.pop(i) |
---|
452 | i=i+1 |
---|
453 | |
---|
454 | i=0 |
---|
455 | for item in self.notifications_list: |
---|
456 | if profile ==item["profile"]: |
---|
457 | self.notifications_list.pop(i) |
---|
458 | |
---|
459 | i=i+1 |
---|
460 | |
---|
461 | #def remove_folder_info |
---|
462 | |
---|
463 | |
---|
464 | def item_clicked(self,widget,profile): |
---|
465 | |
---|
466 | if profile not in self.profile_clicked: |
---|
467 | self.profile_clicked.append(profile) |
---|
468 | id=self.generate_sync_threads(profile) |
---|
469 | self.sp_cont[id]=0 |
---|
470 | self.sp_img[id]=Gtk.Image.new_from_file(SP1) |
---|
471 | GLib.timeout_add(100,self.pulsate_check_sync,id,widget,profile) |
---|
472 | |
---|
473 | |
---|
474 | #def item_clicked |
---|
475 | |
---|
476 | def spinner_sync(self,id): |
---|
477 | |
---|
478 | if self.sp_cont[id]>80: |
---|
479 | self.sp_cont[id]=0 |
---|
480 | |
---|
481 | if self.sp_cont[id]==0: |
---|
482 | img=Gtk.Image.new_from_file(SP1) |
---|
483 | self.sp_img[id]=img |
---|
484 | elif self.sp_cont[id]==10: |
---|
485 | img=Gtk.Image.new_from_file(SP2) |
---|
486 | self.sp_img[id]=img |
---|
487 | elif self.sp_cont[id]==20: |
---|
488 | img=Gtk.Image.new_from_file(SP3) |
---|
489 | self.sp_img[id]=img |
---|
490 | elif self.sp_cont[id]==30: |
---|
491 | img=Gtk.Image.new_from_file(SP4) |
---|
492 | self.sp_img[id]=img |
---|
493 | elif self.sp_cont[id]==40: |
---|
494 | img=Gtk.Image.new_from_file(SP5) |
---|
495 | self.sp_img[id]=img |
---|
496 | elif self.sp_cont[id]==50: |
---|
497 | img=Gtk.Image.new_from_file(SP6) |
---|
498 | self.sp_img[id]=img |
---|
499 | elif self.sp_cont[id]==60: |
---|
500 | img=Gtk.Image.new_from_file(SP7) |
---|
501 | self.sp_img[id]=img |
---|
502 | elif self.sp_cont[id]==70: |
---|
503 | img=Gtk.Image.new_from_file(SP8) |
---|
504 | self.sp_img[id]=img |
---|
505 | |
---|
506 | #def spinner_sync |
---|
507 | |
---|
508 | def pulsate_check_sync(self,id,widget,profile): |
---|
509 | |
---|
510 | |
---|
511 | if self.sync_threads[id]["thread"].is_alive(): |
---|
512 | self.spinner_sync(id) |
---|
513 | widget.set_image(self.sp_img[id]) |
---|
514 | widget.set_tooltip_text(_("Applying changes...")) |
---|
515 | self.sp_cont[id]=self.sp_cont[id]+1 |
---|
516 | return True |
---|
517 | |
---|
518 | else: |
---|
519 | self.profile_clicked.remove(profile) |
---|
520 | self.current_status[profile]=self.result_sync_threads[id]["status_info"]["status"] |
---|
521 | if self.result_sync_threads[id]["status_mod"]["result"]==False: |
---|
522 | if self.result_sync_threads[id]["status_mod"]["code"]==8: |
---|
523 | message=_("Error: Unable to connect with google") |
---|
524 | else: |
---|
525 | status=self.current_status[profile] |
---|
526 | if status: |
---|
527 | action=_("dismount") |
---|
528 | else: |
---|
529 | action=_("mount") |
---|
530 | message=_("Error: Unable to ") + action + " " |
---|
531 | else: |
---|
532 | if self.result_sync_threads[id]["status_mod"]["code"]==9: |
---|
533 | message=_("Status updated. Now you can change it") |
---|
534 | else: |
---|
535 | message=_("Changes applied successfully") |
---|
536 | |
---|
537 | self.show_message(profile + ": " + message) |
---|
538 | |
---|
539 | info=self.item_status_info(self.result_sync_threads[id]["status_info"]) |
---|
540 | |
---|
541 | for item in self.menu.get_children(): |
---|
542 | if item.size: |
---|
543 | if item.profile==profile: |
---|
544 | item.set_label(info["used_size"]) |
---|
545 | |
---|
546 | widget.set_tooltip_text(info["tooltip"]) |
---|
547 | widget.set_image(info["img"]) |
---|
548 | self.sync_threads.pop(id) |
---|
549 | self.sp_cont.pop(id) |
---|
550 | self.sp_img.pop(id) |
---|
551 | self.result_sync_threads.pop(id) |
---|
552 | |
---|
553 | return False |
---|
554 | |
---|
555 | #def pulsate_check_sync |
---|
556 | |
---|
557 | |
---|
558 | def check_sync(self,id,profile): |
---|
559 | |
---|
560 | mountpoint=self.profiles_info[profile]["mountpoint"] |
---|
561 | |
---|
562 | current_status=self.current_status[profile] |
---|
563 | |
---|
564 | self.result_sync_threads[id]["status_mod"],self.result_sync_threads[id]["status_info"]=self.LliurexGoogleDriveManager.sync_profile(profile,mountpoint,current_status) |
---|
565 | |
---|
566 | |
---|
567 | #self.result_connect_threads[id]["value"]=self.LliurexGoogleDriveManager.check_google_connection() |
---|
568 | |
---|
569 | #def check_sync |
---|
570 | |
---|
571 | def open_folder(self,widget,profile): |
---|
572 | |
---|
573 | |
---|
574 | mountpoint=self.profiles_info[profile]["mountpoint"] |
---|
575 | cmd="caja " + mountpoint.encode("utf-8") |
---|
576 | |
---|
577 | os.system(cmd) |
---|
578 | |
---|
579 | #def open_folder |
---|
580 | |
---|
581 | def refresh_cache_clicked (self,widget,profile): |
---|
582 | |
---|
583 | message=self.refresh_cache_command(profile) |
---|
584 | |
---|
585 | message=message.decode("UTF-8") |
---|
586 | |
---|
587 | time.sleep(0.5) |
---|
588 | self.show_message(profile + ": " + message) |
---|
589 | |
---|
590 | #def refresh_cache_menu |
---|
591 | |
---|
592 | def refresh_cache_command(self,profile): |
---|
593 | |
---|
594 | profile=profile.encode("utf-8") |
---|
595 | |
---|
596 | try: |
---|
597 | cmd="google-drive-ocamlfuse -cc -label %s"%profile |
---|
598 | os.system(cmd) |
---|
599 | message=_("Cache updated successfully") |
---|
600 | |
---|
601 | except Exception as e: |
---|
602 | message=_("An error occurred updating the cache. Try again") |
---|
603 | |
---|
604 | return message |
---|
605 | |
---|
606 | #def refresh_cache_command |
---|
607 | |
---|
608 | def open_gui(self,widget): |
---|
609 | |
---|
610 | if not os.path.exists(self.lock_gui): |
---|
611 | cmd='/usr/bin/lliurex-gdrive' +"&" |
---|
612 | os.system(cmd) |
---|
613 | |
---|
614 | #def open_gui |
---|
615 | |
---|
616 | def item_status_info(self,status_info): |
---|
617 | |
---|
618 | size=status_info["size"] |
---|
619 | used=status_info["used"] |
---|
620 | |
---|
621 | if status_info['status']==None: |
---|
622 | img=Gtk.Image.new_from_file(MOUNT_WARNING_IMAGE) |
---|
623 | tooltip=_("Without connection. Clicked to update") |
---|
624 | used_size=_("Used: not available") |
---|
625 | |
---|
626 | elif status_info["status"]: |
---|
627 | img=Gtk.Image.new_from_file(MOUNT_ON_IMAGE) |
---|
628 | tooltip=_("Mounted. Clicked to dismount now") |
---|
629 | used_size=_("Used: %s of %s")%(used,size) |
---|
630 | |
---|
631 | else: |
---|
632 | img=Gtk.Image.new_from_file(MOUNT_OFF_IMAGE) |
---|
633 | tooltip=_("Dismounted. Clicked to mount now") |
---|
634 | used_size=_("Used: not available") |
---|
635 | |
---|
636 | return {"img":img ,"tooltip":tooltip, "used_size":used_size} |
---|
637 | |
---|
638 | #def item_status_info |
---|
639 | |
---|
640 | def check_status(self): |
---|
641 | |
---|
642 | if not self.checking_status: |
---|
643 | self.checking_status=True |
---|
644 | self.init_connect_thread() |
---|
645 | GLib.timeout_add(100,self.get_status_info,id) |
---|
646 | |
---|
647 | return True |
---|
648 | |
---|
649 | |
---|
650 | #def check_status |
---|
651 | |
---|
652 | def get_status_info(self,id): |
---|
653 | |
---|
654 | if self.check_connect_t.is_alive(): |
---|
655 | return True |
---|
656 | |
---|
657 | else: |
---|
658 | self.update_files=[] |
---|
659 | self.checking_status=False |
---|
660 | #self.connect_threads.pop(id) |
---|
661 | for profile in self.profiles_info: |
---|
662 | if self.connection: |
---|
663 | self.refresh_cache_command(profile) |
---|
664 | mountpoint=self.profiles_info[profile]["mountpoint"] |
---|
665 | status_info=self.LliurexGoogleDriveManager.check_mountpoint_status(mountpoint) |
---|
666 | self.update_folders_info(profile,mountpoint) |
---|
667 | |
---|
668 | else: |
---|
669 | status_info={} |
---|
670 | status_info['status']=None |
---|
671 | status_info['size']=0 |
---|
672 | status_info['used']=0 |
---|
673 | |
---|
674 | info=self.item_status_info(status_info) |
---|
675 | self.current_status[profile]=status_info["status"] |
---|
676 | |
---|
677 | for item in self.menu.get_children(): |
---|
678 | if item.profile==profile: |
---|
679 | if item.size: |
---|
680 | item.set_label(info["used_size"]) |
---|
681 | if item.status: |
---|
682 | item.set_tooltip_text(info["tooltip"]) |
---|
683 | item.set_image(info["img"]) |
---|
684 | |
---|
685 | #self.result_connect_threads.pop(id) |
---|
686 | self.notify_changes() |
---|
687 | return False |
---|
688 | |
---|
689 | #def get_status_info |
---|
690 | |
---|
691 | def check_connect(self): |
---|
692 | |
---|
693 | self.connection=self.LliurexGoogleDriveManager.check_google_connection() |
---|
694 | |
---|
695 | #def check_connect |
---|
696 | |
---|
697 | def update_folders_info(self,profile,mountpoint): |
---|
698 | |
---|
699 | self.folders_info={} |
---|
700 | self.folders_info['profile']=profile |
---|
701 | self.files=set() |
---|
702 | |
---|
703 | self.controlDirectory(mountpoint) |
---|
704 | self.folders_info['files']=self.files |
---|
705 | self.update_files.append(self.folders_info) |
---|
706 | |
---|
707 | #def update_folders_info |
---|
708 | |
---|
709 | |
---|
710 | def notify_changes(self): |
---|
711 | |
---|
712 | |
---|
713 | if self.init_files!=self.update_files: |
---|
714 | toInfo=self.remove_local_changes() |
---|
715 | |
---|
716 | for item in toInfo: |
---|
717 | numFiles_up=0 |
---|
718 | numFiles_rm=0 |
---|
719 | numFiles_add=0 |
---|
720 | p=item['profile'] |
---|
721 | if 'files_changes' in item: |
---|
722 | numFiles_up=len(item['files_changes']) |
---|
723 | |
---|
724 | if 'files_delete' in item: |
---|
725 | numFiles_rm=len(item['files_delete']) |
---|
726 | |
---|
727 | if 'files_add' in item: |
---|
728 | numFiles_add=len(item['files_add']) |
---|
729 | |
---|
730 | if numFiles_up>0 or numFiles_rm>0 or numFiles_add>0: |
---|
731 | message1=_('Changes in last 15 minutes in ')+p.encode("utf-8")+':\n' |
---|
732 | message_up="" |
---|
733 | message_rm="" |
---|
734 | message_add="" |
---|
735 | if numFiles_up>0: |
---|
736 | message_up=_("-Files updated: ") |
---|
737 | for i in self.notifications_list: |
---|
738 | if i['profile']==p: |
---|
739 | for element in item['files_changes']: |
---|
740 | i['notifications'].append(element) |
---|
741 | if numFiles_up==1: |
---|
742 | for element in item['files_changes']: |
---|
743 | file=os.path.basename(element.split(":__")[0]) |
---|
744 | file_split=os.path.splitext(file) |
---|
745 | if file_split[1]==".desktop": |
---|
746 | file=file_split[0] |
---|
747 | message_up=message_up + file.encode("utf-8") +'\n' |
---|
748 | |
---|
749 | else: |
---|
750 | message_up= message_up + str(numFiles_up) + '\n' |
---|
751 | |
---|
752 | if numFiles_add>0: |
---|
753 | message_add=_("-Files added: ") |
---|
754 | for i in self.notifications_list: |
---|
755 | if i['profile']==p: |
---|
756 | for element in item['files_add']: |
---|
757 | i['notifications'].append(element) |
---|
758 | if numFiles_add==1: |
---|
759 | for element in item['files_add']: |
---|
760 | file=os.path.basename(element.split(":__")[0]) |
---|
761 | file_split=os.path.splitext(file) |
---|
762 | if file_split[1]==".desktop": |
---|
763 | file=file_split[0] |
---|
764 | message_add=message_add +file.encode("utf-8") +'\n' |
---|
765 | else: |
---|
766 | |
---|
767 | message_add=message_add + str(numFiles_add) + '\n' |
---|
768 | |
---|
769 | if numFiles_rm>0: |
---|
770 | message_rm=_("-Files deleted: ") |
---|
771 | if numFiles_rm==1: |
---|
772 | for element in item['files_delete']: |
---|
773 | file=os.path.basename(element.split(":__")[0]) |
---|
774 | file_split=os.path.splitext(file) |
---|
775 | if file_split[1]==".desktop": |
---|
776 | file=file_split[0] |
---|
777 | message_rm=message_rm + file.encode("utf-8") +'\n' |
---|
778 | else: |
---|
779 | message_rm=message_rm +str(numFiles_rm) |
---|
780 | |
---|
781 | self.show_message(message1 + message_add + message_up + message_rm) |
---|
782 | |
---|
783 | self.init_files=copy.copy(self.update_files) |
---|
784 | self.local_changes={} |
---|
785 | |
---|
786 | #def notify_changes |
---|
787 | |
---|
788 | def remove_local_changes(self): |
---|
789 | |
---|
790 | changes=self.detect_folder_changes() |
---|
791 | |
---|
792 | changes_toNotify=[] |
---|
793 | |
---|
794 | if len(changes)>0: |
---|
795 | |
---|
796 | for item in changes: |
---|
797 | tmp_changes=[] |
---|
798 | tmp_deletes=[] |
---|
799 | tmp_add=[] |
---|
800 | to_info={} |
---|
801 | to_info['profile']=item['profile'] |
---|
802 | |
---|
803 | if 'files_changes' in item: |
---|
804 | #tmp_changes=item['files_changes'] |
---|
805 | tmp_changes=self.detect_previous_notifications(item) |
---|
806 | toInfo_up=tmp_changes.copy() |
---|
807 | toInfo_up=tmp_changes.copy() |
---|
808 | if 'files_deletes' in item: |
---|
809 | tmp_deletes=item['files_deletes'] |
---|
810 | toInfo_rm=tmp_deletes.copy() |
---|
811 | |
---|
812 | if 'files_add' in item: |
---|
813 | tmp_add=item['files_add'] |
---|
814 | toInfo_add=tmp_add.copy() |
---|
815 | |
---|
816 | |
---|
817 | if len(self.local_changes)>0: |
---|
818 | if len(tmp_changes)>0: |
---|
819 | for item in tmp_changes: |
---|
820 | #file=item.keys()[0] |
---|
821 | file=item.split(":__")[0] |
---|
822 | data=item.split(":__")[1] |
---|
823 | for i in self.local_changes: |
---|
824 | if file==i: |
---|
825 | data=datetime.datetime.strptime(data,'%Y-%m-%d %H:%M:%S') |
---|
826 | if data<=self.local_changes[i]: |
---|
827 | toInfo_up.remove(item) |
---|
828 | to_info['files_changes']=toInfo_up |
---|
829 | if len(tmp_deletes)>0: |
---|
830 | for item in tmp_deletes: |
---|
831 | file=os.path.basename(item.split(":__")[0]) |
---|
832 | file=os.path.splitext(file)[0] |
---|
833 | data=item.split(":__")[1] |
---|
834 | for i in self.local_changes: |
---|
835 | if file in i: |
---|
836 | data=datetime.datetime.strptime(data,'%Y-%m-%d %H:%M:%S') |
---|
837 | if data<=self.local_changes[i]: |
---|
838 | toInfo_rm.remove(item) |
---|
839 | to_info['files_delete']=toInfo_rm |
---|
840 | |
---|
841 | if len(tmp_add)>0: |
---|
842 | for item in tmp_add: |
---|
843 | file=item.split(":__")[0] |
---|
844 | data=item.split(":__")[1] |
---|
845 | for i in self.local_changes: |
---|
846 | if file==i: |
---|
847 | data=datetime.datetime.strptime(data,'%Y-%m-%d %H:%M:%S') |
---|
848 | if data<=self.local_changes[i]: |
---|
849 | toInfo_add.remove(item) |
---|
850 | to_info['files_add']=toInfo_add |
---|
851 | else: |
---|
852 | to_info['files_changes']=tmp_changes |
---|
853 | to_info['files_delete']=tmp_deletes |
---|
854 | to_info['files_add']=tmp_add |
---|
855 | |
---|
856 | changes_toNotify.append(to_info) |
---|
857 | |
---|
858 | return changes_toNotify |
---|
859 | |
---|
860 | #def remove_local_changes |
---|
861 | |
---|
862 | |
---|
863 | def detect_folder_changes(self): |
---|
864 | |
---|
865 | changes=[] |
---|
866 | |
---|
867 | |
---|
868 | for item in self.update_files: |
---|
869 | info_changes={} |
---|
870 | info_changes['profile']=item['profile'] |
---|
871 | final_files=item['files'] |
---|
872 | |
---|
873 | for item in self.init_files: |
---|
874 | profile_init=item['profile'] |
---|
875 | original_files=item['files'] |
---|
876 | if len(original_files)>0: |
---|
877 | if len(final_files)>0: |
---|
878 | if info_changes['profile']==profile_init: |
---|
879 | diff_changes=final_files.difference(original_files) |
---|
880 | diff_deletes=original_files.difference(final_files) |
---|
881 | |
---|
882 | if len(diff_changes)>0: |
---|
883 | changes.append(info_changes) |
---|
884 | |
---|
885 | if len(diff_deletes)>0: |
---|
886 | tmp_del=diff_deletes.copy() |
---|
887 | tmp_add=diff_changes.copy() |
---|
888 | for item in diff_deletes: |
---|
889 | file_d=item.split(":__")[0] |
---|
890 | for element in diff_changes: |
---|
891 | file_c=element.split(":__")[0] |
---|
892 | if file_d == file_c: |
---|
893 | tmp_del.remove(item) |
---|
894 | tmp_add.remove(element) |
---|
895 | |
---|
896 | if len(tmp_del)>0: |
---|
897 | info_changes['files_deletes']=tmp_del |
---|
898 | |
---|
899 | if len(tmp_add)>0: |
---|
900 | info_changes['files_add']=tmp_add |
---|
901 | tmp_up=diff_changes.difference(tmp_add) |
---|
902 | info_changes['files_changes']=tmp_up |
---|
903 | else: |
---|
904 | info_changes['files_changes']=diff_changes |
---|
905 | |
---|
906 | else: |
---|
907 | info_changes['files_add']=diff_changes |
---|
908 | else: |
---|
909 | if len(original_files)>len(final_files) and len(diff_deletes)>0: |
---|
910 | info_changes['files_deletes']=diff_deletes |
---|
911 | changes.append(info_changes) |
---|
912 | |
---|
913 | |
---|
914 | return changes |
---|
915 | |
---|
916 | #def detect_folder_changes |
---|
917 | |
---|
918 | |
---|
919 | def detect_previous_notifications(self,item): |
---|
920 | |
---|
921 | changes=item['files_changes'] |
---|
922 | toInfo_up=changes.copy() |
---|
923 | for element in self.notifications_list: |
---|
924 | if item['profile']==element['profile']: |
---|
925 | for f in changes: |
---|
926 | file_changed=f.split(":__")[0] |
---|
927 | data_changed=f.split(":__")[1] |
---|
928 | data_changed=datetime.datetime.strptime(data_changed,'%Y-%m-%d %H:%M:%S') |
---|
929 | for j in element['notifications']: |
---|
930 | file_notified=j.split(":__")[0] |
---|
931 | data_notified=j.split(":__")[1] |
---|
932 | data_notified=datetime.datetime.strptime(data_notified,'%Y-%m-%d %H:%M:%S') |
---|
933 | if file_changed==file_notified: |
---|
934 | diff=(data_changed-data_notified).seconds |
---|
935 | if int(diff)<6: |
---|
936 | toInfo_up.remove(f) |
---|
937 | |
---|
938 | |
---|
939 | return toInfo_up |
---|
940 | |
---|
941 | #def detect_previous_notifications |
---|
942 | |
---|
943 | def show_message(self,message): |
---|
944 | notify2.init("llxgdrive") |
---|
945 | n=notify2.Notification("Lliurex-GDrive",message,"gdrive") |
---|
946 | n.show() |
---|
947 | return |
---|
948 | |
---|
949 | #def show_message |
---|
950 | |
---|
951 | |
---|
952 | def cleanIndicatorLock(self): |
---|
953 | |
---|
954 | if os.path.exists(self.lock_indicator): |
---|
955 | os.remove(self.lock_indicator) |
---|
956 | |
---|
957 | #def cleanIndicatorLock |
---|
958 | |
---|
959 | def quit(self,widget): |
---|
960 | |
---|
961 | self.cleanIndicatorLock() |
---|
962 | Gtk.main_quit() |
---|
963 | |
---|
964 | #def quit |
---|
965 | |
---|
966 | |
---|
967 | #class LliurexGdriveIndicator |
---|
968 | |
---|
969 | if __name__=="__main__": |
---|
970 | |
---|
971 | lliurexgdrive=LliurexGdriveIndicator("gdrive") |
---|
972 | GObject.threads_init() |
---|
973 | Gtk.main() |
---|
974 | |
---|