2014-07-25 14:11:12 +04:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
import os
|
|
|
|
import urllib
|
|
|
|
import socket
|
|
|
|
|
|
|
|
from gi.repository import GObject, Nautilus
|
|
|
|
|
|
|
|
class ownCloudExtension(GObject.GObject, Nautilus.ColumnProvider, Nautilus.InfoProvider):
|
2014-08-31 02:32:37 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
nautilusVFSFile_table = {}
|
2014-07-25 14:11:12 +04:00
|
|
|
registered_paths = {}
|
|
|
|
remainder = ''
|
2014-08-13 14:08:38 +04:00
|
|
|
connected = False
|
|
|
|
watch_id = 0
|
2014-07-25 14:11:12 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
def __init__(self):
|
2014-08-13 14:08:38 +04:00
|
|
|
self.connectToOwnCloud
|
|
|
|
if not self.connected:
|
2014-08-31 02:32:37 +04:00
|
|
|
# try again in 5 seconds - attention, logic inverted!
|
|
|
|
GObject.timeout_add(5000, self.connectToOwnCloud)
|
2014-08-13 14:08:38 +04:00
|
|
|
|
2014-08-15 19:00:44 +04:00
|
|
|
def port(self):
|
2014-08-30 05:32:53 +04:00
|
|
|
return 34001 # Fixme, read from config file.
|
2014-08-15 19:00:44 +04:00
|
|
|
|
2014-08-13 14:08:38 +04:00
|
|
|
def connectToOwnCloud(self):
|
2014-08-30 05:32:53 +04:00
|
|
|
try:
|
2014-08-31 02:32:37 +04:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.sock.connect(("localhost", self.port()))
|
|
|
|
self.sock.settimeout(5)
|
|
|
|
self.connected = True
|
|
|
|
self.watch_id = GObject.io_add_watch(self.sock, GObject.IO_IN, self.handle_notify)
|
2014-08-13 14:08:38 +04:00
|
|
|
except:
|
2014-08-31 02:32:37 +04:00
|
|
|
print("Connect could not be established, try again later!")
|
|
|
|
self.sock.close()
|
2014-08-30 05:32:53 +04:00
|
|
|
return not self.connected
|
2014-08-31 02:32:37 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
def sendCommand(self, cmd):
|
2014-08-13 14:08:38 +04:00
|
|
|
if self.connected:
|
2014-08-31 02:32:37 +04:00
|
|
|
try:
|
|
|
|
self.sock.send(cmd)
|
|
|
|
except:
|
|
|
|
print("Sending failed.")
|
|
|
|
GObject.source_remove(self.watch_id)
|
|
|
|
self.connected = False
|
|
|
|
GObject.timeout_add(5000, self.connectToOwnCloud)
|
|
|
|
|
|
|
|
def find_item_for_file(self, path):
|
2014-08-30 05:32:53 +04:00
|
|
|
if path in self.nautilusVFSFile_table:
|
|
|
|
return self.nautilusVFSFile_table[path]
|
|
|
|
else:
|
|
|
|
return None
|
2014-08-20 19:31:00 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
def askForOverlay(self, file):
|
|
|
|
if os.path.isdir(file):
|
|
|
|
folderStatus = self.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):
|
|
|
|
fileStatus = self.sendCommand("RETRIEVE_FILE_STATUS:"+file+"\n");
|
|
|
|
|
2014-08-31 02:32:37 +04:00
|
|
|
def invalidate_items_underneath(self, path):
|
2014-08-30 05:32:53 +04:00
|
|
|
update_items = []
|
|
|
|
for p in self.nautilusVFSFile_table:
|
2014-08-31 02:32:37 +04:00
|
|
|
if p == path or p.startswith(path):
|
2014-08-30 05:32:53 +04:00
|
|
|
item = self.nautilusVFSFile_table[p]
|
|
|
|
update_items.append(item)
|
2014-08-20 19:31:00 +04:00
|
|
|
|
2014-08-30 05:32:53 +04:00
|
|
|
for item in update_items:
|
|
|
|
item.invalidate_extension_info()
|
|
|
|
# self.update_file_info(item)
|
2014-08-20 19:31:00 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
# Handles a single line of server respoonse and sets the emblem
|
|
|
|
def handle_server_response(self, l):
|
2014-08-04 19:21:49 +04:00
|
|
|
Emblems = { 'OK' : 'oC_ok',
|
2014-08-30 05:32:53 +04:00
|
|
|
'SYNC' : 'oC_sync',
|
|
|
|
'NEW' : 'oC_sync',
|
|
|
|
'IGNORE' : 'oC_warn',
|
|
|
|
'ERROR' : 'oC_error',
|
|
|
|
'OK+SWM' : 'oC_ok_shared',
|
|
|
|
'SYNC+SWM' : 'oC_sync_shared',
|
|
|
|
'NEW+SWM' : 'oC_sync_shared',
|
|
|
|
'IGNORE+SWM': 'oC_warn_shared',
|
|
|
|
'ERROR+SWM' : 'oC_error_shared',
|
|
|
|
'NOP' : 'oC_error'
|
|
|
|
}
|
|
|
|
|
2014-08-31 02:32:37 +04:00
|
|
|
print("Server response: "+l)
|
2014-06-06 19:49:19 +04:00
|
|
|
parts = l.split(':')
|
2014-07-25 14:11:12 +04:00
|
|
|
if len(parts) > 0:
|
2014-08-30 05:32:53 +04:00
|
|
|
action = parts[0]
|
2014-07-25 14:11:12 +04:00
|
|
|
|
2014-08-30 05:32:53 +04:00
|
|
|
# file = parts[1]
|
|
|
|
# print "Action for " + file + ": "+parts[0]
|
2014-07-25 14:11:12 +04:00
|
|
|
if action == 'STATUS':
|
2014-06-06 19:49:19 +04:00
|
|
|
emblem = Emblems[parts[1]]
|
2014-07-25 14:11:12 +04:00
|
|
|
if emblem:
|
|
|
|
item = self.find_item_for_file(parts[2])
|
|
|
|
if item:
|
|
|
|
item.add_emblem(emblem)
|
2014-08-15 19:03:15 +04:00
|
|
|
|
2014-08-30 05:32:53 +04:00
|
|
|
elif action == 'UPDATE_VIEW':
|
|
|
|
# Search all items underneath this path and invalidate them
|
|
|
|
if parts[1] in self.registered_paths:
|
2014-08-31 02:32:37 +04:00
|
|
|
self.invalidate_items_underneath(parts[1])
|
2014-08-30 05:32:53 +04:00
|
|
|
|
|
|
|
elif action == 'REGISTER_PATH':
|
|
|
|
self.registered_paths[parts[1]] = 1
|
2014-08-31 02:32:37 +04:00
|
|
|
self.invalidate_items_underneath(parts[1])
|
2014-08-30 05:32:53 +04:00
|
|
|
elif action == 'UNREGISTER_PATH':
|
|
|
|
del self.registered_paths[parts[1]]
|
2014-08-31 02:32:37 +04:00
|
|
|
self.invalidate_items_underneath(parts[1])
|
2014-08-30 05:32:53 +04:00
|
|
|
|
|
|
|
# check if there are non pathes any more, if so, its usual
|
|
|
|
# that mirall went away. Try reconnect.
|
|
|
|
if not self.registered_paths:
|
|
|
|
self.sock.close()
|
|
|
|
self.connected = False
|
2014-08-31 02:32:37 +04:00
|
|
|
GObject.source_remove(self.watch_id)
|
2014-08-20 19:31:00 +04:00
|
|
|
GObject.timeout_add(5000, self.connectToOwnCloud)
|
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
else:
|
2014-07-25 14:11:12 +04:00
|
|
|
# print "We got unknown action " + action
|
|
|
|
1
|
2014-06-06 19:49:19 +04:00
|
|
|
|
|
|
|
# notify is the raw answer from the socket
|
|
|
|
def handle_notify(self, source, condition):
|
2014-07-25 14:11:12 +04:00
|
|
|
|
2014-08-30 05:32:53 +04:00
|
|
|
data = source.recv(1024)
|
|
|
|
# prepend the remaining data from last call
|
|
|
|
if len(self.remainder) > 0:
|
|
|
|
data = self.remainder+data
|
|
|
|
self.remainder = ''
|
|
|
|
|
|
|
|
if len(data) > 0:
|
|
|
|
# remember the remainder for next round
|
|
|
|
lastNL = data.rfind('\n');
|
|
|
|
if lastNL > -1 and lastNL < len(data):
|
|
|
|
self.remainder = data[lastNL+1:]
|
|
|
|
data = data[:lastNL]
|
|
|
|
|
|
|
|
for l in data.split('\n'):
|
2014-08-31 02:32:37 +04:00
|
|
|
self.handle_server_response(l)
|
2014-08-06 11:52:44 +04:00
|
|
|
else:
|
|
|
|
return False
|
2014-07-25 14:11:12 +04:00
|
|
|
|
2014-08-30 05:32:53 +04:00
|
|
|
return True # run again
|
2014-08-31 02:32:37 +04:00
|
|
|
|
2014-06-06 19:49:19 +04:00
|
|
|
def get_local_path(self, path):
|
|
|
|
return path.replace("file://", "")
|
|
|
|
|
|
|
|
def update_file_info(self, item):
|
|
|
|
if item.get_uri_scheme() != 'file':
|
|
|
|
return
|
|
|
|
|
|
|
|
filename = urllib.unquote(item.get_uri()[7:])
|
2014-07-25 14:11:12 +04:00
|
|
|
if item.is_directory():
|
2014-08-30 05:32:53 +04:00
|
|
|
filename += '/'
|
|
|
|
|
|
|
|
for reg_path in self.registered_paths:
|
|
|
|
if filename.startswith(reg_path):
|
|
|
|
self.nautilusVFSFile_table[filename] = item
|
2014-08-31 02:32:37 +04:00
|
|
|
|
2014-08-30 05:32:53 +04:00
|
|
|
# item.add_string_attribute('share_state', "share state")
|
|
|
|
self.askForOverlay(filename)
|
|
|
|
break
|
|
|
|
else:
|
2014-08-31 02:32:37 +04:00
|
|
|
print("Not in scope:"+filename)
|