UNPKG

4.37 kBJavaScriptView Raw
1/**
2 * Wrapper for the notifu 1.6 (http://www.paralint.com/projects/notifu/)
3
4Usage
5/t <value> The type of message to display values are:
6 info The message is an informational message
7 warn The message is an warning message
8 error The message is an error message
9/d <value> The number of milliseconds to display (omit or 0 for infinit)
10/p <value> The title (or prompt) of the ballon
11/m <value> The message text
12/i <value> Specify an icon to use ("parent" uses the icon of the parent process)
13/e Enable ballon tips in the registry (for this user only)
14/q Do not play a sound when the tooltip is displayed
15/w Show the tooltip even if the user is in the quiet period that follows his very first login (Windows 7 and up)
16/xp Use IUserNotification interface event when IUserNotification2 is available
17
18// Kill codes:
19 2 = Timeout
20 3 = Clicked
21 4 = Closed or faded out
22
23 */
24var path = require('path');
25var notifier = path.resolve(__dirname, '../vendor/notifu/notifu');
26var checkGrowl = require('../lib/checkGrowl');
27var utils = require('../lib/utils');
28var Toaster = require('./toaster');
29var Growl = require('./growl');
30var os = require('os');
31
32var EventEmitter = require('events').EventEmitter;
33var util = require('util');
34
35var hasGrowl = void 0;
36
37module.exports = WindowsBalloon;
38
39function WindowsBalloon(options) {
40 options = utils.clone(options || {});
41 if (!(this instanceof WindowsBalloon)) {
42 return new WindowsBalloon(options);
43 }
44
45 this.options = options;
46
47 EventEmitter.call(this);
48}
49util.inherits(WindowsBalloon, EventEmitter);
50
51function noop() {}
52WindowsBalloon.prototype.notify = function(options, callback) {
53 var fallback;
54 var notifierOptions = this.options;
55 options = utils.clone(options || {});
56 callback = callback || noop;
57
58 if (typeof options === 'string') {
59 options = { title: 'node-notifier', message: options };
60 }
61
62 var actionJackedCallback = utils.actionJackerDecorator(
63 this,
64 options,
65 callback,
66 function(data) {
67 if (data === 'activate') {
68 return 'click';
69 }
70 if (data === 'timeout') {
71 return 'timeout';
72 }
73 return false;
74 }
75 );
76
77 if (!!this.options.withFallback && utils.isWin8()) {
78 fallback = fallback || new Toaster(notifierOptions);
79 return fallback.notify(options, callback);
80 }
81
82 if (
83 !!this.options.withFallback &&
84 (!utils.isLessThanWin8() || hasGrowl === true)
85 ) {
86 fallback = fallback || new Growl(notifierOptions);
87 return fallback.notify(options, callback);
88 }
89
90 if (!this.options.withFallback || hasGrowl === false) {
91 doNotification(options, notifierOptions, actionJackedCallback);
92 return this;
93 }
94
95 checkGrowl(notifierOptions, function(_, hasGrowlResult) {
96 hasGrowl = hasGrowlResult;
97
98 if (hasGrowl) {
99 fallback = fallback || new Growl(notifierOptions);
100 return fallback.notify(options, callback);
101 }
102
103 doNotification(options, notifierOptions, actionJackedCallback);
104 });
105
106 return this;
107};
108
109var allowedArguments = ['t', 'd', 'p', 'm', 'i', 'e', 'q', 'w', 'xp'];
110
111function doNotification(options, notifierOptions, callback) {
112 var is64Bit = os.arch() === 'x64';
113 options = options || {};
114 options = utils.mapToNotifu(options);
115 options.p = options.p || 'Node Notification:';
116
117 var fullNotifierPath = notifier + (is64Bit ? '64' : '') + '.exe';
118 var localNotifier = notifierOptions.customPath || fullNotifierPath;
119
120 if (!options.m) {
121 callback(new Error('Message is required.'));
122 return this;
123 }
124
125 var argsList = utils.constructArgumentList(options, {
126 wrapper: '',
127 noEscape: true,
128 explicitTrue: true,
129 allowedArguments: allowedArguments
130 });
131
132 if (options.wait) {
133 return utils.fileCommand(localNotifier, argsList, function(error, data) {
134 var action = fromErrorCodeToAction(error.code);
135 if (action === 'error') return callback(error, data);
136
137 return callback(null, action);
138 });
139 }
140 utils.immediateFileCommand(localNotifier, argsList, callback);
141}
142
143function fromErrorCodeToAction(errorCode) {
144 switch (errorCode) {
145 case 2:
146 return 'timeout';
147 case 3:
148 case 6:
149 case 7:
150 return 'activate';
151 case 4:
152 return 'close';
153 default:
154 return 'error';
155 }
156}