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

Azilroka's avatar
Azilroka committed
5 6 7
FG.Title = 'Friend Groups'
FG.Header = PA.ACL['|cFF16C3F2Friend|r |cFFFFFFFFGroups|r']
FG.Description = PA.ACL['Manage Firends List with Groups']
Azilroka's avatar
Azilroka committed
8 9
FG.Authors = 'Azilroka'
FG.Credits = 'Mikeprod    frankkkkk'
10

Azilroka's avatar
Update  
Azilroka committed
11
local FRIENDS_GROUP_NAME_COLOR = NORMAL_FONT_COLOR
Azilroka's avatar
Azilroka committed
12

Azilroka's avatar
Update  
Azilroka committed
13 14 15 16 17
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
18

Azilroka's avatar
Azilroka committed
19
local FriendListEntries = {}
Azilroka's avatar
Azilroka committed
20 21 22 23 24
local GroupCount = 0
local GroupTotal = {}
local GroupOnline = {}
local GroupSorted = {}

Azilroka's avatar
Azilroka committed
25
local FriendRequestString = string.sub(FRIEND_REQUESTS,1,-6)
Azilroka's avatar
Azilroka committed
26 27

local OPEN_DROPDOWNMENUS_SAVE = nil
Azilroka's avatar
Azilroka committed
28

Azilroka's avatar
Azilroka committed
29
local friend_popup_menus = { "FRIEND", "FRIEND_OFFLINE", "BN_FRIEND", "BN_FRIEND_OFFLINE" }
Azilroka's avatar
Azilroka committed
30 31 32 33

UnitPopupButtons["FRIEND_GROUP_NEW"] = { text = "Create new group"}
UnitPopupButtons["FRIEND_GROUP_ADD"] = { text = "Add to group", nested = 1}
UnitPopupButtons["FRIEND_GROUP_DEL"] = { text = "Remove from group", nested = 1}
Azilroka's avatar
Azilroka committed
34 35 36
UnitPopupMenus["FRIEND_GROUP_ADD"] = { }
UnitPopupMenus["FRIEND_GROUP_DEL"] = { }

Azilroka's avatar
Azilroka committed
37 38 39 40 41 42 43 44 45
local FriendsScrollFrame
local FriendButtonTemplate

if FriendsListFrameScrollFrame then
	FriendsScrollFrame = FriendsListFrameScrollFrame
	FriendButtonTemplate = "FriendsListButtonTemplate"
else
	FriendsScrollFrame = FriendsFrameFriendsScrollFrame
	FriendButtonTemplate = "FriendsFrameButtonTemplate"
Azilroka's avatar
Azilroka committed
46 47
end

Azilroka's avatar
Azilroka committed
48
function FG:FriendGroups_GetTopButton(offset)
Azilroka's avatar
Update  
Azilroka committed
49
	local usedHeight = 0
Azilroka's avatar
Azilroka committed
50 51
	for i = 1, #FriendListEntries do
		local buttonHeight = FRIENDS_BUTTON_HEIGHTS[FriendListEntries[i].buttonType]
Azilroka's avatar
Azilroka committed
52
		if ( usedHeight + buttonHeight >= offset ) then
Azilroka's avatar
Update  
Azilroka committed
53
			return i - 1, offset - usedHeight
Azilroka's avatar
Azilroka committed
54
		else
Azilroka's avatar
Update  
Azilroka committed
55
			usedHeight = usedHeight + buttonHeight
Azilroka's avatar
Azilroka committed
56 57 58 59
		end
	end
end

Azilroka's avatar
Azilroka committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
function FG:GetOnlineInfoText(client, isMobile, rafLinkType, locationText)
	if not locationText or locationText == "" then
		return UNKNOWN
	end
	if isMobile then
		return LOCATION_MOBILE_APP
	end
	if (client == BNET_CLIENT_WOW) and (rafLinkType ~= Enum.RafLinkType.None) and not isMobile then
		if rafLinkType == Enum.RafLinkType.Recruit then
			return RAF_RECRUIT_FRIEND:format(locationText)
		else
			return RAF_RECRUITER_FRIEND:format(locationText)
		end
	end
	return locationText
end

function FG:FriendGroups_UpdateFriendButton(button)
Azilroka's avatar
Update  
Azilroka committed
78
	local index = button.index
Azilroka's avatar
Azilroka committed
79 80
	button.buttonType = FriendListEntries[index].buttonType
	button.id = FriendListEntries[index].id
Azilroka's avatar
Update  
Azilroka committed
81
	local height = FRIENDS_BUTTON_HEIGHTS[button.buttonType]
Azilroka's avatar
Azilroka committed
82 83 84
	local nameText, nameColor, infoText, broadcastText, isFavoriteFriend
	if button.buttonType == FRIENDS_BUTTON_TYPE_WOW then
		local info = C_FriendList.GetFriendInfoByIndex(FriendListEntries[index].id)
Azilroka's avatar
Update  
Azilroka committed
85
		broadcastText = nil
Azilroka's avatar
Azilroka committed
86
		if info.connected then
Azilroka's avatar
Update  
Azilroka committed
87
			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
88
			if info.afk then
Azilroka's avatar
Update  
Azilroka committed
89
				button.status:SetTexture(FRIENDS_TEXTURE_AFK)
Azilroka's avatar
Azilroka committed
90
			elseif ( info.dnd ) then
Azilroka's avatar
Update  
Azilroka committed
91
				button.status:SetTexture(FRIENDS_TEXTURE_DND)
Azilroka's avatar
Azilroka committed
92 93
			else
				button.status:SetTexture(FRIENDS_TEXTURE_ONLINE)
Azilroka's avatar
Azilroka committed
94 95
			end

Azilroka's avatar
Azilroka committed
96 97 98 99 100
			nameColor = PA:ClassColorCode(info.gameAccountInfo.className)

			nameText = info.name..", "..format(FRIENDS_LEVEL_TEMPLATE, info.level, info.className)
			if PA.Retail then
				infoText = FG:GetOnlineInfoText(BNET_CLIENT_WOW, info.mobile, info.rafLinkType, info.area)
Azilroka's avatar
Azilroka committed
101 102
			end
		else
Azilroka's avatar
Update  
Azilroka committed
103 104
			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)
Azilroka's avatar
Azilroka committed
105
			nameText = info.name
Azilroka's avatar
Update  
Azilroka committed
106
			nameColor = FRIENDS_GRAY_COLOR
Azilroka's avatar
Azilroka committed
107
			infoText = FRIENDS_LIST_OFFLINE
Azilroka's avatar
Azilroka committed
108
		end
Azilroka's avatar
Update  
Azilroka committed
109 110 111 112
		button.gameIcon:Hide()
		button.summonButton:ClearAllPoints()
		button.summonButton:SetPoint("TOPRIGHT", button, "TOPRIGHT", 1, -1)
		FriendsFrame_SummonButton_Update(button.summonButton)
Azilroka's avatar
Azilroka committed
113 114 115 116 117 118 119 120 121 122 123 124
	elseif button.buttonType == FRIENDS_BUTTON_TYPE_BNET then
		local accountInfo = PA:GetBattleNetInfo(button.id);
		if accountInfo then
			nameText = accountInfo.accountName
			infoText = accountInfo.gameAccountInfo.richPresence

			if accountInfo.gameAccountInfo.isOnline then
				button.background:SetColorTexture(FRIENDS_BNET_BACKGROUND_COLOR.r, FRIENDS_BNET_BACKGROUND_COLOR.g, FRIENDS_BNET_BACKGROUND_COLOR.b, FRIENDS_BNET_BACKGROUND_COLOR.a)
				if accountInfo.isAFK or accountInfo.gameAccountInfo.isGameAFK then
					button.status:SetTexture(FRIENDS_TEXTURE_AFK)
				elseif accountInfo.isDND or accountInfo.gameAccountInfo.isGameBusy then
					button.status:SetTexture(FRIENDS_TEXTURE_DND)
Azilroka's avatar
Azilroka committed
125
				else
Azilroka's avatar
Azilroka committed
126
					button.status:SetTexture(FRIENDS_TEXTURE_ONLINE)
Azilroka's avatar
Azilroka committed
127 128
				end

Azilroka's avatar
Azilroka committed
129 130 131 132 133 134
				if accountInfo.gameAccountInfo.clientProgram == BNET_CLIENT_WOW and accountInfo.gameAccountInfo.wowProjectID == WOW_PROJECT_ID then
					if not accountInfo.gameAccountInfo.areaName or accountInfo.gameAccountInfo.areaName == "" then
						infoText = UNKNOWN
					else
						infoText = accountInfo.gameAccountInfo.isWowMobile and LOCATION_MOBILE_APP or info.gameAccountInfo.areaName
					end
Azilroka's avatar
Azilroka committed
135 136
				end

Azilroka's avatar
Azilroka committed
137 138
				button.gameIcon:SetTexture(BNet_GetClientTexture(accountInfo.gameAccountInfo.clientProgram))
				nameColor = FRIENDS_BNET_NAME_COLOR
Azilroka's avatar
Azilroka committed
139

Azilroka's avatar
Azilroka committed
140 141
				local fadeIcon = (accountInfo.gameAccountInfo.clientProgram == BNET_CLIENT_WOW) and (accountInfo.gameAccountInfo.wowProjectID ~= WOW_PROJECT_ID)
				button.gameIcon:SetAlpha(fadeIcon and .6 or 1)
Azilroka's avatar
Azilroka committed
142

Azilroka's avatar
Azilroka committed
143 144 145 146 147 148
				local shouldShowSummonButton = FriendsFrame_ShouldShowSummonButton(button.summonButton)
				button.gameIcon:SetShown(not shouldShowSummonButton)

				local restriction = FriendsFrame_GetInviteRestriction(button.id)
				button.travelPassButton:SetEnabled(restriction == INVITE_RESTRICTION_NONE)
				button.travelPassButton:SetShown(restriction == INVITE_RESTRICTION_NONE)
Azilroka's avatar
Azilroka committed
149
			else
Azilroka's avatar
Azilroka committed
150 151 152 153 154 155 156 157 158
				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()
				if ( not lastOnline or lastOnline == 0 or time() - lastOnline >= ONE_YEAR ) then
					infoText = FRIENDS_LIST_OFFLINE
				else
					infoText = string.format(BNET_LAST_ONLINE_TIME, FriendsFrame_GetLastOnline(lastOnline))
				end
Azilroka's avatar
Azilroka committed
159
			end
Azilroka's avatar
Azilroka committed
160 161 162
			button.summonButton:ClearAllPoints()
			button.summonButton:SetPoint("CENTER", button.gameIcon, "CENTER", 1, 0)
			FriendsFrame_SummonButton_Update(button.summonButton)
Azilroka's avatar
Azilroka committed
163 164
		end
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_DIVIDER ) then
Azilroka's avatar
Update  
Azilroka committed
165
		local title
Azilroka's avatar
Azilroka committed
166
		local group = FriendListEntries[index].text
Azilroka's avatar
Azilroka committed
167 168 169 170 171 172 173
		if group == "" or not group then
			title = "[no group]"
		else
			title = group
		end
		local counts = "(" .. GroupOnline[group] .. "/" .. GroupTotal[group] .. ")"

Azilroka's avatar
Azilroka committed
174 175 176 177 178
		if button["text"] then
			button.text:SetText(title)
			button.text:Show()
			nameText = counts
			button.name:SetJustifyH("RIGHT")
Azilroka's avatar
Azilroka committed
179
		else
Azilroka's avatar
Azilroka committed
180 181
			nameText = title.." "..counts
			button.name:SetJustifyH("CENTER")
Azilroka's avatar
Azilroka committed
182
		end
Azilroka's avatar
Azilroka committed
183 184 185 186 187 188 189
		nameColor = FRIENDS_GROUP_NAME_COLOR

--		if FriendGroups_SavedVars.collapsed[group] then
--			button.status:SetTexture("Interface\\Buttons\\UI-PlusButton-UP")
--		else
			button.status:SetTexture("Interface\\Buttons\\UI-MinusButton-UP")
--		end
Azilroka's avatar
Azilroka committed
190 191 192
		infoText = group
		button.info:Hide()
		button.gameIcon:Hide()
Azilroka's avatar
Update  
Azilroka committed
193
		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
194
		button.background:SetAlpha(0.5)
Azilroka's avatar
Azilroka committed
195 196
		local scrollFrame = FriendsScrollFrame
		--[[local divider = scrollFrame.dividerPool:Acquire()
Azilroka's avatar
Update  
Azilroka committed
197 198
		divider:SetParent(scrollFrame.ScrollChild)
		divider:SetAllPoints(button)
Azilroka's avatar
Azilroka committed
199
		divider:Show()--]]
Azilroka's avatar
Azilroka committed
200
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_INVITE_HEADER ) then
Azilroka's avatar
Azilroka committed
201
		local header = FriendsScrollFrame.PendingInvitesHeaderButton
Azilroka's avatar
Update  
Azilroka committed
202 203 204 205
		header:SetPoint("TOPLEFT", button, 1, 0)
		header:Show()
		header:SetFormattedText(FRIEND_REQUESTS, BNGetNumFriendInvites())
		local collapsed = GetCVarBool("friendInvitesCollapsed")
Azilroka's avatar
Azilroka committed
206
		if ( collapsed ) then
Azilroka's avatar
Update  
Azilroka committed
207 208
			header.DownArrow:Hide()
			header.RightArrow:Show()
Azilroka's avatar
Azilroka committed
209
		else
Azilroka's avatar
Update  
Azilroka committed
210 211
			header.DownArrow:Show()
			header.RightArrow:Hide()
Azilroka's avatar
Azilroka committed
212
		end
Azilroka's avatar
Update  
Azilroka committed
213
		nameText = nil
Azilroka's avatar
Azilroka committed
214
	elseif ( button.buttonType == FRIENDS_BUTTON_TYPE_INVITE ) then
Azilroka's avatar
Azilroka committed
215
		local scrollFrame = FriendsScrollFrame
Azilroka's avatar
Update  
Azilroka committed
216 217 218 219 220 221 222 223 224
		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
225 226
	end
	-- selection
Azilroka's avatar
Azilroka committed
227
	if ( FriendsFrame.selectedFriendType == FriendListEntries[index].buttonType and FriendsFrame.selectedFriend == FriendListEntries[index].id ) then
Azilroka's avatar
Update  
Azilroka committed
228
		button:LockHighlight()
Azilroka's avatar
Azilroka committed
229
	else
Azilroka's avatar
Update  
Azilroka committed
230
		button:UnlockHighlight()
Azilroka's avatar
Azilroka committed
231 232 233 234
	end
	-- finish setting up button if it's not a header
	if ( nameText ) then
		if button.buttonType ~= FRIENDS_BUTTON_TYPE_DIVIDER then
Azilroka's avatar
Azilroka committed
235
		if button["text"] then
Azilroka's avatar
Azilroka committed
236
			button.text:Hide()
Azilroka's avatar
Azilroka committed
237
		end
Azilroka's avatar
Azilroka committed
238 239 240 241
			button.name:SetJustifyH("LEFT")
			button.background:SetAlpha(1)
			button.info:Show()
		end
Azilroka's avatar
Update  
Azilroka committed
242 243 244 245
		button.name:SetText(nameText)
		button.name:SetTextColor(nameColor.r, nameColor.g, nameColor.b)
		button.info:SetText(infoText)
		button:Show()
Azilroka's avatar
Azilroka committed
246 247 248 249 250 251 252
		if isFavoriteFriend and button.Favorite then
			button.Favorite:Show()
			button.Favorite:ClearAllPoints()
			button.Favorite:SetPoint("TOPLEFT", button.name, "TOPLEFT", button.name:GetStringWidth(), 0)
		elseif button.Favorite then
			button.Favorite:Hide()
		end
Azilroka's avatar
Azilroka committed
253
	else
Azilroka's avatar
Update  
Azilroka committed
254
		button:Hide()
Azilroka's avatar
Azilroka committed
255 256
	end
	-- update the tooltip if hovering over a button
Azilroka's avatar
Azilroka committed
257 258 259 260 261 262
	if ( FriendsTooltip.button == button ) or ( GetMouseFocus() == button ) then
		if FriendsFrameTooltip_Show then
			FriendsFrameTooltip_Show(button)
		else
			button:OnEnter()
		end
Azilroka's avatar
Azilroka committed
263
	end
Azilroka's avatar
Update  
Azilroka committed
264
	return height
Azilroka's avatar
Azilroka committed
265 266 267
end


Azilroka's avatar
Azilroka committed
268 269
function FG:FriendGroups_UpdateFriends()
	local scrollFrame = FriendsScrollFrame
Azilroka's avatar
Update  
Azilroka committed
270 271 272
	local offset = HybridScrollFrame_GetOffset(scrollFrame)
	local buttons = scrollFrame.buttons
	local numButtons = #buttons
Azilroka's avatar
Azilroka committed
273
	local numFriendListEntries = #FriendListEntries
Azilroka's avatar
Azilroka committed
274

Azilroka's avatar
Update  
Azilroka committed
275
	local usedHeight = 0
Azilroka's avatar
Azilroka committed
276

Azilroka's avatar
Update  
Azilroka committed
277 278 279
	scrollFrame.dividerPool:ReleaseAll()
	scrollFrame.invitePool:ReleaseAll()
	scrollFrame.PendingInvitesHeaderButton:Hide()
Azilroka's avatar
Azilroka committed
280
	for i = 1, numButtons do
Azilroka's avatar
Update  
Azilroka committed
281 282
		local button = buttons[i]
		local index = offset + i
Azilroka's avatar
Azilroka committed
283
		if ( index <= numFriendListEntries ) then
Azilroka's avatar
Update  
Azilroka committed
284
			button.index = index
Azilroka's avatar
Azilroka committed
285
			local height = FG:FriendGroups_UpdateFriendButton(button)
Azilroka's avatar
Update  
Azilroka committed
286 287
			button:SetHeight(height)
			usedHeight = usedHeight + height
Azilroka's avatar
Azilroka committed
288
		else
Azilroka's avatar
Update  
Azilroka committed
289 290
			button.index = nil
			button:Hide()
Azilroka's avatar
Azilroka committed
291 292
		end
	end
Azilroka's avatar
Update  
Azilroka committed
293
	HybridScrollFrame_Update(scrollFrame, scrollFrame.totalFriendListEntriesHeight, usedHeight)
Azilroka's avatar
Azilroka committed
294 295

	-- Delete unused groups in the collapsed part
Azilroka's avatar
Azilroka committed
296
--[[	for key,_ in pairs(FriendGroups_SavedVars.collapsed) do
Azilroka's avatar
Azilroka committed
297 298 299 300
		if not GroupTotal[key] then
			FriendGroups_SavedVars.collapsed[key] = nil
		end
	end
Azilroka's avatar
Azilroka committed
301
--]]
Azilroka's avatar
Azilroka committed
302 303
end

Azilroka's avatar
Azilroka committed
304
function FG:FillGroups(groups, note, ...)
305 306 307 308 309 310 311 312 313 314 315
	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
316 317
end

Azilroka's avatar
Azilroka committed
318
function FG:NoteAndGroups(note, groups)
319
	if not note then
Azilroka's avatar
Azilroka committed
320
		return FG:FillGroups(groups, "")
321 322
	end
	if groups then
Azilroka's avatar
Azilroka committed
323
		return FG:FillGroups(groups, strsplit("#", note))
324 325
	end
	return strsplit("#", note)
Azilroka's avatar
Azilroka committed
326 327
end

Azilroka's avatar
Azilroka committed
328
function FG:CreateNote(note, groups)
329 330 331 332 333 334 335 336
	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
337 338
end

Azilroka's avatar
Azilroka committed
339
function FG:AddGroup(note, group)
340
	local groups = {}
Azilroka's avatar
Azilroka committed
341
	note = FG:NoteAndGroups(note, groups)
342 343
	groups[""] = nil --ew
	groups[group] = true
Azilroka's avatar
Azilroka committed
344
	return FG:CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
345 346
end

Azilroka's avatar
Azilroka committed
347
function FG:RemoveGroup(note, group)
348
	local groups = {}
Azilroka's avatar
Azilroka committed
349
	note = FG:NoteAndGroups(note, groups)
350 351
	groups[""] = nil --ew
	groups[group] = nil
Azilroka's avatar
Azilroka committed
352
	return FG:CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
353 354
end

Azilroka's avatar
Azilroka committed
355
function FG:IncrementGroup(group, online)
356 357 358 359 360 361 362 363 364
	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
365 366
end

Azilroka's avatar
Azilroka committed
367 368 369 370
function FG:FriendGroups_Update(forceUpdate)
	local numBNetTotal, numBNetOnline, numBNetFavorite, numBNetFavoriteOnline = BNGetNumFriends()
	numBNetFavorite = numBNetFavorite or 0
	numBNetFavoriteOnline = numBNetFavoriteOnline or 0
Azilroka's avatar
Update  
Azilroka committed
371
	local numBNetOffline = numBNetTotal - numBNetOnline
Azilroka's avatar
Azilroka committed
372 373 374
	local numBNetFavoriteOffline = numBNetFavorite - numBNetFavoriteOnline
	local numWoWTotal = C_FriendList.GetNumFriends()
	local numWoWOnline = C_FriendList.GetNumOnlineFriends()
Azilroka's avatar
Update  
Azilroka committed
375
	local numWoWOffline = numWoWTotal - numWoWOnline
Azilroka's avatar
Azilroka committed
376

Azilroka's avatar
Azilroka committed
377 378 379
	if QuickJoinToastButton then
		QuickJoinToastButton:UpdateDisplayedFriendCount()
	end
Azilroka's avatar
Azilroka committed
380
	if ( not FriendsListFrame:IsShown() and not forceUpdate) then
Azilroka's avatar
Update  
Azilroka committed
381
		return
Azilroka's avatar
Azilroka committed
382 383
	end

Azilroka's avatar
Azilroka committed
384
	wipe(FriendListEntries)
385 386 387 388
	wipe(GroupTotal)
	wipe(GroupOnline)
	wipe(GroupSorted)
	GroupCount = 0
Azilroka's avatar
Azilroka committed
389 390

	local BnetFriendGroups = {}
391
	local WowFriendGroups = {}
Azilroka's avatar
Azilroka committed
392 393
	local FriendReqGroup = {}

394
	local buttonCount = 0
Azilroka's avatar
Azilroka committed
395

Azilroka's avatar
Azilroka committed
396
	FriendListEntries.count = 0
Azilroka's avatar
Update  
Azilroka committed
397 398
	local addButtonIndex = 0
	local totalButtonHeight = 0
Azilroka's avatar
Azilroka committed
399
	local function AddButtonInfo(buttonType, id)
Azilroka's avatar
Update  
Azilroka committed
400
		addButtonIndex = addButtonIndex + 1
Azilroka's avatar
Azilroka committed
401 402
		if ( not FriendListEntries[addButtonIndex] ) then
			FriendListEntries[addButtonIndex] = { }
Azilroka's avatar
Azilroka committed
403
		end
Azilroka's avatar
Azilroka committed
404 405 406
		FriendListEntries[addButtonIndex].buttonType = buttonType
		FriendListEntries[addButtonIndex].id = id
		FriendListEntries.count = FriendListEntries.count + 1
Azilroka's avatar
Update  
Azilroka committed
407
		totalButtonHeight = totalButtonHeight + FRIENDS_BUTTON_HEIGHTS[buttonType]
Azilroka's avatar
Azilroka committed
408 409 410
	end

	-- invites
Azilroka's avatar
Update  
Azilroka committed
411
	local numInvites = BNGetNumFriendInvites()
Azilroka's avatar
Azilroka committed
412 413 414 415 416
	if ( numInvites > 0 ) then
		for i = 1, numInvites do
			if not FriendReqGroup[i] then
				FriendReqGroup[i] = {}
			end
Azilroka's avatar
Azilroka committed
417 418 419
			FG:IncrementGroup(FriendRequestString,true)
			FG:NoteAndGroups(nil, FriendReqGroup[i])
			--if not FriendGroups_SavedVars.collapsed[group] then
Azilroka's avatar
Azilroka committed
420
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
421
				AddButtonInfo(FRIENDS_BUTTON_TYPE_INVITE, i)
Azilroka's avatar
Azilroka committed
422
			--end
Azilroka's avatar
Azilroka committed
423 424
		end
	end
Azilroka's avatar
Azilroka committed
425 426 427

	-- favorite friends online
	for i = 1, numBNetFavoriteOnline do
Azilroka's avatar
Azilroka committed
428
		if not BnetFriendGroups[i] then
429 430
			BnetFriendGroups[i] = {}
		end
Azilroka's avatar
Azilroka committed
431
		local noteText = select(13,BNGetFriendInfo(i))
Azilroka's avatar
Azilroka committed
432
		FG:NoteAndGroups(noteText, BnetFriendGroups[i])
Azilroka's avatar
Azilroka committed
433
		for group in pairs(BnetFriendGroups[i]) do
Azilroka's avatar
Azilroka committed
434 435
			FG:IncrementGroup(group, true)
			--if not FriendGroups_SavedVars.collapsed[group] then
436
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
437
				AddButtonInfo(FRIENDS_BUTTON_TYPE_BNET, i)
Azilroka's avatar
Azilroka committed
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
			--end
		end
	end
	--favorite friends offline
	for i = 1, numBNetFavoriteOffline do
		local j = i + numBNetFavoriteOnline
		if not BnetFriendGroups[j] then
			BnetFriendGroups[j] = {}
		end
		local noteText = select(13,BNGetFriendInfo(j))
		FG:NoteAndGroups(noteText, BnetFriendGroups[j])
		for group in pairs(BnetFriendGroups[j]) do
			FG:IncrementGroup(group)
			--if not FriendGroups_SavedVars.collapsed[group] and not FriendGroups_SavedVars.hide_offline then
				buttonCount = buttonCount + 1
				AddButtonInfo(FRIENDS_BUTTON_TYPE_BNET, j)
			--end
		end
	end
	-- online Battlenet friends
	for i = 1, numBNetOnline - numBNetFavoriteOnline do
		local j = i + numBNetFavorite
		if not BnetFriendGroups[j] then
			BnetFriendGroups[j] = {}
		end
		local noteText = select(13,BNGetFriendInfo(j))
		FG:NoteAndGroups(noteText, BnetFriendGroups[j])
		for group in pairs(BnetFriendGroups[j]) do
			FG:IncrementGroup(group, true)
			--if not FriendGroups_SavedVars.collapsed[group] then
				buttonCount = buttonCount + 1
				AddButtonInfo(FRIENDS_BUTTON_TYPE_BNET, j)
			--end
Azilroka's avatar
Azilroka committed
471 472 473 474 475
		end
	end
	-- online WoW friends
	for i = 1, numWoWOnline do
		if not WowFriendGroups[i] then
476 477
			WowFriendGroups[i] = {}
		end
Azilroka's avatar
Azilroka committed
478 479
		local note = C_FriendList.GetFriendInfoByIndex(i) and C_FriendList.GetFriendInfoByIndex(i).notes
		FG:NoteAndGroups(note, WowFriendGroups[i])
480
		for group in pairs(WowFriendGroups[i]) do
Azilroka's avatar
Azilroka committed
481 482
			FG:IncrementGroup(group, true)
			--if not FriendGroups_SavedVars.collapsed[group] then
483
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
484
				AddButtonInfo(FRIENDS_BUTTON_TYPE_WOW, i)
Azilroka's avatar
Azilroka committed
485
			--end
486
		end
Azilroka's avatar
Azilroka committed
487 488
	end
	-- offline Battlenet friends
Azilroka's avatar
Azilroka committed
489 490
	for i = 1, numBNetOffline - numBNetFavoriteOffline do
		local j = i + numBNetFavorite + numBNetOnline - numBNetFavoriteOnline
Azilroka's avatar
Azilroka committed
491
		if not BnetFriendGroups[j] then
492 493
			BnetFriendGroups[j] = {}
		end
Azilroka's avatar
Azilroka committed
494
		local noteText = select(13,BNGetFriendInfo(j))
Azilroka's avatar
Azilroka committed
495
		FG:NoteAndGroups(noteText, BnetFriendGroups[j])
Azilroka's avatar
Azilroka committed
496
		for group in pairs(BnetFriendGroups[j]) do
Azilroka's avatar
Azilroka committed
497 498
			FG:IncrementGroup(group)
			--if not FriendGroups_SavedVars.collapsed[group] and not FriendGroups_SavedVars.hide_offline then
499
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
500
				AddButtonInfo(FRIENDS_BUTTON_TYPE_BNET, j)
Azilroka's avatar
Azilroka committed
501
			--end
Azilroka's avatar
Azilroka committed
502 503 504 505 506 507
		end
	end
	-- offline WoW friends
	for i = 1, numWoWOffline do
		local j = i + numWoWOnline
		if not WowFriendGroups[j] then
508 509
			WowFriendGroups[j] = {}
		end
Azilroka's avatar
Azilroka committed
510 511
		local note = C_FriendList.GetFriendInfoByIndex(j) and C_FriendList.GetFriendInfoByIndex(j).notes
		FG:NoteAndGroups(note, WowFriendGroups[j])
512
		for group in pairs(WowFriendGroups[j]) do
Azilroka's avatar
Azilroka committed
513 514
			FG:IncrementGroup(group)
			--if not FriendGroups_SavedVars.collapsed[group] and not FriendGroups_SavedVars.hide_offline then
515
				buttonCount = buttonCount + 1
Azilroka's avatar
Update  
Azilroka committed
516
				AddButtonInfo(FRIENDS_BUTTON_TYPE_WOW, j)
Azilroka's avatar
Azilroka committed
517
			--end
518
		end
Azilroka's avatar
Azilroka committed
519 520 521
	end

	buttonCount = buttonCount + GroupCount
Azilroka's avatar
Azilroka committed
522
	-- 1.5 is a magic number which prevents the list scroll to be too long
523
	totalScrollHeight = totalButtonHeight + GroupCount * FRIENDS_BUTTON_HEIGHTS[FRIENDS_BUTTON_TYPE_DIVIDER]
Azilroka's avatar
Azilroka committed
524

Azilroka's avatar
Azilroka committed
525 526
	FriendsScrollFrame.totalFriendListEntriesHeight = totalScrollHeight
	FriendsScrollFrame.numFriendListEntries = addButtonIndex
Azilroka's avatar
Azilroka committed
527

Azilroka's avatar
Azilroka committed
528 529 530
	if buttonCount > #FriendListEntries then
		for i = #FriendListEntries + 1, buttonCount do
			FriendListEntries[i] = {}
531 532 533 534 535 536 537 538 539 540 541 542
		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
543 544 545 546 547 548 549 550

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

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

	-- selection
Azilroka's avatar
Update  
Azilroka committed
613
	local selectedFriend = 0
Azilroka's avatar
Azilroka committed
614
	-- check that we have at least 1 friend
Azilroka's avatar
Azilroka committed
615
	if numBNetTotal + numWoWTotal > 0 then
Azilroka's avatar
Azilroka committed
616
		-- get friend
Azilroka's avatar
Azilroka committed
617 618 619
		if FriendsFrame.selectedFriendType == FRIENDS_BUTTON_TYPE_WOW then
			selectedFriend = C_FriendList.GetSelectedFriend()
		elseif FriendsFrame.selectedFriendType == FRIENDS_BUTTON_TYPE_BNET then
Azilroka's avatar
Update  
Azilroka committed
620
			selectedFriend = BNGetSelectedFriend()
Azilroka's avatar
Azilroka committed
621 622
		end
		-- set to first in list if no friend
Azilroka's avatar
Azilroka committed
623 624
		if not selectedFriend or selectedFriend == 0 then
			FriendsFrame_SelectFriend(FriendListEntries[1].buttonType, 1)
Azilroka's avatar
Update  
Azilroka committed
625
			selectedFriend = 1
Azilroka's avatar
Azilroka committed
626 627
		end
		-- check if friend is online
Azilroka's avatar
Azilroka committed
628
		FriendsFrameSendMessageButton:SetEnabled(FriendsList_CanWhisperFriend(FriendsFrame.selectedFriendType, selectedFriend))
Azilroka's avatar
Azilroka committed
629
	else
Azilroka's avatar
Update  
Azilroka committed
630
		FriendsFrameSendMessageButton:Disable()
Azilroka's avatar
Azilroka committed
631
	end
Azilroka's avatar
Update  
Azilroka committed
632
	FriendsFrame.selectedFriend = selectedFriend
Azilroka's avatar
Azilroka committed
633 634

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

Azilroka's avatar
Azilroka committed
661
function FG:FriendGroups_SaveOpenMenu()
Azilroka's avatar
Azilroka committed
662 663 664 665 666 667
	if OPEN_DROPDOWNMENUS then
		OPEN_DROPDOWNMENUS_SAVE = CopyTable(OPEN_DROPDOWNMENUS)
	end
end

-- when one of our new menu items is clicked
Azilroka's avatar
Azilroka committed
668
function FG:FriendGroups_OnFriendMenuClick(self)
669 670 671
	if not self.value then
		return
	end
Azilroka's avatar
Azilroka committed
672

673 674 675
	local add = strmatch(self.value, "FGROUPADD_(.+)")
	local del = strmatch(self.value, "FGROUPDEL_(.+)")
	local creating = self.value == "FRIEND_GROUP_NEW"
Azilroka's avatar
Azilroka committed
676

677 678 679
	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
680

681
		if source == "BN_FRIEND" or source == "BN_FRIEND_OFFLINE" then
Azilroka's avatar
Azilroka committed
682 683 684 685 686
			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
Azilroka's avatar
Azilroka committed
687
					note = FG:AddGroup(note, add)
688
				else
Azilroka's avatar
Azilroka committed
689
					note = FG:RemoveGroup(note, del)
Azilroka's avatar
Azilroka committed
690 691 692
				end
				BNSetFriendNote(dropdown.bnetIDAccount, note)
			end
693
		elseif source == "FRIEND" or source == "FRIEND_OFFLINE" then
Azilroka's avatar
Azilroka committed
694 695 696 697
			for i = 1, C_FriendList.GetNumFriends() do
				local friend_info = C_FriendList.GetFriendInfoByIndex(i)
				local name = friend_info.name
				local note = friend_info.notes
698
				if dropdown.name and name:find(dropdown.name) then
Azilroka's avatar
Azilroka committed
699 700 701 702
					if creating then
						StaticPopup_Show("FRIEND_GROUP_CREATE", nil, nil, { id = i, note = note, set = SetFriendNotes })
					else
						if add then
Azilroka's avatar
Azilroka committed
703 704 705 706
							note = FG:AddGroup(note, add)
						else
							note = FG:RemoveGroup(note, del)
						end
Azilroka's avatar
Azilroka committed
707 708
						SetFriendNotes(i, note)
					end
709 710 711 712
					break
				end
			end
		end
Azilroka's avatar
Azilroka committed
713
		FG:FriendGroups_Update()
714 715
	end
	HideDropDownMenu(1)
Azilroka's avatar
Azilroka committed
716 717 718
end

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

722 723 724 725 726 727 728 729 730
	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
731

732 733 734 735 736 737 738 739 740
	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
Azilroka's avatar
Azilroka committed
741 742 743 744
			for i = 1, C_FriendList.GetNumFriends() do
				local friend_info = C_FriendList.GetFriendInfoByIndex(i)
				local name = friend_info.name
				local noteText = friend_info.notes
745 746 747 748 749 750 751
				if dropdown.name and name:find(dropdown.name) then
					note = noteText
					break
				end
			end
		end

Azilroka's avatar
Azilroka committed
752
		FG:NoteAndGroups(note, groups)
753 754 755 756 757

		for _,group in ipairs(GroupSorted) do
			if group ~= "" and not groups[group] then
				local faux = "FGROUPADD_" .. group
				--polluting the popup buttons list
Azilroka's avatar
Azilroka committed
758
				UnitPopupButtons[faux] = { text = group}
759 760 761 762 763 764
				table.insert(UnitPopupMenus["FRIEND_GROUP_ADD"], faux)
			end
		end
		for group in pairs(groups) do
			if group ~= "" then
				local faux = "FGROUPDEL_" .. group
Azilroka's avatar
Azilroka committed
765
				UnitPopupButtons[faux] = { text = group}
766 767 768 769
				table.insert(UnitPopupMenus["FRIEND_GROUP_DEL"], faux)
			end
		end
	end
Azilroka's avatar
Azilroka committed
770 771
end

Azilroka's avatar
Azilroka committed
772
function FG:FriendGroups_Rename(self, old)
773 774 775 776 777 778 779
	local input = self.editBox:GetText()
	if input == "" then
		return
	end
	local groups = {}
	for i = 1, BNGetNumFriends() do
		local presenceID, _, _, _, _, _, _, _, _, _, _, _, noteText = BNGetFriendInfo(i)
Azilroka's avatar
Azilroka committed
780
		local note = FG:NoteAndGroups(noteText, groups)
781 782 783
		if groups[old] then
			groups[old] = nil
			groups[input] = true
Azilroka's avatar
Azilroka committed
784
			note = FG:CreateNote(note, groups)
785 786 787
			BNSetFriendNote(presenceID, note)
		end
	end
Azilroka's avatar
Azilroka committed
788 789 790
	for i = 1, C_FriendList.GetNumFriends() do
		local note = C_FriendList.GetFriendInfoByIndex(i) and C_FriendList.GetFriendInfoByIndex(i).notes
		note = FG:NoteAndGroups(note, groups)
791 792 793
		if groups[old] then
			groups[old] = nil
			groups[input] = true
Azilroka's avatar
Azilroka committed
794
			note = FG:CreateNote(note, groups)
795 796 797
			SetFriendNotes(i, note)
		end
	end
Azilroka's avatar
Azilroka committed
798
	FG:FriendGroups_Update()
Azilroka's avatar
Azilroka committed
799 800
end

Azilroka's avatar
Azilroka committed
801
function FG:FriendGroups_Create(self, data)
802 803 804 805
	local input = self.editBox:GetText()
	if input == "" then
		return
	end
Azilroka's avatar
Azilroka committed
806
	local note = FG:AddGroup(data.note, input)
Azilroka's avatar
Azilroka committed
807 808 809 810
	data.set(data.id, note)
end

StaticPopupDialogs["FRIEND_GROUP_RENAME"] = {
811 812 813 814
	text = "Enter new group name",
	button1 = ACCEPT,
	button2 = CANCEL,
	hasEditBox = 1,
Azilroka's avatar
Azilroka committed
815
	OnAccept = FG.FriendGroups_Rename,
816 817
	EditBoxOnEnterPressed = function(self)
		local parent = self:GetParent()
Azilroka's avatar
Azilroka committed
818
		FG:FriendGroups_Rename(parent, parent.data)
819 820 821 822 823
		parent:Hide()
	end,
	timeout = 0,
	whileDead = 1,
	hideOnEscape = 1
Azilroka's avatar
Azilroka committed
824 825 826
}

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

Azilroka's avatar
Azilroka committed
842
function FG:InviteOrGroup(clickedgroup, invite)
843
	local groups = {}
Azilroka's avatar
Azilroka committed
844 845
	for i = 1, BNGetNumFriends() do
		local presenceID, _, _, _, _, toonID, _, _, _, _, _, _, noteText = BNGetFriendInfo(i)
Azilroka's avatar
Azilroka committed
846
		local note = FG:NoteAndGroups(noteText, groups)
Azilroka's avatar
Azilroka committed
847 848 849 850
		if groups[clickedgroup] then
			if invite and toonID then
				BNInviteFriend(toonID)
			elseif not invite then
851
				groups[clickedgroup] = nil
Azilroka's avatar
Azilroka committed
852
				note = FG:CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
853 854 855 856
				BNSetFriendNote(presenceID, note)
			end
		end
	end
Azilroka's avatar
Azilroka committed
857 858 859 860 861 862
	for i = 1, C_FriendList.GetNumFriends() do
		local friend_info = C_FriendList.GetFriendInfoByIndex(i)
		local name = friend_info.name
		local connected = friend_info.connected
		local noteText = friend_info.notes
		local note = FG:NoteAndGroups(noteText, groups)
Azilroka's avatar
Azilroka committed
863 864 865 866
		if groups[clickedgroup] then
			if invite and connected then
				InviteUnit(name)
			elseif not invite then
867
				groups[clickedgroup] = nil
Azilroka's avatar
Azilroka committed
868
				note = FG:CreateNote(note, groups)
Azilroka's avatar
Azilroka committed
869 870 871 872 873 874 875 876 877
				SetFriendNotes(i, note)
			end
		end
	end
end

local FriendGroups_Menu = CreateFrame("Frame", "FriendGroups_Menu")
FriendGroups_Menu.displayMode = "MENU"
local menu_items = {
878
	[1] = {
Azilroka's avatar
Azilroka committed
879
		{ text = "", notCheckable = true, isTitle = true },
Azilroka's avatar
Azilroka committed
880
		{ text = "Invite all to party", notCheckable = true, func = function(self, menu, clickedgroup) FG:InviteOrGroup(clickedgroup, true) end },
Azilroka's avatar
Azilroka committed
881
		{ text = "Rename group", notCheckable = true, func = function(self, menu, clickedgroup) StaticPopup_Show("FRIEND_GROUP_RENAME", nil, nil, clickedgroup) end },
Azilroka's avatar
Azilroka committed
882
		{ text = "Remove group", notCheckable = true, func = function(self, menu, clickedgroup) FG:InviteOrGroup(clickedgroup, false) end },
Azilroka's avatar
Azilroka committed
883 884 885
		{ text = "Settings", notCheckable = true, hasArrow = true },
	},
}
Azilroka's avatar
Azilroka committed
886

Azilroka's avatar
Azilroka committed
887 888
FriendGroups_Menu.initialize = function(self, level)
	if not menu_items[level] then return end
889
	for _, items in ipairs(menu_items[level]) do
Azilroka's avatar
Azilroka committed
890 891 892 893 894 895 896 897 898 899
		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
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985