コード例 #1
0
        private static Menu NewMainMenu(Menu searchMenu, IContactsStorage storage)
        {
            var mainMenu = new Menu("Menu:");

            mainMenu.AddItem(new MenuItem("View all contacts", () => {
                IO.PrintContactList("All contacts", storage.GetAllContacts());
            }));

            mainMenu.AddItem(new MenuItem("Search", searchMenu.Invoke));

            mainMenu.AddItem(new MenuItem("New contact", () => {
                var newContact = IO.ReadContact();
                storage.AddContact(newContact, out string message);
                Console.WriteLine(message);
            }));

            mainMenu.AddItem(new MenuItem("Load contacts from VCard", () => {
                IO.LoadContactsFromVCard(IO.ReadString("Filename: "), storage);
            }));

            mainMenu.AddItem(new MenuItem("Save contacts to VCard", () => {
                string filename = IO.ReadString("Filename: ");
                if (File.Exists(filename))
                {
                    Console.WriteLine($"File \"{filename}\" already exists. Rewrite?");
                    if (!IO.ReadBoolean(yesByDefault: false))
                    {
                        return;
                    }
                }
                IO.SaveContactsToVCard(filename, storage);
            }));

            return(mainMenu);
        }
コード例 #2
0
        public static void LoadContactsFromVCard(string filename, IContactsStorage storage)
        {
            if (!File.Exists(filename))
            {
                Console.WriteLine($"File \"{filename}\" doesn't exist!");
                return;
            }

            // github.com/mixerp/MixERP.Net.VCards is licensed under the Apache License 2.0 - private use is allowed
            List <Contact> newContacts = Contact.ParseMany(File.ReadAllText(filename), out int addedCounter, out int totalCounter);

            foreach (var contact in newContacts)
            {
                try {
                    storage.AddContact(contact, out string message);
                    Console.WriteLine(message);
                }
                catch (AggregateException notFlattenedAe) {
                    AggregateException ae = notFlattenedAe.Flatten();
                    Console.WriteLine($"Couldn't add {contact.FullName} to contacts: {ae.InnerExceptions[ae.InnerExceptions.Count - 1].Message}");
                    addedCounter--;
                }
                catch (HttpRequestException e) {
                    Console.WriteLine($"Couldn't add {contact.FullName} to contacts: {e.Message}");
                    addedCounter--;
                }
            }

            Console.WriteLine(ComposeSummaryString("loaded", addedCounter, totalCounter));
        }
コード例 #3
0
        private static Menu NewSearchMenu(IContactsStorage storage)
        {
            var searchMenu = new Menu("Search by:");

            foreach (var fieldKind in (Contact.FieldKind[]) typeof(Contact.FieldKind).GetEnumValues())
            {
                searchMenu.AddItem(new MenuItem(Contact.GetFieldKindName(fieldKind), () => {
                    SearchByField(fieldKind, storage);
                }));
            }

            searchMenu.AddItem(new MenuItem("Back", () => { return; }));

            return(searchMenu);
        }
コード例 #4
0
        private static void SearchByField(Contact.FieldKind fieldKind, IContactsStorage storage)
        {
            string query;

            if (fieldKind != Contact.FieldKind.Birthday)
            {
                query = IO.ReadString(Contact.GetFieldKindName(fieldKind) + " substring: ");
            }
            else
            {
                query = IO.ReadBirthday();
            }

            IO.PrintContactList(
                searchResultsHeader,
                storage.FindByField(fieldKind, query)
                );
        }
コード例 #5
0
 public static void SaveContactsToVCard(string filename, IContactsStorage storage)
 {
     try {
         File.WriteAllText(filename, Contact.ToVCardMany(storage.GetAllContacts()));
         Console.WriteLine("Done.");
     }
     catch (Exception e) when(
         e is ArgumentException ||
         e is PathTooLongException ||
         e is DirectoryNotFoundException ||
         e is IOException ||
         e is UnauthorizedAccessException ||
         e is NotSupportedException
         )
     {
         Console.WriteLine($"Error! {e.Message}");
     }
     catch (AggregateException notFlattenedAe) {
         AggregateException ae = notFlattenedAe.Flatten();
         Console.WriteLine($"Couldn't fetch contacts: {ae.InnerExceptions[ae.InnerExceptions.Count - 1].Message}");
     }
 }