carousel.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Bing Wallpaper GNOME extension
  2. // Copyright (C) 2017-2023 Michael Carroll
  3. // This extension is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Lesser General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // See the GNU General Public License, version 3 or later for details.
  8. // Based on GNOME shell extension NASA APOD by Elia Argentieri https://github.com/Elinvention/gnome-shell-extension-nasa-apod
  9. import Gtk from 'gi://Gtk';
  10. import GdkPixbuf from 'gi://GdkPixbuf';
  11. import Gio from 'gi://Gio';
  12. import * as Utils from './utils.js';
  13. import {gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
  14. const default_dimensions = [30, 30, 1650, 800]; // TODO: pull from and save dimensions to settings, but perhaps verify that dimensions are ok
  15. const GALLERY_THUMB_WIDTH = 320;
  16. const GALLERY_THUMB_HEIGHT = 180;
  17. export default class Carousel {
  18. constructor(settings, button = null, callbackfunc = null, prefs_flowbox = null, extensionPath = null) {
  19. //create_gallery(widget, settings);
  20. this.settings = settings;
  21. this.button = button;
  22. this.callbackfunc = callbackfunc;
  23. this.flowBox = null;
  24. this.window = null;
  25. this.imageList = Utils.imageListSortByDate(Utils.getImageList(this.settings)).reverse(); // get images and reverse order
  26. this.searchEntry = null;
  27. this.extensionPath = extensionPath
  28. this.log('create carousel...');
  29. this.flowBox = prefs_flowbox;
  30. this.flowBox.insert(this._create_placeholder_item(), -1);
  31. }
  32. _enable_button() {
  33. if (this.button) {
  34. this.button.set_sensitive(state);
  35. }
  36. }
  37. _create_gallery() {
  38. Utils.randomIntervals.forEach((x) => {
  39. let item = this._create_random_item(x.value, _(x.title));
  40. this.flowBox.insert(item, -1);
  41. });
  42. this.imageList.forEach((image) => {
  43. let item = this._create_gallery_item(image);
  44. this.flowBox.insert(item, -1);
  45. });
  46. }
  47. _create_gallery_item(image) {
  48. let buildable = new Gtk.Builder();
  49. // grab appropriate object from UI file
  50. buildable.add_objects_from_file(this.extensionPath + '/ui/carousel4.ui', ["flowBoxChild"]);
  51. // assign variables to the UI objects we've just loaded
  52. let galleryImage = buildable.get_object('galleryImage');
  53. let filename = Utils.imageToFilename(this.settings, image);
  54. let viewButton = buildable.get_object('viewButton');
  55. let applyButton = buildable.get_object('applyButton');
  56. let infoButton = buildable.get_object('infoButton');
  57. let deleteButton = buildable.get_object('deleteButton');
  58. let favButton = buildable.get_object('favButton');
  59. let unfavButton = buildable.get_object('unfavButton');
  60. if (Utils.isFavourite(image)) {
  61. favButton.set_visible(false);
  62. this.log('image is favourited');
  63. }
  64. else {
  65. unfavButton.set_visible(false);
  66. }
  67. try {
  68. this._load_image(galleryImage, filename);
  69. }
  70. catch (e) {
  71. galleryImage.set_from_icon_name('image-missing');
  72. galleryImage.set_icon_size = 2; // Gtk.GTK_ICON_SIZE_LARGE;
  73. this.log('create_gallery_image: '+e);
  74. }
  75. galleryImage.set_tooltip_text(image.copyright);
  76. // set up actions for when a image button is clicked
  77. viewButton.connect('clicked', () => {
  78. Utils.openInSystemViewer(filename);
  79. });
  80. applyButton.connect('clicked', () => {
  81. this.settings.set_string('selected-image', Utils.getImageUrlBase(image));
  82. this.log('gallery selected '+Utils.getImageUrlBase(image));
  83. });
  84. infoButton.connect('clicked', () => {
  85. Utils.openInSystemViewer(image.copyrightlink, false);
  86. this.log('info page link opened '+image.copyrightlink);
  87. });
  88. deleteButton.connect('clicked', (widget) => {
  89. this.log('Delete requested for '+filename);
  90. Utils.deleteImage(filename);
  91. //Utils.cleanupImageList(this.settings); // hide image instead
  92. Utils.hideImage(this.settings, [image]);
  93. widget.get_parent().get_parent().set_visible(false); // bit of a hack
  94. if (this.callbackfunc)
  95. this.callbackfunc();
  96. });
  97. // button is unchecked, so we want to make the checked one visible
  98. favButton.connect('clicked', (widget) => {
  99. this.log('favourited '+Utils.getImageUrlBase(image));
  100. widget.set_visible(false);
  101. unfavButton.set_visible(true);
  102. Utils.setImageFavouriteStatus(this.settings, image.urlbase, true);
  103. });
  104. // button is checked, so we want to make the unchecked one visible
  105. unfavButton.connect('clicked', (widget) => {
  106. this.log('unfavourited '+Utils.getImageUrlBase(image));
  107. widget.set_visible(false);
  108. favButton.set_visible(true);
  109. Utils.setImageFavouriteStatus(this.settings, image.urlbase, false);
  110. });
  111. let item = buildable.get_object('flowBoxChild');
  112. return item;
  113. }
  114. _create_random_item(interval, title) {
  115. let buildable = new Gtk.Builder();
  116. // grab appropriate object from UI file
  117. buildable.add_objects_from_file(this.extensionPath + '/ui/carousel4.ui', ["flowBoxRandom"]);
  118. let randomLabel = buildable.get_object('randomLabel');
  119. randomLabel.set_text(title);
  120. let filename = 'random';
  121. let applyButton = buildable.get_object('randomButton');
  122. applyButton.connect('clicked', (widget) => {
  123. this.settings.set_string('random-interval-mode', interval);
  124. this.settings.set_boolean('random-mode-enabled', true);
  125. this.log('gallery selected random with interval '+interval+' ('+title+')');
  126. });
  127. let item = buildable.get_object('flowBoxRandom');
  128. return item;
  129. }
  130. _create_placeholder_item() {
  131. let buildable = new Gtk.Builder();
  132. this.flowBox.set_max_children_per_line(1);
  133. // grab appropriate object from UI file
  134. buildable.add_objects_from_file(this.extensionPath + '/ui/carousel4.ui', ["flowBoxPlaceholder"]);
  135. let loadButton = buildable.get_object('loadButton');
  136. loadButton.connect('clicked', (widget) => {
  137. widget.set_label(_("Loading...")); // does this work???
  138. this.flowBox.remove(widget.get_parent());
  139. this.flowBox.set_max_children_per_line(2);
  140. this._create_gallery();
  141. });
  142. let item = buildable.get_object('flowBoxPlaceholder');
  143. return item;
  144. }
  145. _load_image(galleryImage, filename) {
  146. let thumb_path = Utils.getWallpaperDir(this.settings)+'.thumbs/';
  147. let thumb_dir = Gio.file_new_for_path(thumb_path);
  148. let save_thumbs = !this.settings.get_boolean('delete-previous') && this.settings.get_boolean('create-thumbs'); // create thumbs only if not deleting previous and thumbs are enabled
  149. if (!thumb_dir.query_exists(null)) {
  150. thumb_dir.make_directory_with_parents(null);
  151. }
  152. let image_file = Gio.file_new_for_path(filename);
  153. // load gallery image or create new thumbnail if it doesn't
  154. if (!image_file.query_exists(null)){
  155. this._set_blank_image(galleryImage);
  156. }
  157. else {
  158. let image_thumb_path = thumb_path + image_file.get_basename();
  159. let image_thumb = Gio.file_new_for_path(image_thumb_path);
  160. try {
  161. let pixbuf;
  162. // use thumbnail if available
  163. if (image_thumb.query_exists(null)) {
  164. pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_thumb_path);
  165. }
  166. else { // save changed thumbnail significantly speeds up gallery loading, but costs some addtional disk space
  167. pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filename, GALLERY_THUMB_WIDTH, GALLERY_THUMB_HEIGHT);
  168. if (save_thumbs)
  169. pixbuf.savev(image_thumb_path,'jpeg',['quality'], ['90']);
  170. }
  171. galleryImage.set_pixbuf(pixbuf);
  172. }
  173. catch (e) {
  174. this._set_blank_image(galleryImage);
  175. this.log('create_gallery_image: '+e);
  176. }
  177. }
  178. }
  179. _set_blank_image(galleryImage) {
  180. //galleryImage.set_from_icon_name('image-missing');
  181. //galleryImage.set_icon_size = 2; // Gtk.GTK_ICON_SIZE_LARGE;
  182. }
  183. log(msg) {
  184. if (this.settings.get_boolean('debug-logging'))
  185. console.log("BingWallpaper extension: " + msg); // disable to keep the noise down in journal
  186. }
  187. };