Clean up contacts-utils.vala

* Remove some dead code
* Always log in case of an error (use `debug()` for that reason)
* Clean up some mixed indent
This commit is contained in:
Niels De Graef 2017-07-18 13:14:36 +02:00
parent 51de2b085b
commit 4a1b2ff85d

View file

@ -18,7 +18,6 @@
using Gtk; using Gtk;
using Folks; using Folks;
using Gee; using Gee;
using TelepathyGLib;
using DBus; using DBus;
using GLib; using GLib;
using Gdk; using Gdk;
@ -49,8 +48,7 @@ namespace Contacts {
return provider; return provider;
} }
public void add_separator (ListBoxRow row, public void add_separator (ListBoxRow row, ListBoxRow? before_row) {
ListBoxRow? before_row) {
row.set_header (new Separator (Orientation.HORIZONTAL)); row.set_header (new Separator (Orientation.HORIZONTAL));
} }
@ -58,8 +56,8 @@ namespace Contacts {
interface FreedesktopApplication : Object { interface FreedesktopApplication : Object {
[DBus (name = "ActivateAction")] [DBus (name = "ActivateAction")]
public abstract void ActivateAction (string action, public abstract void ActivateAction (string action,
Variant[] parameter, Variant[] parameter,
HashTable<string, Variant> data) throws IOError; HashTable<string, Variant> data) throws IOError;
} }
public void activate_action (string app_id, public void activate_action (string app_id,
@ -78,10 +76,9 @@ namespace Contacts {
context.set_timestamp (timestamp); context.set_timestamp (timestamp);
Variant[] param_array = {}; Variant[] param_array = {};
if (parameter != null) { if (parameter != null)
param_array += parameter; param_array += parameter;
}
var startup_id = context.get_startup_notify_id (info, var startup_id = context.get_startup_notify_id (info,
new GLib.List<File>()); new GLib.List<File>());
var data = new HashTable<string, Variant>(str_hash, str_equal); var data = new HashTable<string, Variant>(str_hash, str_equal);
@ -146,19 +143,24 @@ public class Center : Bin {
public class Contacts.Utils : Object { public class Contacts.Utils : Object {
public static void compose_mail (string email) { public static void compose_mail (string email) {
var mailto_uri = "mailto:" + Uri.escape_string (email, "@" , false);
try { try {
Gtk.show_uri_on_window (null, "mailto:" + Uri.escape_string (email, "@" , false), 0); Gtk.show_uri_on_window (null, mailto_uri, 0);
} catch { } catch (Error e) {
debug ("Couldn't launch URI \"%s\": %s", mailto_uri, e.message);
} }
} }
public static void start_chat (Contact contact, string protocol, string id) { public static void start_chat (Contact contact, string protocol, string id) {
var im_persona = contact.find_im_persona (protocol, id); var im_persona = contact.find_im_persona (protocol, id);
var account = (im_persona.store as Tpf.PersonaStore).account; var account = (im_persona.store as Tpf.PersonaStore).account;
var request_dict = new HashTable<weak string,GLib.Value?>(str_hash, str_equal); var request_dict = new HashTable<weak string, Value?>(str_hash, str_equal);
request_dict.insert (TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE, TelepathyGLib.IFACE_CHANNEL_TYPE_TEXT); request_dict.insert (TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE,
request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE, (int) TelepathyGLib.HandleType.CONTACT); TelepathyGLib.IFACE_CHANNEL_TYPE_TEXT);
request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_ID, id); request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE,
(int) TelepathyGLib.HandleType.CONTACT);
request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_ID,
id);
// TODO: Should really use the event time like: // TODO: Should really use the event time like:
// tp_user_action_time_from_x11(gtk_get_current_event_time()) // tp_user_action_time_from_x11(gtk_get_current_event_time())
@ -167,30 +169,26 @@ public class Contacts.Utils : Object {
} }
public static void start_call (string contact_id, public static void start_call (string contact_id,
Gee.HashMap<string, Account> accounts) { Gee.HashMap<string, TelepathyGLib.Account> accounts) {
// TODO: prompt for which account to use // TODO: prompt for which account to use
var account = accounts.values.to_array ()[0]; var account = accounts.values.to_array ()[0];
Utils.start_call_with_account (contact_id, account); Utils.start_call_with_account (contact_id, account);
} }
public static void start_call_with_account (string contact_id, public static void start_call_with_account (string contact_id, TelepathyGLib.Account account) {
Account account) { var request_dict = new HashTable<weak string,GLib.Value?>(str_hash, str_equal);
var request_dict = new HashTable<weak string,GLib.Value?>(str_hash,
str_equal);
request_dict.insert (TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE, request_dict.insert (TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE,
TelepathyGLib.IFACE_CHANNEL_TYPE_CALL); TelepathyGLib.IFACE_CHANNEL_TYPE_CALL);
request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE, request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE,
(int) TelepathyGLib.HandleType.CONTACT); (int) TelepathyGLib.HandleType.CONTACT);
request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_ID, contact_id); request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_ID,
request_dict.insert ( contact_id);
TelepathyGLib.PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, request_dict.insert (TelepathyGLib.PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO,
true); true);
var request = new TelepathyGLib.AccountChannelRequest(account, var request = new TelepathyGLib.AccountChannelRequest(account, request_dict, int64.MAX);
request_dict, int64.MAX); request.ensure_channel_async.begin ("org.freedesktop.Telepathy.Client.Empathy.Call", null);
request.ensure_channel_async.begin (
"org.freedesktop.Telepathy.Client.Empathy.Call", null);
} }
public static T? get_first<T> (Collection<T> collection) { public static T? get_first<T> (Collection<T> collection) {
@ -200,33 +198,6 @@ public class Contacts.Utils : Object {
return null; return null;
} }
public static Gtk.MenuItem add_menu_item (Gtk.Menu menu, string label) {
var mi = new Gtk.MenuItem.with_label (label);
menu.append (mi);
mi.show ();
return mi;
}
public static void grid_insert_row_after (Grid grid, Widget widget, bool expand_intersecting) {
int y, h;
grid.child_get (widget,
"top-attach", out y,
"height", out h);
int start = y + h;
foreach (var child in grid.get_children ()) {
grid.child_get (child,
"top-attach", out y,
"height", out h);
if (y >= start) {
grid.child_set (child,
"top-attach", y + 1);
} else if (y + h > start && expand_intersecting) {
grid.child_set (child,
"height", h + 1);
}
}
}
public static void cairo_ellipsis (Cairo.Context cr, public static void cairo_ellipsis (Cairo.Context cr,
double xc, double yc, double xc, double yc,
double xradius, double yradius, double xradius, double yradius,
@ -298,7 +269,7 @@ public class Contacts.Utils : Object {
for (int i = 0; str.get_next_char (ref i, out c);) { for (int i = 0; str.get_next_char (ref i, out c);) {
if (!c.isspace ()) if (!c.isspace ())
return false; return false;
} }
return true; return true;
@ -311,22 +282,14 @@ public class Contacts.Utils : Object {
for (s = str; s[0] != 0; s = s.next_char ()) { for (s = str; s[0] != 0; s = s.next_char ()) {
var c = strip_char (s.get_char ()); var c = strip_char (s.get_char ());
if (c != 0) { if (c != 0) {
var size = c.fully_decompose (false, buf); var size = c.fully_decompose (false, buf);
if (size > 0) if (size > 0)
res.append_unichar (buf[0]); res.append_unichar (buf[0]);
} }
} }
return res.str; return res.str;
} }
public static void grab_widget_later (Widget widget) {
ulong id = 0;
id = widget.size_allocate.connect ( () => {
widget.grab_focus ();
widget.disconnect (id);
});
}
public static void grab_entry_focus_no_select (Entry entry) { public static void grab_entry_focus_no_select (Entry entry) {
int start, end; int start, end;
if (!entry.get_selection_bounds (out start, out end)) { if (!entry.get_selection_bounds (out start, out end)) {
@ -345,9 +308,9 @@ public class Contacts.Utils : Object {
var arg = terminal_settings.get_string("exec-arg"); var arg = terminal_settings.get_string("exec-arg");
string[] args; string[] args;
if (arg != "") if (arg != "")
args = {term, arg, exec, null}; args = {term, arg, exec, null};
else else
args = {term, exec, null}; args = {term, exec, null};
Process.spawn_async (null, args, null, SpawnFlags.SEARCH_PATH, null, null); Process.spawn_async (null, args, null, SpawnFlags.SEARCH_PATH, null, null);
} else { } else {
@ -362,21 +325,21 @@ public class Contacts.Utils : Object {
string[] args = {"evolution", "-c", "calendar", null, null}; string[] args = {"evolution", "-c", "calendar", null, null};
if (day != null) { if (day != null) {
var d = day.to_local (); var d = day.to_local ();
var today = new DateTime.now_local (); var today = new DateTime.now_local ();
args[3] = "calendar:///?startdate=%.4d%.2d%.2d".printf (today.get_year (), d.get_month (), d.get_day_of_month ()); args[3] = "calendar:///?startdate=%.4d%.2d%.2d".printf (today.get_year (), d.get_month (), d.get_day_of_month ());
} }
try { try {
Process.spawn_async (null, args, null, SpawnFlags.SEARCH_PATH, null, null); Process.spawn_async (null, args, null, SpawnFlags.SEARCH_PATH, null, null);
} } catch (Error e) {
catch { debug ("Couldn't spawn process \"%s\": %s", string.joinv(" ", args), e.message);
} }
} else { } else {
try { try {
spawn_app (calendar_settings); spawn_app (calendar_settings);
} } catch (Error e) {
catch { debug ("Couldn't spawn calendar app: %s", e.message);
} }
} }
} }
@ -388,15 +351,16 @@ public class Contacts.Utils : Object {
var path = Path.build_filename (data_dir, "pixmaps", "faces"); var path = Path.build_filename (data_dir, "pixmaps", "faces");
Dir? dir = null; Dir? dir = null;
try { try {
dir = Dir.open (path); dir = Dir.open (path);
} catch { } catch (Error e) {
debug ("Couldn't open stock avatars folder \"%s\": %s", path, e.message);
} }
if (dir != null) { if (dir != null) {
string? face; string? face;
while ((face = dir.read_name ()) != null) { while ((face = dir.read_name ()) != null) {
var filename = Path.build_filename (path, face); var filename = Path.build_filename (path, face);
files += filename; files += filename;
} }
} }
}; };
return files; return files;