REBOL [
	Title: "IOS Sharing Manager 1.0"
	Type: 'link-app
	Version: 1.0.1
	Copyright: "REBOL Technologies 2003"
	Author: "Carl Sassenrath"
]

test-mode: off

all-users:  []
all-groups: []
folder-sets: []
safe-copy: func [val] [copy any [val []]]
fset: none

set [w-users w-groups] [
	users:
	groups:
]

out: layout [
	style lab text 400x20 white black font-size 11 bold para [origin: 4x2] middle
	style text-list text-list with [
		; Patch text list bug:
		update: has [item value] [
			sld/redrag lc / max 1 length? head lines
			if item: find data picked/1 [
				sld/data: min 1 (index? item) / (max 1 lc)
				sn: max 0 to-integer sld/data * ((1 + length? head lines) - lc)
			][
				sld/value: 0.0
				pane/offset: 0x0
			]
			self
		]
	]

	backcolor 230.230.230
	at 0x0 lb: logo-bar 24x300
	at 1x1 btn-help [browse http://www.reboltech.com/express/docs/share.html]
	origin 24x0 space 1x0

	fttl: lab 400 "Private Folders (by Fileset Name):"
	fslst: text-list 400x360 [
		submit-changes
		shared-users: shared-groups: none
		clear-selections/only
		if all [
			fset: value
			tags: find app-reg fset
			files: third tags
			tags: second tags
		][
			shared-users:  safe-copy select tags w-users
			shared-groups: safe-copy select tags w-groups
		]
		show-shared-users
		show-shared-groups
	]
	panel 400x40 [
		across origin 10x3 space 2
		ch-only: radio on [fttl/text: "Private Folders (by Fileset Name):" refresh-data]
		pad 0x-4
		text "Show only private folders"
		return
		ch-all: radio [fttl/text: "All Filesets:" refresh-data]
		pad 0x-4
		text "Show all filesets (folders & system)"
	]
	return
	lab 120 "Users:"
	ulst: text-list 120x160 [
		remove find shared-users value
		show-shared-users
	]
	lab 120 "All Users:"
	aulst: text-list 120x180 [
		if all [
			shared-users
			not find shared-users value
		][
			append shared-users value
			show-shared-users
		]
	]
	here: at
	return
	lab 120 "Groups:"
	glst: text-list 120x160 [
		remove find shared-groups value
		show-shared-groups
	]
	lab 120 "All Groups:"
	aglst: text-list 120x180  [
		if all [
			shared-groups
			not find shared-groups value
		][
			append shared-groups value
			show-shared-groups
		]
	]

	across at here + 90x10
	guide
	btn-enter 72 "Change" [
		submit-changes
	]
	btn-cancel 72 "Close" [quit]
	origin 0
]
lb/resize/y out/size/y

clear-selections: func [/only] [
	clear ulst/picked
	clear glst/picked
	clear aulst/picked
	clear aglst/picked
	either only [show [aulst aglst]][clear fslst/picked]
]

show-shared-users: does [
	if not shared-users [exit]
	insert clear ulst/data shared-users
	show ulst/update
]

show-shared-groups: does [
	if not shared-groups [exit]
	insert clear glst/data shared-groups
	show glst/update
]

changed?: has [users groups] [
	; Return true if user made changes:
	if not fset [exit]
	users:  any [select tags w-users []]
	groups: any [select tags w-groups []]
	any [
		not empty? difference users shared-users
		not empty? difference groups shared-groups
	]
]

make-changes: has [users groups spot s1 s2] [
	if not fset [exit]
	users:  select tags w-users
	groups: select tags w-groups
	either not any [users groups] [
		if not confirm "Change fileset from public to private?" [exit]
	][
		if all [empty? shared-users empty? shared-groups] [
			if not confirm "Change fileset from private to public?" [exit]
		]
	]
	remove/part s1: find tags w-users 2
	remove/part s2: find tags w-groups 2
	spot: any [s1 s2 head tags]
	if not empty? shared-groups [insert spot reduce [w-groups copy shared-groups]] 
	if not empty? shared-users [insert spot reduce [w-users copy shared-users]] 
]

submit-changes: does [
	if all [
		fset
		changed?
		confirm reform ["Do you want to submit your changes to the" fset "fileset?"]
	][
		send-change
	]
]

send-change: does [
	make-changes
	either test-mode [?? tags][
		send-server change-tags reduce [fset tags files] 
	]
]

refresh-data: does [
	flash "Updating..."
	get-link app-reg func [data] [app-reg: data]
	all-users:  extract load link-root/system/users.r 2
	all-groups: extract load link-root/system/groups.r 2
	clear folder-sets
	foreach [a b c] app-reg [
		if any [
			ch-all/data
			find b w-users
			find b w-groups
		][
			append folder-sets a
		]
	]
	sort folder-sets
	insert clear fslst/data folder-sets
	insert clear aulst/data all-users
	insert clear aglst/data all-groups
	clear-selections
	fslst/update
	aulst/update
	aglst/update
	unview
	show out
]

if value? 'send-server [refresh-data]
view center-face out
