/// <summary> /// Adds a url to the user's saved favourites list. /// Will not work if name entered is a duplicate. /// </summary> /// <param name="name">The name to be associated with this favourite</param> /// <param name="url">THe url to be favourited</param> public static void AddFavourite(String name, String url) { if (!Favourites.ContainsKey(name)) { try { using (StreamWriter sw = File.AppendText(savedPagesFileName)) { sw.WriteLine("f," + name + "," + url); } logBox.Text = "Favourite added with name: " + name; //Add favourite within program before writing to file. Ensures user doesn't see update in program but changes are not saved. Favourites.Add(name, url); foreach (TabUserControl tab in AllTabs) { tab.RefreshFavouritesBox(); } } catch (IOException ioe) { logBox.Text = "Failed to add favourite, error saving data."; } catch (Exception e) { logBox.Text = "Failed to add favourite, unknown error." + e.Message;; } } else { logBox.Text = "You tried to add a favourite with a duplicate name. Favourite not added. Name: " + name; } }
/// <summary> /// Removes one or more favourites from the user's current list. /// </summary> /// <param name="names">A list of the names of all favourites to remove</param> public static void RemoveFavourites(List <string> names) { if (names == null) { logBox.Text = "Error removing favourites."; return; } foreach (string name in names) { if (Favourites.ContainsKey(name)) { Favourites.Remove(name); } } foreach (TabUserControl tab in AllTabs) { tab.RefreshFavouritesBox(); } try { using (StreamWriter sw = new StreamWriter(savedPagesFileName)) { sw.WriteLine("h," + homeUrl); //Save homepage foreach (KeyValuePair <string, string> favourite in Favourites) { sw.WriteLine("f," + favourite.Key + "," + favourite.Value); } } }catch (IOException ioe) { logBox.Text = "Error removing favourites from saved data."; } catch (Exception e) { logBox.Text = "Unexpected error removing favourites." + e.Message; } }
/// <summary> /// Loads the user's favourites from a file if there is one. /// </summary> private void LoadFavourites() { if (File.Exists(savedPagesFileName)) //Check if the file where home page is stored exists { using (StreamReader sr = new StreamReader(savedPagesFileName)) //Read through the file of saved pages { string line = ""; while ((line = sr.ReadLine()) != null) { String[] parts = line.Split(','); if (parts.Length == 3 && parts[0].Equals("f")) //If a saved favourite { if (!Favourites.ContainsKey(parts[1])) { Favourites.Add(parts[1], parts[2]); } } } RefreshFavouritesBox(); } } }