Major overhaul #2
@@ -9,6 +9,7 @@ using System.Reflection;
 | 
				
			|||||||
using System.Text;
 | 
					using System.Text;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using System.Windows.Forms;
 | 
					using System.Windows.Forms;
 | 
				
			||||||
 | 
					using Newtonsoft.Json;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace ntfysh_client
 | 
					namespace ntfysh_client
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -94,33 +95,84 @@ namespace ntfysh_client
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        private string GetTopicsFilePath()
 | 
					        private string GetTopicsFilePath()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            string binaryDirectory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 | 
					            string binaryDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 | 
				
			||||||
            return Path.Combine(binaryDirectory, "topics.txt");
 | 
					            return Path.Combine(binaryDirectory ?? throw new InvalidOperationException("Unable to determine path for topics file"), "topics.json");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        private string GetLegacyTopicsFilePath()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            string binaryDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
 | 
				
			||||||
 | 
					            return Path.Combine(binaryDirectory ?? throw new InvalidOperationException("Unable to determine path for legacy topics file"), "topics.txt");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void SaveTopicsToFile()
 | 
					        private void SaveTopicsToFile()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            using (StreamWriter writer = new StreamWriter(GetTopicsFilePath()))
 | 
					            string topicsSerialised = JsonConvert.SerializeObject(notificationListener.SubscribedTopics.Select(st => st.Value).ToList(), Formatting.Indented);
 | 
				
			||||||
            {
 | 
					            
 | 
				
			||||||
                foreach (string topic in notificationTopics.Items)
 | 
					            File.WriteAllText(GetTopicsFilePath(), topicsSerialised);
 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    writer.WriteLine(topic);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void LoadTopics()
 | 
					        private void LoadTopics()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (!File.Exists(GetTopicsFilePath())) return;
 | 
					            string legacyTopicsPath = GetLegacyTopicsFilePath();
 | 
				
			||||||
            using (StreamReader reader = new StreamReader(GetTopicsFilePath()))
 | 
					            string topicsFilePath = GetTopicsFilePath();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //If we have an old format topics file. Convert it to the new format!
 | 
				
			||||||
 | 
					            if (File.Exists(legacyTopicsPath))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                //Read old format
 | 
				
			||||||
 | 
					                List<string> legacyTopics = new List<string>();
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                using (StreamReader reader = new StreamReader(legacyTopicsPath))
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    while (!reader.EndOfStream)
 | 
					                    while (!reader.EndOfStream)
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                    var topic = reader.ReadLine();
 | 
					                        string legacyTopic = reader.ReadLine();
 | 
				
			||||||
                    notificationListener.SubscribeToTopic(topic, "https://ntfy.sh", null, null);
 | 
					                        legacyTopics.Add(legacyTopic);
 | 
				
			||||||
                    notificationTopics.Items.Add(topic);
 | 
					 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                //Assemble new format
 | 
				
			||||||
 | 
					                List<SubscribedTopic> newTopics = legacyTopics.Select(lt => new SubscribedTopic(lt, "https://ntfy.sh", null, null, null)).ToList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                string newFormatSerialised = JsonConvert.SerializeObject(newTopics, Formatting.Indented);
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                //Write new format
 | 
				
			||||||
 | 
					                File.WriteAllText(topicsFilePath, newFormatSerialised);
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                //Delete old format
 | 
				
			||||||
 | 
					                File.Delete(legacyTopicsPath);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            //Check if we have any topics file on disk to load
 | 
				
			||||||
 | 
					            if (!File.Exists(topicsFilePath)) return;
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            //We have a topics file. Load it!
 | 
				
			||||||
 | 
					            string topicsSerialised = File.ReadAllText(topicsFilePath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //Check if the file is empty
 | 
				
			||||||
 | 
					            if (string.IsNullOrWhiteSpace(topicsSerialised))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                //The file is empty. May as well remove it and consider it nonexistent
 | 
				
			||||||
 | 
					                File.Delete(topicsFilePath);
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //Deserialise the topics
 | 
				
			||||||
 | 
					            List<SubscribedTopic> topics = JsonConvert.DeserializeObject<List<SubscribedTopic>>(topicsSerialised);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (topics == null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                //TODO Deserialise error!
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            //Load them in
 | 
				
			||||||
 | 
					            foreach (SubscribedTopic topic in topics)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                notificationListener.SubscribeToTopic(topic.TopicId, topic.ServerUrl, topic.Username, topic.Password);
 | 
				
			||||||
 | 
					                notificationTopics.Items.Add(topic.TopicId);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void OnNotificationReceive(object sender, NotificationReceiveEventArgs e)
 | 
					        private void OnNotificationReceive(object sender, NotificationReceiveEventArgs e)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
 | 
					using Newtonsoft.Json;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace ntfysh_client
 | 
					namespace ntfysh_client
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -18,6 +19,7 @@ namespace ntfysh_client
 | 
				
			|||||||
        public string Username { get; }
 | 
					        public string Username { get; }
 | 
				
			||||||
        public string Password { get; }
 | 
					        public string Password { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [JsonIgnore]
 | 
				
			||||||
        public StreamReader Stream { get; }
 | 
					        public StreamReader Stream { get; }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user