aboutsummaryrefslogtreecommitdiffstats
path: root/lib/prompts.js
blob: bb42c39eea4bd407ec88b0704d8f5c86f9655d19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*global exports: false, require: false, console: false */
/*jslint onevar: false */
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";
// ==============================================================
var {Cc,Ci} = require("chrome");
// just for JSLINT var Cc, Ci = {};
var promptTitle = "Bugzilla Triage Script";

/**
 * shows the text in a simple window
 *
 * @return none
 */
exports.alert = function alert(msg) {
    var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"]
        .getService(Ci.nsIPromptService);
    prompts.alert(null, promptTitle, msg);
};

/**
 * general prompts for a string method
 *
 * @return String with the password
 */
exports.prompt = function prompt(prompt, defaultValue) {
    var stringValue = {
        value: defaultValue ? defaultValue : ""
    };

    var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"]
            .getService(Ci.nsIPromptService);
    var result = prompts.prompt(null, promptTitle, prompt,
            stringValue, null, {});
    if (result) {
        return stringValue.value;
    } else {
        return null;
    }
};

/**
 * returns password with a special password
 *
 * @return String with the password
 */
exports.promptPassword = function promptPassword(prompt) {
    if (!prompt) { // either undefined or null
        prompt = "Enter password:";
    }
    var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"]
            .getService(Ci.nsIPromptService);
    var password = {
        value : ""
    }; // default the password to pass
    var check = {
        value : true
    }; // default the checkbox to true
    var result = prompts.promptPassword(null, "Bugzilla Triage Script", prompt,
            password, null, check);
    // result is true if OK was pressed, false if cancel was pressed.
    // password.value is set if OK was pressed.
    // The checkbox is not displayed.
    if (result) {
        return password.value ? password.value : null;
    } else {
        return null;
    }
};

/**
 * YES/NO prompt; returns boolean or null (for Cancel)
 * https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIPromptService
 */
exports.promptYesNoCancel = function promptOKNoCancel(prompt) {
    if (!prompt) { // either undefined or null
        throw new Error("Prompt is required!");
    }
    var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"]
            .getService(Ci.nsIPromptService);

    var result = prompts.confirmEx(null, "Bugzilla Triage Script", prompt,
            prompts.STD_YES_NO_BUTTONS, null, null, null, null, {});
    if (result === 0) {
        return true;
    } else if (result === 1) {
        return false;
    } else {
        return null;
    }
};

/**
 *
 * documentation is https://developer.mozilla.org/en/NsIFilePicker
 */
exports.promptFileOpenPicker = function promptFilePicker (win) {
    var fp = Cc["@mozilla.org/filepicker;1"]
        .createInstance(Ci.nsIFilePicker);
    fp.init(win, "JSON File Open", Ci.nsIFilePicker.modeOpen);
    fp.appendFilter("JSON files", "*.json");
    fp.appendFilters(Ci.nsIFilePicker.filterAll);
    fp.filterIndex = 0;
    var res = fp.show();

    if (res === Ci.nsIFilePicker.returnOK ||
        res === Ci.nsIFilePicker.returnReplace ) {
            return fp.file.path
    }
    return null;
};

/**
 * Show a system notification with the given message
 *
 * @param msg String or Object with a message to be shown in a default
 * notification or object with properties title, icon, and body
 * @param callback Function called when user clicks on the notification (optional)
 * @return None
 */
exports.notification = function notification(msg) {
    var body = msg;
    var title = "Bugzilla Notification";
    var icon = null;
    var textClickable = false;

    if (typeof(msg) === "object") {
        body = msg.body;
        if ("title" in msg) {
            title = msg.title;
        }
        if ("icon" in msg) {
            icon = msg.icon;
        }
    }
    try {
        var classObj = Cc["@mozilla.org/alerts-service;1"];
        var alertService = classObj.getService(Ci.nsIAlertsService);

        alertService.showAlertNotification(icon, title, body);
        return true;
    } catch (e) {
        console.error("Unable to display notification:", msg);
        return false;
    }
};