Implement full service matching algorithm
[moonshot-ui.git] / src / moonshot-window.vala
index d5de70f..5522953 100644 (file)
@@ -7,7 +7,7 @@ class MainWindow : Window
 
     private UIManager ui_manager = new UIManager();
     private Entry search_entry;
-    private VBox vbox_rigth;
+    private VBox vbox_right;
     private CustomVBox custom_vbox;
     private VBox services_internal_vbox;
 
@@ -17,13 +17,15 @@ class MainWindow : Window
     private ListStore listmodel;
     private TreeModelFilter filter;
 
-    private IdentitiesManager identities_manager;
+    public IdentitiesManager identities_manager;
+    private SList<IdCard>    candidates;
 
     private MoonshotServer ipc_server;
 
-    public IdCardWidget selected_id_card_widget;
+    private IdCard default_id_card;
+    public Queue<IdentityRequest> request_queue;
 
-    private Queue<IdentityRequest> request_queue;
+    private HashTable<Gtk.Button, string> service_button_map;
 
     private enum Columns
     {
@@ -53,6 +55,8 @@ class MainWindow : Window
     public MainWindow()
     {
         request_queue = new Queue<IdentityRequest>();
+        
+        service_button_map = new HashTable<Gtk.Button, string> (direct_hash, direct_equal);
 
         this.title = "Moonshoot";
         this.set_position (WindowPosition.CENTER);
@@ -60,32 +64,72 @@ class MainWindow : Window
 
         build_ui();
         setup_identities_list();
-        load_gss_eap_id_file();
-        //load_id_cards();
+        load_id_cards();
         connect_signals();
         init_ipc_server();
     }
+    
+    public void add_candidate (IdCard idcard)
+    {
+        candidates.append (idcard);
+    }
 
     private bool visible_func (TreeModel model, TreeIter iter)
     {
-        string issuer;
-        string search_text;
-        string issuer_casefold;
-        string search_text_casefold;
+        IdCard id_card;
 
         model.get (iter,
-                   Columns.ISSUER_COL, out issuer);
-        search_text = this.search_entry.get_text ();
-
-        if (issuer == null || search_text == null)
+                   Columns.IDCARD_COL, out id_card);
+        if (id_card == null)
             return false;
 
-        issuer_casefold = issuer.casefold ();
-        search_text_casefold = search_text.casefold ();
-
-        if (issuer_casefold.contains (search_text_casefold))
+        foreach (IdCard candidate in candidates)
+        {
+            if (&candidate == &id_card)
+                return true;
+        }
+        
+        string entry_text = search_entry.get_text ();
+        if (entry_text == null || entry_text == "")
+        {
             return true;
+        }
+
+        foreach (string search_text in entry_text.split(" "))
+        {
+            if (search_text == "")
+                continue;
+         
+
+            string search_text_casefold = search_text.casefold ();
 
+            if (id_card.issuer != null)
+            {
+              string issuer_casefold = id_card.issuer;
+
+              if (issuer_casefold.contains (search_text_casefold))
+                  return true;
+            }
+
+            if (id_card.display_name != null)
+            {
+                string display_name_casefold = id_card.display_name.casefold ();
+              
+                if (display_name_casefold.contains (search_text_casefold))
+                    return true;
+            }
+            
+            if (id_card.services.length > 0)
+            {
+                foreach (string service in id_card.services)
+                {
+                    string service_casefold = service.casefold ();
+
+                    if (service_casefold.contains (search_text_casefold))
+                        return true;
+                }
+            }
+        }
         return false;
     }
 
@@ -103,7 +147,7 @@ class MainWindow : Window
 
     private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
     {
-       if (pos == EntryIconPosition.PRIMARY)
+        if (pos == EntryIconPosition.PRIMARY)
         {
             print ("Search entry icon pressed\n");
         }
@@ -122,8 +166,7 @@ class MainWindow : Window
         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
 
-        this.vbox_rigth.set_visible (false);
-        this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
+        this.vbox_right.set_visible (false);
     }
 
     private bool search_entry_key_press_event_cb (Gdk.EventKey e)
@@ -136,29 +179,20 @@ class MainWindow : Window
         return false;
     }
 
