Skip to content

Commit

Permalink
[Feature] Add ability to set different default actions
Browse files Browse the repository at this point in the history
for training buttons (#11)
  • Loading branch information
moisseev committed Aug 19, 2018
1 parent 002bcb3 commit e85cc4b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
40 changes: 31 additions & 9 deletions chrome/content/options.xul
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<?xml-stylesheet type="text/css" href="chrome://messenger/skin/preferences/preferences.css"?>

<!DOCTYPE prefwindow SYSTEM "chrome://rspamd-spamness/locale/options.dtd">
<!DOCTYPE window [
<!ENTITY % messageHeaderDTD SYSTEM "chrome://rspamd-spamness/locale/messageHeader.dtd">
%messageHeaderDTD;
<!ENTITY % optionsDTD SYSTEM "chrome://rspamd-spamness/locale/options.dtd" >
%optionsDTD;
]>

<prefwindow id="spamnessOptionsWindow" title="&spamnessOptions.title;" buttons="accept,cancel"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
Expand All @@ -20,7 +25,8 @@
<preference id="pref_trainingButtons.enabled" name="extensions.rspamd-spamness.trainingButtons.enabled" type="bool" />
<preference id="pref_uri.folderTrainHam" name="extensions.rspamd-spamness.uri.folderTrainHam" type="string" />
<preference id="pref_uri.folderTrainSpam" name="extensions.rspamd-spamness.uri.folderTrainSpam" type="string" />
<preference id="pref_trainingButtons.defaultAction" name="extensions.rspamd-spamness.trainingButtons.defaultAction" type="string" />
<preference id="pref_trainingButtonHam.defaultAction" name="extensions.rspamd-spamness.trainingButtonHam.defaultAction" type="string" />
<preference id="pref_trainingButtonSpam.defaultAction" name="extensions.rspamd-spamness.trainingButtonSpam.defaultAction" type="string" />
</preferences>
<hbox>
<vbox>
Expand Down Expand Up @@ -63,15 +69,31 @@
<textbox id="folderTrainSpam" size="80" preference="pref_uri.folderTrainSpam"
tooltiptext="&spamnessOptions.prefFolderURITrainHam.tooltip;" />
</hbox>
<groupbox flex="1">
<caption label="&spamnessOptions.prefTrainingButtonsDefaultAction.title;" />
<hbox>
<label control="trainingButtonsDefaultAction" value="&spamnessOptions.prefTrainingButtonsDefaultAction.title;" />
<menulist id="trainingButtonsDefaultAction" preference="pref_trainingButtons.defaultAction">
<menupopup id="default-action-menupopup">
<menuitem value="move" label="Move" />
<menuitem value="copy" label="Copy" />
</menupopup>
</menulist>
<vbox>
<label control="trainingButtonHamDefaultAction" flex="1" value="&spamness.buttonTrainHam.label;" />
<label control="trainingButtonSpamDefaultAction" flex="1" value="&spamness.buttonTrainSpam.label;" />
</vbox>
<vbox>
<menulist id="trainingButtonHamDefaultAction" preference="pref_trainingButtonHam.defaultAction">
<menupopup id="default-action-menupopup">
<menuitem value="move" label="Move" />
<menuitem value="copy" label="Copy" />
</menupopup>
</menulist>

<menulist id="trainingButtonSpamDefaultAction" preference="pref_trainingButtonSpam.defaultAction">
<menupopup id="default-action-menupopup">
<menuitem value="move" label="Move" />
<menuitem value="copy" label="Copy" />
</menupopup>
</menulist>
</vbox>
<spacer flex="1" />
</hbox>
</groupbox>
</groupbox>
</hbox>
<hbox>
Expand Down
13 changes: 12 additions & 1 deletion chrome/content/scripts/spamness-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const prefObserver = {
if (aTopic !== "nsPref:changed") {
return;
}
if (aData === "trainingButtons.defaultAction") {
if (aData === "trainingButtonHam.defaultAction" ||
aData === "trainingButtonSpam.defaultAction"
) {
RspamdSpamness.setBtnCmdLabels();
}
if (aData === "trainingButtons.enabled") {
Expand Down Expand Up @@ -42,6 +44,15 @@ const toolbarObserver = {
RspamdSpamness.onLoad = function () {
const {prefs} = Services;

// Convert legacy preference
if (prefs.getPrefType("extensions.rspamd-spamness.trainingButtons.defaultAction")) {
if (prefs.getCharPref("extensions.rspamd-spamness.trainingButtons.defaultAction") === "copy") {
prefs.setCharPref("extensions.rspamd-spamness.trainingButtonHam.defaultAction", "copy");
prefs.setCharPref("extensions.rspamd-spamness.trainingButtonSpam.defaultAction", "copy");
}
prefs.clearUserPref("extensions.rspamd-spamness.trainingButtons.defaultAction");
}

RspamdSpamness.previousSpamnessHeader = prefs.getCharPref("extensions.rspamd-spamness.header").toLowerCase();
RspamdSpamness.syncHeaderPrefs(RspamdSpamness.previousSpamnessHeader);
RspamdSpamness.setBtnCmdLabels();
Expand Down
40 changes: 27 additions & 13 deletions chrome/content/scripts/spamness.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

// eslint-disable-next-line no-var
var RspamdSpamness = {
customDBHeaders: [],
customHeaders: [],
previousSpamnessHeader: "",
trainingButtonDefaultAction: "move"
customDBHeaders: [],
customHeaders: [],
previousSpamnessHeader: "",
trainingButtonHamDefaultAction: "move",
trainingButtonSpamDefaultAction: "move"
};

Components.utils.import("resource://gre/modules/AddonManager.jsm");
Expand Down Expand Up @@ -221,8 +222,16 @@ RspamdSpamness.openAddonOptions = function () {
RspamdSpamness.moveMessage = function (folder, isDefault) {
const destination =
MailUtils.getFolderForURI(Services.prefs.getCharPref("extensions.rspamd-spamness.uri.folder" + folder));
if (isDefault && RspamdSpamness.trainingButtonDefaultAction === "copy" ||
!isDefault && RspamdSpamness.trainingButtonDefaultAction !== "copy")
if (
folder === "TrainHam" && (
isDefault && RspamdSpamness.trainingButtonHamDefaultAction === "copy" ||
!isDefault && RspamdSpamness.trainingButtonHamDefaultAction !== "copy"
) ||
folder === "TrainSpam" && (
isDefault && RspamdSpamness.trainingButtonSpamDefaultAction === "copy" ||
!isDefault && RspamdSpamness.trainingButtonSpamDefaultAction !== "copy"
)
)
MsgCopyMessage(destination);
else
MsgMoveMessage(destination);
Expand All @@ -235,18 +244,23 @@ RspamdSpamness.setBtnCmdLabels = function () {
el.setAttribute("label", label);
}

RspamdSpamness.trainingButtonDefaultAction =
Services.prefs.getCharPref("extensions.rspamd-spamness.trainingButtons.defaultAction");

if (RspamdSpamness.trainingButtonDefaultAction === "copy") {
RspamdSpamness.trainingButtonHamDefaultAction =
Services.prefs.getCharPref("extensions.rspamd-spamness.trainingButtonHam.defaultAction");
if (RspamdSpamness.trainingButtonHamDefaultAction === "copy") {
setLabel("btnHamCmdPrimary", "Copy");
setLabel("btnSpamCmdPrimary", "Copy");
setLabel("btnHamCmdSecondary", "Move");
setLabel("btnSpamCmdSecondary", "Move");
} else {
setLabel("btnHamCmdPrimary", "Move");
setLabel("btnSpamCmdPrimary", "Move");
setLabel("btnHamCmdSecondary", "Copy");
}

RspamdSpamness.trainingButtonSpamDefaultAction =
Services.prefs.getCharPref("extensions.rspamd-spamness.trainingButtonSpam.defaultAction");
if (RspamdSpamness.trainingButtonSpamDefaultAction === "copy") {
setLabel("btnSpamCmdPrimary", "Copy");
setLabel("btnSpamCmdSecondary", "Move");
} else {
setLabel("btnSpamCmdPrimary", "Move");
setLabel("btnSpamCmdSecondary", "Copy");
}
};
Expand Down
3 changes: 2 additions & 1 deletion defaults/preferences/spamness.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pref("extensions.rspamd-spamness.headers.symbols_order", "score");
pref("extensions.rspamd-spamness.isDefaultColumn", true);
pref("extensions.rspamd-spamness.installationGreeting", true);
pref("extensions.rspamd-spamness.trainingButtons.enabled", true);
pref("extensions.rspamd-spamness.trainingButtons.defaultAction", "move");
pref("extensions.rspamd-spamness.trainingButtonHam.defaultAction", "move");
pref("extensions.rspamd-spamness.trainingButtonSpam.defaultAction", "move");

0 comments on commit e85cc4b

Please sign in to comment.