Created
May 5, 2013 19:09
-
-
Save voldyman/5521828 to your computer and use it in GitHub Desktop.
podcast app demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gtk; | |
using Soup; | |
using Json; | |
namespace Podcasts { | |
public class Application : Gtk.Window { | |
Gtk.Grid grid; | |
Soup.SessionAsync session; | |
Gtk.Box results_box; | |
public Application () { | |
// Prepare Gtk.Window: | |
this.title = "Podcasts"; | |
this.window_position = Gtk.WindowPosition.CENTER; | |
this.destroy.connect (Gtk.main_quit); | |
this.set_default_size (350, 70); | |
this.resizable = true; | |
results_box = new Gtk.Box (Orientation.VERTICAL, 0); | |
grid = new Gtk.Grid(); | |
grid.margin = 12; | |
this.add (grid); | |
/*var scroll = new ScrolledWindow (null, null); | |
scroll.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC); | |
scroll.add (grid); | |
this.add(scroll);*/ | |
Gtk.Entry entry = new Gtk.Entry (); | |
entry.placeholder_text = "Search Podcasts"; | |
entry.hexpand = true; | |
Gtk.Button button = new Gtk.Button.with_label ("Search"); | |
Gtk.Grid searchrow = new Gtk.Grid(); | |
searchrow.hexpand = true; | |
searchrow.attach (entry, 0, 0, 1, 1); | |
searchrow.attach (button, 1, 0, 1, 1); | |
grid.attach (searchrow, 0, 0, 1, 1); | |
grid.attach (results_box, 0, 1, 1, 1); | |
button.clicked.connect (() => { | |
search (entry.text); | |
}); | |
search("bits und so"); | |
} | |
public void search(string query) { | |
// Search for podcasts | |
session = new Soup.SessionAsync (); | |
stdout.printf ("Searching\n"); | |
var uri = "https://gpodder.net/search.json?q=\"%s\"".printf (query); | |
stdout.printf(uri + "\n"); | |
var message = new Soup.Message ("GET", uri); | |
session.queue_message (message, got_response); | |
} | |
public void got_response (Soup.Session session, Soup.Message msg) { | |
if (msg.status_code != Soup.KnownStatusCode.OK) { | |
debug ("error fetching data"); | |
return; | |
} | |
Gtk.Box box = grid.get_child_at (0, 1) as Gtk.Box; | |
box.foreach ((wid) => { | |
box.remove (wid); | |
}); | |
box.set_spacing(12); | |
box.hexpand = true; | |
// Parse JSON results | |
var parser = new Json.Parser (); | |
parser.load_from_data ((string) msg.response_body.flatten ().data, -1); | |
var root_array = parser.get_root().get_array(); | |
foreach (var result in root_array.get_elements ()) { | |
Gtk.Grid row = new Gtk.Grid(); | |
row.hexpand = true; | |
var podcast = result.get_object (); | |
// Load cover | |
string logo_url = podcast.get_string_member ("scaled_logo_url"); | |
Gtk.Image image = new Gtk.Image(); | |
image.set_size_request (64, 64); | |
try { | |
Gdk.Pixbuf.new_from_stream_async.begin (File.new_for_uri (logo_url).read (), null, (obj, res) => { | |
var pix = Gdk.Pixbuf.new_from_stream_async.end (res); | |
image.set_from_pixbuf (pix); | |
}); | |
} catch (Error e) { | |
print ("Image Set Async failed with error %s \n".printf (e.message)); | |
} | |
Label title = new Gtk.Label ("<b>" + podcast.get_string_member ("title") + "</b>"); | |
title.set_use_markup (true); | |
title.set_alignment(0.0f, 0.5f); | |
Label description = new Gtk.Label (podcast.get_string_member ("description")); | |
description.set_alignment(0.0f, 0.5f); | |
description.hexpand = true; | |
description.wrap = true; | |
Button subscribe = new Gtk.Button.with_label("Subscribe"); | |
//subscribe.set_alignment(1.0f, 0.5f); | |
subscribe.valign = Gtk.Align.CENTER; | |
row.attach(image ,0, 0, 1, 2); | |
row.attach(title, 1, 0, 1, 1); | |
row.attach(description, 1, 1, 1, 1); | |
row.attach(subscribe ,2, 0, 1, 2); | |
box.pack_start (row, false, false, 0); | |
} | |
show_all (); | |
} | |
} | |
public static int main (string[] args) { | |
Gtk.init (ref args); | |
Application app = new Application (); | |
app.show_all (); | |
Gtk.main (); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment