-
Notifications
You must be signed in to change notification settings - Fork 31
/
window_commands.py
509 lines (390 loc) · 16.2 KB
/
window_commands.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# coding: utf-8
import sys
import os
import json
import webbrowser
from collections import defaultdict
import sublime_plugin
import sublime
PY2 = sys.version_info < (3, 0)
try:
from .floo import sublime_ui
from .floo.common import api, reactor, msg, shared as G, utils
from .floo.common.exc_fmt import str_e
assert api and G and msg and utils
except (ImportError, ValueError):
from floo import sublime_ui
from floo.common import reactor, msg, shared as G, utils
from floo.common.exc_fmt import str_e
reactor = reactor.reactor
SublimeUI = sublime_ui.SublimeUI()
def disconnect_dialog():
if G.AGENT and G.AGENT.joined_workspace:
disconnect = sublime.ok_cancel_dialog('You can only be in one workspace at a time.', 'Leave %s/%s' % (G.AGENT.owner, G.AGENT.workspace))
if disconnect:
msg.debug('Stopping agent.')
reactor.stop()
G.AGENT = None
return disconnect
return True
class FloobitsBaseCommand(sublime_plugin.WindowCommand):
def is_visible(self, *args, **kwargs):
return True
def is_enabled(self, *args, **kwargs):
return bool(G.AGENT and G.AGENT.is_ready())
class FloobitsOpenSettingsCommand(sublime_plugin.WindowCommand):
def run(self):
window = sublime.active_window()
if window:
window.open_file(G.FLOORC_JSON_PATH)
class FloobitsShareDirCommand(FloobitsBaseCommand):
def is_enabled(self, *args, **kwargs):
return not super(FloobitsShareDirCommand, self).is_enabled()
def run(self, dir_to_share=None, paths=None, current_file=False, api_args=None):
if paths:
if len(paths) != 1:
return sublime.error_message('Only one folder at a time, please. :(')
return SublimeUI.share_dir(self.window, paths[0], api_args)
if dir_to_share is None:
folders = self.window.folders()
if folders:
dir_to_share = folders[0]
else:
dir_to_share = os.path.expanduser(os.path.join('~', 'share_me'))
SublimeUI.prompt_share_dir(self.window, dir_to_share, api_args)
class FloobitsDeleteWorkspaceCommand(FloobitsBaseCommand):
def is_visible(self, *args, **kwargs):
return True
def is_enabled(self, *args, **kwargs):
return utils.can_auth()
def run(self, force=False):
SublimeUI.delete_workspace(self.window, lambda *args, **kwargs: None)
class FloobitsRefreshWorkspaceCommand(FloobitsBaseCommand):
def run(self):
G.AGENT.refresh_workspace()
class FloobitsPromptJoinWorkspaceCommand(sublime_plugin.WindowCommand):
def run(self, workspace=None):
if workspace is None:
workspace = 'https://%s/' % G.DEFAULT_HOST
for d in self.window.folders():
floo_file = os.path.join(d, '.floo')
try:
floo_info = open(floo_file, 'r').read()
wurl = json.loads(floo_info).get('url')
utils.parse_url(wurl)
# TODO: check if workspace actually exists
workspace = wurl
break
except Exception:
pass
self.window.show_input_panel('Workspace URL:', workspace, self.on_input, None, None)
def on_input(self, workspace_url):
if disconnect_dialog():
SublimeUI.join_workspace_by_url(self.window, workspace_url, self.window.folders())
class FloobitsPinocchioCommand(sublime_plugin.WindowCommand):
def is_visible(self, *args, **kwargs):
return self.is_enabled()
def is_enabled(self, *args, **kwargs):
return G.AUTO_GENERATED_ACCOUNT
def run(self):
floorc = utils.load_floorc_json()
auth = floorc.get('AUTH', {}).get(G.DEFAULT_HOST, {})
username = auth.get('username')
secret = auth.get('secret')
print(username, secret)
if not (username and secret):
return sublime.error_message('You don\'t seem to have a Floobits account of any sort')
webbrowser.open('https://%s/%s/pinocchio/%s' % (G.DEFAULT_HOST, username, secret))
class FloobitsLeaveWorkspaceCommand(FloobitsBaseCommand):
def run(self):
if G.AGENT:
message = 'You have left the workspace.'
owner = G.AGENT.owner
workspace = G.AGENT.workspace
if owner and workspace:
message = 'You have left %s/%s' % (owner, workspace)
G.AGENT.update_status_msg(message)
reactor.stop()
G.AGENT = None
# TODO: Mention the name of the thing we left
if not G.EXPERT_MODE:
sublime.error_message(message)
else:
sublime.error_message('You are not joined to any workspace.')
def is_enabled(self, *args, **kwargs):
return bool(G.AGENT)
class FloobitsClearHighlightsCommand(FloobitsBaseCommand):
def run(self):
G.AGENT.clear_highlights(self.window.active_view())
class FloobitsSummonCommand(FloobitsBaseCommand):
# TODO: ghost this option if user doesn't have permissions
def run(self):
G.AGENT.summon(self.window.active_view())
class FloobitsJoinRecentWorkspaceCommand(sublime_plugin.WindowCommand):
def _get_recent_workspaces(self):
self.persistent_data = utils.get_persistent_data()
self.recent_workspaces = self.persistent_data['recent_workspaces']
try:
recent_workspaces = [x.get('url') for x in self.recent_workspaces if x.get('url') is not None]
except Exception:
pass
return recent_workspaces
def run(self, *args):
workspaces = self._get_recent_workspaces()
self.window.show_quick_panel(workspaces, self.on_done)
def on_done(self, item):
if item == -1:
return
workspace = self.recent_workspaces[item]
SublimeUI.join_workspace_by_url(self.window, workspace['url'])
def is_enabled(self, *args, **kwargs):
return bool(len(self._get_recent_workspaces()) > 0)
class FloobitsAddToWorkspaceCommand(FloobitsBaseCommand):
def run(self, paths, current_file=False):
if not self.is_enabled():
return
if paths is None and current_file:
active_view = self.window.active_view()
if active_view is None:
msg.log('AddToWorkspace: No active view found. Perhaps active tab is an image?')
return
paths = [active_view.file_name()]
notshared = []
for path in paths:
if utils.is_shared(path):
G.AGENT.upload(path)
else:
notshared.append(path)
if notshared:
limit = 5
sublime.error_message("The following paths are not a child of\n\n%s\n\nand will not be shared for security reasons:\n\n%s%s." %
(G.PROJECT_PATH, ",\n".join(notshared[:limit]), len(notshared) > limit and ",\n..." or ""))
def description(self):
return 'Add file or directory to currently-joined Floobits workspace.'
class FloobitsRemoveFromWorkspaceCommand(FloobitsBaseCommand):
def run(self, paths, current_file=False):
if not self.is_enabled():
return
if paths is None and current_file:
active_view = self.window.active_view()
if active_view is None:
msg.log('RemoveFromWorkspace: No active view found. Perhaps active tab is an image?')
return
paths = [active_view.file_name()]
if not hasattr(sublime, 'yes_no_cancel_dialog'):
unlink = bool(sublime.ok_cancel_dialog('Delete? Select cancel to remove from the workspace without deleting.', 'Delete'))
else:
ret = sublime.yes_no_cancel_dialog("What should I do with\n%s" % "\n".join(paths[:5]), "Delete!", "Just Remove from Workspace.")
if ret == 0:
return
unlink = ret == 1
for path in paths:
G.AGENT.delete_buf(path, unlink)
def description(self):
return 'Add file or directory to currently-joined Floobits workspace.'
class FloobitsCreateHangoutCommand(FloobitsBaseCommand):
def run(self):
owner = G.AGENT.owner
workspace = G.AGENT.workspace
host = G.AGENT.proto.host
webbrowser.open('https://plus.google.com/hangouts/_?gid=770015849706&gd=%s/%s/%s' % (host, owner, workspace))
def is_enabled(self, *args, **kwargs):
return bool(super(FloobitsCreateHangoutCommand, self).is_enabled() and G.AGENT.owner and G.AGENT.workspace)
class FloobitsPromptHangoutCommand(FloobitsBaseCommand):
def run(self, hangout_url):
confirm = bool(sublime.ok_cancel_dialog('This workspace is being edited in a Google+ Hangout? Do you want to join the hangout?'))
if not confirm:
return
webbrowser.open(hangout_url)
def is_visible(self, *args, **kwargs):
return False
def is_enabled(self, *args, **kwargs):
return bool(super(FloobitsPromptHangoutCommand, self).is_enabled() and G.AGENT.owner and G.AGENT.workspace)
class FloobitsOpenWebEditorCommand(FloobitsBaseCommand):
def run(self):
try:
agent = G.AGENT
d = {
'port': agent.proto.port,
'secure': agent.proto.secure,
'owner': agent.owner,
'workspace': agent.workspace,
'host': agent.proto.host,
}
view = self.window.active_view()
if view:
path = view.file_name()
if path and utils.is_shared(path):
d['path'] = utils.to_rel_path(path)
try:
d['line'] = view.rowcol(view.sel()[0].a)[0]
except Exception:
pass
url = utils.to_workspace_url(d)
webbrowser.open(url)
except Exception as e:
sublime.error_message('Unable to open workspace in web editor: %s' % str_e(e))
class FloobitsHelpCommand(FloobitsBaseCommand):
def run(self):
webbrowser.open('https://floobits.com/help/plugins/sublime', new=2, autoraise=True)
def is_visible(self, *args, **kwargs):
return True
def is_enabled(self, *args, **kwargs):
return True
class FloobitsToggleFollowModeCommand(FloobitsBaseCommand):
def run(self):
if G.FOLLOW_MODE:
self.window.run_command('floobits_disable_follow_mode')
else:
self.window.run_command('floobits_enable_follow_mode')
class FloobitsEnableFollowModeCommand(FloobitsBaseCommand):
def run(self):
G.FOLLOW_MODE = True
msg.log('Follow mode enabled')
G.AGENT.update_status_msg()
G.AGENT.highlight()
def is_visible(self, *args, **kwargs):
if G.AGENT:
return self.is_enabled()
return True
def is_enabled(self, *args, **kwargs):
return bool(super(FloobitsEnableFollowModeCommand, self).is_enabled() and not G.FOLLOW_MODE)
class FloobitsDisableFollowModeCommand(FloobitsBaseCommand):
def run(self):
G.FOLLOW_MODE = False
G.FOLLOW_USERS.clear()
G.SPLIT_MODE = False
msg.log('Follow mode disabled')
G.AGENT.update_status_msg('Stopped following changes.')
def is_visible(self, *args, **kwargs):
return self.is_enabled()
def is_enabled(self, *args, **kwargs):
return bool(super(FloobitsDisableFollowModeCommand, self).is_enabled() and G.FOLLOW_MODE)
class FloobitsFollowUser(FloobitsBaseCommand):
def run(self):
following_users = bool(G.FOLLOW_USERS)
def f():
if G.FOLLOW_USERS:
G.FOLLOW_MODE = True
G.SPLIT_MODE = False
G.AGENT.update_status_msg('Following changes.')
elif following_users:
# If we were following users and now we're not, disable follow mode
G.FOLLOW_MODE = False
G.AGENT.update_status_msg('Stopped following changes.')
SublimeUI.follow_user(self.window, f)
class FloobitsOpenWorkspaceSettingsCommand(FloobitsBaseCommand):
def run(self):
url = G.AGENT.workspace_url + '/settings'
webbrowser.open(url, new=2, autoraise=True)
def is_enabled(self, *args, **kwargs):
return bool(super(FloobitsOpenWorkspaceSettingsCommand, self).is_enabled() and G.PERMS and 'kick' in G.PERMS)
class RequestPermissionCommand(FloobitsBaseCommand):
def run(self, perms, *args, **kwargs):
G.AGENT.send({
'name': 'request_perms',
'perms': perms
})
def is_enabled(self, *args, **kwargs):
if not super(RequestPermissionCommand, self).is_enabled():
return False
if 'patch' in G.PERMS:
return False
return True
class FloobitsFollowSplit(FloobitsBaseCommand):
def run(self):
G.SPLIT_MODE = True
G.FOLLOW_MODE = True
if self.window.num_groups() == 1:
self.window.set_layout({
'cols': [0.0, 1.0],
'rows': [0.0, 0.5, 1.0],
'cells': [[0, 0, 1, 1], [0, 1, 1, 2]]
})
class FloobitsSetupCommand(FloobitsBaseCommand):
def is_visible(self, *args, **kwargs):
return True
def is_enabled(self, *args, **kwargs):
return not utils.can_auth()
def run(self, force=False):
def f(x):
print(x)
SublimeUI.create_or_link_account(self.window, G.DEFAULT_HOST, force, f)
class FloobitsListUsersCommand(FloobitsBaseCommand):
def run(self):
self.users = defaultdict(list)
users = []
try:
users = G.AGENT.workspace_info.get('users', [])
except Exception as e:
print(e)
for k, c in users.items():
self.users[c['username']].append(c)
def format_user(clients):
clients = ', '.join([c['client'] for c in clients])
return clients
self.users = list(self.users.items())
users = [[username, format_user(u)] for username, u in self.users]
# TODO: on highlight, follow user
self.window.show_quick_panel(users, self.on_user_select)
def on_user_select(self, item):
if item == -1:
return
self.user = self.users[item]
username = self.user[0]
self.actions = [
'Kick',
]
if username in G.FOLLOW_USERS:
self.actions.append('Unfollow')
else:
self.actions.append('Follow')
actions = ['%s %s' % (a, username) for a in self.actions]
self.window.show_quick_panel(actions, self.on_user_action)
def on_user_action(self, item):
if item == -1:
return
if not (G.AGENT and G.AGENT.is_ready()):
return
username = self.user[0]
action = self.actions[item]
if action == 'Kick':
for c in self.user[1]:
G.AGENT.kick(c['user_id'])
elif action == 'Follow':
msg.debug('Following %s' % username)
G.FOLLOW_MODE = True
G.FOLLOW_USERS.add(username)
G.AGENT.highlight(user=username)
elif action == 'Unfollow':
msg.debug('Unfollowing %s' % username)
G.FOLLOW_USERS.remove(username)
if not G.FOLLOW_USERS:
G.FOLLOW_MODE = False
class FloobitsNotACommand(sublime_plugin.WindowCommand):
def run(self, *args, **kwargs):
pass
def is_visible(self, *args, **kwargs):
return True
def is_enabled(self, *args, **kwargs):
return False
def description(self):
return
class FloobitsRequestCodeReview(FloobitsBaseCommand):
def run(self):
prompt = "Describe your problem:"
initial = ""
def cb(description):
if not description:
return sublime.error_message('You must give a description')
agent = G.AGENT
host = agent.proto.host
owner = agent.owner
workspace = agent.workspace
try:
res = api.request_review(host, owner, workspace, description)
message = res.body['message']
except Exception as e:
return sublime.error_message('%s' % e)
sublime.message_dialog(message)
SublimeUI.user_charfield(self.window, prompt, initial, cb)