2014-09-22 14:24:06 +04:00
|
|
|
#
|
|
|
|
# Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
#
|
2015-10-31 15:07:18 +03:00
|
|
|
# This program is the core of OwnCloud integration to Nautilus
|
|
|
|
# It will be installed on /usr/share/nautilus-python/extensions/ with the paquet owncloud-client-nautilus
|
|
|
|
# (https://github.com/owncloud/client/edit/master/shell_integration/nautilus/syncstate.py)
|
|
|
|
#
|
2014-09-22 14:24:06 +04:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
# for more details.
|
2014-07-25 14:11:12 +04:00
|
|
|
|
2018-01-23 13:47:23 +03:00
|
|
|
import sys
|
|
|
|
python3 = sys.version_info[0] >= 3
|
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
import os
|
|
|
|
import urllib
|
2018-01-23 13:47:23 +03:00
|
|
|
if python3:
|
|
|
|
import urllib.parse
|
2014-06-06 19:49:19 +04:00
|
|
|
import socket
|
2016-10-25 21:15:17 +03:00
|
|
|
import tempfile
|
2018-01-19 13:32:08 +03:00
|
|
|
import time
|
2014-06-06 19:49:19 +04:00
|
|
|
|
|
|
|
from gi.repository import GObject, Nautilus
|
|
|
|
|
2018-06-04 14:48:31 +03:00
|
|
|
# Note: setappname.sh will search and replace 'ownCloud' on this file to update this line and other
|
|
|
|
# occurrences of the name
|
2018-11-27 21:55:12 +03:00
|
|
|
appname = 'Nextcloud'
|
2015-01-12 16:48:49 +03:00
|
|
|
|
2016-05-03 12:36:56 +03:00
|
|
|
print("Initializing "+appname+"-client-nautilus extension")
|
2018-04-25 13:26:18 +03:00
|
|
|
print("Using python version {}".format(sys.version_info))
|
2016-05-03 12:36:56 +03:00
|
|
|
|
2015-02-26 11:40:54 +03:00
|
|
|
def get_local_path(url):
|
|
|
|
if url[0:7] == 'file://':
|
|
|
|
url = url[7:]
|
2018-01-23 13:47:23 +03:00
|
|
|
unquote = urllib.parse.unquote if python3 else urllib.unquote
|
|
|
|
return unquote(url)
|
2015-01-12 16:48:49 +03:00
|
|
|
|
2014-12-01 14:05:14 +03:00
|
|
|
def get_runtime_dir():
|
|
|
|
"""Returns the value of $XDG_RUNTIME_DIR, a directory path.
|
|
|
|
|
|
|
|
If the value is not set, returns the same default as in Qt5
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return os.environ['XDG_RUNTIME_DIR']
|
|
|
|
except KeyError:
|
2016-10-25 21:15:17 +03:00
|
|
|
fallback = os.path.join(tempfile.gettempdir(), 'runtime-' + os.environ['USER'])
|
2014-12-01 14:05:14 +03:00
|
|
|
return fallback
|
|
|
|
|
|
|
|
|
2015-02-11 16:53:17 +03:00
|
|
|
class SocketConnect(GObject.GObject):
|
2014-06-06 19:49:19 +04:00
|
|
|
def __init__(self):
|
2015-01-12 16:48:49 +03:00
|
|
|
GObject.GObject.__init__(self)
|
2015-02-11 16:53:17 +03:00
|
|
|
self.connected = False
|
|
|
|
self.registered_paths = {}
|
|
|
|
self._watch_id = 0
|
|
|
|
self._sock = None
|
2018-01-19 13:32:08 +03:00
|
|
|
self._listeners = [self._update_registered_paths, self._get_version]
|
2018-09-10 16:05:46 +03:00
|
|
|
self._remainder = ''.encode() if python3 else ''
|
2018-01-19 13:32:08 +03:00
|
|
|
self.protocolVersion = '1.0'
|
2018-04-25 14:22:34 +03:00
|
|
|
self.nautilusVFSFile_table = {} # not needed in this object actually but shared
|
2015-10-31 15:07:18 +03:00
|
|
|
# all over the other objects.
|
2015-02-11 16:53:17 +03:00
|
|
|
|
|
|
|
# returns true when one should try again!
|
|
|
|
if self._connectToSocketServer():
|
|
|
|
GObject.timeout_add(5000, self._connectToSocketServer)
|
|
|
|
|
|
|
|
def reconnect(self):
|
|
|
|
self._sock.close()
|
|
|
|
self.connected = False
|
|
|
|
GObject.source_remove(self._watch_id)
|
|
|
|
GObject.timeout_add(5000, self._connectToSocketServer)
|
|
|
|
|
|
|
|
def sendCommand(self, cmd):
|
2016-04-26 12:49:56 +03:00
|
|
|
# print("Server command: " + cmd)
|
2015-02-11 16:53:17 +03:00
|
|
|
if self.connected:
|
|
|
|
try:
|
2018-09-10 16:05:46 +03:00
|
|
|
self._sock.send(cmd.encode() if python3 else cmd)
|
2015-02-11 16:53:17 +03:00
|
|
|
except:
|
|
|
|
print("Sending failed.")
|
|
|
|
self.reconnect()
|
|
|
|
else:
|
2015-03-22 19:49:09 +03:00
|
|
|
print("Cannot send, not connected!")
|
2014-08-13 14:08:38 +04:00
|
|
|
|
2015-02-11 16:53:17 +03:00
|
|
|
def addListener(self, listener):
|
|
|
|
self._listeners.append(listener)
|
2015-01-12 16:48:49 +03:00
|
|
|
|
2015-02-11 16:53:17 +03:00
|
|
|
def _connectToSocketServer(self):
|
2014-08-30 05:32:53 +04:00
|
|
|
try:
|
2015-02-11 16:53:17 +03:00
|
|
|
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2016-10-25 22:45:47 +03:00
|
|
|
sock_file = os.path.join(get_runtime_dir(), appname, "socket")
|
|
|
|
try:
|
|
|
|
self._sock.connect(sock_file) # fails if sock_file doesn't exist
|
|
|
|
self.connected = True
|
|
|
|
self._watch_id = GObject.io_add_watch(self._sock, GObject.IO_IN, self._handle_notify)
|
2017-05-10 10:37:10 +03:00
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
self.sendCommand('VERSION:\n')
|
2017-05-10 10:37:10 +03:00
|
|
|
self.sendCommand('GET_STRINGS:\n')
|
|
|
|
|
2016-10-25 22:45:47 +03:00
|
|
|
return False # Don't run again
|
|
|
|
except Exception as e:
|
2018-04-25 14:22:34 +03:00
|
|
|
print("Could not connect to unix socket " + sock_file + ". " + str(e))
|
2023-06-02 14:55:08 +03:00
|
|
|
except Exception as e: # Bad habit
|
2015-10-31 15:07:18 +03:00
|
|
|
print("Connect could not be established, try again later.")
|
2015-02-11 16:53:17 +03:00
|
|
|
self._sock.close()
|
2014-08-31 02:32:37 +04:00
|
|
|
|
2015-10-31 15:07:18 +03:00
|
|
|
return True # Run again, if enabled via timeout_add()
|
2015-02-11 16:53:17 +03:00
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
# Reads data that becomes available.
|
|
|
|
# New responses can be accessed with get_available_responses().
|
|
|
|
# Returns false if no data was received within timeout
|
|
|
|
def read_socket_data_with_timeout(self, timeout):
|
|
|
|
self._sock.settimeout(timeout)
|
|
|
|
try:
|
|
|
|
self._remainder += self._sock.recv(1024)
|
|
|
|
except socket.timeout:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
self._sock.settimeout(None)
|
|
|
|
|
|
|
|
# Parses response lines out of collected data, returns list of strings
|
|
|
|
def get_available_responses(self):
|
2018-04-25 14:22:34 +03:00
|
|
|
end = self._remainder.rfind(b'\n')
|
2018-01-19 13:32:08 +03:00
|
|
|
if end == -1:
|
|
|
|
return []
|
|
|
|
data = self._remainder[:end]
|
|
|
|
self._remainder = self._remainder[end+1:]
|
2018-09-10 16:05:46 +03:00
|
|
|
data = data.decode() if python3 else data
|
|
|
|
return data.split('\n')
|
2018-01-19 13:32:08 +03:00
|
|
|
|
2015-10-31 15:07:18 +03:00
|
|
|
# Notify is the raw answer from the socket
|
2015-02-11 16:53:17 +03:00
|
|
|
def _handle_notify(self, source, condition):
|
2018-01-19 13:32:08 +03:00
|
|
|
# Blocking is ok since we're notified of available data
|
|
|
|
self._remainder += self._sock.recv(1024)
|
|
|
|
|
|
|
|
if len(self._remainder) == 0:
|
2015-02-11 16:53:17 +03:00
|
|
|
return False
|
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
for line in self.get_available_responses():
|
|
|
|
self.handle_server_response(line)
|
|
|
|
|
2015-10-31 15:07:18 +03:00
|
|
|
return True # Run again
|
2015-02-11 16:53:17 +03:00
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
def handle_server_response(self, line):
|
2018-04-25 14:22:34 +03:00
|
|
|
# print("Server response: " + line)
|
2015-02-11 16:53:17 +03:00
|
|
|
parts = line.split(':')
|
|
|
|
action = parts[0]
|
|
|
|
args = parts[1:]
|
|
|
|
|
|
|
|
for listener in self._listeners:
|
|
|
|
listener(action, args)
|
2015-01-12 16:48:49 +03:00
|
|
|
|
2015-02-11 16:53:17 +03:00
|
|
|
def _update_registered_paths(self, action, args):
|
|
|
|
if action == 'REGISTER_PATH':
|
|
|
|
self.registered_paths[args[0]] = 1
|
|
|
|
elif action == 'UNREGISTER_PATH':
|
|
|
|
del self.registered_paths[args[0]]
|
2015-01-12 16:48:49 +03:00
|
|
|
|
2015-02-11 16:53:17 +03:00
|
|
|
# Check if there are no paths left. If so, its usual
|
|
|
|
# that mirall went away. Try reconnecting.
|
|
|
|
if not self.registered_paths:
|
|
|
|
self.reconnect()
|
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
def _get_version(self, action, args):
|
|
|
|
if action == 'VERSION':
|
|
|
|
self.protocolVersion = args[1]
|
|
|
|
|
2015-02-11 16:53:17 +03:00
|
|
|
socketConnect = SocketConnect()
|
|
|
|
|
|
|
|
|
2018-06-04 14:48:31 +03:00
|
|
|
class MenuExtension_ownCloud(GObject.GObject, Nautilus.MenuProvider):
|
2015-01-12 16:48:49 +03:00
|
|
|
def __init__(self):
|
2015-02-11 16:53:17 +03:00
|
|
|
GObject.GObject.__init__(self)
|
2015-01-12 16:48:49 +03:00
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
self.strings = {}
|
|
|
|
socketConnect.addListener(self.handle_commands)
|
|
|
|
|
|
|
|
def handle_commands(self, action, args):
|
|
|
|
if action == 'STRING':
|
|
|
|
self.strings[args[0]] = ':'.join(args[1:])
|
|
|
|
|
2016-10-23 00:20:27 +03:00
|
|
|
def check_registered_paths(self, filename):
|
|
|
|
topLevelFolder = False
|
|
|
|
internalFile = False
|
2020-05-02 15:41:54 +03:00
|
|
|
absfilename = os.path.realpath(filename)
|
2016-10-23 00:20:27 +03:00
|
|
|
for reg_path in socketConnect.registered_paths:
|
2020-05-02 15:41:54 +03:00
|
|
|
if absfilename == reg_path:
|
2016-10-23 00:20:27 +03:00
|
|
|
topLevelFolder = True
|
|
|
|
break
|
2020-05-02 15:41:54 +03:00
|
|
|
if absfilename.startswith(reg_path):
|
2016-10-23 00:20:27 +03:00
|
|
|
internalFile = True
|
|
|
|
# you can't have a registered path below another so it is save to break here
|
|
|
|
break
|
|
|
|
return (topLevelFolder, internalFile)
|
|
|
|
|
2022-08-26 21:03:03 +03:00
|
|
|
# The get_file_items method of Nautilus.MenuProvider no longer takes
|
|
|
|
# the window argument. To keep supporting older versions of Nautilus,
|
|
|
|
# we can use variadic arguments.
|
|
|
|
def get_file_items(self, *args):
|
2016-10-23 00:20:27 +03:00
|
|
|
# Show the menu extension to share a file or folder
|
|
|
|
|
2022-08-26 21:03:03 +03:00
|
|
|
files = args[-1]
|
2018-01-22 12:22:51 +03:00
|
|
|
# Get usable file paths from the uris
|
|
|
|
all_internal_files = True
|
|
|
|
for i, file_uri in enumerate(files):
|
|
|
|
filename = get_local_path(file_uri.get_uri())
|
2020-05-02 18:01:35 +03:00
|
|
|
filename = os.path.realpath(filename)
|
2018-01-22 12:22:51 +03:00
|
|
|
|
|
|
|
# Check if its a folder (ends with an /), if yes add a "/"
|
|
|
|
# otherwise it will not find the entry in the table
|
|
|
|
isDir = os.path.isdir(filename + os.sep)
|
|
|
|
if isDir:
|
|
|
|
filename += os.sep
|
|
|
|
|
|
|
|
# Check if toplevel folder, we need to ignore those as they cannot be shared
|
|
|
|
topLevelFolder, internalFile = self.check_registered_paths(filename)
|
|
|
|
if not internalFile:
|
|
|
|
all_internal_files = False
|
|
|
|
|
|
|
|
files[i] = filename
|
|
|
|
|
|
|
|
# Don't show a context menu if some selected files aren't in a sync folder
|
|
|
|
if not all_internal_files:
|
2017-05-10 10:37:10 +03:00
|
|
|
return []
|
2016-10-23 00:20:27 +03:00
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
if socketConnect.protocolVersion >= '1.1': # lexicographic!
|
2018-01-22 12:22:51 +03:00
|
|
|
return self.ask_for_menu_items(files)
|
2018-01-19 13:32:08 +03:00
|
|
|
else:
|
2018-01-22 12:22:51 +03:00
|
|
|
return self.legacy_menu_items(files)
|
2018-01-19 13:32:08 +03:00
|
|
|
|
2018-01-22 12:22:51 +03:00
|
|
|
def ask_for_menu_items(self, files):
|
|
|
|
record_separator = '\x1e'
|
|
|
|
filesstring = record_separator.join(files)
|
2018-08-15 12:06:51 +03:00
|
|
|
socketConnect.sendCommand(u'GET_MENU_ITEMS:{}\n'.format(filesstring))
|
2018-01-19 13:32:08 +03:00
|
|
|
|
|
|
|
done = False
|
|
|
|
start = time.time()
|
|
|
|
timeout = 0.1 # 100ms
|
|
|
|
menu_items = []
|
|
|
|
while not done:
|
|
|
|
dt = time.time() - start
|
|
|
|
if dt >= timeout:
|
|
|
|
break
|
|
|
|
if not socketConnect.read_socket_data_with_timeout(timeout - dt):
|
|
|
|
break
|
|
|
|
for line in socketConnect.get_available_responses():
|
|
|
|
# Process lines we don't care about
|
|
|
|
if done or not (line.startswith('GET_MENU_ITEMS:') or line.startswith('MENU_ITEM:')):
|
|
|
|
socketConnect.handle_server_response(line)
|
|
|
|
continue
|
|
|
|
if line == 'GET_MENU_ITEMS:END':
|
|
|
|
done = True
|
|
|
|
# don't break - we'd discard other responses
|
|
|
|
if line.startswith('MENU_ITEM:'):
|
|
|
|
args = line.split(':')
|
|
|
|
if len(args) < 4:
|
|
|
|
continue
|
|
|
|
menu_items.append([args[1], 'd' not in args[2], ':'.join(args[3:])])
|
|
|
|
|
|
|
|
if not done:
|
2018-01-22 12:22:51 +03:00
|
|
|
return self.legacy_menu_items(files)
|
|
|
|
|
|
|
|
if len(menu_items) == 0:
|
|
|
|
return []
|
2018-01-19 13:32:08 +03:00
|
|
|
|
|
|
|
# Set up the 'ownCloud...' submenu
|
|
|
|
item_owncloud = Nautilus.MenuItem(
|
|
|
|
name='IntegrationMenu', label=self.strings.get('CONTEXT_MENU_TITLE', appname))
|
|
|
|
menu = Nautilus.Menu()
|
|
|
|
item_owncloud.set_submenu(menu)
|
|
|
|
|
|
|
|
for action, enabled, label in menu_items:
|
|
|
|
item = Nautilus.MenuItem(name=action, label=label, sensitive=enabled)
|
2018-01-22 12:22:51 +03:00
|
|
|
item.connect("activate", self.context_menu_action, action, filesstring)
|
2018-01-19 13:32:08 +03:00
|
|
|
menu.append_item(item)
|
|
|
|
|
|
|
|
return [item_owncloud]
|
|
|
|
|
|
|
|
|
2018-01-22 12:22:51 +03:00
|
|
|
def legacy_menu_items(self, files):
|
|
|
|
# No legacy menu for a selection of several files
|
|
|
|
if len(files) != 1:
|
|
|
|
return []
|
|
|
|
filename = files[0]
|
|
|
|
|
2016-10-23 00:20:27 +03:00
|
|
|
entry = socketConnect.nautilusVFSFile_table.get(filename)
|
|
|
|
if not entry:
|
2017-05-10 10:37:10 +03:00
|
|
|
return []
|
2016-10-23 00:20:27 +03:00
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
# Currently 'sharable' also controls access to private link actions,
|
|
|
|
# and we definitely don't want to show them for IGNORED.
|
2016-10-23 00:20:27 +03:00
|
|
|
shareable = False
|
|
|
|
state = entry['state']
|
|
|
|
state_ok = state.startswith('OK')
|
|
|
|
state_sync = state.startswith('SYNC')
|
2020-03-05 02:08:24 +03:00
|
|
|
isDir = os.path.isdir(filename + os.sep)
|
2016-10-23 00:20:27 +03:00
|
|
|
if state_ok:
|
|
|
|
shareable = True
|
|
|
|
elif state_sync and isDir:
|
|
|
|
# some file below is OK or SYNC
|
|
|
|
for key, value in socketConnect.nautilusVFSFile_table.items():
|
|
|
|
if key != filename and key.startswith(filename):
|
|
|
|
state = value['state']
|
|
|
|
if state.startswith('OK') or state.startswith('SYNC'):
|
|
|
|
shareable = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not shareable:
|
2017-05-10 10:37:10 +03:00
|
|
|
return []
|
|
|
|
|
|
|
|
# Set up the 'ownCloud...' submenu
|
|
|
|
item_owncloud = Nautilus.MenuItem(
|
|
|
|
name='IntegrationMenu', label=self.strings.get('CONTEXT_MENU_TITLE', appname))
|
|
|
|
menu = Nautilus.Menu()
|
|
|
|
item_owncloud.set_submenu(menu)
|
|
|
|
|
|
|
|
# Add share menu option
|
|
|
|
item = Nautilus.MenuItem(
|
|
|
|
name='NautilusPython::ShareItem',
|
|
|
|
label=self.strings.get('SHARE_MENU_TITLE', 'Share...'))
|
2018-01-19 13:32:08 +03:00
|
|
|
item.connect("activate", self.context_menu_action, 'SHARE', filename)
|
2017-05-10 10:37:10 +03:00
|
|
|
menu.append_item(item)
|
|
|
|
|
|
|
|
# Add permalink menu options, but hide these options for older clients
|
|
|
|
# that don't have these actions.
|
2017-07-06 12:32:58 +03:00
|
|
|
if 'COPY_PRIVATE_LINK_MENU_TITLE' in self.strings:
|
2017-05-10 10:37:10 +03:00
|
|
|
item_copyprivatelink = Nautilus.MenuItem(
|
2017-07-06 12:32:58 +03:00
|
|
|
name='CopyPrivateLink', label=self.strings.get('COPY_PRIVATE_LINK_MENU_TITLE', 'Copy private link to clipboard'))
|
2018-01-19 13:32:08 +03:00
|
|
|
item_copyprivatelink.connect("activate", self.context_menu_action, 'COPY_PRIVATE_LINK', filename)
|
2017-05-10 10:37:10 +03:00
|
|
|
menu.append_item(item_copyprivatelink)
|
|
|
|
|
2017-07-06 12:32:58 +03:00
|
|
|
if 'EMAIL_PRIVATE_LINK_MENU_TITLE' in self.strings:
|
2017-05-10 10:37:10 +03:00
|
|
|
item_emailprivatelink = Nautilus.MenuItem(
|
2017-07-06 12:32:58 +03:00
|
|
|
name='EmailPrivateLink', label=self.strings.get('EMAIL_PRIVATE_LINK_MENU_TITLE', 'Send private link by email...'))
|
2018-01-19 13:32:08 +03:00
|
|
|
item_emailprivatelink.connect("activate", self.context_menu_action, 'EMAIL_PRIVATE_LINK', filename)
|
2017-05-10 10:37:10 +03:00
|
|
|
menu.append_item(item_emailprivatelink)
|
|
|
|
|
|
|
|
return [item_owncloud]
|
|
|
|
|
|
|
|
|
2018-01-19 13:32:08 +03:00
|
|
|
def context_menu_action(self, menu, action, filename):
|
2018-04-25 14:22:34 +03:00
|
|
|
# print("Context menu: " + action + ' ' + filename)
|
2017-05-10 10:37:10 +03:00
|
|
|
socketConnect.sendCommand(action + ":" + filename + "\n")
|
2015-01-12 16:48:49 +03:00
|
|
|
|
|
|
|
|
2018-06-04 14:48:31 +03:00
|
|
|
class SyncStateExtension_ownCloud(GObject.GObject, Nautilus.InfoProvider):
|
2015-02-11 16:53:17 +03:00
|
|
|
def __init__(self):
|
|
|
|
GObject.GObject.__init__(self)
|
2015-01-21 19:41:13 +03:00
|
|
|
|
2015-07-29 12:29:10 +03:00
|
|
|
socketConnect.nautilusVFSFile_table = {}
|
2015-02-11 16:53:17 +03:00
|
|
|
socketConnect.addListener(self.handle_commands)
|
2014-08-31 02:32:37 +04:00
|
|
|
|
|
|
|
def find_item_for_file(self, path):
|
2015-07-29 12:29:10 +03:00
|
|
|
if path in socketConnect.nautilusVFSFile_table:
|
|
|
|
return socketConnect.nautilusVFSFile_table[path]
|
2014-08-30 05:32:53 +04:00
|
|
|
else:
|
|
|
|
return None
|
2014-08-20 19:31:00 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
def askForOverlay(self, file):
|
2015-10-31 15:07:18 +03:00
|
|
|
# print("Asking for overlay for "+file) # For debug only
|
2014-06-06 19:49:19 +04:00
|
|
|
if os.path.isdir(file):
|
2015-02-11 16:53:17 +03:00
|
|
|
folderStatus = socketConnect.sendCommand("RETRIEVE_FOLDER_STATUS:"+file+"\n");
|
2014-08-31 02:32:37 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
if os.path.isfile(file):
|
2015-02-11 16:53:17 +03:00
|
|
|
fileStatus = socketConnect.sendCommand("RETRIEVE_FILE_STATUS:"+file+"\n");
|
2014-06-06 19:49:19 +04:00
|
|
|
|
2014-08-31 02:32:37 +04:00
|
|
|
def invalidate_items_underneath(self, path):
|
2014-08-30 05:32:53 +04:00
|
|
|
update_items = []
|
2015-07-29 12:29:10 +03:00
|
|
|
if not socketConnect.nautilusVFSFile_table:
|
2014-11-09 18:59:01 +03:00
|
|
|
self.askForOverlay(path)
|
|
|
|
else:
|
2015-07-29 12:29:10 +03:00
|
|
|
for p in socketConnect.nautilusVFSFile_table:
|
2014-11-09 18:59:01 +03:00
|
|
|
if p == path or p.startswith(path):
|
2015-07-29 12:29:10 +03:00
|
|
|
item = socketConnect.nautilusVFSFile_table[p]['item']
|
2016-04-14 16:24:46 +03:00
|
|
|
update_items.append(p)
|
2014-08-20 19:31:00 +04:00
|
|
|
|
2016-04-14 16:24:46 +03:00
|
|
|
for path1 in update_items:
|
|
|
|
socketConnect.nautilusVFSFile_table[path1]['item'].invalidate_extension_info()
|
2014-08-20 19:31:00 +04:00
|
|
|
|
2015-09-10 14:29:08 +03:00
|
|
|
# Handles a single line of server response and sets the emblem
|
2015-02-11 16:53:17 +03:00
|
|
|
def handle_commands(self, action, args):
|
2015-10-31 15:07:18 +03:00
|
|
|
# file = args[0] # For debug only
|
|
|
|
# print("Action for " + file + ": " + args[0]) # For debug only
|
2015-02-11 16:53:17 +03:00
|
|
|
if action == 'STATUS':
|
|
|
|
newState = args[0]
|
2015-07-29 12:47:00 +03:00
|
|
|
filename = ':'.join(args[1:])
|
|
|
|
|
2016-04-26 12:49:56 +03:00
|
|
|
itemStore = self.find_item_for_file(filename)
|
|
|
|
if itemStore:
|
|
|
|
if( not itemStore['state'] or newState != itemStore['state'] ):
|
|
|
|
item = itemStore['item']
|
|
|
|
|
|
|
|
# print("Setting emblem on " + filename + "<>" + emblem + "<>") # For debug only
|
|
|
|
|
|
|
|
# If an emblem is already set for this item, we need to
|
|
|
|
# clear the existing extension info before setting a new one.
|
|
|
|
#
|
|
|
|
# That will also trigger a new call to
|
|
|
|
# update_file_info for this item! That's why we set
|
|
|
|
# skipNextUpdate to True: we don't want to pull the
|
|
|
|
# current data from the client after getting a push
|
|
|
|
# notification.
|
|
|
|
invalidate = itemStore['state'] != None
|
|
|
|
if invalidate:
|
|
|
|
item.invalidate_extension_info()
|
|
|
|
self.set_emblem(item, newState)
|
|
|
|
|
|
|
|
socketConnect.nautilusVFSFile_table[filename] = {
|
|
|
|
'item': item,
|
|
|
|
'state': newState,
|
|
|
|
'skipNextUpdate': invalidate }
|
2015-02-11 16:53:17 +03:00
|
|
|
|
|
|
|
elif action == 'UPDATE_VIEW':
|
|
|
|
# Search all items underneath this path and invalidate them
|
|
|
|
if args[0] in socketConnect.registered_paths:
|
|
|
|
self.invalidate_items_underneath(args[0])
|
|
|
|
|
|
|
|
elif action == 'REGISTER_PATH':
|
|
|
|
self.invalidate_items_underneath(args[0])
|
|
|
|
elif action == 'UNREGISTER_PATH':
|
|
|
|
self.invalidate_items_underneath(args[0])
|
2014-06-06 19:49:19 +04:00
|
|
|
|
2016-04-26 12:49:56 +03:00
|
|
|
def set_emblem(self, item, state):
|
|
|
|
Emblems = { 'OK' : appname +'_ok',
|
|
|
|
'SYNC' : appname +'_sync',
|
|
|
|
'NEW' : appname +'_sync',
|
|
|
|
'IGNORE' : appname +'_warn',
|
|
|
|
'ERROR' : appname +'_error',
|
|
|
|
'OK+SWM' : appname +'_ok_shared',
|
|
|
|
'SYNC+SWM' : appname +'_sync_shared',
|
|
|
|
'NEW+SWM' : appname +'_sync_shared',
|
|
|
|
'IGNORE+SWM': appname +'_warn_shared',
|
|
|
|
'ERROR+SWM' : appname +'_error_shared',
|
|
|
|
'NOP' : ''
|
|
|
|
}
|
|
|
|
|
|
|
|
emblem = 'NOP' # Show nothing if no emblem is defined.
|
|
|
|
if state in Emblems:
|
|
|
|
emblem = Emblems[state]
|
|
|
|
item.add_emblem(emblem)
|
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
def update_file_info(self, item):
|
|
|
|
if item.get_uri_scheme() != 'file':
|
|
|
|
return
|
|
|
|
|
2015-02-26 11:40:54 +03:00
|
|
|
filename = get_local_path(item.get_uri())
|
2020-05-02 15:41:54 +03:00
|
|
|
filename = os.path.realpath(filename)
|
2014-07-25 14:11:12 +04:00
|
|
|
if item.is_directory():
|
2016-10-25 21:15:17 +03:00
|
|
|
filename += os.sep
|
2014-08-30 05:32:53 +04:00
|
|
|
|
2016-04-26 12:49:56 +03:00
|
|
|
inScope = False
|
2015-02-11 16:53:17 +03:00
|
|
|
for reg_path in socketConnect.registered_paths:
|
2014-08-30 05:32:53 +04:00
|
|
|
if filename.startswith(reg_path):
|
2016-04-26 12:49:56 +03:00
|
|
|
inScope = True
|
2014-08-30 05:32:53 +04:00
|
|
|
break
|
2016-04-26 12:49:56 +03:00
|
|
|
|
|
|
|
if not inScope:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Ask for the current state from the client -- unless this update was
|
|
|
|
# triggered by receiving a STATUS message from the client in the first
|
|
|
|
# place.
|
|
|
|
itemStore = self.find_item_for_file(filename)
|
|
|
|
if itemStore and itemStore['skipNextUpdate'] and itemStore['state']:
|
|
|
|
itemStore['skipNextUpdate'] = False
|
|
|
|
itemStore['item'] = item
|
|
|
|
self.set_emblem(item, itemStore['state'])
|
|
|
|
else:
|
|
|
|
socketConnect.nautilusVFSFile_table[filename] = {
|
|
|
|
'item': item,
|
|
|
|
'state': None,
|
|
|
|
'skipNextUpdate': False }
|
|
|
|
|
|
|
|
# item.add_string_attribute('share_state', "share state") # ?
|
|
|
|
self.askForOverlay(filename)
|