Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 33ba74b

Browse files
committed
Merge branch 'fix-and-unify-images-loading' into with-fixes
2 parents 03a801e + d8b5c05 commit 33ba74b

File tree

1 file changed

+77
-57
lines changed

1 file changed

+77
-57
lines changed

src/Network.vala

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,38 @@ public class Tootle.Network : GLib.Object {
100100
});
101101
}
102102

103+
public void queue_noauth (owned Soup.Message message, owned SuccessCallback? cb = null, owned ErrorCallback? errcb = null) {
104+
requests_processing++;
105+
started ();
106+
107+
session.queue_message (message, (sess, msg) => {
108+
var status = msg.status_code;
109+
if (status != Soup.Status.CANCELLED) {
110+
if (status == Soup.Status.OK) {
111+
if (cb != null) {
112+
try {
113+
cb (session, msg);
114+
}
115+
catch (Error e) {
116+
warning ("Caught exception on network request:");
117+
warning (e.message);
118+
if (errcb != null)
119+
errcb (Soup.Status.NONE, e.message);
120+
}
121+
}
122+
}
123+
else {
124+
if (errcb != null)
125+
errcb ((int32)status, get_error_reason ((int32)status));
126+
}
127+
}
128+
// msg.request_body.free ();
129+
// msg.response_body.free ();
130+
// msg.request_headers.free ();
131+
// msg.response_headers.free ();
132+
});
133+
}
134+
103135
public string get_error_reason (int32 status) {
104136
return "Error " + status.to_string () + ": " + Soup.Status.get_phrase (status);
105137
}
@@ -133,78 +165,66 @@ public class Tootle.Network : GLib.Object {
133165
parser.load_from_data ((string) msg.response_body.flatten ().data, -1);
134166
return parser.get_root ().get_array ();
135167
}
136-
137-
//TODO: Cache
138-
public void load_avatar (string url, Granite.Widgets.Avatar avatar, int size){
139-
var message = new Soup.Message("GET", url);
140-
network.queue (message, (sess, msg) => {
141-
if (msg.status_code != Soup.Status.OK) {
142-
avatar.show_default (size);
143-
return;
144-
}
145-
146-
var data = msg.response_body.data;
147-
var stream = new MemoryInputStream.from_data (data);
148-
var pixbuf = new Gdk.Pixbuf.from_stream_at_scale (stream, size, size, true);
149-
150-
avatar.pixbuf = pixbuf.scale_simple (size, size, Gdk.InterpType.BILINEAR);
151-
});
152-
}
153-
168+
154169
//TODO: Cache
155170
public delegate void PixbufCallback (Gdk.Pixbuf pixbuf);
156-
public Soup.Message load_pixbuf (string url, PixbufCallback cb) {
171+
public Soup.Message load_pixbuf (string url, PixbufCallback cb, owned ErrorCallback? errcb = null, int? size = null) {
157172
var message = new Soup.Message("GET", url);
158-
network.queue (message, (sess, msg) => {
159-
Gdk.Pixbuf? pixbuf = null;
160-
try {
161-
var data = msg.response_body.flatten ().data;
162-
var stream = new MemoryInputStream.from_data (data);
163-
pixbuf = new Gdk.Pixbuf.from_stream (stream);
164-
}
165-
catch (Error e) {
166-
warning ("Can't get image: %s".printf (url));
167-
warning ("Reason: " + e.message);
173+
network.queue_noauth (
174+
message,
175+
(sess, msg) => {
176+
Gdk.Pixbuf? pixbuf = null;
177+
try {
178+
var data = msg.response_body.flatten ().data;
179+
var stream = new MemoryInputStream.from_data (data);
180+
if (size == null)
181+
pixbuf = new Gdk.Pixbuf.from_stream (stream);
182+
else
183+
pixbuf = new Gdk.Pixbuf.from_stream_at_scale (stream, size, size, true);
184+
}
185+
catch (Error e) {
186+
warning ("Can't decode image: %s".printf (url));
187+
warning ("Reason: " + e.message);
188+
}
189+
cb (pixbuf);
190+
},
191+
(status, status_message) => {
192+
warning ("Could not get an image %s with http status: %i".printf (url, status));
193+
if (errcb != null)
194+
errcb (status, status_message);
168195
}
169-
finally {
170-
if (msg.status_code != Soup.Status.OK)
171-
warning ("Invalid response code %s: %s".printf (msg.status_code.to_string (), url));
172-
}
173-
cb (pixbuf);
174-
});
196+
);
175197
return message;
176198
}
177199

178-
//TODO: Cache
179200
public void load_image (string url, Gtk.Image image) {
180-
var message = new Soup.Message("GET", url);
181-
network.queue (message, (sess, msg) => {
182-
if (msg.status_code != Soup.Status.OK) {
201+
load_pixbuf(
202+
url,
203+
image.set_from_pixbuf,
204+
(_, __) => {
183205
image.set_from_icon_name ("image-missing", Gtk.IconSize.LARGE_TOOLBAR);
184-
return;
185206
}
186-
187-
var data = msg.response_body.data;
188-
var stream = new MemoryInputStream.from_data (data);
189-
var pixbuf = new Gdk.Pixbuf.from_stream (stream);
190-
image.set_from_pixbuf (pixbuf);
191-
});
207+
);
192208
}
193209

194-
//TODO: Cache
195210
public void load_scaled_image (string url, Gtk.Image image, int size) {
196-
var message = new Soup.Message("GET", url);
197-
network.queue (message, (sess, msg) => {
198-
if (msg.status_code != Soup.Status.OK) {
211+
load_pixbuf(
212+
url,
213+
image.set_from_pixbuf,
214+
(_, __) => {
199215
image.set_from_icon_name ("image-missing", Gtk.IconSize.LARGE_TOOLBAR);
200-
return;
201-
}
216+
},
217+
size
218+
);
219+
}
202220

203-
var data = msg.response_body.data;
204-
var stream = new MemoryInputStream.from_data (data);
205-
var pixbuf = new Gdk.Pixbuf.from_stream_at_scale (stream, size, size, true);
206-
image.set_from_pixbuf (pixbuf);
207-
});
221+
public void load_avatar (string url, Granite.Widgets.Avatar avatar, int size){
222+
load_pixbuf(
223+
url,
224+
(pixbuf) => { avatar.pixbuf = pixbuf.scale_simple (size, size, Gdk.InterpType.BILINEAR); },
225+
(_, __) => { avatar.show_default (size); },
226+
size
227+
);
208228
}
209229

210230
}

0 commit comments

Comments
 (0)