templateManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* DING: Desktop Icons New Generation for GNOME Shell
  2. *
  3. * Copyright (C) 2020 Sergio Costas (rastersoft@gmail.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, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. const Gio = imports.gi.Gio;
  18. const GLib = imports.gi.GLib;
  19. const ExtensionUtils = imports.misc.extensionUtils;
  20. const Me = ExtensionUtils.getCurrentExtension();
  21. const DesktopIconsUtil = Me.imports.desktopIconsUtil;
  22. var TemplateManager = class {
  23. constructor() {
  24. this._templates = [];
  25. this._templatesEnumerateCancellable = null;
  26. this._templateDir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_TEMPLATES);
  27. if (this._templateDir == GLib.get_home_dir())
  28. this._templateDir = null;
  29. this.updated = true;
  30. if (this._templateDir != null) {
  31. this._templateGFile = Gio.File.new_for_path(this._templateDir);
  32. this._monitor = this._templateGFile.monitor_directory(Gio.FileMonitorFlags.NONE, null);
  33. this._monitorId = this._monitor.connect("changed", () => {
  34. this._refreshTemplates();
  35. });
  36. this._refreshTemplates();
  37. } else {
  38. this._templateGFile = null;
  39. this._monitorId = null;
  40. }
  41. }
  42. destroy() {
  43. if (this._monitorId)
  44. this._monitor.disconnect(this._monitorId);
  45. this._monitorId = null;
  46. }
  47. getTemplates() {
  48. let templates = [];
  49. for(let template of this._templates) {
  50. let data = {};
  51. data["icon"] = template.get_icon();
  52. let name = template.get_name();
  53. let offset = DesktopIconsUtil.getFileExtensionOffset(name, false);
  54. data["name"] = name.substring(0, offset);
  55. data["extension"] = name.substring(offset);
  56. data["file"] = name;
  57. templates.push(data);
  58. }
  59. this.updated = false;
  60. return templates;
  61. }
  62. _refreshTemplates() {
  63. if (this._templatesEnumerateCancellable)
  64. this._templatesEnumerateCancellable.cancel();
  65. this._templatesEnumerateCancellable = new Gio.Cancellable();
  66. this._templateGFile.enumerate_children_async(
  67. DesktopIconsUtil.DEFAULT_ATTRIBUTES,
  68. Gio.FileQueryInfoFlags.NONE,
  69. GLib.PRIORITY_DEFAULT,
  70. this._templatesEnumerateCancellable,
  71. (source, result) => {
  72. try {
  73. let fileEnum = source.enumerate_children_finish(result);
  74. this._templates = [];
  75. let info;
  76. while ((info = fileEnum.next_file(null))) {
  77. if (info.get_file_type() != Gio.FileType.DIRECTORY)
  78. this._templates.push(info);
  79. }
  80. this.updated = true;
  81. } catch(e) {
  82. global.log(`Exception while reading templates ${e}`);
  83. }
  84. }
  85. );
  86. }
  87. getTemplateFile(name) {
  88. if (this._templateGFile == null)
  89. return null;
  90. let template = Gio.File.new_for_path(GLib.build_filenamev([this._templateDir, name]));
  91. if (template.query_exists(null))
  92. return template;
  93. else
  94. return null;
  95. }
  96. }