comparison Core.lua @ 3:c6f0976069c7

Default value for the welcome / bye notification is now set to false The mailframe checkbox now primarily toggles the mail opening, however if you hold shift you can still disable the entire addon Now properly removes QuickAuction?s mail count Now tracks when a mail lost all attachments rather than it being deleted in order to continue processing the next item (mail sent by players containing text should now work properly). This should also be good for a nice speed increase. Added a variable called ?busy? to the MailOpener object indicating whether or not Mail Opener is currently working. Other addons (or macros) can retrieve it with ?LibStub("AceAddon-3.0"):GetAddon("MailOpener").busy? A mail refresh from the Postal service should no longer occur while mail is being opened but will happen instantly afterwards. Postal?s module toggling will now be handled by Postal itself. Added a short summary to the top of all modules for other developers. The time remaining until next mail box refresh should be displayed for as long as Mail Opener can be sure.
author Zerotorescue
date Tue, 07 Sep 2010 17:46:27 +0200
parents 57ba1593ac42
children 1ba07a64bf14
comparison
equal deleted inserted replaced
2:57ba1593ac42 3:c6f0976069c7
1 MailOpener = LibStub("AceAddon-3.0"):NewAddon("MailOpener", "AceEvent-3.0", "AceTimer-3.0"); 1 -- You can access this addon's object through: LibStub("AceAddon-3.0"):GetAddon("MailOpener")
2 2 local MailOpener = LibStub("AceAddon-3.0"):NewAddon("MailOpener", "AceEvent-3.0", "AceTimer-3.0");
3 local enabled = true; 3
4 local MailOpenerConfig, lastAmount, lastQuickAuctionsStatus, opening, freshList, mailboxEmptySoundPlayed; 4 -- You can check if MailOpener is busy with LibStub("AceAddon-3.0"):GetAddon("MailOpener").busy (true or false/nil)
5
6 local AutoOpenMail, MailOpenerConfig, lastAmount, lastQuickAuctionsStatus, freshList, mailboxEmptySoundPlayed;
5 7
6 function MailOpener:OnInitialize() 8 function MailOpener:OnInitialize()
7 local defaults = { 9 local defaults = {
8 global = { 10 global = {
9 uses = 0; 11 uses = 0;
22 }, 24 },
23 modules = { 25 modules = {
24 Collected = true, 26 Collected = true,
25 }, 27 },
26 notifications = { 28 notifications = {
27 welcome = true, 29 welcome = false,
28 bye = true, 30 bye = false,
29 finishedCurrentBatch = true, 31 finishedCurrentBatch = true,
30 mailboxIsEmpty = true, 32 mailboxIsEmpty = true,
31 33
32 skipped = { 34 skipped = {
33 all = true, 35 all = true,
107 109
108 -- Make the open all checkbox 110 -- Make the open all checkbox
109 local check = CreateFrame("CheckButton", "cbMailOpenerEnable", MailFrame, "ChatConfigCheckButtonTemplate"); 111 local check = CreateFrame("CheckButton", "cbMailOpenerEnable", MailFrame, "ChatConfigCheckButtonTemplate");
110 check:SetHeight(26); 112 check:SetHeight(26);
111 check:SetWidth(26); 113 check:SetWidth(26);
112 check:SetChecked(enabled); 114 check:SetChecked(true);
113 check:SetHitRectInsets(0, -80, 0, 0); 115 check:SetHitRectInsets(0, -80, 0, 0);
114 check:SetScript("OnClick", function(self) 116 check:SetScript("OnClick", function(self)
115 if self:GetChecked() then 117 if IsShiftKeyDown() then
116 MailOpener:Enable(); 118 -- Shift key = toggle addon on or off, since addon is already on there's only one option
119
120 if not MailOpener:IsEnabled() then
121 print("|cff15ff00Mail Opener|r: Shift key was held down, so |cff00ff00enabling|r the entire addon as well as automatic opening of mail.");
122
123 MailOpener:Enable();
124
125 -- The above calls MAIL_SHOW which changes AutoOpenMail, so we can't remember the old setting
126 AutoOpenMail = true;
127
128 self:SetChecked(true);
129 else
130 print("|cff15ff00Mail Opener|r: Shift key was held down, so |cffff0000disabling|r the entire addon.");
131
132 MailOpener:Disable();
133
134 self:SetChecked(false);
135 end
117 else 136 else
118 MailOpener:Disable(); 137 -- Normal click
119 end 138
120 end); 139 if self:GetChecked() then
121 check.tooltip = "Toggle Mail Opener on or off."; 140 print("|cff15ff00Mail Opener|r: |cff00ff00Enabling|r automatic opening of mail.");
141
142 AutoOpenMail = true;
143 else
144 print("|cff15ff00Mail Opener|r: |cffff0000Disabling|r automatic opening of mail.");
145
146 AutoOpenMail = false;
147 end
148 end
149 end);
150 check.tooltip = "Toggle automatic mail opening on or off (you can force this by holding shift when opening the mailbox.\n\nHold the |cffffffffSHIFT|r key to disable the entire addon.";
122 check:SetPoint("TOPLEFT", MailFrame, "TOPLEFT", 68, -13) 151 check:SetPoint("TOPLEFT", MailFrame, "TOPLEFT", 68, -13)
123 152
124 if self.QuickAuctionsEnabled then 153 if self.QuickAuctionsEnabled then
125 -- QA is enabled so move the checkbox further to the right 154 -- QA is enabled so move the checkbox further to the right
126 155
220 function MailOpener:OnDisable() 249 function MailOpener:OnDisable()
221 self:UnregisterEvent("MAIL_SHOW"); 250 self:UnregisterEvent("MAIL_SHOW");
222 251
223 self.btnConfig:Hide(); 252 self.btnConfig:Hide();
224 253
225 enabled = false;
226 MailOpener:Stop(); 254 MailOpener:Stop();
227 end 255 end
228 256
229 -- We must disable Quick Auction's auto mail (if set up that way in the settings) before opening the mailbox or it will instantly start sending stuff 257 -- We must disable Quick Auction's auto mail (if set up that way in the settings) before opening the mailbox or it will instantly start sending stuff
230 function MailOpener:PLAYER_ENTERING_WORLD() 258 function MailOpener:PLAYER_ENTERING_WORLD()
246 self:RegisterMessage("MO_OPEN_COMPLETE"); 274 self:RegisterMessage("MO_OPEN_COMPLETE");
247 275
248 self:Debug("defaultStatus:" .. self.db.profile.general.defaultStatus); 276 self:Debug("defaultStatus:" .. self.db.profile.general.defaultStatus);
249 -- Change the mail opening status according to our settings 277 -- Change the mail opening status according to our settings
250 if self.db.profile.general.defaultStatus == "_enabled" then 278 if self.db.profile.general.defaultStatus == "_enabled" then
251 enabled = true; 279 AutoOpenMail = true;
252 else 280 self.cbOpenAll:SetChecked(true);
253 enabled = false; 281 elseif self.db.profile.general.defaultStatus == "disabled" then
254 end 282 -- Disable auto opening but leave mail opener enabled
255 self.cbOpenAll:SetChecked(enabled); 283
284 AutoOpenMail = false;
285 self.cbOpenAll:SetChecked(false);
286 elseif self.db.profile.general.defaultStatus == "xdisabled" then
287 -- Disable entire addon
288
289 MailOpener:Disable();
290 self.cbOpenAll:SetChecked(false);
291 end
256 292
257 self:ToggleQAStatus(); 293 self:ToggleQAStatus();
258 294
259 -- Hide the InboxTooMuchMail warning to allow room for our mail remaining info line 295 -- Hide the InboxTooMuchMail warning to allow room for our mail remaining info line
260 InboxTooMuchMail:Hide() 296 InboxTooMuchMail:Hide()
261 InboxTooMuchMail.Show = function() end 297 InboxTooMuchMail.Show = function() end
262 298
263 if self.QuickAuctionsEnabled then 299 if self.QuickAuctionsEnabled then
264 -- Go through all children of the mail frame to find the post button 300 local QAMail = LibStub("AceAddon-3.0"):GetAddon("QuickAuctions"):GetModule("Mail");
265 local kids = { InboxFrame:GetChildren() }; 301
266 302 -- Hide the open all button
267 for _, child in ipairs(kids) do 303 QAMail.massOpening:Hide();
268 if child then 304 -- Hide the x mail remaining text
269 local width = floor( child:GetWidth() + .5 ); 305 QAMail.totalMail:Hide();
270 local height = floor( child:GetHeight() + .5 );
271
272 -- The open all button has a static width and height
273 if width == 130 and height == 24 then
274 child:Hide();
275 break;
276 end
277 end
278 end
279 end 306 end
280 307
281 if self.db.profile.notifications.welcome then 308 if self.db.profile.notifications.welcome then
282 -- Welcome notification 309 -- Welcome notification
283 local _, c = UnitClass("player"); 310 local _, c = UnitClass("player");
299 -- Fired after a successful server sync 326 -- Fired after a successful server sync
300 -- Fired when mail is deleted (which happens after taking all attachments from a mail sent by the game) 327 -- Fired when mail is deleted (which happens after taking all attachments from a mail sent by the game)
301 function MailOpener:MAIL_INBOX_UPDATE() 328 function MailOpener:MAIL_INBOX_UPDATE()
302 local current, total = GetInboxNumItems(); 329 local current, total = GetInboxNumItems();
303 330
331 -- Calculate the amount of mail waiting that actually have attachments
332 -- If we just compare numbers we won't be including mail that isn't automatically deleted when opened, such as mail with attachments sent by other players
333 local currentMailWithAttachments = 0;
334 for i = 1, current do
335 local _, _, _, _, money, _, _, items = GetInboxHeaderInfo(i);
336
337 if (items and items > 0) or (money and money > 0) then
338 currentMailWithAttachments = currentMailWithAttachments + 1;
339 end
340 end
341
304 local tempLastAmount = lastAmount; 342 local tempLastAmount = lastAmount;
305 lastAmount = current; 343 lastAmount = currentMailWithAttachments;
306 344
307 if current ~= tempLastAmount then 345 if currentMailWithAttachments ~= tempLastAmount then
308 self:Debug("MAIL_INBOX_UPDATE - lastAmount:" .. tempLastAmount .. " - current:" .. current); 346 self:Debug("MAIL_INBOX_UPDATE - lastAmount:" .. tempLastAmount .. " - current:" .. currentMailWithAttachments);
309 end 347 end
310 348
311 if current > tempLastAmount then 349 if currentMailWithAttachments > tempLastAmount then
312 -- New messages arrived in our mailbox, so this was a refresh, so set a timer 350 -- New messages arrived in our mailbox, so this was a refresh, so set a timer
351
352 self:Debug("MO_SERVER_SYNCED");
313 353
314 -- Yell that we successfully synced with the server 354 -- Yell that we successfully synced with the server
315 self:SendMessage("MO_SERVER_SYNCED"); 355 self:SendMessage("MO_SERVER_SYNCED");
316 356
317 -- This list is fresh 357 -- This list is fresh
336 -- Allow overriding of mailopening with the shift key 376 -- Allow overriding of mailopening with the shift key
337 377
338 -- Open the current mail 378 -- Open the current mail
339 self:ScheduleOpen(false); 379 self:ScheduleOpen(false);
340 end 380 end
341 elseif current < tempLastAmount then 381 elseif currentMailWithAttachments < tempLastAmount then
342 -- We lost a mail 382 -- We lost a mail
343 383
384 -- TODO: NYI: May need to delay this until the mail is actually deleted
385
386 self:Debug("MO_MAIL_EMPTIED");
387
344 -- Yell that we successfully opened/removed a mail 388 -- Yell that we successfully opened/removed a mail
345 self:SendMessage("MO_MAIL_DELETED"); 389 self:SendMessage("MO_MAIL_EMPTIED");
346 elseif (current == 50 and tempLastAmount == 50) then 390 elseif (currentMailWithAttachments == 50 and tempLastAmount == 50) then
347 if not IsShiftKeyDown() then 391 if not IsShiftKeyDown() then
348 -- Allow overriding of mailopening with the shift key 392 -- Allow overriding of mailopening with the shift key
349 393
350 -- Open the current mail 394 -- Open the current mail
351 self:ScheduleOpen(false); 395 self:ScheduleOpen(false);
352 end 396 end
353 end 397 end
354
355 tempLastAmount = current;
356 end 398 end
357 399
358 function MailOpener:ScheduleOpen(continued) 400 function MailOpener:ScheduleOpen(continued)
359 if lastAmount > 0 then 401 if lastAmount > 0 then
360 local waitTime; 402 local waitTime;
371 self.tmrOpenNow = self:ScheduleTimer("OpenNow", waitTime); 413 self.tmrOpenNow = self:ScheduleTimer("OpenNow", waitTime);
372 end 414 end
373 end 415 end
374 416
375 function MailOpener:OpenNow() 417 function MailOpener:OpenNow()
376 self:Debug("OpenNow (" .. ((opening and "1") or "0") .. ")"); 418 self:Debug("OpenNow (" .. ((self.busy and "1") or "0") .. ")");
377 if not opening and MailFrame:IsVisible() then 419 if not self.busy and MailFrame:IsVisible() and AutoOpenMail then
378 self:Debug("OpenNow"); 420 self:Debug("OpenNow");
379 421
380 -- BeanCounter is the only addon hiding the mail close button while busy, so we can look for that 422 -- BeanCounter is the only addon hiding the mail close button while busy, so we can look for that
381 local BeanCounterActive = not InboxCloseButton:IsVisible(); 423 local BeanCounterActive = not InboxCloseButton:IsVisible();
382 424
396 self:Debug("Turning automail |cffff0000off|r."); 438 self:Debug("Turning automail |cffff0000off|r.");
397 439
398 QuickAuctionsAutoMail:Click(); 440 QuickAuctionsAutoMail:Click();
399 end 441 end
400 442
401 opening = true; 443 self.busy = true;
402 444
403 self:Debug("MO_OPEN_MAIL"); 445 self:Debug("MO_OPEN_MAIL");
404 446
405 -- Summon the mail opening gods 447 -- Summon the mail opening gods
406 self:SendMessage("MO_OPEN_MAIL"); 448 self:SendMessage("MO_OPEN_MAIL");
414 end 456 end
415 end 457 end
416 end 458 end
417 459
418 function MailOpener:MO_OPEN_COMPLETE() 460 function MailOpener:MO_OPEN_COMPLETE()
419 opening = false; 461 self.busy = false;
420 462
421 -- Try a recheck 463 -- Try a recheck
422 self:Recheck(); 464 self:Recheck();
423 465
424 local current, total = GetInboxNumItems(); 466 local current, total = GetInboxNumItems();
425 467
426 if (total - current) == 0 then 468 if (total - current) == 0 then
427 -- Play the sound 469 -- There is probably no unopenable mail remaining, so play the sound (if enabled)
470
428 if self.db.profile.notifications.mailboxEmptySound and (not MailOpener.db.profile.notifications.mailboxEmptySoundOnlyOnce or not mailboxEmptySoundPlayed) then 471 if self.db.profile.notifications.mailboxEmptySound and (not MailOpener.db.profile.notifications.mailboxEmptySoundOnlyOnce or not mailboxEmptySoundPlayed) then
429 PlaySoundFile(self.db.profile.notifications.mailboxEmptySoundFile); 472 PlaySoundFile(self.db.profile.notifications.mailboxEmptySoundFile);
430 mailboxEmptySoundPlayed = true; 473 mailboxEmptySoundPlayed = true;
431 end 474 end
432 end 475 end
433 476
434 if self.db.profile.general.autoReenableQAAutoMail and self.QuickAuctionsEnabled and QuickAuctionsAutoMail and not QuickAuctionsAutoMail:GetChecked() then 477 if self.QuickAuctionsEnabled then
435 -- If auto re-enable "QA Auto mail" is enabled and QA's auto mail is currently toggled OFF, turn it on 478 -- Quick Auctions enabled?
436 -- We need to do this with a :click to trigger the right events 479 -- Toggle automailing as per settings
437 480
438 self:Debug("Turning automail |cff00ff00on|r."); 481 if self.db.profile.general.autoReenableQAAutoMail and QuickAuctionsAutoMail and not QuickAuctionsAutoMail:GetChecked() then
439 482 -- If auto re-enable "QA Auto mail" is enabled and QA's auto mail is currently toggled OFF, turn it on
440 QuickAuctionsAutoMail:Click(); 483 -- We need to do this with a :click to trigger the right events
441 elseif self.db.profile.general.autoSetBackQAAutoMail and self.QuickAuctionsEnabled and QuickAuctionsAutoMail and lastQuickAuctionsStatus ~= QuickAuctionsAutoMail:GetChecked() then 484
442 -- If auto set back "QA Auto mail" is enabled and QA's auto mail is currently not the same as it was before starting, toggle it
443 -- We need to do this with a :click to trigger the right events
444
445 if lastQuickAuctionsStatus then
446 self:Debug("Turning automail |cff00ff00on|r."); 485 self:Debug("Turning automail |cff00ff00on|r.");
447 else 486
448 self:Debug("Turning automail |cffff0000off|r."); 487 QuickAuctionsAutoMail:Click();
449 end 488 elseif self.db.profile.general.autoSetBackQAAutoMail and QuickAuctionsAutoMail and lastQuickAuctionsStatus ~= QuickAuctionsAutoMail:GetChecked() then
450 489 -- If auto set back "QA Auto mail" is enabled and QA's auto mail is currently not the same as it was before starting, toggle it
451 QuickAuctionsAutoMail:Click(); 490 -- We need to do this with a :click to trigger the right events
491
492 if lastQuickAuctionsStatus then
493 self:Debug("Turning automail |cff00ff00on|r.");
494 else
495 self:Debug("Turning automail |cffff0000off|r.");
496 end
497
498 QuickAuctionsAutoMail:Click();
499 end
452 end 500 end
453 501
454 if self.db.profile.general.continueOpening then 502 if self.db.profile.general.continueOpening then
455 self:ScheduleOpen(true); 503 self:ScheduleOpen(true);
456 end 504 end
461 self:Debug("|cffffff00Recheck|r"); 509 self:Debug("|cffffff00Recheck|r");
462 510
463 -- Freshlist prevents this from being run too often 511 -- Freshlist prevents this from being run too often
464 -- It is set to true after a server sync 512 -- It is set to true after a server sync
465 -- and set to false 61 seconds afterwards with a recheck called instantly after it 513 -- and set to false 61 seconds afterwards with a recheck called instantly after it
466 if not freshList and MailFrame:IsVisible() then 514
515 -- We're not refreshing while we're opening because it is automatically done when current batch was completely opened
516 if not freshList and not self.busy and MailFrame:IsVisible() then
467 self:Debug("|cff00ff00Recheck|r"); 517 self:Debug("|cff00ff00Recheck|r");
468 518
469 -- If this isn't a fresh list (so messages weren't received within the last 60 seconds) and the mailbox wasn't closed 519 -- If this isn't a fresh list (so messages weren't received within the last 60 seconds) and the mailbox wasn't closed
470 520
471 -- BeanCounter is the only addon hiding the mail close button while busy, so we can look for that 521 -- BeanCounter is the only addon hiding the mail close button while busy, so we can look for that
472 local BeanCounterActive = not InboxCloseButton:IsVisible(); 522 local BeanCounterActive = not InboxCloseButton:IsVisible();
473 523
474 if not BeanCounterActive then 524 if not BeanCounterActive and AutoOpenMail then
475 -- Query the server 525 -- Query the server
476 CheckInbox(); 526 CheckInbox();
477 end 527 end
478 528
479 -- Stop previous timer 529 -- Stop previous timer
489 function MailOpener:Stop() 539 function MailOpener:Stop()
490 if self.db.profile.notifications.bye then 540 if self.db.profile.notifications.bye then
491 print("|cff15ff00Mail Opener|r: Have a nice day. :)"); 541 print("|cff15ff00Mail Opener|r: Have a nice day. :)");
492 end 542 end
493 543
494 opening = false; 544 self.busy = false;
495 545
496 -- We won't need this anymore 546 -- We won't need this anymore
497 self:UnregisterEvent("MAIL_CLOSED"); 547 self:UnregisterEvent("MAIL_CLOSED");
498 self:UnregisterEvent("PLAYER_LEAVING_WORLD"); 548 self:UnregisterEvent("PLAYER_LEAVING_WORLD");
499 self:UnregisterEvent("MAIL_INBOX_UPDATE"); 549 self:UnregisterEvent("MAIL_INBOX_UPDATE");
539 -- Toggle Postal's opening modules on or off 589 -- Toggle Postal's opening modules on or off
540 function MailOpener:TogglePostalModule(name, status) 590 function MailOpener:TogglePostalModule(name, status)
541 if MailOpener.PostalEnabled and Postal then 591 if MailOpener.PostalEnabled and Postal then
542 -- Postal must be enabled 592 -- Postal must be enabled
543 593
544 -- Remember the setting in Postal 594 -- Toggle module (let Postal handle this)
545 Postal.db.profile.ModuleEnabledState[name] = status; 595 Postal.ToggleModule(nil, name, Postal:GetModule(name), status);
546
547 -- Toggle module
548 if status then
549 Postal:GetModule(name):Enable();
550 else
551 Postal:GetModule(name):Disable();
552 end
553 end 596 end
554 end 597 end
555 598
556 -- Change Quick Auction's auto mail status based on our prefered settings 599 -- Change Quick Auction's auto mail status based on our prefered settings
557 function MailOpener:ToggleQAStatus() 600 function MailOpener:ToggleQAStatus()