Merge pull request #11 from martensitingale/master

don't spawn a subprocess to parse dates
This commit is contained in:
Bleak Grey 2018-05-17 19:43:39 +03:00 committed by GitHub
commit b9c0489019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 105 deletions

View File

@ -24,7 +24,6 @@ executable(
'src/AccountManager.vala',
'src/NetManager.vala',
'src/Utils.vala',
'src/CmdRunner.vala',
'src/Notificator.vala',
'src/API/Account.vala',
'src/API/Relationship.vala',

View File

@ -4,7 +4,6 @@ src/SettingsManager.vala
src/AccountManager.vala
src/NetManager.vala
src/Utils.vala
src/CmdRunner.vala
src/API/StatusVisibility.vala
src/API/NotificationType.vala
src/API/Attachment.vala

View File

@ -1,91 +0,0 @@
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

@ -199,7 +199,9 @@ public class Tootle.StatusWidget : Gtk.EventBox {
title_acct.label = "@" + formal.account.acct;
content_label.label = formal.content;
content_label.mentions = formal.mentions;
parse_date_iso8601 (formal.created_at);
var datetime = parse_date_iso8601 (formal.created_at);
title_date.label = Granite.DateTime.get_relative_datetime (datetime);
reblogs.label = formal.reblogs_count.to_string ();
favorites.label = formal.favourites_count.to_string ();
@ -224,17 +226,12 @@ public class Tootle.StatusWidget : Gtk.EventBox {
return status.get_formal ().spoiler_text != null || status.get_formal ().sensitive;
}
// elementary OS has old GLib, so I have to improvise
// Version >=2.56 provides DateTime.from_iso8601
public void parse_date_iso8601 (string date) {
var cmd = "date -d " + date + " +%s";
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);
title_date.label = Granite.DateTime.get_relative_datetime (date_obj);
});
runner.run ();
private GLib.DateTime? parse_date_iso8601 (string date) {
var timeval = GLib.TimeVal();
if (timeval.from_iso8601 (date)) {
return new GLib.DateTime.from_timeval_local (timeval);
}
return null;
}
public bool on_avatar_clicked () {