Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print('loading insync caja plugin..')
- import caja
- import gtk # gtk2
- import json
- import os
- import socket
- import sys
- import urllib
- DEV_MODE = False
- #http://www.gtk.org/tutorial1.2/gtk_tut-13.html
- #http://pygtk.org/docs/pygtk/class-gtkaction.html
- #gtk.Action(name, label, tooltip, stock_id)
- FS_ENCODING = sys.getfilesystemencoding() # C is 'ANSI_X3.4-1968'
- def ipc_insync(**kw):
- data = json.dumps(kw)
- socket_file = u'/tmp/insync%r.sock' % os.getuid()
- if not os.path.exists(socket_file):
- if DEV_MODE:
- print('WARN: insync socket not found')
- return None
- sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- sock.settimeout(0.1)
- try:
- sock.connect(socket_file)
- sock.send(data)
- res = sock.recv(4096)
- sock.close()
- except:
- return None
- if res.strip():
- return json.loads(res.strip())
- else:
- return None
- _EMBLEM_KEYS = {
- 'SYNCED': 'emblem-insync-synced',
- 'SYNCING': 'emblem-insync-syncing',
- 'ERROR': 'emblem-insync-error',
- 'DES_ERROR': 'emblem-insync-des-error',
- 'DES_SYNCING': 'emblem-insync-syncing',
- }
- class InsyncPlugin(caja.MenuProvider, caja.InfoProvider):
- # Caja crashes if a plugin doesn't implement the __init__ method.
- # See Bug #374958
- def __init__(self):
- pass
- def get_background_items(self, window, file):
- return None
- def get_file_items(self, window, files):
- if DEV_MODE:
- print('get file items', files)
- if len(files) != 1:
- # TODO what can we do with mutiple files here
- return None
- file = files[0]
- if file.get_uri_scheme() != 'file':
- return None
- path = os.path.realpath(urllib.unquote(file.get_uri()[len('file://'):]))
- path = unicode(path, FS_ENCODING)
- if DEV_MODE:
- print path
- is_dir = os.path.isdir(path)
- cm_info = ipc_insync(command='CONTEXT-MENU-ITEMS', full_path=path)
- if cm_info:
- title, cm_items = cm_info
- tip = 'Insync folder actions' if is_dir else 'Insync file actions'
- item = caja.MenuItem(
- name=title,
- label=title,
- tip=tip,
- icon='insync'
- )
- sub_menu = caja.Menu()
- item.set_submenu(sub_menu)
- sep_id = 0
- for text, cmd in cm_items:
- if text == 'separator':
- text = u'\u2015' * 10
- name = 'separator:' + str(sep_id)
- sep_id += 1
- else:
- name = text
- menu_item = caja.MenuItem(name=name, label=text, tip=text)
- if cmd:
- menu_item.connect('activate', self.do_action, file, cmd)
- else:
- menu_item.sensitive = False
- sub_menu.append_item(menu_item)
- return [item]
- return None
- def do_action(self, menu, file, method):
- path = urllib.unquote(
- file.get_uri()[len('file://'):]
- )
- ipc_insync(method=method, full_path=path)
- def update_file_info(self, file):
- if file.get_uri_scheme() != 'file':
- return
- filename = urllib.unquote(file.get_uri()[len('file://'):])
- filename = os.path.realpath(filename)
- status = ipc_insync(command='GET-FILE-STATUS', full_path=filename)
- if DEV_MODE:
- print filename, status
- if status and status != 'UNKNOWN':
- emblem = _EMBLEM_KEYS[status]
- if os.path.isdir(filename):
- is_shared = ipc_insync(command='IS-FILE-SHARED', full_path=filename)
- if is_shared:
- emblem += '-shared'
- file.add_emblem(emblem)
- file.invalidate_extension_info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement