FriendGroups.lua 31.1 KB
Newer Older
1
local PA = _G.ProjectAzilroka
Azilroka's avatar
Azilroka committed
2
local FG = PA:NewModule('FriendGroup', 'AceEvent-3.0', 'AceTimer-3.0', 'AceHook-3.0')
3 4 5
_G.FriendGroup = FG

FG.Title = '|cFF16C3F2Friend|r |cFFFFFFFFGroups|r'
Azilroka's avatar
Azilroka committed
6
FG.Desciption = 'Manage Firends List with Groups'
Azilroka's avatar
Azilroka committed
7 8
FG.Authors = 'Azilroka'
FG.Credits = 'Mikeprod    frankkkkk'
9

Azilroka's avatar
Azilroka committed
10 11 12
local hooks = {}

local function Hook(source, target, secure)
13 14 15 16 17 18
	hooks[source] = _G[source]
	if secure then
		hooksecurefunc(source, target)
	else
		_G[source] = target
	end
Azilroka's avatar
Azilroka committed
19 20
end

Azilroka's avatar
Update  
Azilroka committed
21
local FRIENDS_GROUP_NAME_COLOR = NORMAL_FONT_COLOR
Azilroka's avatar
Azilroka committed
22

Azilroka's avatar
Update  
Azilroka committed
23 24 25 26 27 28 29 30 31 32 33 34
local INVITE_RESTRICTION_NO_TOONS = 0
local INVITE_RESTRICTION_CLIENT = 1
local INVITE_RESTRICTION_LEADER = 2
local INVITE_RESTRICTION_FACTION = 3
local INVITE_RESTRICTION_INFO = 4
local INVITE_RESTRICTION_NONE = 5

local ONE_MINUTE = 60
local ONE_HOUR = 60 * ONE_MINUTE
local ONE_DAY = 24 * ONE_HOUR
local ONE_MONTH = 30 * ONE_DAY
local ONE_YEAR = 12 * ONE_MONTH
Azilroka's avatar
Azilroka committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

local FriendButtons = { count = 0 }
local GroupCount = 0
local GroupTotal = {}
local GroupOnline = {}
local GroupSorted = {}

local FriendRequestString = string.sub(FRIEND_REQUESTS,1,-5)

local OPEN_DROPDOWNMENUS_SAVE = nil
local friend_popup_menus = { "FRIEND", "FRIEND_OFFLINE", "BN_FRIEND", "BN_FRIEND_OFFLINE" }
UnitPopupButtons["FRIEND_GROUP_NEW"] = { text = "Create new group", dist = 0 }
UnitPopupButtons["FRIEND_GROUP_ADD"] = { text = "Add to group", dist = 0, nested = 1 }
UnitPopupButtons["FRIEND_GROUP_DEL"] = { text = "Remove from group", dist = 0, nested = 1 }
UnitPopupMenus["FRIEND_GROUP_ADD"] = { }
UnitPopupMenus["FRIEND_GROUP_DEL"] = { }

local function ClassColourCode(class,table)
	local initialClass = class
54 55 56 57 58 59
	for k, v in pairs(LOCALIZED_CLASS_NAMES_FEMALE) do
		if class == v then
			class = k
			break
		end
	end
Azilroka's avatar
Azilroka committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	if class == initialClass then
		for k, v in pairs(LOCALIZED_CLASS_NAMES_MALE) do
			if class == v then
				class = k
				break
			end
		end
	end
	if table then
		return RAID_CLASS_COLORS[class]
	else
		local colour = RAID_CLASS_COLORS[class]
		return string.format("|cFF%02x%02x%02x", colour.r*255, colour.g*255, colour.b*255)
	end
end

local function FriendGroups_GetTopButton(offset)
Azilroka's avatar
Update  
Azilroka committed
77
	local usedHeight = 0
Azilroka's avatar
Azilroka committed
78
	for i = 1, FriendButtons.count do
Azilroka's avatar
Update  
Azilroka committed
79
		local buttonHeight = FRIENDS_BUTTON_HEIGHTS[FriendButtons[i].buttonType]
Azilroka's avatar
Azilroka committed
80
		if ( usedHeight + buttonHeight >= offset ) then
Azilroka's avatar
Update  
Azilroka committed
81
			return i - 1, offset - usedHeight
Azilroka's avatar
Azilroka committed
82
		else
Azilroka's avatar
Update  
Azilroka committed
83
			usedHeight = usedHeight + buttonHeight
Azilroka's avatar
Azilroka committed
84 85 86 87 88 89
		end
	end
	return 0,0
end

local function FriendGroups_UpdateFriendButton(button)
Azilroka's avatar
Update  
Azilroka committed
90 91 92 93 94 95
	local index = button.index
	button.buttonType = FriendButtons[index].buttonType
	button.id = FriendButtons[index].id
	local height = FRIENDS_BUTTON_HEIGHTS[button.buttonType]
	local nameText, nameColor, infoText, broadcastText
	local hasTravelPassButton = false
Azilroka's avatar
Azilroka committed
96
	if ( button.buttonType == FRIENDS_BUTTON_TYPE_WOW ) then
Azilroka's avatar
Update  
Azilroka committed
97 98
		local name, level, class, area, connected, status, note, isRaF, guid = GetFriendInfo(FriendButtons[index].id)
		broadcastText = nil
Azilroka's avatar
Azilroka committed
99
		if ( connected ) then
Azilroka's avatar
Update  
Azilroka committed
100
			button.background:SetColorTexture(FRIENDS_WOW_BACKGROUND_COLOR.r, FRIENDS_WOW_BACKGROUND_COLOR.g, FRIENDS_WOW_BACKGROUND_COLOR.b, FRIENDS_WOW_BACKGROUND_COLOR.a)
Azilroka's avatar
Azilroka committed
101
			if ( status == "" ) then
Azilroka's avatar
Update  
Azilroka committed
102
				button.status:SetTexture(FRIENDS_TEXTURE_ONLINE)
Azilroka's avatar
Azilroka committed
103
			elseif ( status == CHAT_FLAG_AFK ) then
Azilroka's avatar
Update  
Azilroka committed
104
				button.status:SetTexture(FRIENDS_TEXTURE_AFK)
Azilroka's avatar
Azilroka committed
105
			elseif ( status == CHAT_FLAG_DND ) then
Azilroka's avatar
Update  
Azilroka committed
106
				button.status:SetTexture(FRIENDS_TEXTURE_DND)
Azilroka's avatar
Azilroka committed
107 108 109
			end

			if FriendGroups_SavedVars.colour_classes then
Azilroka's avatar
Update  
Azilroka committed
110
				nameColor = ClassColourCode(class,true)
Azilroka's avatar
Azilroka committed
111
			else
Azilroka's avatar
Update  
Azilroka committed
112
				nameColor = FRIENDS_WOW_NAME_COLOR
Azilroka's avatar
Azilroka committed
113
			end
Azilroka's avatar
Update  
Azilroka committed
114
			nameText = name..", "..format(FRIENDS_LEVEL_TEMPLATE, level, class)
Azilroka's avatar
Azilroka committed
115
		else
Azilroka's avatar
Update  
Azilroka committed
116 117 118 119
			button.background:SetColorTexture(FRIENDS_OFFLINE_BACKGROUND_COLOR.r, FRIENDS_OFFLINE_BACKGROUND_COLOR.g, FRIENDS_OFFLINE_BACKGROUND_COLOR.b, FRIENDS_OFFLINE_BACKGROUND_COLOR.a)
			button.status:SetTexture(FRIENDS_TEXTURE_OFFLINE)
			nameText = name
			nameColor = FRIENDS_GRAY_COLOR
Azilroka's avatar
Azilroka committed
120
		end
Azilroka's avatar
Update  
Azilroka committed
121 122 123 124 125
		infoText = area
		button.gameIcon:Hide()
		button.summonButton:ClearAllPoints()
		button.summonButton:SetPoint("TOPRIGHT", button, "TOPRIGHT", 1, -1)
		FriendsFrame_SummonButton_Update(button.summonButton)
Azilroka's avatar
Azilroka committed
126
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_BNET ) then
Azilroka's avatar
Update  
Azilroka committed
127 128
		local bnetIDAccount, accountName, battleTag, isBattleTag, characterName, bnetIDGameAccount, client, isOnline, lastOnline, isBnetAFK, isBnetDND, messageText, noteText, isRIDFriend, messageTime, canSoR = BNGetFriendInfo(FriendButtons[index].id)
		broadcastText = messageText
Azilroka's avatar
Azilroka committed
129
		-- set up player name and character name
Azilroka's avatar
Update  
Azilroka committed
130
		local characterName = characterName
Azilroka's avatar
Azilroka committed
131
		if ( accountName ) then
Azilroka's avatar
Update  
Azilroka committed
132
			nameText = accountName
Azilroka's avatar
Azilroka committed
133
			if ( isOnline ) then
Azilroka's avatar
Update  
Azilroka committed
134
				characterName = BNet_GetValidatedCharacterName(characterName, battleTag, client)
Azilroka's avatar
Azilroka committed
135 136
			end
		else
Azilroka's avatar
Update  
Azilroka committed
137
			nameText = UNKNOWN
Azilroka's avatar
Azilroka committed
138 139 140 141 142
		end

		-- append character name
		if ( characterName ) then
			if ( client == BNET_CLIENT_WOW and CanCooperateWithGameAccount(bnetIDGameAccount) ) then
Azilroka's avatar
Update  
Azilroka committed
143
				local level = select(11, BNGetGameAccountInfo(bnetIDGameAccount))
Azilroka's avatar
Azilroka committed
144 145
				if FriendGroups_SavedVars.colour_classes then
					local class = select(8, BNGetGameAccountInfo(bnetIDGameAccount))
Azilroka's avatar
Update  
Azilroka committed
146
					nameText = nameText.." "..ClassColourCode(class).."("..characterName.."-"..level..")"..FONT_COLOR_CODE_CLOSE
Azilroka's avatar
Azilroka committed
147
				else
Azilroka's avatar
Update  
Azilroka committed
148
					nameText = nameText.." "..FRIENDS_WOW_NAME_COLOR_CODE.."("..characterName.."-"..level..")"..FONT_COLOR_CODE_CLOSE
Azilroka's avatar
Azilroka committed
149 150
				end
			else
Azilroka's avatar
Update  
Azilroka committed
151
				local level = select(11, BNGetGameAccountInfo(bnetIDGameAccount))
Azilroka's avatar
Azilroka committed
152
				if ( ENABLE_COLORBLIND_MODE == "1" ) then
Azilroka's avatar
Update  
Azilroka committed
153
					characterName = characterName..CANNOT_COOPERATE_LABEL
Azilroka's avatar
Azilroka committed
154 155
				end
				if level ~= "" then
Azilroka's avatar
Update  
Azilroka committed
156
					nameText = nameText.." "..FRIENDS_OTHER_NAME_COLOR_CODE.."("..characterName.."-"..level..")"..FONT_COLOR_CODE_CLOSE
Azilroka's avatar
Azilroka committed
157
				else
Azilroka's avatar
Update  
Azilroka committed
158
					nameText = nameText.." "..FRIENDS_OTHER_NAME_COLOR_CODE.."("..characterName..")"..FONT_COLOR_CODE_CLOSE
Azilroka's avatar
Azilroka committed
159 160 161 162 163
				end
			end
		end

		if ( isOnline ) then
Azilroka's avatar
Update  
Azilroka committed
164 165
			local _, _, _, realmName, realmID, faction, _, _, _, zoneName, _, gameText, _, _, _, _, _, isGameAFK, isGameBusy, guid = BNGetGameAccountInfo(bnetIDGameAccount)
			button.background:SetColorTexture(FRIENDS_BNET_BACKGROUND_COLOR.r, FRIENDS_BNET_BACKGROUND_COLOR.g, FRIENDS_BNET_BACKGROUND_COLOR.b, FRIENDS_BNET_BACKGROUND_COLOR.a)
Azilroka's avatar
Azilroka committed
166
			if ( isBnetAFK or isGameAFK ) then
Azilroka's avatar
Update  
Azilroka committed
167
				button.status:SetTexture(FRIENDS_TEXTURE_AFK)
Azilroka's avatar
Azilroka committed
168
			elseif ( isBnetDND or isGameBusy ) then
Azilroka's avatar
Update  
Azilroka committed
169
				button.status:SetTexture(FRIENDS_TEXTURE_DND)
Azilroka's avatar
Azilroka committed
170
			else
Azilroka's avatar
Update  
Azilroka committed
171
				button.status:SetTexture(FRIENDS_TEXTURE_ONLINE)
Azilroka's avatar
Azilroka committed
172 173 174
			end
			if ( client == BNET_CLIENT_WOW ) then
				if ( not zoneName or zoneName == "" ) then
Azilroka's avatar
Update  
Azilroka committed
175
					infoText = UNKNOWN
Azilroka's avatar
Azilroka committed
176
				else
Azilroka's avatar
Update  
Azilroka committed
177
					infoText = zoneName
Azilroka's avatar
Azilroka committed
178 179
				end
			else
Azilroka's avatar
Update  
Azilroka committed
180
				infoText = gameText
Azilroka's avatar
Azilroka committed
181
			end
Azilroka's avatar
Update  
Azilroka committed
182 183
			button.gameIcon:SetTexture(BNet_GetClientTexture(client))
			nameColor = FRIENDS_BNET_NAME_COLOR
Azilroka's avatar
Azilroka committed
184 185 186

			--Note - this logic should match the logic in FriendsFrame_ShouldShowSummonButton

Azilroka's avatar
Update  
Azilroka committed
187 188
			local shouldShowSummonButton = FriendsFrame_ShouldShowSummonButton(button.summonButton)
			button.gameIcon:SetShown(not shouldShowSummonButton)
Azilroka's avatar
Azilroka committed
189 190

			-- travel pass
Azilroka's avatar
Update  
Azilroka committed
191 192
			hasTravelPassButton = true
			local restriction = FriendsFrame_GetInviteRestriction(button.id)
Azilroka's avatar
Azilroka committed
193
			if ( restriction == INVITE_RESTRICTION_NONE ) then
Azilroka's avatar
Update  
Azilroka committed
194
				button.travelPassButton:Enable()
Azilroka's avatar
Azilroka committed
195
			else
Azilroka's avatar
Update  
Azilroka committed
196
				button.travelPassButton:Disable()
Azilroka's avatar
Azilroka committed
197 198
			end
		else
Azilroka's avatar
Update  
Azilroka committed
199 200 201 202
			button.background:SetColorTexture(FRIENDS_OFFLINE_BACKGROUND_COLOR.r, FRIENDS_OFFLINE_BACKGROUND_COLOR.g, FRIENDS_OFFLINE_BACKGROUND_COLOR.b, FRIENDS_OFFLINE_BACKGROUND_COLOR.a)
			button.status:SetTexture(FRIENDS_TEXTURE_OFFLINE)
			nameColor = FRIENDS_GRAY_COLOR
			button.gameIcon:Hide()
Azilroka's avatar
Azilroka committed
203
			if ( not lastOnline or lastOnline == 0 or time() - lastOnline >= ONE_YEAR ) then
Azilroka's avatar
Update  
Azilroka committed
204
				infoText = FRIENDS_LIST_OFFLINE
Azilroka's avatar
Azilroka committed
205
			else
Azilroka's avatar
Update  
Azilroka committed
206
				infoText = string.format(BNET_LAST_ONLINE_TIME, FriendsFrame_GetLastOnline(lastOnline))
Azilroka's avatar
Azilroka committed
207 208
			end
		end
Azilroka's avatar
Update  
Azilroka committed
209 210 211
		button.summonButton:ClearAllPoints()
		button.summonButton:SetPoint("CENTER", button.gameIcon, "CENTER", 1, 0)
		FriendsFrame_SummonButton_Update(button.summonButton)
Azilroka's avatar
Azilroka committed
212
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_DIVIDER ) then
Azilroka's avatar
Update  
Azilroka committed
213
		local title
Azilroka's avatar
Azilroka committed
214 215 216 217 218 219 220
		local group = FriendButtons[index].text
		if group == "" or not group then
			title = "[no group]"
		else
			title = group
		end
		button.text:SetText(title)
221
		button.text:Show()
Azilroka's avatar
Azilroka committed
222 223

		local counts = "(" .. GroupOnline[group] .. "/" .. GroupTotal[group] .. ")"
Azilroka's avatar
Update  
Azilroka committed
224
		nameText = counts
Azilroka's avatar
Azilroka committed
225 226 227 228 229 230 231 232 233 234 235
		nameColor = FRIENDS_GROUP_NAME_COLOR
		button.name:SetJustifyH("RIGHT")

		if FriendGroups_SavedVars.collapsed[group] then
			button.status:SetTexture("Interface\\Buttons\\UI-PlusButton-UP")
		else
			button.status:SetTexture("Interface\\Buttons\\UI-MinusButton-UP")
		end
		infoText = group
		button.info:Hide()
		button.gameIcon:Hide()
Azilroka's avatar
Update  
Azilroka committed
236
		button.background:SetColorTexture(FRIENDS_OFFLINE_BACKGROUND_COLOR.r, FRIENDS_OFFLINE_BACKGROUND_COLOR.g, FRIENDS_OFFLINE_BACKGROUND_COLOR.b, FRIENDS_OFFLINE_BACKGROUND_COLOR.a)
Azilroka's avatar
Azilroka committed
237
		button.background:SetAlpha(0.5)
Azilroka's avatar
Update  
Azilroka committed
238 239 240 241 242
		local scrollFrame = FriendsFrameFriendsScrollFrame
		local divider = scrollFrame.dividerPool:Acquire()
		divider:SetParent(scrollFrame.ScrollChild)
		divider:SetAllPoints(button)
		divider:Show()
Azilroka's avatar
Azilroka committed
243
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_INVITE_HEADER ) then
Azilroka's avatar
Update  
Azilroka committed
244 245 246 247 248
		local header = FriendsFrameFriendsScrollFrame.PendingInvitesHeaderButton
		header:SetPoint("TOPLEFT", button, 1, 0)
		header:Show()
		header:SetFormattedText(FRIEND_REQUESTS, BNGetNumFriendInvites())
		local collapsed = GetCVarBool("friendInvitesCollapsed")
Azilroka's avatar
Azilroka committed
249
		if ( collapsed ) then
Azilroka's avatar
Update  
Azilroka committed
250 251
			header.DownArrow:Hide()
			header.RightArrow:Show()
Azilroka's avatar
Azilroka committed
252
		else
Azilroka's avatar
Update  
Azilroka committed
253 254
			header.DownArrow:Show()
			header.RightArrow:Hide()
Azilroka's avatar
Azilroka committed
255
		end
Azilroka's avatar
Update  
Azilroka committed
256
		nameText = nil
Azilroka's avatar
Azilroka committed
257
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_INVITE ) then
Azilroka's avatar
Update  
Azilroka committed
258 259 260 261 262 263 264 265 266 267
		local scrollFrame = FriendsFrameFriendsScrollFrame
		local invite = scrollFrame.invitePool:Acquire()
		invite:SetParent(scrollFrame.ScrollChild)
		invite:SetAllPoints(button)
		invite:Show()
		local inviteID, accountName = BNGetFriendInviteInfo(button.id)
		invite.Name:SetText(accountName)
		invite.inviteID = inviteID
		invite.inviteIndex = button.id
		nameText = nil
Azilroka's avatar
Azilroka committed
268 269 270
	end
	-- travel pass?
	if ( hasTravelPassButton ) then
Azilroka's avatar
Update  
Azilroka committed
271
		button.travelPassButton:Show()
Azilroka's avatar
Azilroka committed
272
	else
Azilroka's avatar
Update  
Azilroka committed
273
		button.travelPassButton:Hide()
Azilroka's avatar
Azilroka committed
274 275 276
	end
	-- selection
	if ( FriendsFrame.selectedFriendType == FriendButtons[index].buttonType and FriendsFrame.selectedFriend == FriendButtons[index].id ) then
Azilroka's avatar
Update  
Azilroka committed
277
		button:LockHighlight()
Azilroka's avatar
Azilroka committed
278
	else
Azilroka's avatar
Update  
Azilroka committed
279
		button:UnlockHighlight()
Azilroka's avatar
Azilroka committed
280 281 282 283 284 285 286 287 288
	end
	-- finish setting up button if it's not a header
	if ( nameText ) then
		if button.buttonType ~= FRIENDS_BUTTON_TYPE_DIVIDER then
			button.text:Hide()
			button.name:SetJustifyH("LEFT")
			button.background:SetAlpha(1)
			button.info:Show()
		end
Azilroka's avatar
Update  
Azilroka committed
289 290 291 292
		button.name:SetText(nameText)
		button.name:SetTextColor(nameColor.r, nameColor.g, nameColor.b)
		button.info:SetText(infoText)
		button:Show()
Azilroka's avatar
Azilroka committed
293
	else
Azilroka's avatar
Update  
Azilroka committed
294
		button:Hide()
Azilroka's avatar
Azilroka committed
295 296 297
	end
	-- update the tooltip if hovering over a button
	if ( FriendsTooltip.button == button ) then
Azilroka's avatar
Update  
Azilroka committed
298
		FriendsFrameTooltip_Show(button)
Azilroka's avatar
Azilroka committed
299 300
	end
	if ( GetMouseFocus() == button ) then
Azilroka's avatar
Update  
Azilroka committed
301
		FriendsFrameTooltip_Show(button)
Azilroka's avatar
Azilroka committed
302
	end
Azilroka's avatar
Update  
Azilroka committed
303
	return height
Azilroka's avatar
Azilroka committed
304 305 306 307
end


local function FriendGroups_UpdateFriends()
Azilroka's avatar
Update  
Azilroka committed
308 309 310 311 312
	local scrollFrame = FriendsFrameFriendsScrollFrame
	local offset = HybridScrollFrame_GetOffset(scrollFrame)
	local buttons = scrollFrame.buttons
	local numButtons = #buttons
	local numFriendButtons = FriendButtons.count
Azilroka's avatar
Azilroka committed
313

Azilroka's avatar
Update  
Azilroka committed
314
	local usedHeight = 0
Azilroka's avatar
Azilroka committed
315

Azilroka's avatar
Update  
Azilroka committed
316 317 318
	scrollFrame.dividerPool:ReleaseAll()
	scrollFrame.invitePool:ReleaseAll()
	scrollFrame.PendingInvitesHeaderButton:Hide()
Azilroka's avatar
Azilroka committed
319
	for i = 1, numButtons do
Azilroka's avatar
Update  
Azilroka committed
320 321
		local button = buttons[i]
		local index = offset + i
Azilroka's avatar
Azilroka committed
322
		if ( index <= numFriendButtons ) then
Azilroka's avatar
Update  
Azilroka committed
323 324 325 326
			button.index = index
			local height = FriendGroups_UpdateFriendButton(button)
			button:SetHeight(height)
			usedHeight = usedHeight + height
Azilroka's avatar
Azilroka committed
327
		else
Azilroka's avatar
Update  
Azilroka committed
328 329
			button.index = nil
			button:Hide()
Azilroka's avatar
Azilroka committed
330 331
		end
	end
Azilroka's avatar
Update  
Azilroka committed
332
	HybridScrollFrame_Update(scrollFrame, scrollFrame.totalFriendListEntriesHeight, usedHeight)
Azilroka's avatar
Azilroka committed
333 334

	if hooks["FriendsFrame_UpdateFriends"] then
335 336
		hooks["FriendsFrame_UpdateFriends"]()
	end
Azilroka's avatar
Azilroka committed
337 338 339 340 341 342 343 344 345 346

	-- Delete unused groups in the collapsed part
	for key,_ in pairs(FriendGroups_SavedVars.collapsed) do
		if not GroupTotal[key] then
			FriendGroups_SavedVars.collapsed[key] = nil
		end
	end
end

local function FillGroups(groups, note, ...)
347 348 349 350 351 352 353 354 355 356 357
	wipe(groups)
	local n = select('#', ...)
	for i = 1, n do
		local v = select(i, ...)
		v = strtrim(v)
		groups[v] = true
	end
	if n == 0 then
		groups[""] = true
	end
	return note
Azilroka's avatar
Azilroka committed
358 359 360
end

local function NoteAndGroups(note, groups)
361 362 363 364 365 366 367
	if not note then
		return FillGroups(groups, "")
	end
	if groups then
		return FillGroups(groups, strsplit("#", note))
	end
	return strsplit("#", note)
Azilroka's avatar
Azilroka committed
368 369 370
end

local function CreateNote(note, groups)
371 372 373 374 375 376 377 378
	local value = ""
	if note then
		value = note
	end
	for group in pairs(groups) do
		value = value .. "#" .. group
	end
	return value
Azilroka's avatar
Azilroka committed
379 380 381
end

local function AddGroup(note, group)
382 383 384 385 386
	local groups = {}
	note = NoteAndGroups(note, groups)
	groups[""] = nil --ew
	groups[group] = true
	return CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
387 388 389
end

local function RemoveGroup(note, group)
390 391 392 393 394
	local groups = {}
	note = NoteAndGroups(note, groups)
	groups[""] = nil --ew
	groups[group] = nil
	return CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
395 396 397
end

local function IncrementGroup(group, online)
398 399 400 401 402 403 404 405 406
	if not GroupTotal[group] then
		GroupCount = GroupCount + 1
		GroupTotal[group] = 0
		GroupOnline[group] = 0
	end
	GroupTotal[group] = GroupTotal[group] + 1
	if online then
		GroupOnline[group] = GroupOnline[group] + 1
	end
Azilroka's avatar
Azilroka committed
407 408 409
end

local function FriendGroups_Update(forceUpdate)
Azilroka's avatar
Update  
Azilroka committed
410 411 412 413
	local numBNetTotal, numBNetOnline = BNGetNumFriends()
	local numBNetOffline = numBNetTotal - numBNetOnline
	local numWoWTotal, numWoWOnline = GetNumFriends()
	local numWoWOffline = numWoWTotal - numWoWOnline
Azilroka's avatar
Azilroka committed
414

Azilroka's avatar
Update  
Azilroka committed
415
	QuickJoinToastButton:UpdateDisplayedFriendCount()
Azilroka's avatar
Azilroka committed
416
	if ( not FriendsListFrame:IsShown() and not forceUpdate) then
Azilroka's avatar
Update  
Azilroka committed
417
		return
Azilroka's avatar
Azilroka committed
418 419 420
	end

	wipe(FriendButtons)
421 422 423 424
	wipe(GroupTotal)
	wipe(GroupOnline)
	wipe(GroupSorted)
	GroupCount = 0
Azilroka's avatar
Azilroka committed
425 426

	local BnetFriendGroups = {}
427
	local WowFriendGroups = {}
Azilroka's avatar
Azilroka committed
428 429
	local FriendReqGroup = {}

430
	local buttonCount = 0
Azilroka's avatar
Azilroka committed
431

Azilroka's avatar
Update  
Azilroka committed
432 433 434
	FriendButtons.count = 0
	local addButtonIndex = 0
	local totalButtonHeight = 0
Azilroka's avatar
Azilroka committed
435
	local function AddButtonInfo(buttonType, id)
Azilroka's avatar
Update  
Azilroka committed
436
		addButtonIndex = addButtonIndex + 1
Azilroka's avatar
Azilroka committed
437
		if ( not FriendButtons[addButtonIndex] ) then
Azilroka's avatar
Update  
Azilroka committed
438
			FriendButtons[addButtonIndex] = { }
Azilroka's avatar
Azilroka committed
439
		end
Azilroka's avatar
Update  
Azilroka committed
440 441 442 443
		FriendButtons[addButtonIndex].buttonType = buttonType
		FriendButtons[addButtonIndex].id = id
		FriendButtons.count = FriendButtons.count+1
		totalButtonHeight = totalButtonHeight + FRIENDS_BUTTON_HEIGHTS[buttonType]
Azilroka's avatar
Azilroka committed
444 445 446
	end

	-- invites
Azilroka's avatar
Update  
Azilroka committed
447
	local numInvites = BNGetNumFriendInvites()
Azilroka's avatar
Azilroka committed
448 449 450 451 452 453 454 455 456
	if ( numInvites > 0 ) then
		for i = 1, numInvites do
			if not FriendReqGroup[i] then
				FriendReqGroup[i] = {}
			end
			IncrementGroup(FriendRequestString,true)
			NoteAndGroups(_, FriendReqGroup[i])
			if not FriendGroups_SavedVars.collapsed[group] then
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
457
				AddButtonInfo(FRIENDS_BUTTON_TYPE_INVITE, i)
Azilroka's avatar
Azilroka committed
458 459 460 461 462 463
			end
		end
	end
	-- online Battlenet friends
	for i = 1, numBNetOnline do
		if not BnetFriendGroups[i] then
464 465 466
			-- print('Bnet Online', i)
			BnetFriendGroups[i] = {}
		end
Azilroka's avatar
Azilroka committed
467 468 469
		local noteText = select(13,BNGetFriendInfo(i))
		NoteAndGroups(noteText, BnetFriendGroups[i])
		for group in pairs(BnetFriendGroups[i]) do
470
			IncrementGroup(group, true)
Azilroka's avatar
Azilroka committed
471
			 if not FriendGroups_SavedVars.collapsed[group] then
472
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
473
				AddButtonInfo(FRIENDS_BUTTON_TYPE_BNET, i)
474
			end
Azilroka's avatar
Azilroka committed
475 476 477 478 479
		end
	end
	-- online WoW friends
	for i = 1, numWoWOnline do
		if not WowFriendGroups[i] then
480 481 482 483 484 485 486 487 488
			WowFriendGroups[i] = {}
			-- print('WoW Online', i)
		end
		local note = select(7,GetFriendInfo(i))
		NoteAndGroups(note, WowFriendGroups[i])
		for group in pairs(WowFriendGroups[i]) do
			IncrementGroup(group, true)
			if not FriendGroups_SavedVars.collapsed[group] then
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
489
				AddButtonInfo(FRIENDS_BUTTON_TYPE_WOW, i)
490 491
			end
		end
Azilroka's avatar
Azilroka committed
492 493 494 495 496
	end
	-- offline Battlenet friends
	for i = 1, numBNetOffline do
		local j = i + numBNetOnline
		if not BnetFriendGroups[j] then
497 498 499
			BnetFriendGroups[j] = {}
			-- print('Bnet Offline', j)
		end
Azilroka's avatar
Azilroka committed
500 501 502
		local noteText = select(13,BNGetFriendInfo(j))
		NoteAndGroups(noteText, BnetFriendGroups[j])
		for group in pairs(BnetFriendGroups[j]) do
503
			IncrementGroup(group)
Azilroka's avatar
Azilroka committed
504
			 if not FriendGroups_SavedVars.collapsed[group] and not FriendGroups_SavedVars.hide_offline then
505
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
506
				AddButtonInfo(FRIENDS_BUTTON_TYPE_BNET, j)
507
			end
Azilroka's avatar
Azilroka committed
508 509 510 511 512 513
		end
	end
	-- offline WoW friends
	for i = 1, numWoWOffline do
		local j = i + numWoWOnline
		if not WowFriendGroups[j] then
514 515 516 517 518 519 520 521 522
			WowFriendGroups[j] = {}
			-- print('WoW Offline', j)
		end
		local note = select(7,GetFriendInfo(j))
		NoteAndGroups(note, WowFriendGroups[j])
		for group in pairs(WowFriendGroups[j]) do
			IncrementGroup(group)
			if not FriendGroups_SavedVars.collapsed[group] and not FriendGroups_SavedVars.hide_offline then
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
523
				AddButtonInfo(FRIENDS_BUTTON_TYPE_WOW, j)
524 525
			end
		end
Azilroka's avatar
Azilroka committed
526 527 528
	end

	buttonCount = buttonCount + GroupCount
529
	totalScrollHeight = totalButtonHeight + GroupCount * FRIENDS_BUTTON_HEIGHTS[FRIENDS_BUTTON_TYPE_DIVIDER]
Azilroka's avatar
Azilroka committed
530

Azilroka's avatar
Update  
Azilroka committed
531 532
	FriendsFrameFriendsScrollFrame.totalFriendListEntriesHeight = totalScrollHeight
	FriendsFrameFriendsScrollFrame.numFriendListEntries = addButtonIndex
Azilroka's avatar
Azilroka committed
533 534

	if buttonCount > #FriendButtons then
535 536 537 538 539 540 541 542 543 544 545 546 547 548
		for i = #FriendButtons + 1, buttonCount do
			FriendButtons[i] = {}
		end
	end

	for group in pairs(GroupTotal) do
		table.insert(GroupSorted, group)
	end
	table.sort(GroupSorted)

	if GroupSorted[1] == "" then
		table.remove(GroupSorted, 1)
		table.insert(GroupSorted, "")
	end
Azilroka's avatar
Azilroka committed
549 550 551 552 553 554 555 556

	for key,val in pairs(GroupSorted) do
		if val == FriendRequestString then
			table.remove(GroupSorted,key)
			table.insert(GroupSorted,1,FriendRequestString)
		end
	end

557 558 559 560 561 562
	local index = 0
	for _,group in ipairs(GroupSorted) do
		index = index + 1
		FriendButtons[index].buttonType = FRIENDS_BUTTON_TYPE_DIVIDER
		FriendButtons[index].text = group
		if not FriendGroups_SavedVars.collapsed[group] then
Azilroka's avatar
Azilroka committed
563 564 565 566 567 568 569
			for i = 1, #FriendReqGroup do
				if group == FriendRequestString then
					index = index + 1
					FriendButtons[index].buttonType = FRIENDS_BUTTON_TYPE_INVITE
					FriendButtons[index].id = i
				end
			end
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
			for i = 1, numBNetOnline do
				if BnetFriendGroups[i][group] then
					index = index + 1
					FriendButtons[index].buttonType = FRIENDS_BUTTON_TYPE_BNET
					FriendButtons[index].id = i
				end
			end
			for i = 1, numWoWOnline do
				if WowFriendGroups[i][group] then
					index = index + 1
					FriendButtons[index].buttonType = FRIENDS_BUTTON_TYPE_WOW
					FriendButtons[index].id = i
				end
			end
			if not FriendGroups_SavedVars.hide_offline then
				for i = numBNetOnline + 1, numBNetTotal do
					if BnetFriendGroups[i][group] then
						index = index + 1
						FriendButtons[index].buttonType = FRIENDS_BUTTON_TYPE_BNET
						FriendButtons[index].id = i
					end
				end
				for i = numWoWOnline + 1, numWoWTotal do
					if WowFriendGroups[i][group] then
						index = index + 1
						FriendButtons[index].buttonType = FRIENDS_BUTTON_TYPE_WOW
						FriendButtons[index].id = i
					end
				end
			end
		end
	end
	FriendButtons.count = index
Azilroka's avatar
Azilroka committed
603 604

	-- selection
Azilroka's avatar
Update  
Azilroka committed
605
	local selectedFriend = 0
Azilroka's avatar
Azilroka committed
606 607 608 609
	-- check that we have at least 1 friend
	if ( numBNetTotal + numWoWTotal > 0 ) then
		-- get friend
		if ( FriendsFrame.selectedFriendType == FRIENDS_BUTTON_TYPE_WOW ) then
Azilroka's avatar
Update  
Azilroka committed
610
			selectedFriend = GetSelectedFriend()
Azilroka's avatar
Azilroka committed
611
		elseif ( FriendsFrame.selectedFriendType == FRIENDS_BUTTON_TYPE_BNET ) then
Azilroka's avatar
Update  
Azilroka committed
612
			selectedFriend = BNGetSelectedFriend()
Azilroka's avatar
Azilroka committed
613 614 615
		end
		-- set to first in list if no friend
		if ( not selectedFriend or selectedFriend == 0 ) then
Azilroka's avatar
Update  
Azilroka committed
616 617
			FriendsFrame_SelectFriend(FriendButtons[1].buttonType, 1)
			selectedFriend = 1
Azilroka's avatar
Azilroka committed
618 619
		end
		-- check if friend is online
Azilroka's avatar
Update  
Azilroka committed
620
		local isOnline
Azilroka's avatar
Azilroka committed
621
		if ( FriendsFrame.selectedFriendType == FRIENDS_BUTTON_TYPE_WOW ) then
Azilroka's avatar
Update  
Azilroka committed
622 623
			local name, level, class, area
			name, level, class, area, isOnline = GetFriendInfo(selectedFriend)
Azilroka's avatar
Azilroka committed
624
		elseif ( FriendsFrame.selectedFriendType == FRIENDS_BUTTON_TYPE_BNET ) then
Azilroka's avatar
Update  
Azilroka committed
625 626
			local bnetIDAccount, accountName, battleTag, isBattleTag, characterName, bnetIDGameAccount, client
			bnetIDAccount, accountName, battleTag, isBattleTag, characterName, bnetIDGameAccount, client, isOnline = BNGetFriendInfo(selectedFriend)
Azilroka's avatar
Azilroka committed
627
			if ( not accountName ) then
Azilroka's avatar
Update  
Azilroka committed
628
				isOnline = false
Azilroka's avatar
Azilroka committed
629 630 631
			end
		end
		if ( isOnline ) then
Azilroka's avatar
Update  
Azilroka committed
632
			FriendsFrameSendMessageButton:Enable()
Azilroka's avatar
Azilroka committed
633
		else
Azilroka's avatar
Update  
Azilroka committed
634
			FriendsFrameSendMessageButton:Disable()
Azilroka's avatar
Azilroka committed
635 636
		end
	else
Azilroka's avatar
Update  
Azilroka committed
637
		FriendsFrameSendMessageButton:Disable()
Azilroka's avatar
Azilroka committed
638
	end
Azilroka's avatar
Update  
Azilroka committed
639
	FriendsFrame.selectedFriend = selectedFriend
Azilroka's avatar
Azilroka committed
640 641

	-- RID warning, upon getting the first RID invite
Azilroka's avatar
Update  
Azilroka committed
642 643
	local showRIDWarning = false
	local numInvites = BNGetNumFriendInvites()
Azilroka's avatar
Azilroka committed
644
	if ( numInvites > 0 and not GetCVarBool("pendingInviteInfoShown") ) then
Azilroka's avatar
Update  
Azilroka committed
645
		local _, _, _, _, _, _, isRIDEnabled = BNGetInfo()
Azilroka's avatar
Azilroka committed
646 647
		if ( isRIDEnabled ) then
			for i = 1, numInvites do
Azilroka's avatar
Update  
Azilroka committed
648
				local inviteID, accountName, isBattleTag = BNGetFriendInviteInfo(i)
Azilroka's avatar
Azilroka committed
649 650
				if ( not isBattleTag ) then
					-- found one
Azilroka's avatar
Update  
Azilroka committed
651 652
					showRIDWarning = true
					break
Azilroka's avatar
Azilroka committed
653 654 655 656 657
				end
			end
		end
	end
	if ( showRIDWarning ) then
Azilroka's avatar
Update  
Azilroka committed
658 659 660 661
		FriendsListFrame.RIDWarning:Show()
		FriendsFrameFriendsScrollFrame.scrollBar:Disable()
		FriendsFrameFriendsScrollFrame.scrollUp:Disable()
		FriendsFrameFriendsScrollFrame.scrollDown:Disable()
Azilroka's avatar
Azilroka committed
662
	else
Azilroka's avatar
Update  
Azilroka committed
663
		FriendsListFrame.RIDWarning:Hide()
Azilroka's avatar
Azilroka committed
664
	end
Azilroka's avatar
Update  
Azilroka committed
665
	FriendGroups_UpdateFriends()
Azilroka's avatar
Azilroka committed
666 667 668
end

local function FriendGroups_OnClick(self, button)
669 670 671 672 673 674 675 676 677 678 679 680
	if not self.text:IsShown() then
		hooks["FriendsFrameFriendButton_OnClick"](self, button)
		return
	end

	local group = self.info:GetText() or ""
	if button == "RightButton" then
		ToggleDropDownMenu(1, group, FriendGroups_Menu, "cursor", 0, 0)
	else
		FriendGroups_SavedVars.collapsed[group] = not FriendGroups_SavedVars.collapsed[group]
		FriendGroups_Update()
	end
Azilroka's avatar
Azilroka committed
681 682 683 684 685 686 687 688 689 690
end

local function FriendGroups_SaveOpenMenu()
	if OPEN_DROPDOWNMENUS then
		OPEN_DROPDOWNMENUS_SAVE = CopyTable(OPEN_DROPDOWNMENUS)
	end
end

-- when one of our new menu items is clicked
local function FriendGroups_OnFriendMenuClick(self)
691 692 693
	if not self.value then
		return
	end
Azilroka's avatar
Azilroka committed
694

695 696 697
	local add = strmatch(self.value, "FGROUPADD_(.+)")
	local del = strmatch(self.value, "FGROUPDEL_(.+)")
	local creating = self.value == "FRIEND_GROUP_NEW"
Azilroka's avatar
Azilroka committed
698

699 700 701
	if add or del or creating then
		local dropdown = UIDROPDOWNMENU_INIT_MENU
		local source = OPEN_DROPDOWNMENUS_SAVE[1] and OPEN_DROPDOWNMENUS_SAVE[1].which or self.owner -- OPEN_DROPDOWNMENUS is nil on click
Azilroka's avatar
Azilroka committed
702

703
		if source == "BN_FRIEND" or source == "BN_FRIEND_OFFLINE" then
Azilroka's avatar
Azilroka committed
704 705 706 707 708 709
			local note = select(13, BNGetFriendInfoByID(dropdown.bnetIDAccount))
			if creating then
				StaticPopup_Show("FRIEND_GROUP_CREATE", nil, nil, { id = dropdown.bnetIDAccount, note = note, set = BNSetFriendNote })
			else
				if add then
					note = AddGroup(note, add)
710 711
				else
					note = RemoveGroup(note, del)
Azilroka's avatar
Azilroka committed
712 713 714
				end
				BNSetFriendNote(dropdown.bnetIDAccount, note)
			end
715 716 717 718
		elseif source == "FRIEND" or source == "FRIEND_OFFLINE" then
			for i = 1, GetNumFriends() do
				local name, _, _, _, _, _, note = GetFriendInfo(i)
				if dropdown.name and name:find(dropdown.name) then
Azilroka's avatar
Azilroka committed
719 720 721 722 723
					if creating then
						StaticPopup_Show("FRIEND_GROUP_CREATE", nil, nil, { id = i, note = note, set = SetFriendNotes })
					else
						if add then
							note = AddGroup(note, add)
724 725 726
					else
						note = RemoveGroup(note, del)
					end
Azilroka's avatar
Azilroka committed
727 728
						SetFriendNotes(i, note)
					end
729 730 731 732 733 734 735
					break
				end
			end
		end
		FriendGroups_Update()
	end
	HideDropDownMenu(1)
Azilroka's avatar
Azilroka committed
736 737 738 739
end

-- hide the add/remove group buttons if we're not right clicking on a friendlist item
local function FriendGroups_HideButtons()
740
	local dropdown = UIDROPDOWNMENU_INIT_MENU
Azilroka's avatar
Azilroka committed
741

742 743 744 745 746 747 748 749 750
	local hidden = false
	for index, value in ipairs(UnitPopupMenus[UIDROPDOWNMENU_MENU_VALUE] or UnitPopupMenus[dropdown.which]) do
		if value == "FRIEND_GROUP_ADD" or value == "FRIEND_GROUP_DEL" or value == "FRIEND_GROUP_NEW" then
			if not dropdown.friendsList then
				UnitPopupShown[UIDROPDOWNMENU_MENU_LEVEL][index] = 0
				hidden = true
			end
		end
	end
Azilroka's avatar
Azilroka committed
751

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	if not hidden then
		wipe(UnitPopupMenus["FRIEND_GROUP_ADD"])
		wipe(UnitPopupMenus["FRIEND_GROUP_DEL"])
		local groups = {}
		local note = nil

		if dropdown.bnetIDAccount then
			note = select(13, BNGetFriendInfoByID(dropdown.bnetIDAccount))
		else
			for i = 1, GetNumFriends() do
				local name, _, _, _, _, _, noteText = GetFriendInfo(i)
				if dropdown.name and name:find(dropdown.name) then
					note = noteText
					break
				end
			end
		end

		NoteAndGroups(note, groups)

		for _,group in ipairs(GroupSorted) do
			if group ~= "" and not groups[group] then
				local faux = "FGROUPADD_" .. group
				--polluting the popup buttons list
				UnitPopupButtons[faux] = { text = group, dist = 0 }
				table.insert(UnitPopupMenus["FRIEND_GROUP_ADD"], faux)
			end
		end
		for group in pairs(groups) do
			if group ~= "" then
				local faux = "FGROUPDEL_" .. group
				UnitPopupButtons[faux] = { text = group, dist = 0 }
				table.insert(UnitPopupMenus["FRIEND_GROUP_DEL"], faux)
			end
		end
	end
Azilroka's avatar
Azilroka committed
788 789 790
end

local function FriendGroups_Rename(self, old)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	local input = self.editBox:GetText()
	if input == "" then
		return
	end
	local groups = {}
	for i = 1, BNGetNumFriends() do
		local presenceID, _, _, _, _, _, _, _, _, _, _, _, noteText = BNGetFriendInfo(i)
		local note = NoteAndGroups(noteText, groups)
		if groups[old] then
			groups[old] = nil
			groups[input] = true
			note = CreateNote(note, groups)
			BNSetFriendNote(presenceID, note)
		end
	end
	for i = 1, GetNumFriends() do
		local note = select(7, GetFriendInfo(i))
		note = NoteAndGroups(note, groups)
		if groups[old] then
			groups[old] = nil
			groups[input] = true
			note = CreateNote(note, groups)
			SetFriendNotes(i, note)
		end
	end
	FriendGroups_Update()
Azilroka's avatar
Azilroka committed
817 818 819
end

local function FriendGroups_Create(self, data)
820 821 822 823 824
	local input = self.editBox:GetText()
	if input == "" then
		return
	end
	local note = AddGroup(data.note, input)
Azilroka's avatar
Azilroka committed
825 826 827 828
	data.set(data.id, note)
end

StaticPopupDialogs["FRIEND_GROUP_RENAME"] = {
829 830 831 832 833 834 835 836 837 838 839 840 841
	text = "Enter new group name",
	button1 = ACCEPT,
	button2 = CANCEL,
	hasEditBox = 1,
	OnAccept = FriendGroups_Rename,
	EditBoxOnEnterPressed = function(self)
		local parent = self:GetParent()
		FriendGroups_Rename(parent, parent.data)
		parent:Hide()
	end,
	timeout = 0,
	whileDead = 1,
	hideOnEscape = 1
Azilroka's avatar
Azilroka committed
842 843 844
}

