add GUI to manage contacts
- show hint when trying to save an illegal contact (empty name or collision) - contacts list (selection logic is a bit less straight forward than with messages because contacts are not a list but have strings as keys to prevent duplicate entries) - contacts list: only show first line of notes - add direct recipients (in TO) to contacts when sending (in gui.lua, as it's a comfort feature) - set messages immediately when clicking read/unread, not *after* redrawing the inbox - set messages only when something changed to reduce disk writes - move marking mails read to the show message formspec (exept the direct button) - adjust "No Mail" label
This commit is contained in:
parent
f82c3d2b82
commit
b60cd978b9
200
gui.lua
200
gui.lua
@ -1,4 +1,5 @@
|
|||||||
selected_message_idxs = {}
|
selected_message_idxs = {}
|
||||||
|
selected_contact_idxs = {}
|
||||||
|
|
||||||
local theme
|
local theme
|
||||||
if minetest.get_modpath("default") then
|
if minetest.get_modpath("default") then
|
||||||
@ -23,15 +24,15 @@ mail.inbox_formspec = "size[8,9;]" .. theme .. [[
|
|||||||
tablecolumns[color;text;text]
|
tablecolumns[color;text;text]
|
||||||
table[0,0;5.75,9;messages;#999,From,Subject]]
|
table[0,0;5.75,9;messages;#999,From,Subject]]
|
||||||
|
|
||||||
mail.contacts_formspec = "size[8,10;]" .. theme .. [[
|
mail.contacts_formspec = "size[8,9;]" .. theme .. [[
|
||||||
button[6,0.00;2,0.5;new;New]
|
button[6,0.10;2,0.5;new;New]
|
||||||
button[6,0.75;2,0.5;edit;Edit]
|
button[6,0.85;2,0.5;edit;Edit]
|
||||||
button[6,1.50;2,0.5;delete;Delete]
|
button[6,1.60;2,0.5;delete;Delete]
|
||||||
button[6,8.25;2,0.5;back;Back]
|
button[6,8.25;2,0.5;back;Back]
|
||||||
tablecolumns[color;text;text]
|
tablecolumns[color;text;text]
|
||||||
table[0,0;5.75,9;contacts;#999,Name,Note]]
|
table[0,0;5.75,9;contacts;#999,Name,Note]]
|
||||||
|
|
||||||
mail.select_contact_formspec = "size[8,10;]" .. theme .. [[
|
mail.select_contact_formspec = "size[8,9;]" .. theme .. [[
|
||||||
button[6,0.00;2,0.5;select;Select]
|
button[6,0.00;2,0.5;select;Select]
|
||||||
button[6,8.25;2,0.5;back;Back]
|
button[6,8.25;2,0.5;back;Back]
|
||||||
tablecolumns[color;text]
|
tablecolumns[color;text]
|
||||||
@ -96,7 +97,7 @@ function mail.show_inbox(name)
|
|||||||
end
|
end
|
||||||
formspec[#formspec + 1] = "]"
|
formspec[#formspec + 1] = "]"
|
||||||
else
|
else
|
||||||
formspec[#formspec + 1] = "]label[2,4.5;No mail]"
|
formspec[#formspec + 1] = "]label[2.25,4.5;No mail]"
|
||||||
end
|
end
|
||||||
minetest.show_formspec(name, "mail:inbox", table.concat(formspec, ""))
|
minetest.show_formspec(name, "mail:inbox", table.concat(formspec, ""))
|
||||||
end
|
end
|
||||||
@ -107,17 +108,32 @@ function mail.show_contacts(name)
|
|||||||
minetest.show_formspec(name, "mail:contacts", table.concat(formspec, ""))
|
minetest.show_formspec(name, "mail:contacts", table.concat(formspec, ""))
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.show_edit_contact(name, contact_name, note)
|
function mail.show_edit_contact(name, contact_name, note, illegal_name_hint)
|
||||||
local formspec = [[
|
local formspec = [[
|
||||||
size[8,7.2]
|
size[6,7]
|
||||||
button[7,0;1,0.5;back;X]
|
button[4,6.25;2,0.5;back;Back]
|
||||||
label[0,0;Player name: %s]
|
field[0.25,0.5;4,1;name;Player name:;%s]
|
||||||
label[0,0.5;Note: %s]
|
textarea[0.25,1.6;4,6.25;note;Note:;%s]
|
||||||
button[1,6.7;2,1;reply;Reply]
|
button[4,0.10;2,1;save;Save]
|
||||||
button[3,6.7;2,1;forward;Forward]
|
]]
|
||||||
button[5,6.7;2,1;delete;Delete]
|
if illegal_name_hint == "collision" then
|
||||||
]] .. theme
|
formspec = formspec .. [[
|
||||||
minetest.show_formspec(name, "mail:editcontact", table.concat(formspec, ""))
|
label[4,1;That name]
|
||||||
|
label[4,1.5;is already in]
|
||||||
|
label[4,2;your contacts.]
|
||||||
|
]]
|
||||||
|
elseif illegal_name_hint == "empty" then
|
||||||
|
formspec = formspec .. [[
|
||||||
|
label[4,1;The contact]
|
||||||
|
label[4,1.5;name cannot]
|
||||||
|
label[4,2;be empty.]
|
||||||
|
]]
|
||||||
|
end
|
||||||
|
formspec = formspec .. theme
|
||||||
|
formspec = string.format(formspec,
|
||||||
|
minetest.formspec_escape(contact_name or ""),
|
||||||
|
minetest.formspec_escape(note or ""))
|
||||||
|
minetest.show_formspec(name, "mail:editcontact", formspec)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.show_select_contact(name)
|
function mail.show_select_contact(name)
|
||||||
@ -129,17 +145,29 @@ end
|
|||||||
function mail.compile_contact_list(name, formspec)
|
function mail.compile_contact_list(name, formspec)
|
||||||
local contacts = mail.getContacts(name)
|
local contacts = mail.getContacts(name)
|
||||||
|
|
||||||
if contacts[1] then
|
local i = 0
|
||||||
for _, contact in ipairs(contacts) do
|
local selected = nil
|
||||||
formspec[#formspec + 1] = ","
|
for k, contact in pairs(contacts) do
|
||||||
formspec[#formspec + 1] = ","
|
formspec[#formspec + 1] = ","
|
||||||
formspec[#formspec + 1] = minetest.formspec_escape(contact.name)
|
formspec[#formspec + 1] = ","
|
||||||
formspec[#formspec + 1] = ","
|
formspec[#formspec + 1] = minetest.formspec_escape(contact.name)
|
||||||
formspec[#formspec + 1] = minetest.formspec_escape(contact.note)
|
formspec[#formspec + 1] = ","
|
||||||
|
local note = contact.note
|
||||||
|
-- display an ellipsis if the note spans multiple lines
|
||||||
|
local idx = string.find(note, '\n')
|
||||||
|
if idx ~= nil then
|
||||||
|
note = string.sub(note, 1, idx-1) .. ' ...'
|
||||||
end
|
end
|
||||||
if selected_contact_idxs[name] then
|
formspec[#formspec + 1] = minetest.formspec_escape(note)
|
||||||
|
i = i + 1
|
||||||
|
if selected_contact_idxs[name] == k then
|
||||||
|
selected = i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if i > 0 then
|
||||||
|
if selected then
|
||||||
formspec[#formspec + 1] = ";"
|
formspec[#formspec + 1] = ";"
|
||||||
formspec[#formspec + 1] = tostring(selected_contact_idxs[name] + 1)
|
formspec[#formspec + 1] = tostring(selected + 1)
|
||||||
end
|
end
|
||||||
formspec[#formspec + 1] = "]"
|
formspec[#formspec + 1] = "]"
|
||||||
else
|
else
|
||||||
@ -172,6 +200,11 @@ function mail.show_message(name, msgnumber)
|
|||||||
local body = minetest.formspec_escape(message.body)
|
local body = minetest.formspec_escape(message.body)
|
||||||
formspec = string.format(formspec, from, to, cc, subject, body)
|
formspec = string.format(formspec, from, to, cc, subject, body)
|
||||||
|
|
||||||
|
if message.unread then
|
||||||
|
message.unread = false
|
||||||
|
mail.setMessages(name, messages)
|
||||||
|
end
|
||||||
|
|
||||||
minetest.show_formspec(name,"mail:message",formspec)
|
minetest.show_formspec(name,"mail:message",formspec)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -252,21 +285,19 @@ function mail.handle_receivefields(player, formname, fields)
|
|||||||
local evt = minetest.explode_table_event(fields.messages)
|
local evt = minetest.explode_table_event(fields.messages)
|
||||||
selected_message_idxs[name] = evt.row - 1
|
selected_message_idxs[name] = evt.row - 1
|
||||||
if evt.type == "DCL" and messages[selected_message_idxs[name]] then
|
if evt.type == "DCL" and messages[selected_message_idxs[name]] then
|
||||||
messages[selected_message_idxs[name]].unread = false
|
|
||||||
mail.show_message(name, selected_message_idxs[name])
|
mail.show_message(name, selected_message_idxs[name])
|
||||||
end
|
end
|
||||||
mail.setMessages(name, messages)
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
if fields.read then
|
if fields.read then
|
||||||
if messages[selected_message_idxs[name]] then
|
if messages[selected_message_idxs[name]] then
|
||||||
messages[selected_message_idxs[name]].unread = false
|
|
||||||
mail.show_message(name, selected_message_idxs[name])
|
mail.show_message(name, selected_message_idxs[name])
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif fields.delete then
|
elseif fields.delete then
|
||||||
if messages[selected_message_idxs[name]] then
|
if messages[selected_message_idxs[name]] then
|
||||||
table.remove(messages, selected_message_idxs[name])
|
table.remove(messages, selected_message_idxs[name])
|
||||||
|
mail.setMessages(name, messages)
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.show_inbox(name)
|
mail.show_inbox(name)
|
||||||
@ -285,24 +316,27 @@ function mail.handle_receivefields(player, formname, fields)
|
|||||||
elseif fields.markread then
|
elseif fields.markread then
|
||||||
if messages[selected_message_idxs[name]] then
|
if messages[selected_message_idxs[name]] then
|
||||||
messages[selected_message_idxs[name]].unread = false
|
messages[selected_message_idxs[name]].unread = false
|
||||||
|
-- set messages immediately, so it shows up already when updating the inbox
|
||||||
|
mail.setMessages(name, messages)
|
||||||
end
|
end
|
||||||
-- set messages immediately, so it shows up already when updating the inbox
|
|
||||||
mail.setMessages(name, messages)
|
|
||||||
mail.show_inbox(name)
|
mail.show_inbox(name)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
elseif fields.markunread then
|
elseif fields.markunread then
|
||||||
if messages[selected_message_idxs[name]] then
|
if messages[selected_message_idxs[name]] then
|
||||||
messages[selected_message_idxs[name]].unread = true
|
messages[selected_message_idxs[name]].unread = true
|
||||||
|
-- set messages immediately, so it shows up already when updating the inbox
|
||||||
|
mail.setMessages(name, messages)
|
||||||
end
|
end
|
||||||
-- set messages immediately, so it shows up already when updating the inbox
|
|
||||||
mail.setMessages(name, messages)
|
|
||||||
mail.show_inbox(name)
|
mail.show_inbox(name)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
elseif fields.new then
|
elseif fields.new then
|
||||||
mail.show_compose(name)
|
mail.show_compose(name)
|
||||||
|
|
||||||
|
elseif fields.contacts then
|
||||||
|
mail.show_contacts(name)
|
||||||
|
|
||||||
elseif fields.quit then
|
elseif fields.quit then
|
||||||
if minetest.get_modpath("unified_inventory") then
|
if minetest.get_modpath("unified_inventory") then
|
||||||
unified_inventory.set_inventory_formspec(player, "craft")
|
unified_inventory.set_inventory_formspec(player, "craft")
|
||||||
@ -313,7 +347,6 @@ function mail.handle_receivefields(player, formname, fields)
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.setMessages(name, messages)
|
|
||||||
return true
|
return true
|
||||||
elseif formname == "mail:message" then
|
elseif formname == "mail:message" then
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
@ -334,44 +367,43 @@ function mail.handle_receivefields(player, formname, fields)
|
|||||||
elseif fields.delete then
|
elseif fields.delete then
|
||||||
if messages[selected_message_idxs[name]] then
|
if messages[selected_message_idxs[name]] then
|
||||||
table.remove(messages,selected_message_idxs[name])
|
table.remove(messages,selected_message_idxs[name])
|
||||||
|
mail.setMessages(name, messages)
|
||||||
end
|
end
|
||||||
mail.show_inbox(name)
|
mail.show_inbox(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.setMessages(name, messages)
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
elseif formname == "mail:compose" then
|
elseif formname == "mail:compose" then
|
||||||
if fields.send then
|
if fields.send then
|
||||||
|
local name = player:get_player_name()
|
||||||
mail.send({
|
mail.send({
|
||||||
from = player:get_player_name(),
|
from = name,
|
||||||
to = fields.to,
|
to = fields.to,
|
||||||
cc = fields.cc,
|
cc = fields.cc,
|
||||||
bcc = fields.bcc,
|
bcc = fields.bcc,
|
||||||
subject = fields.subject,
|
subject = fields.subject,
|
||||||
body = fields.body,
|
body = fields.body,
|
||||||
})
|
})
|
||||||
|
local contacts = mail.getContacts(name)
|
||||||
|
local recipients = mail.parse_player_list(fields.to)
|
||||||
|
local changed = false
|
||||||
|
for _,v in pairs(recipients) do
|
||||||
|
if contacts[string.lower(v)] == nil then
|
||||||
|
contacts[string.lower(v)] = {
|
||||||
|
name = v,
|
||||||
|
note = "",
|
||||||
|
}
|
||||||
|
changed = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if changed then
|
||||||
|
mail.setContacts(name, contacts)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
minetest.after(0.5, function()
|
minetest.after(0.5, function()
|
||||||
mail.show_inbox(player:get_player_name())
|
mail.show_inbox(player:get_player_name())
|
||||||
end)
|
end)
|
||||||
return true
|
return true
|
||||||
--[[elseif formname == "mail:contacts" then
|
|
||||||
if fields.back then
|
|
||||||
mail.show_inbox(name)
|
|
||||||
elseif fields.new then
|
|
||||||
mail.show_edit_contact(name, "", "")
|
|
||||||
elseif fields.edit then
|
|
||||||
if contacts[selected_contact_idxs[name]]--[[ then
|
|
||||||
mail.show_edit_contact(name, selected_contact_idxs[name], "")
|
|
||||||
elseif fields.delete then
|
|
||||||
if contacts[selected_contact_idxs[name]]--[[ then
|
|
||||||
table.remove(contacts,selected_contact_idxs[name])
|
|
||||||
end
|
|
||||||
mail.show_contacts(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
mail.setContacts(name, contacts)
|
|
||||||
return true]]--
|
|
||||||
|
|
||||||
elseif formname == "mail:contacts" then
|
elseif formname == "mail:contacts" then
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
@ -379,30 +411,72 @@ function mail.handle_receivefields(player, formname, fields)
|
|||||||
|
|
||||||
if fields.contacts then
|
if fields.contacts then
|
||||||
local evt = minetest.explode_table_event(fields.contacts)
|
local evt = minetest.explode_table_event(fields.contacts)
|
||||||
selected_contact_idxs[name] = evt.row - 1
|
--selected_contact_idxs[name] = evt.row - 1
|
||||||
if evt.type == "DCL" and contacts[selected_contact_idxs[name]] then
|
local i = 0
|
||||||
mail.show_contact(name, selected_contact_idxs[name])
|
for k,c in pairs(contacts) do
|
||||||
|
i = i + 1
|
||||||
|
if i == evt.row - 1 then
|
||||||
|
selected_contact_idxs[name] = k
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if evt.type == "DCL" and contacts[selected_contact_idxs[name]] then
|
||||||
|
mail.show_edit_contact(name, contacts[selected_contact_idxs[name]].name, contacts[selected_contact_idxs[name]].note)
|
||||||
end
|
end
|
||||||
mail.setContacts(name, contacts)
|
|
||||||
return true
|
return true
|
||||||
elseif fields.new then
|
elseif fields.new then
|
||||||
mail.show_edit_contact(name,"", "")
|
selected_contact_idxs[name] = "#NEW#"
|
||||||
|
mail.show_edit_contact(name, "", "")
|
||||||
elseif fields.edit then
|
elseif fields.edit then
|
||||||
mail.show_contact(name, selected_contact_idxs[name])
|
mail.show_edit_contact(name, contacts[selected_contact_idxs[name]].name, contacts[selected_contact_idxs[name]].note)
|
||||||
elseif fields.delete then
|
elseif fields.delete then
|
||||||
if contacts[selected_contact_idxs[name]] then
|
if contacts[selected_contact_idxs[name]] then
|
||||||
table.remove(contacts, selected_contact_idxs[name])
|
contacts[selected_contact_idxs[name]] = nil
|
||||||
|
mail.setContacts(name, contacts)
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.show_inbox(name)
|
mail.show_contacts(name)
|
||||||
|
|
||||||
elseif fields.back then
|
elseif fields.back then
|
||||||
mail.show_inbox(name)
|
mail.show_inbox(name)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
elseif formname == "mail:editcontact" then
|
||||||
|
local name = player:get_player_name()
|
||||||
|
local contacts = mail.getContacts(name)
|
||||||
|
|
||||||
mail.setMessages(name, messages)
|
if fields.save then
|
||||||
return true
|
if selected_contact_idxs[name] and selected_contact_idxs[name] ~= "#NEW#" then
|
||||||
|
local contact = contacts[selected_contact_idxs[name]]
|
||||||
|
if selected_contact_idxs[name] ~= string.lower(fields.name) then
|
||||||
|
-- name changed!
|
||||||
|
if #fields.name == 0 then
|
||||||
|
mail.show_edit_contact(name, contact.name, fields.note, "empty")
|
||||||
|
return true
|
||||||
|
elseif contacts[string.lower(fields.name)] ~= nil then
|
||||||
|
mail.show_edit_contact(name, contact.name, fields.note, "collision")
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
contacts[string.lower(fields.name)] = contact
|
||||||
|
contacts[selected_contact_idxs[name]] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
contact.name = fields.name
|
||||||
|
contact.note = fields.note
|
||||||
|
else
|
||||||
|
local contact = {
|
||||||
|
name = fields.name,
|
||||||
|
note = fields.note,
|
||||||
|
}
|
||||||
|
contacts[string.lower(contact.name)] = contact
|
||||||
|
end
|
||||||
|
mail.setContacts(name, contacts)
|
||||||
|
mail.show_contacts(name)
|
||||||
|
|
||||||
|
elseif fields.back then
|
||||||
|
mail.show_contacts(name)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
elseif fields.mail then
|
elseif fields.mail then
|
||||||
mail.show_inbox(player:get_player_name())
|
mail.show_inbox(player:get_player_name())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user