void Initialize() { myNoteConfiguraiton = new ApplicationConfiguration(); xmlInteraction = new XMLInteraction(myNoteConfiguraiton); if (null != xmlInteraction && Common.DesktopApplicaiton) { Sync.CheckSyncRequired(myNoteConfiguraiton.SyncFile, ref xmlInteraction); } UpdateTreeView(); if (treeView1.Nodes.Count > 0) { string title; string note; string lastUpdated; if (xmlInteraction.GetNodeDetails(treeView1.Nodes[0].Name, out title, out note, out lastUpdated)) { noteForm = new NoteForm(treeView1.Nodes[0].Name, title, note, lastUpdated); } else { noteForm = new NoteForm(); } } else { CreateNote(); } ShowNoteForm(); }
public ApplicationConfiguration() { if (!File.Exists(Common.MyNoteAppConfigFile)) { XMLInteraction.CreateNoteFile(Common.MyNoteAppConfigFile, Common.DefaultConfigString); } ReadConfigFile(); }
public static void CheckSyncRequired(string file, ref XMLInteraction xmlInteraction) { string filePath = Path.GetFullPath(file); string fileName = Path.GetFileName(filePath); filePath = Path.GetDirectoryName(filePath); // Create a new FileSystemWatcher and set its properties. FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = filePath; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite; watcher.Filter = fileName; watcher.Changed += new FileSystemEventHandler(xmlInteraction.OnlineChangesObserved); watcher.EnableRaisingEvents = true; }
// Sync with another file public void syncWithCloud() { while (true) { try { bool needSync = false; //sleep till sync interval specified for (int i = 0; i < AppConf.SyncInteval; i++) { Thread.Sleep(1000); lock (thisOnlineSyncLock) { //If user requested for force update quit sleep and sync if (ForceSyncRequired) { needSync = true; break; } } } if (!needSync) { // check sync required lock (thisOnlineSyncLock) { if (OnlineSyncRequired) { needSync = true; } } } if (needSync) { Logger.GetInstance.LogMessage(System.Reflection.MethodBase.GetCurrentMethod().Name, "Performing sync"); if (File.Exists(AppConf.SyncFile)) { //lock the object such that no updates made to xml lock (thisXMLLock) { using (Mutex mutex = new Mutex(false, AppConf.SyncFile)) { if (!mutex.WaitOne(0, true)) { Logger.GetInstance.LogMessage(System.Reflection.MethodBase.GetCurrentMethod().Name, "Couldn't aquire sync file lock. Sync delayed till next cycle."); } else { XMLInteraction syncXMLInteraction = new XMLInteraction(AppConf.SyncFile); //check for common note changes and local changes foreach (XmlElement changeElement in xmlTrackChangesNode) { bool useLocalToCloud = true; XmlElement changeElementInClound = syncXMLInteraction.xmlTrackChangesNode[changeElement.Name]; if (null != changeElementInClound) { DateTime localTime, cloudTime; DateTime.TryParse(changeElement.GetAttribute(Common.XmlTimeNodeString), out localTime); DateTime.TryParse(changeElementInClound.GetAttribute(Common.XmlTimeNodeString), out cloudTime); if (DateTime.Compare(localTime, cloudTime) < 0) { useLocalToCloud = false; } else { syncXMLInteraction.xmlTrackChangesNode.RemoveChild(changeElementInClound); } } if (useLocalToCloud) { Operation op; Enum.TryParse <Operation>(changeElement.GetAttribute(Common.XmlOperationNodeString), out op); XmlNode tempNode = xmlNotesNode[changeElement.Name]; DateTime tempDateTime; DateTime.TryParse(tempNode[Common.XmlLastUpdatedNodeString].InnerText, out tempDateTime); Note tempNote = new Note(op, changeElement.Name, tempDateTime, tempNode[Common.XmlTitleNodeString].InnerText, tempNode[Common.XmlNoteNodeString].InnerText ); syncXMLInteraction.PushChangesToXML(tempNote, false); } else { Operation op; Enum.TryParse <Operation>(changeElementInClound.GetAttribute(Common.XmlOperationNodeString), out op); XmlNode tempNode = syncXMLInteraction.xmlNotesNode[changeElementInClound.Name]; DateTime tempDateTime; DateTime.TryParse(tempNode[Common.XmlLastUpdatedNodeString].InnerText, out tempDateTime); Note tempNote = new Note(op, changeElement.Name, tempDateTime, tempNode[Common.XmlTitleNodeString].InnerText, tempNode[Common.XmlNoteNodeString].InnerText ); PushChangesToXML(tempNote, false); } } xmlTrackChangesNode.RemoveAll(); //retrive could changes and sync foreach (XmlElement changeElementInClound in syncXMLInteraction.xmlTrackChangesNode) { Operation op; Enum.TryParse <Operation>(changeElementInClound.GetAttribute(Common.XmlOperationNodeString), out op); XmlNode tempNode = syncXMLInteraction.xmlNotesNode[changeElementInClound.Name]; DateTime tempDateTime; DateTime.TryParse(tempNode[Common.XmlLastUpdatedNodeString].InnerText, out tempDateTime); Note tempNote = new Note(op, changeElementInClound.Name, tempDateTime, tempNode[Common.XmlTitleNodeString].InnerText, tempNode[Common.XmlNoteNodeString].InnerText ); PushChangesToXML(tempNote, false); } syncXMLInteraction.xmlTrackChangesNode.RemoveAll(); //save xml files SaveXML(); syncXMLInteraction.SaveXML(); } mutex.ReleaseMutex(); } } //reset sync variables lock (thisOnlineSyncLock) { ForceSyncRequired = false; OnlineSyncRequired = false; } } } } catch (Exception ex) { Logger.GetInstance.LogMessage(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Data.ToString()); } } }