108 gchar *thumb_path = g_build_filename(path,
"thumbnailers", NULL);
110 GDir *dir = g_dir_open(thumb_path, 0, NULL);
119 while ((dirent = g_dir_read_name(dir))) {
123 gchar *filename = g_build_filename(thumb_path, dirent, NULL);
124 GKeyFile *key_file = g_key_file_new();
125 GError *error = NULL;
127 if (!g_key_file_load_from_file(key_file, filename, 0, &error)) {
128 g_warning(
"Error loading thumbnailer %s: %s", filename, error->message);
133 gchar **mime_types = g_key_file_get_string_list(
136 if (mime_types && command) {
138 for (i = 0; mime_types[i] != NULL; i++) {
141 g_info(
"Loading thumbnailer %s for mimetype %s", filename,
144 g_strdup(mime_types[i]), g_strdup(command));
150 g_strfreev(mime_types);
155 g_key_file_free(key_file);
164 const gchar *filename,
165 const gchar *encoded_uri,
166 const gchar *output_path,
int size) {
167 gchar **command_parts = g_strsplit(command,
" ", 0);
168 guint command_parts_count = g_strv_length(command_parts);
170 gchar **command_args = NULL;
173 command_args = g_malloc0(
sizeof(gchar *) * (command_parts_count + 3 + 1));
176 guint current_index = 0;
178 command_args[current_index++] = g_strdup(
"nice");
179 command_args[current_index++] = g_strdup(
"-n");
180 command_args[current_index++] = g_strdup(
"19");
184 for (i = 0; command_parts[i] != NULL; i++) {
185 if (strcmp(command_parts[i],
"%i") == 0) {
186 command_args[current_index++] = g_strdup(filename);
187 }
else if (strcmp(command_parts[i],
"%u") == 0) {
188 command_args[current_index++] = g_strdup(encoded_uri);
189 }
else if (strcmp(command_parts[i],
"%o") == 0) {
190 command_args[current_index++] = g_strdup(output_path);
191 }
else if (strcmp(command_parts[i],
"%s") == 0) {
192 command_args[current_index++] = g_strdup_printf(
"%d", size);
194 command_args[current_index++] = g_strdup(command_parts[i]);
198 command_args[current_index++] = NULL;
200 g_strfreev(command_parts);
289 g_hash_table_new(g_direct_hash, g_direct_equal);
293 GSList *l = gdk_pixbuf_get_formats();
294 for (GSList *li = l; li != NULL; li = g_slist_next(li)) {
296 gdk_pixbuf_format_get_extensions((GdkPixbufFormat *)li->data);
298 for (
unsigned int i = 0; exts && exts[i]; i++) {
301 g_info(
"Add image extension: %s", exts[i]);
311 g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)g_free);
313 const gchar *
const *system_data_dirs = g_get_system_data_dirs();
314 const gchar *user_data_dir = g_get_user_data_dir();
319 for (i = 0; system_data_dirs[i] != NULL; i++) {
391 const guchar *pixels;
395 if (pixbuf == NULL) {
399 width = gdk_pixbuf_get_width(pixbuf);
400 height = gdk_pixbuf_get_height(pixbuf);
401 pixels = gdk_pixbuf_read_pixels(pixbuf);
402 stride = gdk_pixbuf_get_rowstride(pixbuf);
403 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
405 cairo_surface_t *surface = NULL;
410 const guchar *pixels_end, *line;
413 pixels_end = pixels + height * stride;
417 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
418 cpixels = cairo_image_surface_get_data(surface);
419 cstride = cairo_image_surface_get_stride(surface);
421 cairo_surface_flush(surface);
422 while (pixels < pixels_end) {
424 const guchar *line_end = line + lo;
425 guchar *cline = cpixels;
427 while (line < line_end) {
443 cairo_surface_mark_dirty(surface);
444 cairo_surface_flush(surface);
535 G_GNUC_UNUSED gpointer user_data) {
536 g_debug(
"starting up icon fetching thread.");
542 const gchar *icon_path;
543 gchar *icon_path_ = NULL;
545 if (g_str_has_prefix(sentry->
entry->
name,
"thumbnail://")) {
547 gchar *entry_name = &sentry->
entry->
name[12];
549 if (strcmp(entry_name,
"") == 0) {
557 int requested_size = MAX(sentry->
wsize, sentry->
hsize);
561 entry_name, requested_size, &thumb_size);
563 if (!g_file_test(icon_path, G_FILE_TEST_EXISTS)) {
564 char **command_args = NULL;
566 gchar *size_str = g_strdup_printf(
"%d", thumb_size);
569 entry_name,
"{output}", icon_path_,
"{size}",
576 g_strfreev(command_args);
579 }
else if (g_path_is_absolute(entry_name)) {
581 if (g_str_has_suffix(entry_name,
".desktop")) {
585 if (icon_key == NULL || strlen(icon_key) == 0) {
587 icon_path = icon_path_ = nk_xdg_theme_get_icon(
592 }
else if (g_path_is_absolute(icon_key)) {
594 icon_path = icon_path_ = icon_key;
597 icon_path = icon_path_ = nk_xdg_theme_get_icon(
605 gchar *encoded_uri = g_filename_to_uri(entry_name, NULL, NULL);
606 int requested_size = MAX(sentry->
wsize, sentry->
hsize);
611 encoded_uri, requested_size, &thumb_size);
613 if (!g_file_test(icon_path, G_FILE_TEST_EXISTS)) {
615 char *content_type = g_content_type_guess(entry_name, NULL, 0, NULL);
616 char *mime_type = g_content_type_get_mime_type(content_type);
620 mime_type, entry_name, encoded_uri, icon_path_, thumb_size);
626 while (mime_type[index]) {
627 if (mime_type[index] ==
'/')
628 mime_type[index] =
'-';
635 icon_path = icon_path_ = nk_xdg_theme_get_icon(
641 g_free(content_type);
650 if (icon_path_ == NULL || !g_file_test(icon_path, G_FILE_TEST_EXISTS)) {
655 }
else if (g_path_is_absolute(sentry->
entry->
name)) {
657 }
else if (g_str_has_prefix(sentry->
entry->
name,
"<span")) {
658 cairo_surface_t *surface = cairo_image_surface_create(
659 CAIRO_FORMAT_ARGB32, sentry->
wsize, sentry->
hsize);
660 cairo_t *cr = cairo_create(surface);
661 PangoLayout *layout = pango_cairo_create_layout(cr);
662 pango_layout_set_markup(layout, sentry->
entry->
name, -1);
665 pango_layout_get_size(layout, &width, &height);
666 double ws = sentry->
wsize / ((double)width / PANGO_SCALE);
667 double wh = sentry->
hsize / ((double)height / PANGO_SCALE);
668 double scale = MIN(ws, wh);
671 cr, (sentry->
wsize - ((
double)width / PANGO_SCALE) * scale) / 2.0,
672 (sentry->
hsize - ((
double)height / PANGO_SCALE) * scale) / 2.0);
673 cairo_scale(cr, scale, scale);
674 pango_cairo_update_layout(cr, layout);
675 pango_layout_get_size(layout, &width, &height);
676 pango_cairo_show_layout(cr, layout);
677 g_object_unref(layout);
680 g_atomic_pointer_set(&sentry->
surface, surface);
686 icon_path = icon_path_ = nk_xdg_theme_get_icon(
689 if (icon_path_ == NULL) {
690 g_debug(
"failed to get icon %s(%dx%d): n/a", sentry->
entry->
name,
693 const char *ext = g_strrstr(sentry->
entry->
name,
".");
695 const char *exts2[2] = {ext, NULL};
696 icon_path = icon_path_ =
699 if (icon_path_ == NULL) {
705 g_debug(
"found icon %s(%dx%d): %s", sentry->
entry->
name, sentry->
wsize,
706 sentry->
hsize, icon_path);
709 cairo_surface_t *icon_surf = NULL;
712 const char *suf = strrchr(icon_path,
'.');
721 int width = sentry->
wsize, height = sentry->
hsize;
723 width *= sentry->
scale;
725 height *= sentry->
scale;
727 GError *error = NULL;
729 gdk_pixbuf_new_from_file_at_scale(icon_path, width, height, TRUE, &error);
736 if (error != NULL && g_error_matches(error, GDK_PIXBUF_ERROR,
737 GDK_PIXBUF_ERROR_INCOMPLETE_ANIMATION)) {
738 g_clear_error(&error);
742 g_warning(
"Failed to load image: |%s| %d %d %s (%p)", icon_path,
743 sentry->
wsize, sentry->
hsize, error->message, (
void *)pb);
754 g_atomic_pointer_set(&sentry->
surface, icon_surf);