-    private void load_gss_eap_id_file ()
-    {
-        IdCard id_card;
-
-        this.identities_manager = new IdentitiesManager ();
-
-        id_card = this.identities_manager.load_gss_eap_id_file ();
-        if (id_card != null)
-        {
-            add_id_card_data (id_card);
-            add_id_card_widget (id_card);
-        }
-    }
-
     private void load_id_cards ()
     {
-        this.identities_manager = new IdentitiesManager ();
+        identities_manager = new IdentitiesManager ();
+        
+        if (identities_manager.id_card_list == null)
+          return;
 
         foreach (IdCard id_card in identities_manager.id_card_list)
         {
             add_id_card_data (id_card);
             add_id_card_widget (id_card);
         }
+
+        this.default_id_card = identities_manager.id_card_list.data;
     }
 
     private void fill_details (IdCardWidget id_card_widget)
@@ -171,13 +205,14 @@ class MainWindow : Window
        foreach (var hbox in children)
            hbox.destroy();
        fill_services_vbox (id_card_widget.id_card);
+       identities_manager.store_id_cards();
     }
 
     private void show_details (IdCard id_card)
     {
-       this.vbox_rigth.set_visible (!vbox_rigth.get_visible ());
+       this.vbox_right.set_visible (!vbox_right.get_visible ());
 
-       if (this.vbox_rigth.get_visible () == false)
+       if (this.vbox_right.get_visible () == false)
        {
            this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
        }
@@ -193,26 +228,28 @@ class MainWindow : Window
     {
         var id_card = new IdCard ();
 
+        id_card.display_name = dialog.display_name;
         id_card.issuer = dialog.issuer;
         if (id_card.issuer == "")
             id_card.issuer = "Issuer";
         id_card.username = dialog.username;
         id_card.password = dialog.password;
-        id_card.nai = id_card.username + "@" + id_card.issuer;
-        id_card.pixbuf = find_icon ("avatar-default", 48);
-        id_card.services = {"email","jabber","irc"};
+        id_card.services = {};
+        id_card.set_data("pixbuf", find_icon ("avatar-default", 48));
 
         return id_card;
     }
 
     private void add_id_card_data (IdCard id_card)
     {
-        TreeIter iter;
+        TreeIter   iter;
+        Gdk.Pixbuf pixbuf;
 
         this.listmodel.append (out iter);
+        pixbuf = id_card.get_data("pixbuf");
         listmodel.set (iter,
                        Columns.IDCARD_COL, id_card,
-                       Columns.LOGO_COL, id_card.pixbuf,
+                       Columns.LOGO_COL, pixbuf,
                        Columns.ISSUER_COL, id_card.issuer,
                        Columns.USERNAME_COL, id_card.username,
                        Columns.PASSWORD_COL, id_card.password);
@@ -248,31 +285,86 @@ class MainWindow : Window
 
         id_card_widget.details_id.connect (details_identity_cb);
         id_card_widget.remove_id.connect (remove_identity_cb);
-        id_card_widget.send_id.connect (send_identity_cb);
+        id_card_widget.send_id.connect ((w) => send_identity_cb (w.id_card));
         id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
         id_card_widget.expanded.connect (fill_details);
     }
 
-    private void add_identity (AddIdentityDialog dialog)
+    /* This method finds a valid display name */
+    public bool display_name_is_valid (string name,
+                                       out string? candidate)
     {
-        var id_card = get_id_card_data (dialog);
-
+        foreach (IdCard id_card in identities_manager.id_card_list)
+        {
+          if (id_card.display_name == name)
+          {
+            if (&candidate != null)
+            {
+              for (int i=0; i<1000; i++)
+              {
+                string tmp = "%s %d".printf (name, i);
+                if (display_name_is_valid (tmp, null))
+                {
+                  candidate = tmp;
+                  break;
+                }
+              }
+            }
+            return false;
+          }
+        }
+        
+        return true;
+    }
+    
+    public void insert_id_card (IdCard id_card)
+    {
+        string candidate;
+        
+        if (!display_name_is_valid (id_card.display_name, out candidate))
+        {
+          id_card.display_name = candidate;
+        }
+    
         this.identities_manager.id_card_list.prepend (id_card);
         this.identities_manager.store_id_cards ();
-        this.identities_manager.store_gss_eap_id_file (id_card);
 
         add_id_card_data (id_card);
         add_id_card_widget (id_card);
     }
 
-    private void add_identity_cb ()
+    public bool add_identity (IdCard id_card)
+    {
+        /* TODO: Check if display name already exists */
+
+        var dialog = new Gtk.MessageDialog (this,
+                                            Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                            Gtk.MessageType.QUESTION,
+                                            Gtk.ButtonsType.YES_NO,
+                                            _("Would you like to add '%s' ID Card to the ID Card Organizer?"),
+                                            id_card.display_name);
+
+        dialog.show_all ();
+        var ret = dialog.run ();
+        dialog.hide ();
+
+        if (ret == Gtk.ResponseType.YES) {
+            id_card.set_data ("pixbuf", find_icon ("avatar-default", 48));
+            this.insert_id_card (id_card);
+            return true;
+        }
+
+        return false;
+    }
+
+    private void add_identity_manual_cb ()
     {
         var dialog = new AddIdentityDialog ();
         var result = dialog.run ();
 
         switch (result) {
         case ResponseType.OK:
-            add_identity (dialog);
+            insert_id_card (get_id_card_data (dialog));
             break;
         default:
             break;
@@ -293,7 +385,6 @@ class MainWindow : Window
 
         this.identities_manager.id_card_list.remove (id_card);
         this.identities_manager.store_id_cards ();
-        this.identities_manager.store_gss_eap_id_file (null);
 
         remove_id_card_widget (id_card_widget);
     }
@@ -342,20 +433,131 @@ class MainWindow : Window
 
     public void select_identity (IdentityRequest request)
     {
-        /* Automatic service matching rules can go here */
+        IdCard identity = null;
 
-        /* Resort to manual selection */
         this.request_queue.push_tail (request);
-        this.show ();
+        
+        if (custom_vbox.current_idcard != null &&
+            custom_vbox.current_idcard.send_button != null)
+          custom_vbox.current_idcard.send_button.set_sensitive (true);
+
+        if (request.select_default)
+        {
+            identity = default_id_card;
+        }
+
+        if (identity == null)
+        {
+            bool has_nai = request.nai != null && request.nai != "";
+            bool has_srv = request.service != null && request.service != "";
+            bool confirm = false;
+            IdCard nai_provided = null;
+            
+            foreach (IdCard id in identities_manager.id_card_list)
+            {
+                /* If NAI matches we add id card to the candidate list */
+                if (has_nai && request.nai == id.nai)
+                {
+                    nai_provided = id;
+                    add_candidate (id);
+                    continue;
+                }
+
+                /* If any service matches we add id card to the candidate list */
+                if (has_srv)
+                {
+                    foreach (string srv in id.services)
+                    {
+                        if (request.service == srv)
+                        {
+                            add_candidate (id);
+                            continue;
+                        }
+                    }
+                }
+            }
+
+            /* If more than one candidate we dissasociate service from all ids */
+            if (has_srv && candidates.length() > 1)
+            {
+                foreach (IdCard id in candidates)
+                {
+                    int i = 0;
+                    SList<string> services_list = null;
+                    foreach (string srv in id.services)
+                    {
+                        if (srv == request.service)
+                            continue;
+                        services_list.append (srv);
+                    }
+
+                    if (services_list.length () == 0)
+                    {
+                        id.services = {};
+                        continue;
+                    }
+
+                    string[] services = new string[services_list.length ()];
+                    foreach (string srv in services_list)
+                    {
+                        services[i] = srv;
+                        i++;
+                    }
+
+                    id.services = services;
+                }
+            }
+
+            /* If there are no candidates we use the service matching rules */
+            if (candidates.length () == 0)
+            {
+                foreach (IdCard id in identities_manager.id_card_list)
+                {
+                    foreach (Rule rule in id.rules)
+                    {
+                        if (!match_service_pattern (request.service, rule.pattern))
+                            continue;
+                       
+                        candidates.append (id);
+
+                        if (rule.always_confirm == "true")
+                            confirm = true;
+                    }
+                }
+            }
+            
+            if (candidates.length () > 1)
+                confirm = true;
+            else
+                identity = candidates.nth_data (0);
+            
+            
+            if (confirm)
+            {
+                filter.refilter();
+                redraw_id_card_widgets ();
+                show ();
+                return;
+            }
+        }
+        // Send back the identity (we can't directly run the
+        // callback because we may be being called from a 'yield')
+        Idle.add (() => { send_identity_cb (identity); return false; });
+        return;
+    }
+    
+    private bool match_service_pattern (string service, string pattern)
+    {
+        var pspec = new PatternSpec (pattern);
+        return pspec.match_string (service);
     }
 
-    public void send_identity_cb (IdCardWidget id_card_widget)
+    public void send_identity_cb (IdCard identity)
     {
         return_if_fail (request_queue.length > 0);
 
         var request = this.request_queue.pop_head ();
-        var identity = id_card_widget.id_card;
-        this.selected_id_card_widget = id_card_widget;
+        bool reset_password = false;
 
         if (identity.password == null)
         {
@@ -365,6 +567,7 @@ class MainWindow : Window
             switch (result) {
             case ResponseType.OK:
                 identity.password = dialog.password;
+                reset_password = ! dialog.remember;
                 break;
             default:
                 identity = null;
@@ -377,7 +580,15 @@ class MainWindow : Window
         if (this.request_queue.is_empty())
             this.hide ();
 
+        if (identity != null)
+            this.default_id_card = identity;
+
         request.return_identity (identity);
+
+        if (reset_password)
+            identity.password = null;
+
+        candidates = new SList<IdCard>();
     }
 
     private void label_make_bold (Label label)
@@ -403,6 +614,8 @@ class MainWindow : Window
         services_table.set_col_spacings (10);
         services_table.set_row_spacings (10);
         this.services_internal_vbox.add (services_table);
+        
+        service_button_map.remove_all ();
 
         foreach (string service in id_card.services)
         {
@@ -413,6 +626,50 @@ class MainWindow : Window
 #else
             var remove_button = new Button.from_stock (STOCK_REMOVE);
 #endif
+
+
+            service_button_map.insert (remove_button, service);
+            
+            remove_button.clicked.connect ((remove_button) =>
+            {
+              var dialog = new Gtk.MessageDialog (this,
+                                      Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                      Gtk.MessageType.QUESTION,
+                                      Gtk.ButtonsType.YES_NO,
+                                      _("Are you sure you want to stop '%s' ID Card to use %s?"),
+                                      custom_vbox.current_idcard.id_card.display_name);
+              var ret = dialog.run();
+              dialog.hide();
+              
+              if (ret == Gtk.ResponseType.YES)
+              {
+                IdCard idcard = custom_vbox.current_idcard.id_card;
+                var candidate = service_button_map.lookup (remove_button);
+
+                SList<string> services = new SList<string>();
+                
+                foreach (string srv in idcard.services)
+                {
+                  if (srv == candidate)
+                    continue;
+                  services.append (srv);
+                }
+                
+                idcard.services = new string[services.length()];
+                for (int j=0; j<idcard.services.length; j++)
+                {
+                  idcard.services[j] = services.nth_data(j);
+                }
+                
+                var children = services_internal_vbox.get_children ();
+                foreach (var hbox in children)
+                  hbox.destroy();
+                
+                fill_services_vbox (idcard);
+                custom_vbox.current_idcard.update_id_card_label ();
+              }
+              
+            });
             services_table.attach_defaults (label, 0, 1, i, i+1);
             services_table.attach_defaults (remove_button, 1, 2, i, i+1);
             i++;
@@ -425,6 +682,7 @@ class MainWindow : Window
         string[] authors = {
             "Javier Jardón <jjardon@codethink.co.uk>",
             "Sam Thursfield <samthursfield@codethink.co.uk>",
+            "Alberto Ruiz <alberto.ruiz@codethink.co.uk>",
             null
         };
 
@@ -492,7 +750,7 @@ SUCH DAMAGE.
                                 N_("Add ID Card"),
                                 null,
                                 N_("Add a new ID Card"),
-                                add_identity_cb };
+                                add_identity_manual_cb };
         actions += add;
         Gtk.ActionEntry quit = { "QuitAction",
 #if VALA_0_12
@@ -567,7 +825,7 @@ SUCH DAMAGE.
         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
 
-        this.custom_vbox = new CustomVBox (false, 6);
+        this.custom_vbox = new CustomVBox (this, false, 6);
 
         var viewport = new Viewport (null, null);
         viewport.set_border_width (6);
@@ -621,13 +879,13 @@ SUCH DAMAGE.
         services_vbox.pack_start (services_vbox_title, false, true, 0);
         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
 
-        this.vbox_rigth = new VBox (false, 18);
-        vbox_rigth.pack_start (login_vbox, false, true, 0);
-        vbox_rigth.pack_start (services_vbox, false, true, 0);
+        this.vbox_right = new VBox (false, 18);
+        vbox_right.pack_start (login_vbox, false, true, 0);
+        vbox_right.pack_start (services_vbox, false, true, 0);
 
         var hbox = new HBox (false, 12);
-        hbox.pack_start (vbox_left, false, false, 0);
-        hbox.pack_start (vbox_rigth, false, false, 0);
+        hbox.pack_start (vbox_left, true, true, 0);
+        hbox.pack_start (vbox_right, false, false, 0);
 
         var main_vbox = new VBox (false, 0);
         main_vbox.set_border_width (12);
@@ -637,7 +895,7 @@ SUCH DAMAGE.
         add (main_vbox);
 
         main_vbox.show_all();
-        this.vbox_rigth.hide ();
+        this.vbox_right.hide ();
     }
 
     private void set_atk_name_description (Widget widget, string name, string description)