forked from liquidboy/popcorn-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
191 lines (145 loc) · 4.62 KB
/
app.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var
// Minimum percentage to open video
MIN_PERCENTAGE_LOADED = 0.5,
// Minimum bytes loaded to open video
MIN_SIZE_LOADED = 10 * 1024 * 1024,
// Configuration variable
applicationRoot = './',
// Load native UI library
gui = require('nw.gui'),
// Debug flag
isDebug = gui.App.argv.indexOf('--debug') > -1,
// browser window object
win = gui.Window.get(),
// os object
os = require('os'),
// path object
path = require('path'),
// fs object
fs = require('fs'),
// url object
url = require('url'),
// TMP Folder
tmpFolder = path.join(os.tmpDir(), 'Popcorn-Time'),
// i18n module (translations)
i18n = require("i18n");
isWin = (process.platform === 'win32');
isLinux = (process.platform === 'linux');
isOSX = (process.platform === 'darwin');
BUTTON_ORDER = ['close', 'min', 'max'];
if (isWin) { BUTTON_ORDER = ['min', 'max', 'close']; }
if (isLinux) { BUTTON_ORDER = ['min', 'max', 'close']; }
if (isOSX) { BUTTON_ORDER = ['close', 'min', 'max']; }
// Global App skeleton for backbone
var App = {
Controller: {},
View: {},
Model: {},
Page: {},
Scrapers: {},
Providers: {}
};
// render header buttons
$("#header").html(_.template($('#header-tpl').html(), {buttons: BUTTON_ORDER}));
// Create the System Temp Folder. This is used to store temporary data like movie files.
if( ! fs.existsSync(tmpFolder) ) { fs.mkdir(tmpFolder); }
var wipeTmpFolder = function() {
if( typeof tmpFolder != 'string' ){ return; }
fs.readdir(tmpFolder, function(err, files){
for( var i in files ) {
fs.unlink(tmpFolder+'/'+files[i]);
}
});
}
// Wipe the tmpFolder when closing the app (this frees up disk space)
win.on('close', function(){
wipeTmpFolder();
win.close(true);
});
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
// Not debugging, hide all messages!
if (!isDebug) {
console.log = function () {};
} else {
// Developer Menu building
var menubar = new gui.Menu({ type: 'menubar' }),
developerSubmenu = new gui.Menu(),
developerItem = new gui.MenuItem({
label: 'Developer',
submenu: developerSubmenu
}),
debugItem = new gui.MenuItem({
label: 'Show developer tools',
click: function () {
win.showDevTools();
}
});
menubar.append(developerItem);
developerSubmenu.append(debugItem);
win.menu = menubar;
// Developer Shortcuts
document.addEventListener('keydown', function(event){
// F12 Opens DevTools
if( event.keyCode == 123 ) { win.showDevTools(); }
// F11 Reloads
if( event.keyCode == 122 ) { win.reloadIgnoringCache(); }
});
}
// Set the app title (for Windows mostly)
win.title = 'Popcorn Time';
// Focus the window when the app opens
win.focus();
// Cancel all new windows (Middle clicks / New Tab)
win.on('new-win-policy', function (frame, url, policy) {
policy.ignore();
});
var preventDefault = function(e) {
e.preventDefault();
}
// Prevent dropping files into the window
window.addEventListener("dragover", preventDefault, false);
window.addEventListener("drop", preventDefault, false);
// Prevent dragging files outside the window
window.addEventListener("dragstart", preventDefault, false);
// Show the disclaimer if the user hasn't accepted it yet.
if( ! Settings.get('disclaimerAccepted') ) {
$('.popcorn-disclaimer').removeClass('hidden');
$('.popcorn-disclaimer .btn.confirmation.continue').click(function(event){
event.preventDefault();
Settings.set('disclaimerAccepted', 1);
$('.popcorn-disclaimer').addClass('hidden');
});
$('.popcorn-disclaimer .btn.confirmation.quit').click(function(event){
event.preventDefault();
// We need to give the tracker some time to send the event
// Also, prevent multiple clicks
if( $('.popcorn-disclaimer').hasClass('quitting') ){ return; }
$('.popcorn-disclaimer').addClass('quitting');
setTimeout(function(){
gui.App.quit();
}, 2000);
});
}
/**
* Show 404 page on uncaughtException
*/
process.on('uncaughtException', function(err) {
if (console) {
console.log(err);
}
});
// TODO: I have no idea what this is
App.throttle = function(handler, time) {
var throttle;
time = time || 300;
return function() {
var args = arguments,
context = this;
clearTimeout(throttle);
throttle = setTimeout(function() {
handler.apply(context, args);
}, time);
};
};