Add copyright to source code
[moonshot-ui.git] / src / moonshot-identities-manager.vala
index cc57f43..a0506d8 100644 (file)
@@ -1,3 +1,34 @@
+/*
+ * Copyright (c) 2011-2014, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+*/
 using Gee;
 
 public class Password {
@@ -85,7 +116,8 @@ public class IdentityManagerModel : Object {
     public bool display_name_is_valid (string name,
                                        out string? candidate)
     {
-        candidate = null;
+        if (&candidate != null)
+          candidate = null;
         foreach (IdCard id_card in this.get_card_list())
         {
           if (id_card.display_name == name)
@@ -108,6 +140,44 @@ public class IdentityManagerModel : Object {
         return true;
     }
 
+    private bool remove_duplicates(IdCard card)
+    {
+        bool duplicate_found = false;
+        bool found = false;
+        do {
+           var cards = get_card_list();
+           found = false;
+           foreach (IdCard id_card in cards) {
+               if ((card != id_card) && (id_card.nai == card.nai)) {
+                  stdout.printf("removing duplicate id for '%s'\n", card.nai);
+                  remove_card_internal(id_card);
+                  found = duplicate_found = true;
+                  break;
+               }
+           }
+        } while (found);
+        return duplicate_found;
+    }
+
+    public IdCard? find_id_card(string nai, bool force_flat_file_store) {
+        IdCard? retval = null;
+        IIdentityCardStore.StoreType saved_store_type = get_store_type();
+        if (force_flat_file_store)
+            set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
+
+        foreach (IdCard id in get_card_list()) {
+            if (id.nai == nai) {
+                retval = id;
+                break;
+            }
+        }
+        set_store_type(saved_store_type);
+        if (force_flat_file_store && 
+            (saved_store_type != IIdentityCardStore.StoreType.FLAT_FILE))
+            card_list_changed();
+        return retval;
+    }
+
     public void add_card(IdCard card, bool force_flat_file_store) {
         if (card.temporary)
             return;
@@ -118,6 +188,8 @@ public class IdentityManagerModel : Object {
         if (force_flat_file_store)
             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
 
+        remove_duplicates(card);
+
         if (!display_name_is_valid (card.display_name, out candidate))
         {
           card.display_name = candidate;
@@ -146,10 +218,19 @@ public class IdentityManagerModel : Object {
         return retval;
      }
 
-     public void remove_card(IdCard card) {
-        password_table.RemovePassword(card, store);
-        store.remove_card(card);
-        card_list_changed();
+     private bool remove_card_internal(IdCard card) {
+         if (card.temporary)
+             return false;
+         password_table.RemovePassword(card, store);
+         return store.remove_card(card);
+     }
+
+     public bool remove_card(IdCard card) {
+         if (remove_card_internal(card)) {
+            card_list_changed();
+            return true;
+         }
+         return false;
      }
 
      public void set_store_type(IIdentityCardStore.StoreType type) {