StaticPopupDialogs["FRIEND_GROUP_CREATE"] = {
845 846 847 848 849 850 851 852 853 854 855 856 857
	text = "Enter new group name",
	button1 = ACCEPT,
	button2 = CANCEL,
	hasEditBox = 1,
	OnAccept = FriendGroups_Create,
	EditBoxOnEnterPressed = function(self)
		local parent = self:GetParent()
		FriendGroups_Create(parent, parent.data)
		parent:Hide()
	end,
	timeout = 0,
	whileDead = 1,
	hideOnEscape = 1
Azilroka's avatar
Azilroka committed
858 859 860
}

local function InviteOrGroup(clickedgroup, invite)
861
	local groups = {}
Azilroka's avatar
Azilroka committed
862 863 864 865 866 867 868
	for i = 1, BNGetNumFriends() do
		local presenceID, _, _, _, _, toonID, _, _, _, _, _, _, noteText = BNGetFriendInfo(i)
		local note = NoteAndGroups(noteText, groups)
		if groups[clickedgroup] then
			if invite and toonID then
				BNInviteFriend(toonID)
			elseif not invite then
869 870
				groups[clickedgroup] = nil
				note = CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
871 872 873 874 875 876 877 878 879 880 881
				BNSetFriendNote(presenceID, note)
			end
		end
	end
	for i = 1, GetNumFriends() do
		local name, _, _, _, connected, _, noteText = GetFriendInfo(i)
		local note = NoteAndGroups(noteText, groups)
		if groups[clickedgroup] then
			if invite and connected then
				InviteUnit(name)
			elseif not invite then
882 883
				groups[clickedgroup] = nil
				note = CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
884 885 886 887 888 889 890 891 892
				SetFriendNotes(i, note)
			end
		end
	end
end

local FriendGroups_Menu = CreateFrame("Frame", "FriendGroups_Menu")
FriendGroups_Menu.displayMode = "MENU"
local menu_items = {
893
	[1] = {
Azilroka's avatar
Azilroka committed
894 895 896 897 898 899 900 901 902 903 904
		{ text = "", notCheckable = true, isTitle = true },
		{ text = "Invite all to party", notCheckable = true, func = function(self, menu, clickedgroup) InviteOrGroup(clickedgroup, true) end },
		{ text = "Rename group", notCheckable = true, func = function(self, menu, clickedgroup) StaticPopup_Show("FRIEND_GROUP_RENAME", nil, nil, clickedgroup) end },
		{ text = "Remove group", notCheckable = true, func = function(self, menu, clickedgroup) InviteOrGroup(clickedgroup, false) end },
		{ text = "Settings", notCheckable = true, hasArrow = true },
	},
	[2] = {
		{ text = "Hide all offline", checked = function() return FriendGroups_SavedVars.hide_offline end, func = function() CloseDropDownMenus() FriendGroups_SavedVars.hide_offline = not FriendGroups_SavedVars.hide_offline FriendGroups_Update() end },
		{ text = "Colour names", checked = function() return FriendGroups_SavedVars.colour_classes end, func = function() CloseDropDownMenus() FriendGroups_SavedVars.colour_classes = not FriendGroups_SavedVars.colour_classes FriendGroups_Update() end },
	},
}
Azilroka's avatar
Azilroka committed
905

Azilroka's avatar
Azilroka committed
906 907
FriendGroups_Menu.initialize = function(self, level)
	if not menu_items[level] then return end
908
	for _, items in ipairs(menu_items[level]) do
Azilroka's avatar
Azilroka committed
909 910 911 912 913 914 915 916 917 918
		local info = UIDropDownMenu_CreateInfo()
		for prop, value in pairs(items) do
			info[prop] = value ~= "" and value or UIDROPDOWNMENU_MENU_VALUE ~= "" and UIDROPDOWNMENU_MENU_VALUE or "[no group]"
		end
		info.arg1 = k
		info.arg2 = UIDROPDOWNMENU_MENU_VALUE
		UIDropDownMenu_AddButton(info, level)
	end
end

Azilroka's avatar
Azilroka committed
919
--local frame = CreateFrame("Frame")
920
-- frame:RegisterEvent("PLAYER_LOGIN")
Azilroka's avatar
Azilroka committed
921 922

-- frame:SetScript("OnEvent", function(self, event, ...)
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
	-- if event == "PLAYER_LOGIN" then
		-- Hook("FriendsList_Update", FriendGroups_Update, true)
		-- if other addons have hooked this, we should too
		-- if not issecurevariable("FriendsFrame_UpdateFriends") then
			-- Hook("FriendsFrame_UpdateFriends", FriendGroups_UpdateFriends)
		-- end
		-- Hook("FriendsFrameFriendButton_OnClick", FriendGroups_OnClick)
		-- Hook("UnitPopup_ShowMenu", FriendGroups_SaveOpenMenu, true)
		-- Hook("UnitPopup_OnClick", FriendGroups_OnFriendMenuClick, true)
		-- Hook("UnitPopup_HideButtons", FriendGroups_HideButtons, true)
		-- Hook("FriendsFrameTooltip_Show",function(button)
			-- if ( button.buttonType == FRIENDS_BUTTON_TYPE_DIVIDER ) then
				-- if FriendsTooltip:IsShown() then
					-- FriendsTooltip:Hide()
				-- end
				-- return
			-- end
		-- end,true)-- Fixes tooltip showing on groups

		-- FriendsFrameFriendsScrollFrame.dynamic = FriendGroups_GetTopButton
		-- FriendsFrameFriendsScrollFrame.update = FriendGroups_UpdateFriends
Azilroka's avatar
Azilroka committed
944

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
		-- add some more buttons
		-- FriendsFrameFriendsScrollFrame.buttons[1]:SetHeight(FRIENDS_FRAME_FRIENDS_FRIENDS_HEIGHT)
		-- HybridScrollFrame_CreateButtons(FriendsFrameFriendsScrollFrame, "FriendsFrameButtonTemplate")

		-- table.remove(UnitPopupMenus["BN_FRIEND"], 5) --remove target option

		-- add our add/remove group buttons to the friend list popup menus
		-- for _,menu in ipairs(friend_popup_menus) do
			-- table.insert(UnitPopupMenus[menu], #UnitPopupMenus[menu], "FRIEND_GROUP_NEW")
			-- table.insert(UnitPopupMenus[menu], #UnitPopupMenus[menu], "FRIEND_GROUP_ADD")
			-- table.insert(UnitPopupMenus[menu], #UnitPopupMenus[menu], "FRIEND_GROUP_DEL")
		-- end

		-- if not FriendGroups_SavedVars then
			-- FriendGroups_SavedVars = {
				-- collapsed = {},
				-- hide_offline = false,
				-- colour_classes = true,
			-- }
		-- end
	-- end
Azilroka's avatar
Azilroka committed
966
-- end)