-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopFile.js
255 lines (211 loc) · 6.96 KB
/
DesktopFile.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var File = require('./File.js'),
XdgDesktopEntry = require('./XdgDesktopEntry.js'),
child_process = require("child_process"),
exec = child_process.exec,
execFile = child_process.execFile,
spawn = child_process.spawn;
function DesktopFile( options ) {
File.call(this, options);
this.iconPathFetcher = options.iconPathFetcher;
this.xdgDesktopEntry = new XdgDesktopEntry({
path: this._absolutePath
});
}
DesktopFile.prototype = Object.create(File.prototype);
DesktopFile.prototype.getDisplayName = function( callback ) {
if ( typeof this._displayName !== 'undefined' ) {
callback( null, this._displayName);
return;
}
this.getDesktopFileProperty('Name', function( err, value ) {
// use name in desktop file
if ( ! err && value ) {
this._displayName = value;
callback( null, this._displayName );
// use fileName
} else {
this.getFileName(function( err, fileName ){
if ( err ) {
callback( err, null );
return;
}
this._displayName = fileName.replace(/\.desktop$/,'');
callback( null, this._displayName );
});
}
}.bind(this));
};
DesktopFile.prototype.getDesktopFileProperty = function( property, callback ) {
this.xdgDesktopEntry.getProperty( property, function( err, value ) {
if ( err || ! value ) {
callback( new Error('Missing key or value for ' + err), null );
return;
}
callback( null, value );
});
};
DesktopFile.prototype.setDesktopFileProperty = function( property, value, callback ) {
this.xdgDesktopEntry.setProperty( property, value, callback );
};
DesktopFile.prototype.getIconPath = function( callback ) {
this.getDesktopFileProperty('Icon', function( err, value ) {
if ( err ) {
callback( err, null );
return;
}
// if given abs path
// for icon name
if ( value.match( /^\// ) ) {
callback( null, value );
return;
}
// if only icon name (no path)
this.iconPathFetcher.getIconPath( value.replace(/\.(png)$/,''), 48, function( err, iconPath ) {
callback( err, iconPath );
});
}.bind(this));
};
DesktopFile.prototype.open = function() {
"use strict";
this.getFileName(function( err, fileName ) {
if ( err ) {
console.error( err, null );
return;
}
this.getDesktopFileProperty('Exec', function( err, value ) {
if ( err ) throw err;
console.log('Exec=', value);
var commandString = value.replace(/\s+%[fFuU](\s+|$)/, ' ').trim(),
commandSegments = commandString.split(/\s+/),
command = commandSegments.shift();
console.log('Opening App:', command, commandSegments);
var app = spawn(command, commandSegments, {
detached: true
});
app.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
app.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
app.on('error', function (err) {
console.log('child process error ' + err);
throw err;
// window.alert('Error calling' + fileName + '\n' + err);
});
app.on('close', function (code) {
console.log('child process exited with code ' + code);
});
}.bind(this));
}.bind(this));
};
DesktopFile.prototype.isHidden = function( callback ) {
this.getDesktopFileProperty('NoDisplay', function( err, value ) {
if ( err || ! value || value === 'false' ) {
this._isHidden = false;
} else {
this._isHidden = true;
}
callback( null, this._isHidden );
}.bind(this));
};
DesktopFile.prototype.getFilePackageOwner = function( absPath, callback ) {
execFile('/usr/bin/pacman', ['-Qo', absPath.trim()], function( error, stdout, stderr ) {
console.log('Error', error);
console.log('stderr', stderr);
console.log('stdout', stdout);
if ( error || stderr ) {
callback(new Error('Package owner could not be determined\n' + error + '\n' + stderr), null);
} else {
var packageString = stdout.split(' is owned by ')[1];
var segments = packageString.split(/\s+/);
var packageName = segments[0];
var packageVersion = segments[1];
console.log( 'packageName', packageName );
console.log( 'packageVersion', packageVersion );
callback( null, packageName );
}
});
};
DesktopFile.prototype.getPackageNameByExecutableName = function( fileName, callback ) {
execFile('/usr/bin/which', [fileName.trim()], function( error, stdout, stderr ) {
console.log('Which Error', error);
console.log('Which stderr', stderr);
console.log('Which stdout', stdout);
if ( error || stderr ) {
callback(new Error('No executable exists with this name: ' + fileName + '\n' + error + '\n' + stderr), null);
} else {
this.getFilePackageOwner(stdout, callback);
}
}.bind(this));
};
DesktopFile.prototype.getPackageName = function( callback ) {
this.getAbsolutePath(function( err, absPath ) {
if ( err ) {
callback ( new Error('Failed to get associated package name\n' + err), null);
return;
}
this.getFilePackageOwner(absPath, function( err, packagName ) {
if ( err ) {
// if the desktop file is
// not under /usr/share/applications/
// then we suspect that it might
// be a custom .desktop file
// possibly to override an existing
// .desktop file in /usr/share/applications.
// So will check if there is a matching .desktop
// file in /usr/share/applications/
if ( ! absPath.match(/^\/usr\/share\/applications\//) ) {
this.getFileName(function( err, fileName ) {
if ( err ) {
callback ( new Error('Failed to get associated package name\n' + err), null);
return;
}
this.getFilePackageOwner('/usr/share/applications/' + fileName, function( err, packagName ) {
if ( err ) {
this.getPackageNameByExecutableName(fileName.replace(/\.desktop$/, ''), callback);
return;
}
callback( null, packagName );
}.bind(this));
}.bind(this));
}
} else {
callback( null, packagName );
}
}.bind(this));
}.bind(this));
};
DesktopFile.prototype.edit = function( callback ) {
var asRoot = false;
this.isWritable(function( err, isWritable ) {
if ( err ) {
console.error( 'Failed to edit DesktopFile', err );
return;
} else {
if ( ! isWritable ) {
asRoot = true;
console.warn('Opening app as root!');
}
File.prototype.open.call(this, asRoot);
}
}.bind(this));
};
DesktopFile.prototype.uninstall = function( callback ) {
this.getPackageName(function( err, packageName ) {
if ( err ) {
console.log('Failed to determine associated Package.\n' + err);
callback(new Error('Failed to determine associated Package.\n' + err), null)
return;
}
// the sudo -k option
// ensures that the user always
// has to enter his password
var uninstallCommand = 'sudo -k pacman -Rs ' + packageName;
terminalCommand = 'gnome-terminal -x bash -c "echo \\"Running \'' + uninstallCommand + '\'\\"; ' + uninstallCommand + ' && echo \\"Successfully uninstalled\\"; sleep 3"';
console.log('uninstallCommand', uninstallCommand);
console.log('terminal command', terminalCommand);
exec(terminalCommand);
});
};
module.exports = DesktopFile;