Skip to content

Commit e48df0a

Browse files
committed
Src: small refactoring.
1 parent a5b8f3e commit e48df0a

4 files changed

Lines changed: 95 additions & 42 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(VALA_FILES_CORE
1414
snconfig.vala
1515
snitemproxy.vala
1616
qrichtextparser.vala
17+
utils.vala
1718
${CMAKE_CURRENT_BINARY_DIR}/config.vala
1819
)
1920
vala_precompile(VALA_C_CORE sntray-core

src/snitem.vala

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -353,24 +353,7 @@ namespace StatusNotifier
353353
Gdk.Pixbuf? pixbuf = null;
354354
foreach (var pixmap in pixmaps)
355355
{
356-
uint[] new_bytes = (uint[]) pixmap.bytes;
357-
for (int i = 0; i < new_bytes.length; i++) {
358-
new_bytes[i] = new_bytes[i].to_big_endian();
359-
}
360-
361-
pixmap.bytes = (uint8[]) new_bytes;
362-
for (int i = 0; i < pixmap.bytes.length; i = i+4) {
363-
uint8 red = pixmap.bytes[i];
364-
pixmap.bytes[i] = pixmap.bytes[i+2];
365-
pixmap.bytes[i+2] = red;
366-
}
367-
pixbuf = new Gdk.Pixbuf.from_data(pixmap.bytes,
368-
Gdk.Colorspace.RGB,
369-
true,
370-
8,
371-
pixmap.width,
372-
pixmap.height,
373-
Cairo.Format.ARGB32.stride_for_width(pixmap.width));
356+
pixbuf = pixmap.gicon() as Gdk.Pixbuf;
374357
if (pixmap.height >= icon_size && pixmap.width >= icon_size)
375358
break;
376359
}
@@ -381,28 +364,6 @@ namespace StatusNotifier
381364
}
382365
return null;
383366
}
384-
private Icon? find_file_icon(string? icon_name, string? path)
385-
{
386-
if (path == null || path.length == 0)
387-
return null;
388-
try
389-
{
390-
var dir = Dir.open(path);
391-
for (var ch = dir.read_name(); ch!= null; ch = dir.read_name())
392-
{
393-
var f = File.new_for_path(path+"/"+ch);
394-
if (ch[0:ch.last_index_of(".")] == icon_name)
395-
return new FileIcon(f);
396-
var t = f.query_file_type(FileQueryInfoFlags.NONE);
397-
Icon? ret = null;
398-
if (t == FileType.DIRECTORY)
399-
ret = find_file_icon(icon_name,path+"/"+ch);
400-
if (ret != null)
401-
return ret;
402-
}
403-
} catch (Error e) {stderr.printf("%s\n",e.message);}
404-
return null;
405-
}
406367
private void iface_new_tooltip_cb()
407368
{
408369
try{

src/snitemproxy.vala

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,63 @@ namespace StatusNotifier
5151
int width;
5252
int height;
5353
uint8[] bytes;
54-
}
54+
public GLib.Icon? gicon()
55+
{
56+
uint[] new_bytes = (uint[]) this.bytes;
57+
for (int i = 0; i < new_bytes.length; i++) {
58+
new_bytes[i] = new_bytes[i].to_big_endian();
59+
}
5560

56-
public struct ToolTip
61+
this.bytes = (uint8[]) new_bytes;
62+
for (int i = 0; i < this.bytes.length; i = i+4) {
63+
uint8 red = this.bytes[i];
64+
this.bytes[i] = this.bytes[i+2];
65+
this.bytes[i+2] = red;
66+
}
67+
return new Gdk.Pixbuf.from_data(this.bytes,
68+
Gdk.Colorspace.RGB,
69+
true,
70+
8,
71+
this.width,
72+
this.height,
73+
Cairo.Format.ARGB32.stride_for_width(this.width));
74+
}
75+
}
76+
public struct ToolTip
5777
{
5878
string icon_name;
5979
IconPixmap[] pixmap;
6080
string title;
6181
string description;
82+
public ToolTip.from_variant(Variant variant)
83+
{
84+
variant.get_child(0, "s", &this.icon_name);
85+
this.pixmap = unbox_pixmaps(variant.get_child_value(1));
86+
variant.get_child(2, "s", &this.title);
87+
variant.get_child(3, "s", &this.description);
88+
}
89+
public static IconPixmap[] unbox_pixmaps(Variant variant)
90+
{
91+
IconPixmap[] pixmaps = { };
92+
VariantIter pixmap_iterator = variant.iterator();
93+
Variant pixmap_variant = pixmap_iterator.next_value();
94+
while (pixmap_variant != null)
95+
{
96+
var pixmap = IconPixmap();
97+
pixmap_variant.get_child(0, "i", &pixmap.width);
98+
pixmap_variant.get_child(1, "i", &pixmap.height);
99+
Variant bytes_variant = pixmap_variant.get_child_value(2);
100+
uint8[] bytes = { };
101+
VariantIter bytes_iterator = bytes_variant.iterator();
102+
uint8 byte = 0;
103+
while (bytes_iterator.next("y", &byte))
104+
bytes += byte;
105+
pixmap.bytes = bytes;
106+
pixmaps += pixmap;
107+
pixmap_variant = pixmap_iterator.next_value();
108+
}
109+
return pixmaps;
110+
}
62111
}
63112
[DBus (name = "org.kde.StatusNotifierItem")]
64113
private interface ItemIface : Object

src/utils.vala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* xfce4-sntray-plugin
3+
* Copyright (C) 2015-2018 Konstantin Pugin <ria.freelander@gmail.com>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
namespace StatusNotifier
19+
{
20+
private Icon? find_file_icon(string? icon_name, string? path)
21+
{
22+
if (path == null || path.length == 0)
23+
return null;
24+
try
25+
{
26+
var dir = Dir.open(path);
27+
for (var ch = dir.read_name(); ch!= null; ch = dir.read_name())
28+
{
29+
var f = File.new_for_path(path+"/"+ch);
30+
if (ch[0:ch.last_index_of(".")] == icon_name)
31+
return new FileIcon(f);
32+
var t = f.query_file_type(FileQueryInfoFlags.NONE);
33+
Icon? ret = null;
34+
if (t == FileType.DIRECTORY)
35+
ret = find_file_icon(icon_name,path+"/"+ch);
36+
if (ret != null)
37+
return ret;
38+
}
39+
} catch (Error e) {stderr.printf("%s\n",e.message);}
40+
return null;
41+
}
42+
}

0 commit comments

Comments
 (0)