Add refresh menu item

This commit is contained in:
bleakgrey 2018-05-03 11:11:31 +03:00
parent 2d62596a21
commit cb7590f341
9 changed files with 125 additions and 9 deletions

View File

@ -25,6 +25,7 @@ executable(
'src/NetManager.vala',
'src/CacheManager.vala',
'src/Utils.vala',
'src/CmdRunner.vala',
'src/API/Account.vala',
'src/API/Relationship.vala',
'src/API/Mention.vala',

View File

@ -31,7 +31,7 @@ public class Tootle.Status{
var id = int64.parse (obj.get_string_member ("id"));
var status = new Status (id);
status.account = Account.parse(obj.get_object_member ("account"));
status.account = Account.parse (obj.get_object_member ("account"));
status.uri = obj.get_string_member ("uri");
status.url = obj.get_string_member ("url");
status.created_at = obj.get_string_member ("created_at");

91
src/CmdRunner.vala Normal file
View File

@ -0,0 +1,91 @@
public class Tootle.CmdRunner : GLib.Object{
public signal void done(int exit);
public signal void output_changed(string text);
public signal void standard_changed(string text);
public signal void error_changed(string text);
public string standard_output_str = "";
public string error_output_str = "";
public string output_str = "";
GLib.IOChannel out_make;
GLib.IOChannel error_out;
string dir;
string command;
Pid pid;
public CmdRunner(string dir, string command){
this.dir = dir;
this.command = command;
}
public void run(){
int standard_output = 0;
int standard_error = 0;
try{
Process.spawn_async_with_pipes(dir,
command.split(" "),
null,
SpawnFlags.DO_NOT_REAP_CHILD,
null,
out pid,
null,
out standard_output,
out standard_error);
}
catch(Error e){
critical("Couldn't launch command %s in the directory %s: %s", command, dir, e.message);
}
ChildWatch.add(pid, (pid, exit) => {
Process.close_pid (pid);
error_out.shutdown (false);
out_make.shutdown (false);
done(exit);
});
out_make = new GLib.IOChannel.unix_new(standard_output);
out_make.add_watch(IOCondition.IN | IOCondition.HUP, (source, condition) => {
if(condition == IOCondition.HUP){
return false;
}
string output = null;
try{
out_make.read_line(out output, null, null);
}
catch(Error e){
critical("Error in the output retrieving of %s: %s", command, e.message);
}
standard_output_str += output;
output_str += output;
standard_changed(output);
output_changed(output);
return true;
});
error_out = new GLib.IOChannel.unix_new(standard_error);
error_out.add_watch(IOCondition.IN | IOCondition.HUP, (source, condition) => {
if(condition == IOCondition.HUP){
return false;
}
string output = null;
try{
error_out.read_line(out output, null, null);
}
catch(Error e){
critical("Error in the output retrieving of %s: %s", command, e.message);
}
error_output_str += output;
output_str += output;
error_changed(output);
output_changed(output);
return true;
});
}
}

View File

@ -6,6 +6,7 @@ public class Tootle.NetManager : GLib.Object{
public abstract signal void started();
public abstract signal void finished();
public abstract signal void refresh();
private int requests_processing = 0;
private Soup.Session session;

View File

@ -35,7 +35,7 @@ public abstract class Tootle.AbstractView : Gtk.ScrolledWindow {
public void clear (){
max_id = -1;
view.forall (widget => view.remove (widget));
view.forall (widget => widget.destroy ());
pre_construct ();
}

View File

@ -11,6 +11,7 @@ public class Tootle.HomeView : Tootle.AbstractView {
view.remove.connect (on_remove);
Tootle.accounts.switched.connect(on_account_changed);
Tootle.network.refresh.connect(on_refresh);
// var s = new Status(1);
// s.content = "Test content, wow!";
@ -81,10 +82,14 @@ public class Tootle.HomeView : Tootle.AbstractView {
});
}
public virtual void on_refresh (){
clear ();
request ();
}
public virtual void on_account_changed (Account? account){
if(account == null)
return;
clear ();
request ();
}

View File

@ -8,6 +8,7 @@ public class Tootle.NotificationsView : Tootle.AbstractView {
view.remove.connect (on_remove);
Tootle.accounts.switched.connect(on_account_changed);
Tootle.network.refresh.connect(on_refresh);
}
public override string get_icon () {
@ -37,10 +38,20 @@ public class Tootle.NotificationsView : Tootle.AbstractView {
image.icon_name = get_icon ();
}
public virtual void on_refresh (){
clear ();
request ();
}
public virtual void on_account_changed (Account? account){
if(account == null)
return;
clear ();
request ();
}
public void request (){
var url = Tootle.settings.instance_url;
url += "/api/v1/notifications";

View File

@ -7,6 +7,7 @@ public class Tootle.AccountsButton : Gtk.MenuButton{
Gtk.Popover menu;
AccountView default_account;
Gtk.ModelButton item_settings;
Gtk.ModelButton item_refresh;
Gtk.ModelButton item_favs;
private class AccountView : Gtk.Grid{
@ -51,6 +52,10 @@ public class Tootle.AccountsButton : Gtk.MenuButton{
var item_separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
item_separator.hexpand = true;
item_refresh = new Gtk.ModelButton ();
item_refresh.text = _("Refresh");
item_refresh.clicked.connect (() => Tootle.network.refresh ());
item_favs = new Gtk.ModelButton ();
item_favs.text = _("Favorites");
item_favs.clicked.connect (() => Tootle.window.open_secondary_view (new FavoritesView ()));
@ -63,8 +68,10 @@ public class Tootle.AccountsButton : Gtk.MenuButton{
grid.width_request = 200;
grid.attach(default_account, 0, 1, 1, 1);
grid.attach(item_separator, 0, 2, 1, 1);
grid.attach(item_favs, 0, 3, 1, 1);
//grid.attach(item_settings, 0, 4, 1, 1);
grid.attach(item_favs, 0, 4, 1, 1);
grid.attach(new Gtk.Separator (Gtk.Orientation.HORIZONTAL), 0, 5, 1, 1);
grid.attach(item_refresh, 0, 6, 1, 1);
grid.attach(item_settings, 0, 7, 1, 1);
grid.show_all ();
menu = new Gtk.Popover (null);

View File

@ -7,13 +7,13 @@ public class Tootle.StatusWidget : Gtk.EventBox {
public int64? date_utc;
public int avatar_size;
public Gtk.Separator? separator;
public Granite.Widgets.Avatar avatar;
public Gtk.Label title_user;
public Gtk.Label title_date;
public Gtk.Label title_acct;
public Gtk.Revealer revealer;
public Tootle.RichLabel content;
public Gtk.Separator? separator;
public Gtk.Label? spoiler_content;
Gtk.Box title_box;
Gtk.Grid grid;
@ -178,7 +178,7 @@ public class Tootle.StatusWidget : Gtk.EventBox {
// Version >=2.56 provides DateTime.from_iso8601
public void parse_date_iso8601 (string date){
var cmd = "date -d " + date + " +%s";
var runner = new Granite.Services.SimpleCommand ("/bin/", cmd);
var runner = new CmdRunner ("/bin/", cmd); //Workaround for Granite SimpleCommand pipes bug
runner.done.connect (exit => {
date_utc = int64.parse (runner.standard_output_str);
var date_obj = new GLib.DateTime.from_unix_local (date_utc);