1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#!/usr/bin/python3
#
# Copyright (C) by Klaas Freitag <freitag@owncloud.com>
#
# 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.
import os
import urllib
import socket
from gi.repository import GObject, Nautilus
class syncStateExtension(GObject.GObject, Nautilus.ColumnProvider, Nautilus.InfoProvider):
nautilusVFSFile_table = {}
registered_paths = {}
remainder = ''
connected = False
watch_id = 0
appname = 'ownCloud'
def __init__(self):
self.connectToSocketServer
if not self.connected:
# try again in 5 seconds - attention, logic inverted!
GObject.timeout_add(5000, self.connectToSocketServer)
def connectToSocketServer(self):
do_reconnect = True
try:
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
postfix = "/"+self.appname+"/socket"
sock_file = os.environ["XDG_RUNTIME_DIR"]+postfix
print ("XXXX " + sock_file + " <=> " + postfix)
if sock_file != postfix:
try:
print("Socket File: "+sock_file)
self.sock.connect(sock_file)
self.connected = True
print("Setting connected to %r" % self.connected )
self.watch_id = GObject.io_add_watch(self.sock, GObject.IO_IN, self.handle_notify)
do_reconnect = False
except Exception, e:
print("Could not connect to unix socket." + str(e))
else:
print("Sock-File not valid: "+sock_file)
except Exception, e:
print("Connect could not be established, try again later " + str(e))
self.sock.close()
# print("Returning %r" % do_reconnect)
return do_reconnect
def sendCommand(self, cmd):
if self.connected:
try:
self.sock.send(cmd)
except:
print("Sending failed.")
GObject.source_remove(self.watch_id)
self.connected = False
GObject.timeout_add(5000, self.connectToSocketServer)
def find_item_for_file(self, path):
if path in self.nautilusVFSFile_table:
return self.nautilusVFSFile_table[path]
else:
return None
def askForOverlay(self, file):
# print("Asking for overlay for "+file)
if os.path.isdir(file):
folderStatus = self.sendCommand("RETRIEVE_FOLDER_STATUS:"+file+"\n");
if os.path.isfile(file):
fileStatus = self.sendCommand("RETRIEVE_FILE_STATUS:"+file+"\n");
def invalidate_items_underneath(self, path):
update_items = []
if not self.nautilusVFSFile_table:
self.askForOverlay(path)
else:
for p in self.nautilusVFSFile_table:
if p == path or p.startswith(path):
item = self.nautilusVFSFile_table[p]['item']
update_items.append(item)
for item in update_items:
item.invalidate_extension_info()
# Handles a single line of server respoonse and sets the emblem
def handle_server_response(self, l):
Emblems = { 'OK' : self.appname +'_ok',
'SYNC' : self.appname +'_sync',
'NEW' : self.appname +'_sync',
'IGNORE' : self.appname +'_warn',
'ERROR' : self.appname +'_error',
'OK+SWM' : self.appname +'_ok_shared',
'SYNC+SWM' : self.appname +'_sync_shared',
'NEW+SWM' : self.appname +'_sync_shared',
'IGNORE+SWM': self.appname +'_warn_shared',
'ERROR+SWM' : self.appname +'_error_shared',
'NOP' : self.appname +'_error'
}
print("Server response: "+l)
parts = l.split(':')
if len(parts) > 0:
action = parts[0]
# file = parts[1]
# print "Action for " + file + ": "+parts[0]
if action == 'STATUS':
newState = parts[1]
emblem = Emblems[newState]
if emblem:
itemStore = self.find_item_for_file(parts[2])
if itemStore:
if( not itemStore['state'] or newState != itemStore['state'] ):
item = itemStore['item']
item.add_emblem(emblem)
# print "Setting emblem on " + parts[2]+ "<>"+emblem+"<>"
self.nautilusVFSFile_table[parts[2]] = {'item': item, 'state':newState}
elif action == 'UPDATE_VIEW':
# Search all items underneath this path and invalidate them
if parts[1] in self.registered_paths:
self.invalidate_items_underneath(parts[1])
elif action == 'REGISTER_PATH':
self.registered_paths[parts[1]] = 1
self.invalidate_items_underneath(parts[1])
elif action == 'UNREGISTER_PATH':
del self.registered_paths[parts[1]]
self.invalidate_items_underneath(parts[1])
# 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
GObject.source_remove(self.watch_id)
GObject.timeout_add(5000, self.connectToSocketServer)
else:
# print "We got unknown action " + action
1
# notify is the raw answer from the socket
def handle_notify(self, source, condition):
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'):
self.handle_server_response(l)
else:
return False
return True # run again
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:])
if item.is_directory():
filename += '/'
for reg_path in self.registered_paths:
if filename.startswith(reg_path):
self.nautilusVFSFile_table[filename] = {'item': item, 'state':''}
# item.add_string_attribute('share_state', "share state")
self.askForOverlay(filename)
break
else:
# print("Not in scope:"+filename)
pass
|