Try putting this at /usr/share/nemo-python/extensions/insync-nemo-plugin.py
:
print('loading insync nemo plugin..')
from gi.repository import GObject
from gi.repository import Nemo
import importlib
import traceback
DEV_MODE = False
import json
import os
import socket
import sys
import urllib.request, urllib.parse, urllib.error
if sys.getdefaultencoding() != 'utf-8':
importlib.reload(sys)
sys.setdefaultencoding('utf-8') # pylint: disable=E1101
def ipc_insync(**kw):
if DEV_MODE:
print('<ipc-insync>',kw)
data = json.dumps( kw )
socket_file = '/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.5)
try:
sock.connect(socket_file)
sock.send(data.encode())
res = sock.recv(4096)
sock.close()
except:
if DEV_MODE:
traceback.print_exc()
return None
if res.strip():
return json.loads(res.strip())
else:
if DEV_MODE:
print("else")
return None
_EMBLEM_KEYS = {
'SYNCED': 'emblem-insync-synced',
'SYNCING': 'emblem-insync-syncing',
'ERROR': 'emblem-insync-error',
}
class InsyncMenuProvider(GObject.GObject, Nemo.MenuProvider, Nemo.InfoProvider):
def __init__(self):
self.file_list = dict()
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.parse.unquote(file.get_uri()[len('file://'):]))
path = str(path)
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 = Nemo.MenuItem(
name='Insync:' + title,
label=title,
tip=tip,
icon='insync'
)
sub_menu = Nemo.Menu()
item.set_submenu(sub_menu)
item_id = 0
for text, cmd in cm_items:
if text == 'separator':
text = '\u2015' * 10
name = 'Insync:separator:' + str(item_id)
else:
name = 'Insync:%s:%d' % (text, item_id)
item_id += 1
menu_item = Nemo.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.parse.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.parse.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("status", 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)
old_emblem = self.file_list.get(filename)
self.file_list[filename] = emblem
if old_emblem != emblem:
file.invalidate_extension_info()
elif self.file_list.pop(filename, None):
file.invalidate_extension_info()