Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1.  
  2. // GaGa.
  3. // A minimal radio player for the Windows Tray.
  4.  
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Windows.Forms;
  10.  
  11.  
  12. namespace GaGa
  13. {
  14. internal class StreamsMenuLoader
  15. {
  16. private readonly StreamsFile file;
  17. private readonly StreamsFileReader reader;
  18. private Nullable<DateTime> lastUpdated;
  19.  
  20. /// <summary>
  21. /// Can read a StreamsFile as an INI, monitor changes,
  22. /// and add all the sections and items to a ContextMenu
  23. /// as submenus and clickable items.
  24. /// </summary>
  25. /// <param name="file">Streams file to read from.</param>
  26. public StreamsMenuLoader(StreamsFile file)
  27. {
  28. this.file = file;
  29. this.reader = new StreamsFileReader();
  30. this.lastUpdated = null;
  31. }
  32.  
  33. /// <summary>
  34. /// Determine whether we need to reload our streams file.
  35. ///
  36. /// Returns true when the file does not exist
  37. /// (so LoadTo can recreate it on the next call)
  38. /// or when it changed since the last update.
  39. /// </summary>
  40. public Boolean MustReload()
  41. {
  42. return lastUpdated != file.GetLastWriteTime();
  43. }
  44.  
  45. /// <summary>
  46. /// Read the streams file again, adding submenus and items
  47. /// to the given ContextMenu.
  48. /// </summary>
  49. /// <param name="menu">Target context menu.</param>
  50. /// <param name="onClick">Click event to attach to menu items.</param>
  51. public void LoadTo(ContextMenu menu, EventHandler onClick)
  52. {
  53. file.CreateUnlessExists();
  54. DateTime lastWriteTime = file.GetLastWriteTime();
  55.  
  56. // Corner case:
  57. //
  58. // We made an attempt to recreate the file when needed
  59. // but it could be deleted right before calling GetLastWriteTime().
  60. //
  61. // In this case, raise an exception now, don't touch lastUpdated.
  62. // lastUpdated should only contain *valid* dates or null, so that
  63. // when the file doesn't exist, MustReload() returns true.
  64.  
  65. if (lastWriteTime.IsFileNotFound())
  66. throw new IOException("Streams file deleted during load.");
  67.  
  68. try
  69. {
  70. reader.Read(file, menu, onClick);
  71. lastUpdated = lastWriteTime;
  72. }
  73.  
  74. // update our time on StreamFileReadErrors too
  75. // because the file will still be wrong until it changes:
  76. catch (StreamsFileReadError)
  77. {
  78. lastUpdated = lastWriteTime;
  79. throw;
  80. }
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement