Skip to content

Commit

Permalink
Src: small refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
rilian-la-te committed Mar 13, 2018
1 parent a5b8f3e commit e48df0a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 1 addition & 40 deletions src/snitem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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{
Expand Down
53 changes: 51 additions & 2 deletions src/snitemproxy.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/utils.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* xfce4-sntray-plugin
* Copyright (C) 2015-2018 Konstantin Pugin <ria.freelander@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

0 comments on commit e48df0a

Please sign in to comment.