diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f62240..4c772b9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ set(VALA_FILES_CORE snconfig.vala snitemproxy.vala qrichtextparser.vala + utils.vala ${CMAKE_CURRENT_BINARY_DIR}/config.vala ) vala_precompile(VALA_C_CORE sntray-core diff --git a/src/snitem.vala b/src/snitem.vala index 3f8bb4d..631da0a 100644 --- a/src/snitem.vala +++ b/src/snitem.vala @@ -353,24 +353,7 @@ namespace StatusNotifier Gdk.Pixbuf? pixbuf = null; foreach (var pixmap in pixmaps) { - uint[] new_bytes = (uint[]) pixmap.bytes; - for (int i = 0; i < new_bytes.length; i++) { - new_bytes[i] = new_bytes[i].to_big_endian(); - } - - pixmap.bytes = (uint8[]) new_bytes; - for (int i = 0; i < pixmap.bytes.length; i = i+4) { - uint8 red = pixmap.bytes[i]; - pixmap.bytes[i] = pixmap.bytes[i+2]; - pixmap.bytes[i+2] = red; - } - pixbuf = new Gdk.Pixbuf.from_data(pixmap.bytes, - Gdk.Colorspace.RGB, - true, - 8, - pixmap.width, - pixmap.height, - Cairo.Format.ARGB32.stride_for_width(pixmap.width)); + pixbuf = pixmap.gicon() as Gdk.Pixbuf; if (pixmap.height >= icon_size && pixmap.width >= icon_size) break; } @@ -381,28 +364,6 @@ namespace StatusNotifier } return null; } - private Icon? find_file_icon(string? icon_name, string? path) - { - if (path == null || path.length == 0) - return null; - try - { - var dir = Dir.open(path); - for (var ch = dir.read_name(); ch!= null; ch = dir.read_name()) - { - var f = File.new_for_path(path+"/"+ch); - if (ch[0:ch.last_index_of(".")] == icon_name) - return new FileIcon(f); - var t = f.query_file_type(FileQueryInfoFlags.NONE); - Icon? ret = null; - if (t == FileType.DIRECTORY) - ret = find_file_icon(icon_name,path+"/"+ch); - if (ret != null) - return ret; - } - } catch (Error e) {stderr.printf("%s\n",e.message);} - return null; - } private void iface_new_tooltip_cb() { try{ diff --git a/src/snitemproxy.vala b/src/snitemproxy.vala index 792af8e..2aaccf6 100644 --- a/src/snitemproxy.vala +++ b/src/snitemproxy.vala @@ -51,14 +51,63 @@ namespace StatusNotifier int width; int height; uint8[] bytes; - } + public GLib.Icon? gicon() + { + uint[] new_bytes = (uint[]) this.bytes; + for (int i = 0; i < new_bytes.length; i++) { + new_bytes[i] = new_bytes[i].to_big_endian(); + } - public struct ToolTip + this.bytes = (uint8[]) new_bytes; + for (int i = 0; i < this.bytes.length; i = i+4) { + uint8 red = this.bytes[i]; + this.bytes[i] = this.bytes[i+2]; + this.bytes[i+2] = red; + } + return new Gdk.Pixbuf.from_data(this.bytes, + Gdk.Colorspace.RGB, + true, + 8, + this.width, + this.height, + Cairo.Format.ARGB32.stride_for_width(this.width)); + } + } + public struct ToolTip { string icon_name; IconPixmap[] pixmap; string title; string description; + public ToolTip.from_variant(Variant variant) + { + variant.get_child(0, "s", &this.icon_name); + this.pixmap = unbox_pixmaps(variant.get_child_value(1)); + variant.get_child(2, "s", &this.title); + variant.get_child(3, "s", &this.description); + } + public static IconPixmap[] unbox_pixmaps(Variant variant) + { + IconPixmap[] pixmaps = { }; + VariantIter pixmap_iterator = variant.iterator(); + Variant pixmap_variant = pixmap_iterator.next_value(); + while (pixmap_variant != null) + { + var pixmap = IconPixmap(); + pixmap_variant.get_child(0, "i", &pixmap.width); + pixmap_variant.get_child(1, "i", &pixmap.height); + Variant bytes_variant = pixmap_variant.get_child_value(2); + uint8[] bytes = { }; + VariantIter bytes_iterator = bytes_variant.iterator(); + uint8 byte = 0; + while (bytes_iterator.next("y", &byte)) + bytes += byte; + pixmap.bytes = bytes; + pixmaps += pixmap; + pixmap_variant = pixmap_iterator.next_value(); + } + return pixmaps; + } } [DBus (name = "org.kde.StatusNotifierItem")] private interface ItemIface : Object diff --git a/src/utils.vala b/src/utils.vala new file mode 100644 index 0000000..8b16861 --- /dev/null +++ b/src/utils.vala @@ -0,0 +1,42 @@ +/* + * xfce4-sntray-plugin + * Copyright (C) 2015-2018 Konstantin Pugin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +namespace StatusNotifier +{ + private Icon? find_file_icon(string? icon_name, string? path) + { + if (path == null || path.length == 0) + return null; + try + { + var dir = Dir.open(path); + for (var ch = dir.read_name(); ch!= null; ch = dir.read_name()) + { + var f = File.new_for_path(path+"/"+ch); + if (ch[0:ch.last_index_of(".")] == icon_name) + return new FileIcon(f); + var t = f.query_file_type(FileQueryInfoFlags.NONE); + Icon? ret = null; + if (t == FileType.DIRECTORY) + ret = find_file_icon(icon_name,path+"/"+ch); + if (ret != null) + return ret; + } + } catch (Error e) {stderr.printf("%s\n",e.message);} + return null; + } +}