Permalink
Browse files

Add pop-up reddit message notifications.

  • Loading branch information...
1 parent 740c722 commit cf54cce50962862439977bbaa4aec8e32582538d @chromakode committed Aug 31, 2009
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -27,3 +27,4 @@ pref("extensions.socialite.sites.reddit.RedditSite.showHide", false);
pref("extensions.socialite.sites.reddit.RedditSite.showRandom", false);
pref("extensions.socialite.sites.reddit.RedditSite.showProfile", false);
pref("extensions.socialite.sites.reddit.RedditSite.watchRedditSiteLinks", true);
+pref("extensions.socialite.sites.reddit.RedditSite.notificationsEnabled", false);
View
@@ -4,6 +4,7 @@ The following images in the directory "content/reddit/" are (C) 2008 CondeNet, I
upmod.png
downgray.png
downmod.png
+ mail.png
The following images in the directory "content/reddit/" are (C) 2008 Mark James, licensed under the Creative Commons Attribution 2.5 License:
comments.png
@@ -1,6 +1,8 @@
generalGroup.caption=General
compactDisplayPreference.label=Use compact icon buttons
compactDisplayPreference.accesskey=i
+notificationsEnabledPreference.label=Display new message notifications
+notificationsEnabledPreference.accesskey=n
displayGroup.caption=Display
showScorePreference.label=Display score
@@ -20,4 +22,8 @@ showProfilePreference.accesskey=p
failureMsg.tryAgain=Unable to perform the requested action. Please try again.
failureMsg.httpStatus=HTTP status %S received.
-failureMsg.noSubreddits=No subscribed subreddits found.
+failureMsg.noSubreddits=No subscribed subreddits found.
+
+messageNotification.title-single=%S new message on %S
+messageNotification.title-plural=%S new messages on %S
+messageNotification.message=Click here to open your inbox
View
@@ -33,6 +33,7 @@ RedditSite.prototype.onLoad = function() {
version.api = this.sitePreferences.getCharPref("version.api");
this.newMessages = [];
+ this.lastNewMessageCount = null;
this.API.init(version);
};
@@ -41,6 +42,7 @@ RedditSite.prototype.setDefaultPreferences = function(siteDefaultBranch) {
siteDefaultBranch.setCharPref("version.dom", "");
siteDefaultBranch.setCharPref("version.api", "");
siteDefaultBranch.setBoolPref("compactDisplay", true);
+ siteDefaultBranch.setBoolPref("notificationsEnabled", false);
siteDefaultBranch.setBoolPref("showScore", true);
siteDefaultBranch.setBoolPref("showSubreddit", true);
siteDefaultBranch.setBoolPref("showComments", true);
@@ -446,7 +448,7 @@ RedditSite.prototype.createBarSubmitUI = function(document) {
json.data.children.sort(subredditSort);
if (json.data.children.length == 0) {
- Socialite.siteFailureMessage(site, "createBarSubmitUI", stringBundle.GetStringFromName("failureMsg.noSubreddits"));
+ Socialite.utils.siteFailureMessage(site, "createBarSubmitUI", stringBundle.GetStringFromName("failureMsg.noSubreddits"));
barSubmit.menulistSubreddit.hidden = true;
} else {
for each (var subredditInfo in json.data.children) {
@@ -527,6 +529,7 @@ RedditSite.prototype.createPreferencesUI = function(document, propertiesWindow)
var generalGroup = addGroupbox(stringBundle.GetStringFromName("generalGroup.caption"));
addBooleanPreferenceUI(generalGroup, "compactDisplay");
+ addBooleanPreferenceUI(generalGroup, "notificationsEnabled");
var displayGroup = addGroupbox(stringBundle.GetStringFromName("displayGroup.caption"));
addBooleanPreferenceUI(displayGroup, "showScore");
@@ -545,28 +548,64 @@ RedditSite.prototype.refreshAlertState = function() {
let site = this;
this.API.messages(
function success(r, json) {
- site.newMessages = json.data.children.filter(function(message) {
- return message.data.new;
- });
-
- site.alertState = site.newMessages.length > 0
+ if (json) {
+ site.newMessages = json.data.children.filter(function(message) {
+ return message.data.new;
+ });
+
+ site.alertState = site.newMessages.length > 0;
+ site.showMessageNotification();
+ }
}/*,
this.actionFailureHandler*/
).perform(false);
}
};
+RedditSite.prototype.showMessageNotification = function() {
+ if (this.sitePreferences.getBoolPref("notificationsEnabled")) {
+ let count = this.newMessages.length;
+ if (count > 0 && this.lastNewMessageCount < count) {
+ let title;
+ if (count == 1) {
+ title = stringBundle.formatStringFromName("messageNotification.title-single", [ count, this.siteName ], 2);
+ } else {
+ title = stringBundle.formatStringFromName("messageNotification.title-plural", [ count, this.siteName ], 2);
+ }
+
+ Socialite.utils.showNotification(
+ title,
+ stringBundle.GetStringFromName("messageNotification.message"),
+ "chrome://socialite/content/reddit/mail_large.png",
+ null,
+ RedditNotificationClickHandler,
+ this.siteURL + "/message/inbox/"
+ );
+ }
+ this.lastNewMessageCount = this.newMessages.length;
+ }
+};
+
RedditSite.prototype.actionFailureHandler = function(r, action) {
// 5xx error codes
+ let text;
if (r.status >= 500 && r.status < 600) {
text = stringBundle.GetStringFromName("failureMsg.tryAgain");
} else {
text = stringBundle.formatStringFromName("failureMsg.httpStatus", [ r.status ], 1);
}
- Socialite.siteFailureMessage(this, action.name, text);
+ Socialite.utils.siteFailureMessage(this, action.name, text);
};
+RedditNotificationClickHandler = {
+ observe: function(subject, topic, data) {
+ if (topic == "alertclickcallback") {
+ Socialite.utils.openUILinkIn(data, "tab");
+ }
+ }
+}
+
// Register this class for instantiation
RedditSite.prototype.siteClassID = "RedditSite";
RedditSite.prototype.siteClassName = "Reddit API";
View
@@ -24,30 +24,6 @@ var Socialite =
Socialite.loaded = true;
}
},
-
- failureMessage: function(title, message) {
- logger.log("Socialite", "Failure occurred, message: " + message);
-
- let titlePart;
- if (title) {
- titlePart = " ("+title+")";
- } else {
- titlePart = "";
- }
-
- if (alertsService) {
- alertsService.showAlertNotification(
- "chrome://global/skin/icons/Error.png",
- Socialite.stringBundle.GetStringFromName("failureMessage.title") + titlePart,
- message,
- null, null, null, "socialite-failure"
- );
- }
- },
-
- siteFailureMessage: function(site, subject, message) {
- Socialite.failureMessage(site.siteName, subject+": "+message);
- },
utils: {
@@ -59,6 +35,43 @@ var Socialite =
openUILinkIn: function(url, where) {
window = windowManager.getMostRecentWindow("navigator:browser");
window.openUILinkIn(url, where);
+ },
+
+ showNotification: function(title, message, icon, name, listener, data) {
+ if (!icon) {
+ icon = "chrome://socialite/skin/socialite.png";
+ }
+
+ if (!name) {
+ name = "socialite-message";
+ }
+
+ let clickable = listener != null;
+ if (alertsService) {
+ alertsService.showAlertNotification(icon, title, message, clickable, data, listener, name);
+ }
+ },
+
+ failureMessage: function(title, message) {
+ logger.log("Socialite", "Failure occurred, message: " + message);
+
+ let titlePart;
+ if (title) {
+ titlePart = " ("+title+")";
+ } else {
+ titlePart = "";
+ }
+
+ Socialite.utils.showNotification(
+ Socialite.stringBundle.GetStringFromName("failureMessage.title") + titlePart,
+ message,
+ "chrome://global/skin/icons/Error.png",
+ "socialite-failure"
+ );
+ },
+
+ siteFailureMessage: function(site, subject, message) {
+ Socialite.failureMessage(site.siteName, subject+": "+message);
}
}

0 comments on commit cf54cce

Please sign in to comment.