Extract icon path function from get_pixbuf_from_icon
This commit is contained in:
		
							parent
							
								
									e90f605a52
								
							
						
					
					
						commit
						59f994ee94
					
				
							
								
								
									
										39
									
								
								src/icon.c
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/icon.c
									
									
									
									
									
								
							| @ -195,14 +195,14 @@ GdkPixbuf *get_pixbuf_from_file(const char *filename) | |||||||
|         return pixbuf; |         return pixbuf; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| GdkPixbuf *get_pixbuf_from_icon(const char *iconname) | char *get_path_from_icon_name(const char *iconname) | ||||||
| { | { | ||||||
|         if (STR_EMPTY(iconname)) |         if (STR_EMPTY(iconname)) | ||||||
|                 return NULL; |                 return NULL; | ||||||
| 
 | 
 | ||||||
|         const char *suffixes[] = { ".svg", ".svgz", ".png", ".xpm", NULL }; |         const char *suffixes[] = { ".svg", ".svgz", ".png", ".xpm", NULL }; | ||||||
|         GdkPixbuf *pixbuf = NULL; |  | ||||||
|         gchar *uri_path = NULL; |         gchar *uri_path = NULL; | ||||||
|  |         char *new_name = NULL; | ||||||
| 
 | 
 | ||||||
|         if (g_str_has_prefix(iconname, "file://")) { |         if (g_str_has_prefix(iconname, "file://")) { | ||||||
|                 uri_path = g_filename_from_uri(iconname, NULL, NULL); |                 uri_path = g_filename_from_uri(iconname, NULL, NULL); | ||||||
| @ -212,7 +212,7 @@ GdkPixbuf *get_pixbuf_from_icon(const char *iconname) | |||||||
| 
 | 
 | ||||||
|         /* absolute path? */ |         /* absolute path? */ | ||||||
|         if (iconname[0] == '/' || iconname[0] == '~') { |         if (iconname[0] == '/' || iconname[0] == '~') { | ||||||
|                 pixbuf = get_pixbuf_from_file(iconname); |                 new_name = g_strdup(iconname); | ||||||
|         } else { |         } else { | ||||||
|         /* search in icon_path */ |         /* search in icon_path */ | ||||||
|                 char *start = settings.icon_path, |                 char *start = settings.icon_path, | ||||||
| @ -224,26 +224,47 @@ GdkPixbuf *get_pixbuf_from_icon(const char *iconname) | |||||||
|                         current_folder = g_strndup(start, end - start); |                         current_folder = g_strndup(start, end - start); | ||||||
| 
 | 
 | ||||||
|                         for (const char **suf = suffixes; *suf; suf++) { |                         for (const char **suf = suffixes; *suf; suf++) { | ||||||
|                                 maybe_icon_path = g_strconcat(current_folder, "/", iconname, *suf, NULL); |                                 gchar *name_with_extension = g_strconcat(iconname, *suf, NULL); | ||||||
|                                 if (is_readable_file(maybe_icon_path)) |                                 maybe_icon_path = g_build_filename(current_folder, name_with_extension, NULL); | ||||||
|                                         pixbuf = get_pixbuf_from_file(maybe_icon_path); |                                 if (is_readable_file(maybe_icon_path)) { | ||||||
|  |                                         new_name = g_strdup(maybe_icon_path); | ||||||
|  |                                 } | ||||||
|  |                                 g_free(name_with_extension); | ||||||
|                                 g_free(maybe_icon_path); |                                 g_free(maybe_icon_path); | ||||||
| 
 | 
 | ||||||
|                                 if (pixbuf) |                                 if (new_name) | ||||||
|                                         break; |                                         break; | ||||||
|                         } |                         } | ||||||
| 
 | 
 | ||||||
|                         g_free(current_folder); |                         g_free(current_folder); | ||||||
|                         if (pixbuf) |                         if (new_name) | ||||||
|                                 break; |                                 break; | ||||||
| 
 | 
 | ||||||
|                         start = end + 1; |                         start = end + 1; | ||||||
|                 } while (STR_FULL(end)); |                 } while (STR_FULL(end)); | ||||||
|                 if (!pixbuf) |                 if (!new_name) | ||||||
|                         LOG_W("No icon found in path: '%s'", iconname); |                         LOG_W("No icon found in path: '%s'", iconname); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         g_free(uri_path); |         g_free(uri_path); | ||||||
|  |         return new_name; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | GdkPixbuf *get_pixbuf_from_icon(const char *iconname) | ||||||
|  | { | ||||||
|  |         char *path = get_path_from_icon_name(iconname); | ||||||
|  |         if (!path) { | ||||||
|  |                 return NULL; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         GdkPixbuf *pixbuf = NULL; | ||||||
|  | 
 | ||||||
|  |         pixbuf = get_pixbuf_from_file(path); | ||||||
|  |         g_free(path); | ||||||
|  | 
 | ||||||
|  |         if (!pixbuf) | ||||||
|  |                 LOG_W("No icon found in path: '%s'", iconname); | ||||||
|  | 
 | ||||||
|         return pixbuf; |         return pixbuf; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								src/icon.h
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/icon.h
									
									
									
									
									
								
							| @ -17,6 +17,17 @@ cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf); | |||||||
|  */ |  */ | ||||||
| GdkPixbuf *get_pixbuf_from_file(const char *filename); | GdkPixbuf *get_pixbuf_from_file(const char *filename); | ||||||
| 
 | 
 | ||||||
|  | /** Retrieve a path from an icon name.
 | ||||||
|  |  * | ||||||
|  |  * @param iconname A string describing a `file://` URL, an arbitary filename
 | ||||||
|  |  *                 or an icon name, which then gets searched for in the | ||||||
|  |  *                 settings.icon_path | ||||||
|  |  * | ||||||
|  |  * @return a newly allocated string with the icon path | ||||||
|  |  * @retval NULL: file does not exist, not readable, etc.. | ||||||
|  |  */ | ||||||
|  | char *get_path_from_icon_name(const char *iconname); | ||||||
|  | 
 | ||||||
| /** Retrieve an icon by its name sent via the notification bus, scaled according to settings
 | /** Retrieve an icon by its name sent via the notification bus, scaled according to settings
 | ||||||
|  * |  * | ||||||
|  * @param iconname A string describing a `file://` URL, an arbitary filename
 |  * @param iconname A string describing a `file://` URL, an arbitary filename
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 fwsmit
						fwsmit