[gnome-contacts] Add support to make calls



commit 0767f457d527bd6ebb88b9445674ec76aad03a88
Author: Raul Gutierrez Segales <rgs collabora co uk>
Date:   Sat Aug 27 16:53:56 2011 +0100

    Add support to make calls
    
    If the contact has a phone number and if we have an
    account that supports the tel URI (i.e.: it can make calls),
    we enable calling the contact.

 src/contacts-contact-pane.vala |   11 ++++++++++
 src/contacts-store.vala        |   41 ++++++++++++++++++++++++++++++++++++++++
 src/contacts-utils.vala        |   28 +++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 0 deletions(-)
---
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 82d3923..1ae8e90 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -1239,6 +1239,17 @@ public class Contacts.ContactPane : EventBox {
       var phone = p as PhoneFieldDetails;
       var type = TypeSet.phone.format_type (phone);
       fields_layout.add_label_detail (type, phone.value);
+      if (this.contacts_store.can_call) {
+	  Button? button = fields_layout.add_button (null);
+	  var phone_image = new Image ();
+	  phone_image.set_no_show_all (true);
+	  phone_image.set_from_icon_name ("phone-symbolic", IconSize.MENU);
+	  phone_image.show ();
+	  button.add (phone_image);
+	  button.clicked.connect ( () => {
+		Utils.start_call (phone.value, this.contacts_store.calling_accounts);
+	      });
+	}
     }
 
     var postals = contact.individual.postal_addresses;
diff --git a/src/contacts-store.vala b/src/contacts-store.vala
index 232a8ae..13a2e16 100644
--- a/src/contacts-store.vala
+++ b/src/contacts-store.vala
@@ -20,6 +20,7 @@
 using Gtk;
 using Folks;
 using Gee;
+using TelepathyGLib;
 
 public class Contacts.Store : GLib.Object {
   public signal void changed (Contact c);
@@ -29,6 +30,14 @@ public class Contacts.Store : GLib.Object {
   public IndividualAggregator aggregator { get; private set; }
   Gee.ArrayList<Contact> contacts;
 
+  public Gee.HashMap<string, Account> calling_accounts;
+
+  public bool can_call {
+    get {
+      return this.calling_accounts.size > 0 ? true : false;
+    }
+  }
+
   public Store () {
     contacts = new Gee.ArrayList<Contact>();
 
@@ -77,6 +86,8 @@ public class Contacts.Store : GLib.Object {
 	}
       });
     aggregator.prepare ();
+
+    check_call_capabilities ();
   }
 
   private void contact_changed_cb (Contact c) {
@@ -135,4 +146,34 @@ public class Contacts.Store : GLib.Object {
 
     removed (c);
   }
+
+  // TODO: listen for changes in Account#URISchemes
+  private async void check_call_capabilities () {
+    this.calling_accounts = new Gee.HashMap<string, Account> ();
+    var account_manager = AccountManager.dup ();
+    yield account_manager.prepare_async (null);
+
+    account_manager.account_enabled.connect (this.check_account_caps);
+    account_manager.account_disabled.connect (this.check_account_caps);
+
+    foreach (var account in account_manager.get_valid_accounts ()) {
+      yield this.check_account_caps (account);
+    }
+  }
+
+  private async void check_account_caps (Account account) {
+    GLib.Quark addressing = Account.get_feature_quark_addressing ();
+    if (!account.is_prepared (addressing)) {
+      GLib.Quark[] features = { addressing };
+      yield account.prepare_async (features);
+    }
+
+    var k = account.get_object_path ();
+    if (account.is_enabled () &&
+	account.associated_with_uri_scheme ("tel")) {
+      this.calling_accounts.set (k, account);
+    } else {
+      this.calling_accounts.unset (k);
+    }
+  }
 }
diff --git a/src/contacts-utils.vala b/src/contacts-utils.vala
index e266042..fb6ab9a 100644
--- a/src/contacts-utils.vala
+++ b/src/contacts-utils.vala
@@ -20,6 +20,7 @@
 using Gtk;
 using Folks;
 using Gee;
+using TelepathyGLib;
 
 public class Contacts.Utils : Object {
   public static void compose_mail (string email) {
@@ -43,6 +44,33 @@ public class Contacts.Utils : Object {
     request.ensure_channel_async.begin ("org.freedesktop.Telepathy.Client.Empathy.Chat", null);
   }
 
+  public static void start_call (string contact_id,
+      Gee.HashMap<string, Account> accounts) {
+    // TODO: prompt for which account to use
+    var account = accounts.values.to_array ()[0];
+    Utils.start_call_with_account (contact_id, account);
+  }
+
+  public static void start_call_with_account (string contact_id,
+      Account account) {
+    var request_dict = new HashTable<weak string,GLib.Value?>(str_hash,
+	str_equal);
+
+    request_dict.insert (TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE,
+	TelepathyGLib.IFACE_CHANNEL_TYPE_STREAMED_MEDIA);
+    request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE,
+	(int) TelepathyGLib.HandleType.CONTACT);
+    request_dict.insert (TelepathyGLib.PROP_CHANNEL_TARGET_ID, contact_id);
+    request_dict.insert (
+	TelepathyGLib.PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO,
+	true);
+
+    var request = new TelepathyGLib.AccountChannelRequest(account,
+	request_dict, int64.MAX);
+    request.ensure_channel_async.begin (
+	"org.freedesktop.Telepathy.Client.Empathy.Call", null);
+  }
+
   public static T? get_first<T> (Collection<T> collection) {
     var i = collection.iterator();
     if (i.next())



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]