utils.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. import Gio from 'gi://Gio';
  5. import GLib from 'gi://GLib';
  6. import Gtk from 'gi://Gtk';
  7. import Config from '../config.mjs';
  8. let St = null; // St is not available for prefs.js importing this file.
  9. try {
  10. St = (await import('gi://St')).default;
  11. } catch (e) { }
  12. /**
  13. * Get a themed icon, using fallbacks from GSConnect's GResource when necessary.
  14. *
  15. * @param {string} name - A themed icon name
  16. * @return {Gio.Icon} A themed icon
  17. */
  18. export function getIcon(name) {
  19. if (getIcon._resource === undefined) {
  20. // Setup the desktop icons
  21. const settings = St.Settings.get();
  22. getIcon._desktop = new Gtk.IconTheme();
  23. getIcon._desktop.set_theme_name(settings.gtk_icon_theme);
  24. settings.connect('notify::gtk-icon-theme', (settings_, key_) => {
  25. getIcon._desktop.set_theme_name(settings_.gtk_icon_theme);
  26. });
  27. // Preload our fallbacks
  28. const iconPath = 'resource://org/gnome/Shell/Extensions/GSConnect/icons';
  29. const iconNames = [
  30. 'org.gnome.Shell.Extensions.GSConnect',
  31. 'org.gnome.Shell.Extensions.GSConnect-symbolic',
  32. 'computer-symbolic',
  33. 'laptop-symbolic',
  34. 'smartphone-symbolic',
  35. 'tablet-symbolic',
  36. 'tv-symbolic',
  37. 'phonelink-ring-symbolic',
  38. 'sms-symbolic',
  39. ];
  40. getIcon._resource = {};
  41. for (const iconName of iconNames) {
  42. getIcon._resource[iconName] = new Gio.FileIcon({
  43. file: Gio.File.new_for_uri(`${iconPath}/${iconName}.svg`),
  44. });
  45. }
  46. }
  47. // Check the desktop icon theme
  48. if (getIcon._desktop.has_icon(name))
  49. return new Gio.ThemedIcon({name: name});
  50. // Check our GResource
  51. if (getIcon._resource[name] !== undefined)
  52. return getIcon._resource[name];
  53. // Fallback to hoping it's in the theme somewhere
  54. return new Gio.ThemedIcon({name: name});
  55. }
  56. /**
  57. * Get the contents of a GResource file, replacing `@PACKAGE_DATADIR@` where
  58. * necessary.
  59. *
  60. * @param {string} relativePath - A path relative to GSConnect's resource path
  61. * @return {string} The file contents as a string
  62. */
  63. function getResource(relativePath) {
  64. try {
  65. const bytes = Gio.resources_lookup_data(
  66. GLib.build_filenamev([Config.APP_PATH, relativePath]),
  67. Gio.ResourceLookupFlags.NONE
  68. );
  69. const source = new TextDecoder().decode(bytes.toArray());
  70. return source.replace('@PACKAGE_DATADIR@', Config.PACKAGE_DATADIR);
  71. } catch (e) {
  72. logError(e, 'GSConnect');
  73. return null;
  74. }
  75. }
  76. /**
  77. * Install file contents, to an absolute directory path.
  78. *
  79. * @param {string} dirname - An absolute directory path
  80. * @param {string} basename - The file name
  81. * @param {string} contents - The file contents
  82. * @return {boolean} A success boolean
  83. */
  84. function _installFile(dirname, basename, contents) {
  85. try {
  86. const filename = GLib.build_filenamev([dirname, basename]);
  87. GLib.mkdir_with_parents(dirname, 0o755);
  88. return GLib.file_set_contents(filename, contents);
  89. } catch (e) {
  90. logError(e, 'GSConnect');
  91. return false;
  92. }
  93. }
  94. /**
  95. * Install file contents from a GResource, to an absolute directory path.
  96. *
  97. * @param {string} dirname - An absolute directory path
  98. * @param {string} basename - The file name
  99. * @param {string} relativePath - A path relative to GSConnect's resource path
  100. * @return {boolean} A success boolean
  101. */
  102. function _installResource(dirname, basename, relativePath) {
  103. try {
  104. const contents = getResource(relativePath);
  105. return _installFile(dirname, basename, contents);
  106. } catch (e) {
  107. logError(e, 'GSConnect');
  108. return false;
  109. }
  110. }
  111. /**
  112. * Use Gio.File to ensure a file's executable bits are set.
  113. *
  114. * @param {string} filepath - An absolute path to a file
  115. * @returns {boolean} - True if the file already was, or is now, executable
  116. */
  117. function _setExecutable(filepath) {
  118. try {
  119. const file = Gio.File.new_for_path(filepath);
  120. const finfo = file.query_info(
  121. `${Gio.FILE_ATTRIBUTE_STANDARD_TYPE},${Gio.FILE_ATTRIBUTE_UNIX_MODE}`,
  122. Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
  123. null);
  124. if (!finfo.has_attribute(Gio.FILE_ATTRIBUTE_UNIX_MODE))
  125. return false;
  126. const mode = finfo.get_attribute_uint32(
  127. Gio.FILE_ATTRIBUTE_UNIX_MODE);
  128. const new_mode = (mode | 0o111);
  129. if (mode === new_mode)
  130. return true;
  131. return file.set_attribute_uint32(
  132. Gio.FILE_ATTRIBUTE_UNIX_MODE,
  133. new_mode,
  134. Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
  135. null);
  136. } catch (e) {
  137. logError(e, 'GSConnect');
  138. return false;
  139. }
  140. }
  141. /**
  142. * Ensure critical files in the extension directory have the
  143. * correct permissions.
  144. */
  145. export function ensurePermissions() {
  146. if (Config.IS_USER) {
  147. const executableFiles = [
  148. 'gsconnect-preferences',
  149. 'service/daemon.js',
  150. 'service/nativeMessagingHost.js',
  151. ];
  152. for (const file of executableFiles)
  153. _setExecutable(GLib.build_filenamev([Config.PACKAGE_DATADIR, file]));
  154. }
  155. }
  156. /**
  157. * Install the files necessary for the GSConnect service to run.
  158. */
  159. export function installService() {
  160. const settings = new Gio.Settings({
  161. settings_schema: Config.GSCHEMA.lookup(
  162. 'org.gnome.Shell.Extensions.GSConnect',
  163. null
  164. ),
  165. path: '/org/gnome/shell/extensions/gsconnect/',
  166. });
  167. const confDir = GLib.get_user_config_dir();
  168. const dataDir = GLib.get_user_data_dir();
  169. const homeDir = GLib.get_home_dir();
  170. // DBus Service
  171. const dbusDir = GLib.build_filenamev([dataDir, 'dbus-1', 'services']);
  172. const dbusFile = `${Config.APP_ID}.service`;
  173. // Desktop Entry
  174. const appDir = GLib.build_filenamev([dataDir, 'applications']);
  175. const appFile = `${Config.APP_ID}.desktop`;
  176. const appPrefsFile = `${Config.APP_ID}.Preferences.desktop`;
  177. // Application Icon
  178. const iconDir = GLib.build_filenamev([dataDir, 'icons', 'hicolor', 'scalable', 'apps']);
  179. const iconFull = `${Config.APP_ID}.svg`;
  180. const iconSym = `${Config.APP_ID}-symbolic.svg`;
  181. // File Manager Extensions
  182. const fileManagers = [
  183. [`${dataDir}/nautilus-python/extensions`, 'nautilus-gsconnect.py'],
  184. [`${dataDir}/nemo-python/extensions`, 'nemo-gsconnect.py'],
  185. ];
  186. // WebExtension Manifests
  187. const manifestFile = 'org.gnome.shell.extensions.gsconnect.json';
  188. const google = getResource(`webextension/${manifestFile}.google.in`);
  189. const mozilla = getResource(`webextension/${manifestFile}.mozilla.in`);
  190. const manifests = [
  191. [`${confDir}/chromium/NativeMessagingHosts/`, google],
  192. [`${confDir}/google-chrome/NativeMessagingHosts/`, google],
  193. [`${confDir}/google-chrome-beta/NativeMessagingHosts/`, google],
  194. [`${confDir}/google-chrome-unstable/NativeMessagingHosts/`, google],
  195. [`${confDir}/BraveSoftware/Brave-Browser/NativeMessagingHosts/`, google],
  196. [`${confDir}/BraveSoftware/Brave-Browser-Beta/NativeMessagingHosts/`, google],
  197. [`${confDir}/BraveSoftware/Brave-Browser-Nightly/NativeMessagingHosts/`, google],
  198. [`${homeDir}/.mozilla/native-messaging-hosts/`, mozilla],
  199. [`${homeDir}/.config/microsoft-edge-dev/NativeMessagingHosts`, google],
  200. [`${homeDir}/.config/microsoft-edge-beta/NativeMessagingHosts`, google],
  201. ];
  202. // If running as a user extension, ensure the DBus service, desktop entry,
  203. // file manager scripts, and WebExtension manifests are installed.
  204. if (Config.IS_USER) {
  205. // DBus Service
  206. if (!_installResource(dbusDir, dbusFile, `${dbusFile}.in`))
  207. throw Error('GSConnect: Failed to install DBus Service');
  208. // Desktop Entries
  209. _installResource(appDir, appFile, appFile);
  210. _installResource(appDir, appPrefsFile, appPrefsFile);
  211. // Application Icon
  212. _installResource(iconDir, iconFull, `icons/${iconFull}`);
  213. _installResource(iconDir, iconSym, `icons/${iconSym}`);
  214. // File Manager Extensions
  215. const target = `${Config.PACKAGE_DATADIR}/nautilus-gsconnect.py`;
  216. for (const [dir, name] of fileManagers) {
  217. const script = Gio.File.new_for_path(GLib.build_filenamev([dir, name]));
  218. if (!script.query_exists(null)) {
  219. GLib.mkdir_with_parents(dir, 0o755);
  220. script.make_symbolic_link(target, null);
  221. }
  222. }
  223. // WebExtension Manifests
  224. if (settings.get_boolean('create-native-messaging-hosts')) {
  225. for (const [dirname, contents] of manifests)
  226. _installFile(dirname, manifestFile, contents);
  227. }
  228. // Otherwise, if running as a system extension, ensure anything previously
  229. // installed when running as a user extension is removed.
  230. } else {
  231. GLib.unlink(GLib.build_filenamev([dbusDir, dbusFile]));
  232. GLib.unlink(GLib.build_filenamev([appDir, appFile]));
  233. GLib.unlink(GLib.build_filenamev([appDir, appPrefsFile]));
  234. GLib.unlink(GLib.build_filenamev([iconDir, iconFull]));
  235. GLib.unlink(GLib.build_filenamev([iconDir, iconSym]));
  236. for (const [dir, name] of fileManagers)
  237. GLib.unlink(GLib.build_filenamev([dir, name]));
  238. for (const manifest of manifests)
  239. GLib.unlink(GLib.build_filenamev([manifest[0], manifestFile]));
  240. }
  241. }