Advertisement
lpugoy

Insync Caja plugin context menu fix

Aug 12th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. print('loading insync caja plugin..')
  2. import caja
  3. import gtk # gtk2
  4. import json
  5. import os
  6. import socket
  7. import sys
  8. import urllib
  9.  
  10. DEV_MODE = False
  11.  
  12. #http://www.gtk.org/tutorial1.2/gtk_tut-13.html
  13. #http://pygtk.org/docs/pygtk/class-gtkaction.html
  14. #gtk.Action(name, label, tooltip, stock_id)
  15.  
  16. FS_ENCODING = sys.getfilesystemencoding()   # C is 'ANSI_X3.4-1968'
  17.  
  18. def ipc_insync(**kw):
  19.   data = json.dumps(kw)
  20.   socket_file = u'/tmp/insync%r.sock' % os.getuid()
  21.   if not os.path.exists(socket_file):
  22.     if DEV_MODE:
  23.       print('WARN: insync socket not found')
  24.     return None
  25.   sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  26.   sock.settimeout(0.1)
  27.   try:
  28.     sock.connect(socket_file)
  29.     sock.send(data)
  30.     res = sock.recv(4096)
  31.     sock.close()
  32.   except:
  33.     return None
  34.   if res.strip():
  35.     return json.loads(res.strip())
  36.   else:
  37.     return None
  38.  
  39.  
  40. _EMBLEM_KEYS = {
  41.   'SYNCED': 'emblem-insync-synced',
  42.   'SYNCING': 'emblem-insync-syncing',
  43.   'ERROR': 'emblem-insync-error',
  44.   'DES_ERROR': 'emblem-insync-des-error',
  45.   'DES_SYNCING': 'emblem-insync-syncing',
  46. }
  47.  
  48.  
  49. class InsyncPlugin(caja.MenuProvider, caja.InfoProvider):
  50.  
  51.   # Caja crashes if a plugin doesn't implement the __init__ method.
  52.   # See Bug #374958
  53.   def __init__(self):
  54.     pass
  55.  
  56.   def get_background_items(self, window, file):
  57.     return None
  58.  
  59.   def get_file_items(self, window, files):
  60.     if DEV_MODE:
  61.       print('get file items', files)
  62.  
  63.     if len(files) != 1:
  64.       # TODO what can we do with mutiple files here
  65.       return None
  66.  
  67.     file = files[0]
  68.     if file.get_uri_scheme() != 'file':
  69.       return None
  70.  
  71.     path = os.path.realpath(urllib.unquote(file.get_uri()[len('file://'):]))
  72.     path = unicode(path, FS_ENCODING)
  73.     if DEV_MODE:
  74.       print path
  75.     is_dir = os.path.isdir(path)
  76.  
  77.     cm_info = ipc_insync(command='CONTEXT-MENU-ITEMS', full_path=path)
  78.  
  79.     if cm_info:
  80.       title, cm_items = cm_info
  81.       tip = 'Insync folder actions' if is_dir else 'Insync file actions'
  82.       item = caja.MenuItem(
  83.         name=title,
  84.         label=title,
  85.         tip=tip,
  86.         icon='insync'
  87.       )
  88.       sub_menu = caja.Menu()
  89.       item.set_submenu(sub_menu)
  90.  
  91.       sep_id = 0
  92.       for text, cmd in cm_items:
  93.         if text == 'separator':
  94.           text = u'\u2015' * 10
  95.           name = 'separator:' + str(sep_id)
  96.           sep_id += 1
  97.         else:
  98.           name = text
  99.         menu_item = caja.MenuItem(name=name, label=text, tip=text)
  100.         if cmd:
  101.           menu_item.connect('activate', self.do_action, file, cmd)
  102.         else:
  103.           menu_item.sensitive = False
  104.         sub_menu.append_item(menu_item)
  105.  
  106.       return [item]
  107.     return None
  108.  
  109.   def do_action(self, menu, file, method):
  110.     path = urllib.unquote(
  111.       file.get_uri()[len('file://'):]
  112.     )
  113.     ipc_insync(method=method, full_path=path)
  114.  
  115.   def update_file_info(self, file):
  116.     if file.get_uri_scheme() != 'file':
  117.       return
  118.  
  119.     filename = urllib.unquote(file.get_uri()[len('file://'):])
  120.     filename = os.path.realpath(filename)
  121.     status = ipc_insync(command='GET-FILE-STATUS', full_path=filename)
  122.     if DEV_MODE:
  123.       print filename, status
  124.     if status and status != 'UNKNOWN':
  125.       emblem = _EMBLEM_KEYS[status]
  126.       if os.path.isdir(filename):
  127.         is_shared = ipc_insync(command='IS-FILE-SHARED', full_path=filename)
  128.         if is_shared:
  129.           emblem += '-shared'
  130.       file.add_emblem(emblem)
  131.     file.invalidate_extension_info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement