async Task updateNotifications() { try { NotificationCounts counts = await client.PollNotificationCountsAsync(); if (counts != null) { CredentialStore.NotifyAuthAttempt(true); updateUi(counts); } } catch (System.Net.Http.HttpRequestException ex) { if (ex.Message.Contains("401")) { // Login info expired CredentialStore.NotifyAuthAttempt(false); if (CredentialStore.ShouldClearAuth()) { logOut(); } } else { markException(Properties.Resources.ErrorPolling + ex.Message); } } catch (Exception ex) { markException(Properties.Resources.Exception + ex.Message); } }
async Task updateNotifications() { try { NotificationCounts counts = await client.PollNotificationCountsAsync(); if (counts != null) { updateUi(counts); } } catch (System.Net.Http.HttpRequestException ex) { if (ex.Message.Contains("401")) { // Login info expired logOut(); } else { markException(Properties.Resources.ErrorPolling + ex.Message); } } catch (Exception ex) { markException(Properties.Resources.Exception + ex.Message); } }
void updatePopupCounts(NotificationCounts counts) { var settings = Settings.Default; commentsMenuItem.Text = counts.Comments == 1 ? Resources.CommentsSingular : string.Format(Resources.CommentsPlural, counts.Comments); commentsMenuItem.Tag = counts.Comments; commentsMenuItem.Available = counts.Comments > 0 || settings.AlwaysShowComments; itemsMenuItem.Text = counts.Items == 1 ? Resources.ItemsSingular : string.Format(Resources.ItemsPlural, counts.Items); itemsMenuItem.Tag = counts.Items; itemsSeparator.Available = itemsMenuItem.Available = counts.Items > 0 || settings.AlwaysShowItems; invitesMenuItem.Text = counts.Invites == 1 ? Resources.InvitesSingular : string.Format(Resources.InvitesPlural, counts.Invites); invitesMenuItem.Tag = counts.Invites; invitesSeparator.Available = invitesMenuItem.Available = counts.Invites > 0 || settings.AlwaysShowInvites; giftsMenuItem.Text = counts.Gifts == 1 ? Resources.GiftsSingular : string.Format(Resources.GiftsPlural, counts.Gifts); giftsMenuItem.Tag = counts.Gifts; giftsSeparator.Available = giftsMenuItem.Available = counts.Gifts > 0 || settings.AlwaysShowGifts; offlineMessagesMenuItem.Text = counts.OfflineMessages == 1 ? Resources.OfflineMessagesSingular : string.Format(Resources.OfflineMessagesPlural, counts.OfflineMessages); offlineMessagesMenuItem.Tag = counts.OfflineMessages; offlineMessagesSeparator.Available = offlineMessagesMenuItem.Available = counts.OfflineMessages > 0 || settings.AlwaysShowOfflineMessages; tradeOffersMenuItem.Text = counts.TradeOffers == 1 ? Resources.TradeOffersSingular : string.Format(Resources.TradeOffersPlural, counts.TradeOffers); tradeOffersMenuItem.Tag = counts.TradeOffers; tradeOffersSeparator.Available = tradeOffersMenuItem.Available = counts.TradeOffers > 0 || settings.AlwaysShowTradeOffers; asyncGameMenuItem.Text = counts.AsyncGames == 1 ? Resources.AsyncGamesSingular : string.Format(Resources.AsyncGamesPlural, counts.AsyncGames); asyncGameMenuItem.Tag = counts.AsyncGames; asyncGameMenuSeparator.Available = asyncGameMenuItem.Available = counts.AsyncGames > 0 || settings.AlwaysShowAsyncGames; moderatorMessageMenuItem.Text = counts.ModeratorMessages == 1 ? Resources.ModeratorMessagesSingular : string.Format(Resources.ModeratorMessagesPlural, counts.ModeratorMessages); moderatorMessageMenuItem.Tag = counts.ModeratorMessages; moderatorMessageSeparator.Available = moderatorMessageMenuItem.Available = counts.ModeratorMessages > 0 || settings.AlwaysShowModeratorMessages; helpRequestReplyMenuItem.Text = counts.HelpRequestReplies == 1 ? Resources.HelpRequestRepliesSingular : string.Format(Resources.HelpRequestRepliesPlural, counts.HelpRequestReplies); helpRequestReplyMenuItem.Tag = counts.HelpRequestReplies; helpRequestReplySeparator.Available = helpRequestReplyMenuItem.Available = counts.HelpRequestReplies > 0 || settings.AlwaysShowHelpRequestReplies; accountAlertReplyMenuItem.Text = counts.AccountAlerts == 1 ? Resources.AccountAlertsSingular : string.Format(Resources.AccountAlertsPlural, counts.AccountAlerts); accountAlertReplyMenuItem.Tag = counts.AccountAlerts; accountAlertReplySeparator.Available = accountAlertReplyMenuItem.Available = counts.AccountAlerts > 0 || settings.AlwaysShowAccountAlerts; // Hide top separator if it's the first item foreach (ToolStripItem item in notificationsContextMenu.Items) { if (item.Available) { if (item is ToolStripMenuItem) { break; } item.Available = false; break; } } }
public async Task <NotificationCounts> PollNotificationCountsAsync() { HttpClientHandler handler = new HttpClientHandler { CookieContainer = cookies }; using (HttpClient client = new HttpClient(handler)) { string response = await client.GetStringAsync("https://steamcommunity.com/actions/GetNotificationCounts"); if (response == "null") { return(null); } NotificationCounts counts = new NotificationCounts(); JObject respObj = JObject.Parse(response); JToken notifsObj = respObj["notifications"]; foreach (JProperty notif in notifsObj) { switch (notif.Name) { case "4": counts.Comments = (int)notif.Value; break; case "5": counts.Items = (int)notif.Value; break; case "6": counts.Invites = (int)notif.Value; break; case "8": counts.Gifts = (int)notif.Value; break; case "9": counts.OfflineMessages = (int)notif.Value; break; case "1": counts.TradeOffers = (int)notif.Value; break; case "2": counts.AsyncGames = (int)notif.Value; break; case "3": counts.ModeratorMessages = (int)notif.Value; break; case "10": counts.HelpRequestReplies = (int)notif.Value; break; } counts.TotalNotifications += (int)notif.Value; } PrevCounts = CurrentCounts; CurrentCounts = counts; return(counts); } }
void updateUi(NotificationCounts counts) { try { if (oldCounts == null) { countsDiff = new NotificationCounts(); } else { // Experimental anti-flapping: don't set counts to zero unless we have updatesUntilSetCountsToZero // of confirmed polls with zero notifications if (Properties.Settings.Default.EnableAntiFlapping) { if (counts.TotalNotifications > 0) { updatesSinceLastNonZeroNotificationsCount = 0; } else { ++updatesSinceLastNonZeroNotificationsCount; if (updatesSinceLastNonZeroNotificationsCount < updatesUntilSetCountsToZero) { return; } } } var prev = oldCounts; countsDiff = new NotificationCounts { Comments = counts.Comments - prev.Comments, Items = counts.Items - prev.Items, Invites = counts.Invites - prev.Invites, Gifts = counts.Gifts - prev.Gifts, OfflineMessages = counts.OfflineMessages - prev.OfflineMessages, TradeOffers = counts.TradeOffers - prev.TradeOffers, AsyncGames = counts.AsyncGames - prev.AsyncGames, ModeratorMessages = counts.ModeratorMessages - prev.ModeratorMessages, HelpRequestReplies = counts.HelpRequestReplies - prev.HelpRequestReplies, AccountAlerts = counts.AccountAlerts - prev.AccountAlerts, TotalNotifications = counts.TotalNotifications - prev.TotalNotifications, }; } updatePopupCounts(counts); mainIcon.Text = Application.ProductName; countIcon.Text = string.Format(counts.TotalNotifications == 1 ? Properties.Resources.UnreadNotificationsSingular : Properties.Resources.UnreadNotificationsPlural, counts.TotalNotifications); if (counts.TotalNotifications == 0) { hasNotifications = false; countIcon.Visible = false; ReplaceNotifyIcon(mainIcon, IconUtils.CreateIconWithBackground(Properties.Resources.NotificationDefault, Properties.Settings.Default.InboxNoneColor, SystemInformation.SmallIconSize)); mainIcon.Visible = true; } else { hasNotifications = true; Color newColor; if (oldCounts == null) { newNotifAcknowledged = true; newColor = Properties.Settings.Default.InboxAvailableColor; } else { if (counts.TotalNotifications > oldCounts.TotalNotifications) { newNotifAcknowledged = false; newColor = Properties.Settings.Default.InboxNewColor; } else if (counts.TotalNotifications == oldCounts.TotalNotifications) { newColor = newNotifAcknowledged ? Properties.Settings.Default.InboxAvailableColor : Properties.Settings.Default.InboxNewColor; } else { newNotifAcknowledged = true; newColor = Properties.Settings.Default.InboxAvailableColor; } } ReplaceNotifyIcon(mainIcon, IconUtils.CreateIconWithBackground(Properties.Resources.NotificationActive, newColor, SystemInformation.SmallIconSize)); // 7 point for 3 digits // 8 point for 2 digits // 9 point for 1 digit string text = counts.TotalNotifications.ToString(); ReplaceNotifyIcon(countIcon, IconUtils.CreateIconWithText(text, new Font("Arial", 10 - text.Length, FontStyle.Regular, GraphicsUnit.Point), Properties.Settings.Default.NotificationCountColor, newColor, SystemInformation.SmallIconSize)); if (!countIcon.Visible) { // Hide main icon first, then show in this order so the count is on the left mainIcon.Visible = false; countIcon.Visible = true; } mainIcon.Visible = !Properties.Settings.Default.SingleIcon; if (Properties.Settings.Default.EnableBalloons && !muted) { List <string> notifications = new List <string>(); if (countsDiff.Comments > 0) { notifications.Add(countsDiff.Comments == 1 ? Properties.Resources.CommentsSingular : string.Format(Properties.Resources.CommentsPlural, countsDiff.Comments)); } if (countsDiff.Items > 0) { notifications.Add(countsDiff.Items == 1 ? Properties.Resources.ItemsSingular : string.Format(Properties.Resources.ItemsPlural, countsDiff.Items)); } if (countsDiff.Invites > 0) { notifications.Add(countsDiff.Invites == 1 ? Properties.Resources.InvitesSingular : string.Format(Properties.Resources.InvitesPlural, countsDiff.Invites)); } if (countsDiff.Gifts > 0) { notifications.Add(countsDiff.Gifts == 1 ? Properties.Resources.GiftsSingular : string.Format(Properties.Resources.GiftsPlural, countsDiff.Gifts)); } if (countsDiff.OfflineMessages > 0) { notifications.Add(countsDiff.OfflineMessages == 1 ? Properties.Resources.OfflineMessagesSingular : string.Format(Properties.Resources.OfflineMessagesPlural, countsDiff.OfflineMessages)); } if (countsDiff.TradeOffers > 0) { notifications.Add(countsDiff.TradeOffers == 1 ? Properties.Resources.TradeOffersSingular : string.Format(Properties.Resources.TradeOffersPlural, countsDiff.TradeOffers)); } if (countsDiff.AsyncGames > 0) { notifications.Add(countsDiff.AsyncGames == 1 ? Properties.Resources.AsyncGamesSingular : string.Format(Properties.Resources.AsyncGamesPlural, countsDiff.AsyncGames)); } if (countsDiff.ModeratorMessages > 0) { notifications.Add(countsDiff.ModeratorMessages == 1 ? Properties.Resources.ModeratorMessagesSingular : string.Format(Properties.Resources.ModeratorMessagesPlural, countsDiff.ModeratorMessages)); } if (countsDiff.HelpRequestReplies > 0) { notifications.Add(countsDiff.HelpRequestReplies == 1 ? Properties.Resources.HelpRequestRepliesSingular : string.Format(Properties.Resources.HelpRequestRepliesPlural, countsDiff.HelpRequestReplies)); } if (countsDiff.AccountAlerts > 0) { notifications.Add(countsDiff.AccountAlerts == 1 ? Properties.Resources.AccountAlertsSingular : string.Format(Properties.Resources.AccountAlertsPlural, countsDiff.AccountAlerts)); } if (notifications.Count > 0) { countIcon.BalloonTipIcon = ToolTipIcon.Info; countIcon.BalloonTipTitle = countIcon.Text; StringBuilder sb = new StringBuilder(); sb.AppendLine(Properties.Resources.NewNotifsSince); sb.AppendLine(); foreach (string notif in notifications) { sb.AppendLine(notif); } countIcon.BalloonTipText = sb.ToString(); countIcon.ShowBalloonTip(10000); // Per MSDN, timeout doesn't make a difference (since Vista) } } } oldCounts = counts; } catch (Exception ex) { markException(Properties.Resources.Exception + ex.Message); } }