zz_Mailbox/core.lua

174 lines
5.2 KiB
Lua

local addonName, addonTable = ...
local childName = "zz_Mailbox"
local db, dbf, dbr,dbp, scanned, ldbobj
addonTable['events'] = addonTable['events'] or {}
local events = addonTable['events']
local ldbicon = LibStub:GetLibrary("LibDBIcon-1.0")
local function prepareDB()
local faction = UnitFactionGroup('player')
local realm = GetNormalizedRealmName()
local player = UnitName("player")
addonTable['db']['profile'][childName] = addonTable['db']['profile'][childName] or {}
db = addonTable['db']['profile'][childName]
db[faction] = db[faction] or {}
dbf = db[faction]
dbf[realm] = dbf[realm] or {}
dbr = dbf[realm]
dbr[player] = dbr[player] or {
['mail'] = 0,
['mailunread'] = 0
}
dbp = dbr[player]
end
local folder = addonName ~= childName and format("Interface\\AddOns\\%s\\%s", addonName, childName) or format("Interface\\AddOns\\%s", childName)
local function iterateMail()
for i = 1, GetInboxNumItems() do
if(not select(9,GetInboxHeaderInfo(i))) then
return 1
end
end
return
end
local function OnText(message)
if(ldbobj and ldbobj['text'] ~= message) then
ldbobj['text'] = message
end
end
local function mailupdate()
if(not scanned) then
return dbp['mailunread'], dbp['mail']
else
local cur, tot = GetInboxNumItems()
dbp['mail'] = tot
return dbp['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(dbp['mail'] > 0 and GetInboxNumItems() == 0)) then
dbp['mailunread'] = newunread
end
if(dbp['newmail']) then
if(HasNewMail() and tot == 0) then
tot = #{GetLatestThreeSenders()}
if(scanned and select(2,GetInboxNumItems()) == 0) then
tot = 0
end
end
if(dbp['mailunread'] == 0) then
dbp['mailunread'] = 1
end
if(tot == 0) then
return tot
else
return (dbp['mailunread'] .. "/" .. tot)
end
else
return tot
end
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
events.MAIL_SHOW = function(self, event)
-- print(event)
dbp['newmail'] = HasNewMail()
if(MailFrame:IsVisible() and GetInboxNumItems() > 0) then
dbp['newmail'] = iterateMail()
end
OnText(addonTable:colorize(mail(),dbp['newmail'] and "00ff00" or "ffffff"))
if(event == "MAIL_SHOW") then
scanned = true
elseif(event == "UPDATE_PENDING_MAIL") then
scanned = false
end
end
events.MAIL_INBOX_UPDATE = events.MAIL_SHOW
events.UPDATE_PENDING_MAIL = events.MAIL_SHOW
function events:PLAYER_ENTERING_WORLD(event)
prepareDB()
ldbobj = LibStub:GetLibrary("zzLDB"):GetLDB(childName)
addonTable:UnregisterEvent('PLAYER_ENTERING_WORLD')
local config = LibStub:GetLibrary("zzConfig")
local main = addonTable['options']['args'][childName]
config:AddConfigEntry(main, "showOrgMail", "toggle", 'Show Mail', 'Show original Minimap Symbol',1)
if(not db['showOrgMail']) then hideMapMail() end
end
local function OnClick(self, button)
if(IsShiftKeyDown() and button == "LeftButton") then
addonTable['db']['global']['ldbicons'][childName]['hide'] = not addonTable['db']['global']['ldbicons'][childName]['hide']
if(ldbicon) then
if(addonTable['db']['global']['ldbicons'][childName]['hide']) then
ldbicon:Hide(childName)
else
ldbicon:Show(childName)
end
end
else
if(not InCombatLockdown()) then
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
end
local function OnTooltipShow(tip)
tip:AddLine(childName)
local senderlist = {GetLatestThreeSenders()}
for _,v in ipairs(senderlist) do
tip:AddLine(addonTable:colorize(v,"ffffff"))
end
events.MAIL_SHOW(addonTable, 'FIRE')
end
function addonTable:PrefChanged(pref, value, oldvalue)
if(not db['showOrgMail'] and not origME) then
hideMapMail()
elseif(db['showOrgMail']) then
showMapMail()
end
end
local specs = {
['name'] = childName,
['sv'] = true,
['cfg'] = true,
['cfgReset'] = prepareDB,
['ldb'] = { -- https://github.com/tekkub/libdatabroker-1-1/wiki/Data-Specifications
['type'] = 'data source', -- required: 'data source' or 'launcher'
['text'] = childName, -- required/optional for launcher
['icon'] = format("%s\\icon2.tga", folder), -- optional/required for launcher
['OnClick'] = OnClick, -- optional/required for launcher
['OnTooltipShow'] = OnTooltipShow, -- optional
} ,
['defaults'] = {
['profile'] = {
[childName] = {}
}
}
}
LibStub:GetLibrary("zzAddOn"):NewAddOn(specs, ...)