fileItem.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /* Desktop Icons GNOME Shell extension
  2. *
  3. * Copyright (C) 2017 Carlos Soriano <csoriano@redhat.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. const Gtk = imports.gi.Gtk;
  19. const Clutter = imports.gi.Clutter;
  20. const Gio = imports.gi.Gio;
  21. const GLib = imports.gi.GLib;
  22. const GObject = imports.gi.GObject;
  23. const St = imports.gi.St;
  24. const Pango = imports.gi.Pango;
  25. const Meta = imports.gi.Meta;
  26. const GdkPixbuf = imports.gi.GdkPixbuf;
  27. const Cogl = imports.gi.Cogl;
  28. const GnomeDesktop = imports.gi.GnomeDesktop;
  29. const Mainloop = imports.mainloop;
  30. const Config = imports.misc.config;
  31. const Background = imports.ui.background;
  32. const Main = imports.ui.main;
  33. const PopupMenu = imports.ui.popupMenu;
  34. const Util = imports.misc.util;
  35. const ExtensionUtils = imports.misc.extensionUtils;
  36. const Me = ExtensionUtils.getCurrentExtension();
  37. const Extension = Me.imports.extension;
  38. const Prefs = Me.imports.prefs;
  39. const DBusUtils = Me.imports.dbusUtils;
  40. const DesktopIconsUtil = Me.imports.desktopIconsUtil;
  41. const Gettext = imports.gettext.domain('desktop-icons');
  42. const _ = Gettext.gettext;
  43. const DRAG_TRESHOLD = 8;
  44. var S_IXUSR = 0o00100;
  45. var S_IWOTH = 0o00002;
  46. var State = {
  47. NORMAL: 0,
  48. GONE: 1,
  49. };
  50. var FileItem = GObject.registerClass({
  51. GTypeName: 'DesktopIcons_FileItem',
  52. Signals: {
  53. 'rename-clicked': {},
  54. 'selected': {
  55. param_types: [GObject.TYPE_BOOLEAN, GObject.TYPE_BOOLEAN, GObject.TYPE_BOOLEAN]
  56. }
  57. }
  58. }, class FileItem extends St.Bin {
  59. _init(file, fileInfo, fileExtra) {
  60. super._init({ visible: true });
  61. this._fileExtra = fileExtra;
  62. this._loadThumbnailDataCancellable = null;
  63. this._thumbnailScriptWatch = 0;
  64. this._setMetadataCancellable = null;
  65. this._queryFileInfoCancellable = null;
  66. this._isSpecial = this._fileExtra != Prefs.FileType.NONE;
  67. this._file = file;
  68. this._mount = null;
  69. if (this._fileExtra == Prefs.FileType.MOUNT_DISK)
  70. this._mount = this._file.find_enclosing_mount(null);
  71. this._savedCoordinates = null;
  72. let savedCoordinates = fileInfo.get_attribute_as_string('metadata::nautilus-icon-position');
  73. if (savedCoordinates != null)
  74. this._savedCoordinates = savedCoordinates.split(',').map(x => Number(x));
  75. this._state = State.NORMAL;
  76. try {
  77. this.set_fill(true, true);
  78. } catch(error) {}
  79. let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
  80. this._delegate = this;
  81. this.connect('destroy', () => this._onDestroy());
  82. this._container = new St.BoxLayout({ reactive: true,
  83. track_hover: true,
  84. can_focus: true,
  85. style_class: 'file-item',
  86. x_expand: true,
  87. y_expand: true,
  88. x_align: Clutter.ActorAlign.FILL,
  89. vertical: true });
  90. this.set_child(this._container);
  91. this._icon = new St.Bin();
  92. this._icon.set_height(Prefs.get_icon_size() * scaleFactor);
  93. this._iconAllocationIdleId = 0;
  94. this._iconAllocationId = this._icon.connect("notify::allocation", () => {
  95. if (this._iconAllocationIdleId)
  96. GLib.source_remove(this._iconAllocationIdleId);
  97. this._iconAllocationIdleId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
  98. GLib.source_remove(this._iconAllocationIdleId);
  99. this._iconAllocationIdleId = 0;
  100. this._updateIcon();
  101. return GLib.SOURCE_REMOVE;
  102. });
  103. });
  104. this._iconContainer = new St.Bin({ visible: true });
  105. this._iconContainer.child = this._icon;
  106. this._container.add_child(this._iconContainer);
  107. this._label = new St.Label({
  108. style_class: 'name-label'
  109. });
  110. this._container.add_child(this._label);
  111. let clutterText = this._label.get_clutter_text();
  112. /* TODO: Convert to gobject.set for 3.30 */
  113. clutterText.set_line_wrap(true);
  114. clutterText.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR);
  115. clutterText.set_ellipsize(Pango.EllipsizeMode.END);
  116. this._container.connect('button-press-event', (actor, event) => this._onPressButton(actor, event));
  117. this._container.connect('motion-event', (actor, event) => this._onMotion(actor, event));
  118. this._container.connect('leave-event', (actor, event) => this._onLeave(actor, event));
  119. this._container.connect('enter-event', (actor, event) => this._onEnter(actor, event));
  120. this._container.connect('button-release-event', (actor, event) => this._onReleaseButton(actor, event));
  121. /* Set the metadata and update relevant UI */
  122. this._updateMetadataFromFileInfo(fileInfo);
  123. this._menuManager = null;
  124. this._menu = null;
  125. this._updateIcon();
  126. this._isSelected = false;
  127. this._primaryButtonPressed = false;
  128. if (this._attributeCanExecute && !this._isValidDesktopFile)
  129. this._execLine = this.file.get_path();
  130. if (fileExtra == Prefs.FileType.USER_DIRECTORY_TRASH) {
  131. // if this icon is the trash, monitor the state of the directory to update the icon
  132. this._trashChanged = false;
  133. this._queryTrashInfoCancellable = null;
  134. this._scheduleTrashRefreshId = 0;
  135. this._monitorTrashDir = this._file.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, null);
  136. this._monitorTrashId = this._monitorTrashDir.connect('changed', (obj, file, otherFile, eventType) => {
  137. switch(eventType) {
  138. case Gio.FileMonitorEvent.DELETED:
  139. case Gio.FileMonitorEvent.MOVED_OUT:
  140. case Gio.FileMonitorEvent.CREATED:
  141. case Gio.FileMonitorEvent.MOVED_IN:
  142. if (this._queryTrashInfoCancellable || this._scheduleTrashRefreshId) {
  143. if (this._scheduleTrashRefreshId)
  144. GLib.source_remove(this._scheduleTrashRefreshId);
  145. this._scheduleTrashRefreshId = Mainloop.timeout_add(200, () => this._refreshTrashIcon());
  146. } else {
  147. this._refreshTrashIcon();
  148. }
  149. break;
  150. }
  151. });
  152. }
  153. this._writebleByOthersId = Extension.desktopManager.connect('notify::writable-by-others', () => {
  154. if (!this._isValidDesktopFile)
  155. return;
  156. this._refreshMetadataAsync(true);
  157. });
  158. }
  159. set_margins(width, height) {
  160. let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
  161. this.set_width(Prefs.getDesiredWidth(scaleFactor, width));
  162. this.set_height(Prefs.getDesiredHeight(scaleFactor, height));
  163. }
  164. onAttributeChanged() {
  165. this._refreshMetadataAsync(this._isDesktopFile);
  166. }
  167. _onDestroy() {
  168. /* Regular file data */
  169. if (this._setMetadataCancellable)
  170. this._setMetadataCancellable.cancel();
  171. if (this._queryFileInfoCancellable)
  172. this._queryFileInfoCancellable.cancel();
  173. Extension.desktopManager.disconnect(this._writebleByOthersId);
  174. /* Thumbnailing */
  175. if (this._thumbnailScriptWatch)
  176. GLib.source_remove(this._thumbnailScriptWatch);
  177. if (this._loadThumbnailDataCancellable)
  178. this._loadThumbnailDataCancellable.cancel();
  179. /* Desktop file */
  180. if (this._monitorDesktopFileId) {
  181. this._monitorDesktopFile.disconnect(this._monitorDesktopFileId);
  182. this._monitorDesktopFile.cancel();
  183. }
  184. /* Trash */
  185. if (this._monitorTrashDir) {
  186. this._monitorTrashDir.disconnect(this._monitorTrashId);
  187. this._monitorTrashDir.cancel();
  188. }
  189. if (this._queryTrashInfoCancellable)
  190. this._queryTrashInfoCancellable.cancel();
  191. if (this._scheduleTrashRefreshId)
  192. GLib.source_remove(this._scheduleTrashRefreshId);
  193. /* Icon */
  194. this._icon.disconnect(this._iconAllocationId);
  195. if (this._iconAllocationIdleId)
  196. GLib.source_remove(this._iconAllocationIdleId);
  197. /* Menu */
  198. this._removeMenu();
  199. }
  200. _refreshMetadataAsync(rebuild) {
  201. if (this._queryFileInfoCancellable)
  202. this._queryFileInfoCancellable.cancel();
  203. this._queryFileInfoCancellable = new Gio.Cancellable();
  204. this._file.query_info_async(DesktopIconsUtil.DEFAULT_ATTRIBUTES,
  205. Gio.FileQueryInfoFlags.NONE,
  206. GLib.PRIORITY_DEFAULT,
  207. this._queryFileInfoCancellable,
  208. (source, result) => {
  209. try {
  210. let newFileInfo = source.query_info_finish(result);
  211. this._queryFileInfoCancellable = null;
  212. this._updateMetadataFromFileInfo(newFileInfo);
  213. if (rebuild)
  214. this._recreateMenu();
  215. this._updateIcon();
  216. } catch(error) {
  217. if (!error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
  218. global.log("Error getting the file info: " + error);
  219. }
  220. });
  221. }
  222. _updateMetadataFromFileInfo(fileInfo) {
  223. this._fileInfo = fileInfo;
  224. let oldLabelText = this._label.text;
  225. this._displayName = fileInfo.get_attribute_as_string('standard::display-name');
  226. this._attributeCanExecute = fileInfo.get_attribute_boolean('access::can-execute');
  227. this._unixmode = fileInfo.get_attribute_uint32('unix::mode');
  228. this._writableByOthers = (this._unixmode & S_IWOTH) != 0;
  229. this._trusted = fileInfo.get_attribute_as_string('metadata::trusted') == 'true';
  230. this._attributeContentType = fileInfo.get_content_type();
  231. this._isDesktopFile = this._attributeContentType == 'application/x-desktop';
  232. if (this._isDesktopFile && this._writableByOthers)
  233. log(`desktop-icons: File ${this._displayName} is writable by others - will not allow launching`);
  234. if (this._isDesktopFile) {
  235. this._desktopFile = Gio.DesktopAppInfo.new_from_filename(this._file.get_path());
  236. if (!this._desktopFile) {
  237. log(`Couldn’t parse ${this._displayName} as a desktop file, will treat it as a regular file.`);
  238. this._isValidDesktopFile = false;
  239. } else {
  240. this._isValidDesktopFile = true;
  241. }
  242. } else {
  243. this._isValidDesktopFile = false;
  244. }
  245. if (this.displayName != oldLabelText) {
  246. this._label.text = this.displayName;
  247. }
  248. this._fileType = fileInfo.get_file_type();
  249. this._isDirectory = this._fileType == Gio.FileType.DIRECTORY;
  250. this._isSpecial = this._fileExtra != Prefs.FileType.NONE;
  251. this._isHidden = fileInfo.get_is_hidden() | fileInfo.get_is_backup();
  252. this._isSymlink = fileInfo.get_is_symlink();
  253. this._modifiedTime = this._fileInfo.get_attribute_uint64("time::modified");
  254. /*
  255. * This is a glib trick to detect broken symlinks. If a file is a symlink, the filetype
  256. * points to the final file, unless it is broken; thus if the file type is SYMBOLIC_LINK,
  257. * it must be a broken link.
  258. * https://developer.gnome.org/gio/stable/GFile.html#g-file-query-info
  259. */
  260. this._isBrokenSymlink = this._isSymlink && this._fileType == Gio.FileType.SYMBOLIC_LINK;
  261. }
  262. onFileRenamed(file) {
  263. this._file = file;
  264. this._refreshMetadataAsync(false);
  265. }
  266. _updateIcon() {
  267. if (this._fileExtra == Prefs.FileType.USER_DIRECTORY_TRASH) {
  268. this._icon.child = this._createEmblemedStIcon(this._fileInfo.get_icon(), null);
  269. return;
  270. }
  271. if (this._fileExtra == Prefs.FileType.MOUNT_DISK) {
  272. this._icon.child = this._createEmblemedStIcon(this._mount.get_icon(), null);
  273. return;
  274. }
  275. let thumbnailFactory = GnomeDesktop.DesktopThumbnailFactory.new(GnomeDesktop.DesktopThumbnailSize.LARGE);
  276. if ((Prefs.nautilusSettings.get_string('show-image-thumbnails') != 'never') &&
  277. (thumbnailFactory.can_thumbnail(this._file.get_uri(),
  278. this._attributeContentType,
  279. this._modifiedTime))) {
  280. let thumbnail = thumbnailFactory.lookup(this._file.get_uri(), this._modifiedTime);
  281. if (thumbnail == null) {
  282. if (!thumbnailFactory.has_valid_failed_thumbnail(this._file.get_uri(),
  283. this._modifiedTime)) {
  284. let argv = [];
  285. argv.push(GLib.build_filenamev([ExtensionUtils.getCurrentExtension().path,
  286. 'createThumbnail.js']));
  287. argv.push(this._file.get_path());
  288. let [success, pid] = GLib.spawn_async(null, argv, null,
  289. GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD, null);
  290. if (this._thumbnailScriptWatch)
  291. GLib.source_remove(this._thumbnailScriptWatch);
  292. this._thumbnailScriptWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT,
  293. pid,
  294. (pid, exitCode) => {
  295. this._thumbnailScriptWatch = 0;
  296. if (exitCode == 0)
  297. this._updateIcon();
  298. else
  299. global.log('Failed to generate thumbnail for ' + this._filePath);
  300. GLib.spawn_close_pid(pid);
  301. return false;
  302. }
  303. );
  304. }
  305. } else {
  306. if (this._loadThumbnailDataCancellable)
  307. this._loadThumbnailDataCancellable.cancel();
  308. this._loadThumbnailDataCancellable = new Gio.Cancellable();
  309. let thumbnailFile = Gio.File.new_for_path(thumbnail);
  310. thumbnailFile.load_bytes_async(this._loadThumbnailDataCancellable,
  311. (source, result) => {
  312. try {
  313. this._loadThumbnailDataCancellable = null;
  314. let [thumbnailData, etag_out] = source.load_bytes_finish(result);
  315. let thumbnailStream = Gio.MemoryInputStream.new_from_bytes(thumbnailData);
  316. let thumbnailPixbuf = GdkPixbuf.Pixbuf.new_from_stream(thumbnailStream, null);
  317. if (thumbnailPixbuf != null) {
  318. let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
  319. let thumbnailImage = new Clutter.Image();
  320. thumbnailImage.set_data(thumbnailPixbuf.get_pixels(),
  321. thumbnailPixbuf.has_alpha ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888,
  322. thumbnailPixbuf.width,
  323. thumbnailPixbuf.height,
  324. thumbnailPixbuf.rowstride
  325. );
  326. let icon = new Clutter.Actor();
  327. icon.set_content(thumbnailImage);
  328. let containerWidth = (this._icon.allocation.x2 - this._icon.allocation.x1) * scaleFactor;
  329. let containerHeight = Prefs.get_icon_size() * scaleFactor;
  330. let containerAspectRatio = containerWidth / containerHeight;
  331. let iconAspectRatio = thumbnailPixbuf.width / thumbnailPixbuf.height;
  332. if (containerAspectRatio > iconAspectRatio) {
  333. let iconWidth = containerHeight * iconAspectRatio;
  334. icon.set_size(iconWidth, containerHeight);
  335. let margin = (containerWidth - iconWidth) / 2;
  336. icon.margin_left = Math.ceil(margin);
  337. icon.margin_right = Math.floor(margin);
  338. } else {
  339. let iconHeight = containerWidth / iconAspectRatio;
  340. icon.set_size(containerWidth, iconHeight);
  341. let margin = (containerHeight - iconHeight) / 2;
  342. icon.margin_top = Math.ceil(margin);
  343. icon.margin_bottom = Math.floor(margin);
  344. }
  345. this._icon.child = icon;
  346. }
  347. } catch (error) {
  348. if (!error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
  349. global.log('Error while loading thumbnail: ' + error);
  350. this._icon.child = this._createEmblemedStIcon(this._fileInfo.get_icon(), null);
  351. }
  352. }
  353. }
  354. );
  355. }
  356. }
  357. if (this._isBrokenSymlink) {
  358. this._icon.child = this._createEmblemedStIcon(null, 'text-x-generic');
  359. } else {
  360. if (this.trustedDesktopFile && this._desktopFile.has_key('Icon'))
  361. this._icon.child = this._createEmblemedStIcon(null, this._desktopFile.get_string('Icon'));
  362. else
  363. this._icon.child = this._createEmblemedStIcon(this._fileInfo.get_icon(), null);
  364. }
  365. }
  366. _refreshTrashIcon() {
  367. if (this._queryTrashInfoCancellable)
  368. this._queryTrashInfoCancellable.cancel();
  369. this._queryTrashInfoCancellable = new Gio.Cancellable();
  370. this._file.query_info_async(DesktopIconsUtil.DEFAULT_ATTRIBUTES,
  371. Gio.FileQueryInfoFlags.NONE,
  372. GLib.PRIORITY_DEFAULT,
  373. this._queryTrashInfoCancellable,
  374. (source, result) => {
  375. try {
  376. this._fileInfo = source.query_info_finish(result);
  377. this._queryTrashInfoCancellable = null;
  378. this._updateIcon();
  379. } catch(error) {
  380. if (!error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
  381. global.log('Error getting the number of files in the trash: ' + error);
  382. }
  383. });
  384. this._scheduleTrashRefreshId = 0;
  385. return false;
  386. }
  387. get file() {
  388. return this._file;
  389. }
  390. get isHidden() {
  391. return this._isHidden;
  392. }
  393. _createEmblemedStIcon(icon, iconName) {
  394. if (icon == null) {
  395. if (GLib.path_is_absolute(iconName)) {
  396. let iconFile = Gio.File.new_for_commandline_arg(iconName);
  397. icon = new Gio.FileIcon({ file: iconFile });
  398. } else {
  399. icon = Gio.ThemedIcon.new_with_default_fallbacks(iconName);
  400. }
  401. }
  402. let itemIcon = Gio.EmblemedIcon.new(icon, null);
  403. if (this._isSymlink) {
  404. if (this._isBrokenSymlink)
  405. itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-unreadable')));
  406. else
  407. itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-symbolic-link')));
  408. } else if (this.trustedDesktopFile) {
  409. itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-symbolic-link')));
  410. }
  411. return new St.Icon({ gicon: itemIcon,
  412. icon_size: Prefs.get_icon_size()
  413. });
  414. }
  415. doRename() {
  416. if (!this.canRename()) {
  417. log (`Error: ${this.file.get_uri()} cannot be renamed`);
  418. return;
  419. }
  420. this.emit('rename-clicked');
  421. }
  422. _doOpenContext(context) {
  423. if (this._isBrokenSymlink) {
  424. log(`Error: Can’t open ${this.file.get_uri()} because it is a broken symlink.`);
  425. return;
  426. }
  427. if (this.trustedDesktopFile) {
  428. this._desktopFile.launch_uris_as_manager([], context, GLib.SpawnFlags.SEARCH_PATH, null, null);
  429. return;
  430. }
  431. if (this._attributeCanExecute &&
  432. !this._isDirectory &&
  433. !this._isValidDesktopFile &&
  434. Gio.content_type_can_be_executable(this._attributeContentType)) {
  435. if (this._execLine)
  436. Util.spawnCommandLine(this._execLine);
  437. return;
  438. }
  439. Gio.AppInfo.launch_default_for_uri_async(this.file.get_uri(),
  440. null, null,
  441. (source, result) => {
  442. try {
  443. Gio.AppInfo.launch_default_for_uri_finish(result);
  444. } catch (e) {
  445. log('Error opening file ' + this.file.get_uri() + ': ' + e.message);
  446. }
  447. }
  448. );
  449. }
  450. doOpen() {
  451. this._doOpenContext(null);
  452. }
  453. _doDiscreteGpu() {
  454. let gpus = Extension.desktopManager.switcherooProxyGPUs;
  455. if (!gpus) {
  456. log('Could not apply discrete GPU environment, no GPUs in list');
  457. }
  458. for(let gpu in gpus) {
  459. if (!gpus[gpu])
  460. continue;
  461. let default_variant = gpus[gpu]['Default'];
  462. if (!default_variant || default_variant.get_boolean())
  463. continue;
  464. let env = gpus[gpu]['Environment'];
  465. if (!env)
  466. continue;
  467. let env_s = env.get_strv();
  468. let context = new Gio.AppLaunchContext;
  469. for (let i = 0; i < env_s.length; i=i+2) {
  470. context.setenv(env_s[i], env_s[i+1]);
  471. }
  472. this._doOpenContext(context);
  473. return;
  474. }
  475. log('Could not find discrete GPU data in switcheroo-control');
  476. }
  477. _onCopyClicked() {
  478. Extension.desktopManager.doCopy();
  479. }
  480. _onCutClicked() {
  481. Extension.desktopManager.doCut();
  482. }
  483. _onShowInFilesClicked() {
  484. DBusUtils.FreeDesktopFileManagerProxy.ShowItemsRemote([this.file.get_uri()], '',
  485. (result, error) => {
  486. if (error)
  487. log('Error showing file on desktop: ' + error.message);
  488. }
  489. );
  490. }
  491. _onPropertiesClicked() {
  492. DBusUtils.FreeDesktopFileManagerProxy.ShowItemPropertiesRemote([this.file.get_uri()], '',
  493. (result, error) => {
  494. if (error)
  495. log('Error showing properties: ' + error.message);
  496. }
  497. );
  498. }
  499. _onMoveToTrashClicked() {
  500. Extension.desktopManager.doTrash();
  501. }
  502. _onEmptyTrashClicked() {
  503. Extension.desktopManager.doEmptyTrash();
  504. }
  505. _onEjectClicked() {
  506. DesktopIconsUtil.eject(this._mount);
  507. }
  508. get _allowLaunchingText() {
  509. if (this.trustedDesktopFile)
  510. return _("Don’t Allow Launching");
  511. return _("Allow Launching");
  512. }
  513. get metadataTrusted() {
  514. return this._trusted;
  515. }
  516. set metadataTrusted(value) {
  517. this._trusted = value;
  518. let info = new Gio.FileInfo();
  519. info.set_attribute_string('metadata::trusted',
  520. value ? 'true' : 'false');
  521. this._file.set_attributes_async(info,
  522. Gio.FileQueryInfoFlags.NONE,
  523. GLib.PRIORITY_LOW,
  524. null,
  525. (source, result) => {
  526. try {
  527. source.set_attributes_finish(result);
  528. this._refreshMetadataAsync(true);
  529. } catch(e) {
  530. log(`Failed to set metadata::trusted: ${e.message}`);
  531. }
  532. });
  533. }
  534. _onAllowDisallowLaunchingClicked() {
  535. this.metadataTrusted = !this.trustedDesktopFile;
  536. /*
  537. * we're marking as trusted, make the file executable too. note that we
  538. * do not ever remove the executable bit, since we don't know who set
  539. * it.
  540. */
  541. if (this.metadataTrusted && !this._attributeCanExecute) {
  542. let info = new Gio.FileInfo();
  543. let newUnixMode = this._unixmode | S_IXUSR;
  544. info.set_attribute_uint32(Gio.FILE_ATTRIBUTE_UNIX_MODE, newUnixMode);
  545. this._file.set_attributes_async(info,
  546. Gio.FileQueryInfoFlags.NONE,
  547. GLib.PRIORITY_LOW,
  548. null,
  549. (source, result) => {
  550. try {
  551. source.set_attributes_finish (result);
  552. } catch(e) {
  553. log(`Failed to set unix mode: ${e.message}`);
  554. }
  555. });
  556. }
  557. }
  558. canRename() {
  559. return !this.trustedDesktopFile && this._fileExtra == Prefs.FileType.NONE;
  560. }
  561. _doOpenWith() {
  562. DBusUtils.openFileWithOtherApplication(this.file.get_path());
  563. }
  564. _getSelectionStyle() {
  565. let rgba = DesktopIconsUtil.getGtkClassBackgroundColor('view', Gtk.StateFlags.SELECTED);
  566. let background_color =
  567. 'rgba(' + rgba.red * 255 + ', ' + rgba.green * 255 + ', ' + rgba.blue * 255 + ', 0.6)';
  568. let border_color =
  569. 'rgba(' + rgba.red * 255 + ', ' + rgba.green * 255 + ', ' + rgba.blue * 255 + ', 0.8)';
  570. return 'background-color: ' + background_color + ';' +
  571. 'border-color: ' + border_color + ';';
  572. }
  573. get menu() {
  574. return this._menu;
  575. }
  576. _removeMenu() {
  577. if (this._menu != null) {
  578. if (this._menuManager != null)
  579. this._menuManager.removeMenu(this._menu);
  580. Main.layoutManager.uiGroup.remove_child(this._menu.actor);
  581. this._menu.destroy();
  582. this._menu = null;
  583. }
  584. this._menuManager = null;
  585. }
  586. _recreateMenu() {
  587. this._removeMenu();
  588. this._menuManager = new PopupMenu.PopupMenuManager(this);
  589. let side = St.Side.LEFT;
  590. if (Clutter.get_default_text_direction() == Clutter.TextDirection.RTL)
  591. side = St.Side.RIGHT;
  592. this._menu = new PopupMenu.PopupMenu(this, 0.5, side);
  593. this._menu.addAction(_('Open'), () => this.doOpen());
  594. switch (this._fileExtra) {
  595. case Prefs.FileType.NONE:
  596. if (!this._isDirectory) {
  597. this._actionOpenWith = this._menu.addAction(_('Open With Other Application'), () => this._doOpenWith());
  598. if (Extension.desktopManager.discreteGpuAvailable)
  599. this._menu.addAction(_('Launch using Dedicated Graphics Card'), () => this._doDiscreteGpu());
  600. } else {
  601. this._actionOpenWith = null;
  602. }
  603. this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  604. this._actionCut = this._menu.addAction(_('Cut'), () => this._onCutClicked());
  605. this._actionCopy = this._menu.addAction(_('Copy'), () => this._onCopyClicked());
  606. if (this.canRename())
  607. this._menu.addAction(_('Rename…'), () => this.doRename());
  608. this._actionTrash = this._menu.addAction(_('Move to Trash'), () => this._onMoveToTrashClicked());
  609. if (this._isValidDesktopFile && !Extension.desktopManager.writableByOthers && !this._writableByOthers) {
  610. this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  611. this._allowLaunchingMenuItem = this._menu.addAction(this._allowLaunchingText,
  612. () => this._onAllowDisallowLaunchingClicked());
  613. }
  614. break;
  615. case Prefs.FileType.USER_DIRECTORY_TRASH:
  616. this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  617. this._menu.addAction(_('Empty Trash'), () => this._onEmptyTrashClicked());
  618. break;
  619. case Prefs.FileType.MOUNT_DISK:
  620. this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  621. this._menu.addAction(_('Eject'), () => this._onEjectClicked());
  622. break;
  623. default:
  624. break;
  625. }
  626. this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  627. this._menu.addAction(_('Properties'), () => this._onPropertiesClicked());
  628. this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  629. this._menu.addAction(_('Show in Files'), () => this._onShowInFilesClicked());
  630. if (this._isDirectory && this.file.get_path() != null)
  631. this._actionOpenInTerminal = this._menu.addAction(_('Open in Terminal'), () => this._onOpenTerminalClicked());
  632. this._menuManager.addMenu(this._menu);
  633. Main.layoutManager.uiGroup.add_child(this._menu.actor);
  634. this._menu.actor.hide();
  635. }
  636. _ensureMenu() {
  637. if (this._menu == null)
  638. this._recreateMenu();
  639. return this._menu;
  640. }
  641. _onOpenTerminalClicked () {
  642. DesktopIconsUtil.launchTerminal(this.file.get_path());
  643. }
  644. _onPressButton(actor, event) {
  645. let button = event.get_button();
  646. if (button == 3) {
  647. if (!this.isSelected)
  648. this.emit('selected', false, false, true);
  649. this._ensureMenu().toggle();
  650. if (this._actionOpenWith) {
  651. let allowOpenWith = (Extension.desktopManager.getNumberOfSelectedItems() == 1);
  652. this._actionOpenWith.setSensitive(allowOpenWith);
  653. }
  654. let specialFilesSelected = Extension.desktopManager.checkIfSpecialFilesAreSelected();
  655. if (this._actionCut)
  656. this._actionCut.setSensitive(!specialFilesSelected);
  657. if (this._actionCopy)
  658. this._actionCopy.setSensitive(!specialFilesSelected);
  659. if (this._actionTrash)
  660. this._actionTrash.setSensitive(!specialFilesSelected);
  661. return Clutter.EVENT_STOP;
  662. } else if (button == 1) {
  663. if (event.get_click_count() == 1) {
  664. let [x, y] = event.get_coords();
  665. this._primaryButtonPressed = true;
  666. this._buttonPressInitialX = x;
  667. this._buttonPressInitialY = y;
  668. let shiftPressed = !!(event.get_state() & Clutter.ModifierType.SHIFT_MASK);
  669. let controlPressed = !!(event.get_state() & Clutter.ModifierType.CONTROL_MASK);
  670. if (controlPressed || shiftPressed)
  671. this.emit('selected', true, false, !this._isSelected);
  672. else
  673. this.emit('selected', false, false, true);
  674. }
  675. return Clutter.EVENT_STOP;
  676. }
  677. return Clutter.EVENT_PROPAGATE;
  678. }
  679. _onEnter(actor, event) {
  680. if (Prefs.CLICK_POLICY_SINGLE)
  681. global.display.set_cursor(Meta.Cursor.POINTING_HAND);
  682. else
  683. global.display.set_cursor(Meta.Cursor.DEFAULT);
  684. }
  685. _onLeave(actor, event) {
  686. this._primaryButtonPressed = false;
  687. if (Prefs.CLICK_POLICY_SINGLE)
  688. global.display.set_cursor(Meta.Cursor.DEFAULT);
  689. }
  690. _onMotion(actor, event) {
  691. let [x, y] = event.get_coords();
  692. if (this._primaryButtonPressed) {
  693. let xDiff = x - this._buttonPressInitialX;
  694. let yDiff = y - this._buttonPressInitialY;
  695. let distance = Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
  696. if (distance > DRAG_TRESHOLD) {
  697. // Don't need to track anymore this if we start drag, and also
  698. // avoids reentrance here
  699. this._primaryButtonPressed = false;
  700. let event = Clutter.get_current_event();
  701. let [x, y] = event.get_coords();
  702. Extension.desktopManager.dragStart();
  703. }
  704. }
  705. return Clutter.EVENT_PROPAGATE;
  706. }
  707. _onReleaseButton(actor, event) {
  708. let button = event.get_button();
  709. if (button == 1) {
  710. // primaryButtonPressed is TRUE only if the user has pressed the button
  711. // over an icon, and if (s)he has not started a drag&drop operation
  712. if (this._primaryButtonPressed) {
  713. this._primaryButtonPressed = false;
  714. let shiftPressed = !!(event.get_state() & Clutter.ModifierType.SHIFT_MASK);
  715. let controlPressed = !!(event.get_state() & Clutter.ModifierType.CONTROL_MASK);
  716. if ((event.get_click_count() == 1) && Prefs.CLICK_POLICY_SINGLE && !shiftPressed && !controlPressed)
  717. this.doOpen();
  718. return Clutter.EVENT_STOP;
  719. }
  720. if ((event.get_click_count() == 2) && (!Prefs.CLICK_POLICY_SINGLE))
  721. this.doOpen();
  722. }
  723. return Clutter.EVENT_PROPAGATE;
  724. }
  725. get savedCoordinates() {
  726. return this._savedCoordinates;
  727. }
  728. _onSetMetadataFileFinished(source, result) {
  729. try {
  730. let [success, info] = source.set_attributes_finish(result);
  731. } catch (error) {
  732. if (!error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
  733. log(`Error setting metadata to desktop files ${error}`);
  734. }
  735. }
  736. set savedCoordinates(pos) {
  737. if (this._setMetadataCancellable)
  738. this._setMetadataCancellable.cancel();
  739. this._setMetadataCancellable = new Gio.Cancellable();
  740. this._savedCoordinates = [pos[0], pos[1]];
  741. let info = new Gio.FileInfo();
  742. info.set_attribute_string('metadata::nautilus-icon-position',
  743. `${pos[0]},${pos[1]}`);
  744. this.file.set_attributes_async(info,
  745. Gio.FileQueryInfoFlags.NONE,
  746. GLib.PRIORITY_DEFAULT,
  747. this._setMetadataCancellable,
  748. (source, result) => {
  749. this._setMetadataCancellable = null;
  750. this._onSetMetadataFileFinished(source, result);
  751. }
  752. );
  753. }
  754. intersectsWith(argX, argY, argWidth, argHeight) {
  755. let rect = new Meta.Rectangle({ x: argX, y: argY, width: argWidth, height: argHeight });
  756. let [containerX, containerY] = this._container.get_transformed_position();
  757. let boundingBox = new Meta.Rectangle({ x: containerX,
  758. y: containerY,
  759. width: this._container.allocation.x2 - this._container.allocation.x1,
  760. height: this._container.allocation.y2 - this._container.allocation.y1 });
  761. let [intersects, _] = rect.intersect(boundingBox);
  762. return intersects;
  763. }
  764. set isSelected(isSelected) {
  765. isSelected = !!isSelected;
  766. if (isSelected == this._isSelected)
  767. return;
  768. if (isSelected) {
  769. this._container.set_style(this._getSelectionStyle());
  770. } else {
  771. this._container.set_style('background-color: transparent');
  772. this._container.set_style('border-color: transparent');
  773. }
  774. this._isSelected = isSelected;
  775. }
  776. get isSelected() {
  777. return this._isSelected;
  778. }
  779. get isSpecial() {
  780. return this._isSpecial;
  781. }
  782. get state() {
  783. return this._state;
  784. }
  785. set state(state) {
  786. if (state == this._state)
  787. return;
  788. this._state = state;
  789. }
  790. get isDirectory() {
  791. return this._isDirectory;
  792. }
  793. get trustedDesktopFile() {
  794. return this._isValidDesktopFile &&
  795. this._attributeCanExecute &&
  796. this.metadataTrusted &&
  797. !Extension.desktopManager.writableByOthers &&
  798. !this._writableByOthers;
  799. }
  800. get fileName() {
  801. return this._fileInfo.get_name();
  802. }
  803. get displayName() {
  804. if (this._fileExtra == Prefs.FileType.USER_DIRECTORY_HOME)
  805. return _("Home");
  806. if (this.trustedDesktopFile)
  807. return this._desktopFile.get_name();
  808. return this._displayName || null;
  809. }
  810. acceptDrop() {
  811. return Extension.desktopManager.selectionDropOnFileItem(this);
  812. }
  813. });