zz_Mailbox/core.lua

160 lines
4.7 KiB
Lua

local name, addon = ...
local parentName = addon['parentName'] or "BrokerPack"
local childName = "zz_Mailbox"
local defaults = {
}
local options = {
}
local db
local scanned = false
local ldbicon = LibStub:GetLibrary("LibDBIcon-1.0")
local function OnClick(self, button)
if(IsShiftKeyDown() and button == "LeftButton") then
addon['db']['global']['ldbicons'][childName]['hide'] = not addon['db']['global']['ldbicons'][childName]['hide']
if(ldbicon) then
if(addon['db']['global']['ldbicons'][childName]['hide']) then
ldbicon:Hide(childName)
else
ldbicon:Show(childName)
end
end
else
if(InterfaceOptionsFrame:IsVisible() and not InCombatLockdown()) then
InterfaceOptionsFrame:Hide()
else
InterfaceOptionsFrame_OpenToCategory(childName)
InterfaceOptionsFrame_OpenToCategory(childName) -- Twice because of a bug in InterfaceOptionsFrame_OpenToCategory
end
end
end
local function OnText(message)
addon:OnText(childName, message)
end
local character = UnitName("player")
local realm = GetRealmName()
local faction,_ = UnitFactionGroup('player')
local function mailupdate()
if(not scanned) then
return db[faction][realm][character]['mailunread'],db[faction][realm][character]['mail']
else
local cur,tot = GetInboxNumItems()
db[faction][realm][character]['mail'] = tot
return db[faction][realm][character]['mailunread'], tot
end
end
local function mail()
local packageIcon, stationeryIcon, sender, subject, money, CODAmount, daysLeft, hasItem, wasRead, wasReturned, textCreated, canReply, isGM
local index,newunread = 0,0
local unread, tot = mailupdate()
subject = "start"
while(subject) do
index = index + 1
packageIcon, stationeryIcon, sender, subject, money, CODAmount, daysLeft, hasItem, wasRead, wasReturned, textCreated, canReply, isGM = GetInboxHeaderInfo(index)
if(subject and not wasRead) then
newunread = newunread + 1
if(GetInboxNumItems() == 0 and scanned) then
newunread = 0
end
end
end
if(not(db[faction][realm][character]['mail'] > 0 and GetInboxNumItems() == 0)) then
db[faction][realm][character]['mailunread'] = newunread
end
if(db[faction][realm][character]['newmail']) then
if(HasNewMail() and tot == 0) then
tot = #{GetLatestThreeSenders()}
if(scanned and select(2,GetInboxNumItems()) == 0) then
tot = 0
end
end
if(db[faction][realm][character]['mailunread'] == 0) then
db[faction][realm][character]['mailunread'] = 1
end
if(tot == 0) then
return tot
else
return (db[faction][realm][character]['mailunread'] .. "/" .. tot)
end
else
return tot
end
end
local function iterateMail()
for i = 1, GetInboxNumItems() do
if(not select(9,GetInboxHeaderInfo(i))) then
return 1
end
end
return nil
end
local function updateText(self, event)
db[faction][realm][character]['newmail'] = HasNewMail()
if(MailFrame:IsVisible() and GetInboxNumItems() > 0) then
db[faction][realm][character]['newmail'] = iterateMail()
end
OnText(addon:colorize(mail(),db[faction][realm][character]['newmail'] and "00ff00" or "ffffff"))
if(event == "MAIL_SHOW") then
scanned = true
elseif(event == "UPDATE_PENDING_MAIL") then
scanned = false
end
end
local function OnTooltip(tip)
tip:AddLine(childName)
local senderlist = {GetLatestThreeSenders()}
for _,v in ipairs(senderlist) do
tip:AddLine(addon:colorize(v,"ffffff"))
end
updateText()
end
local origME
local function hideMapMail()
origME = MiniMapMailFrame:GetScript("OnEvent")
MiniMapMailFrame:Hide()
MiniMapMailFrame:SetScript("OnEvent", nil)
end
local function showMapMail()
MiniMapMailFrame:Show()
MiniMapMailFrame:SetScript("OnEvent", origME)
origME = nil
end
local function getPref(info)
return db[info[#info]]
end
local function setPref(info,value)
db[info[#info]] = value
if(not db['showOrgMail'] and not origME) then
hideMapMail()
elseif(db['showOrgMail']) then
showMapMail()
end
end
local function init()
addon:RegisterFunc({'MAIL_SHOW',"MAIL_INBOX_UPDATE","UPDATE_PENDING_MAIL"},"OnEvent", updateText)
options = addon:InitConfig(childName, true, {
['name'] = childName,
['type'] = "data source",
['OnClick'] = OnClick,
['OnTooltipShow'] = OnTooltip,
}, getPref, setPref)
db = addon['db']['profile'][childName]
db[faction] = db[faction] or {}
db[faction][realm] = db[faction][realm] or {}
db[faction][realm][character] = db[faction][realm][character] or {}
db[faction][realm][character]['mail'] = db[faction][realm][character]['mail'] or 0
db[faction][realm][character]['mailunread'] = db[faction][realm][character]['mailunread'] or 0
addon:AddConfigEntry(childName, "toggle","showOrgMail",'Show Mail','Show original Minimap Symbol',1,nil,nil,nil,nil,options['args'][childName])
if(not db['showOrgMail']) then hideMapMail() end
end
addon:startup(name, childName, init, true, defaults)