public void LoadData() { if( !CurrentProfile.PasswordValidated ) return; foreach (ICryptoEditor plugin in plugins) { CryptoXML xmlRoot = new CryptoXML(); string filename = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\CryptoEditor\" + CurrentProfile.Id + "." + plugin.ToString().ToLower() + ".data"; try { xmlRoot.Load(filename); } catch (System.IO.FileNotFoundException) { try { xmlRoot.Load(filename); } catch(System.IO.FileNotFoundException) { plugin.Load(null); continue; } } if (xmlRoot.DocumentElement.Name.Equals("CryptoEditorDefault")) { // This is text only data or a not well formed XML document #if !NO_ENCRYPT xmlRoot.Decrypt("/*", true); #endif plugin.Load(xmlRoot.DocumentElement.InnerText); continue; } xmlRoot.Password = CurrentProfile.Password; #if !NO_ENCRYPT xmlRoot.Decrypt(plugin.EncryptionXPath, true); #endif plugin.Load(xmlRoot.OuterXml); filename = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\CryptoEditor\" + CurrentProfile.Id + "." + plugin.ToString().ToLower() + ".properties"; plugin.LoadProperties(filename); } }
public void Synchronize(string serverPassword) { if(CurrentProfile.Email.Length == 0 || CurrentProfile.Name.Length == 0) { MessageBox.Show( "You must provide both a profile name and an email address in order to use web synchronization", "Incomplete profile!", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } BackupService client = new BackupService(); if (!CurrentProfile.PasswordValidated) return; foreach (ICryptoEditor plugin in plugins) { try { if(plugin is CryptoEditorHome.CryptoEditorHome) continue; // Load the XML of that plugin from the web string webData = client.Load(CurrentProfile.Email, CurrentProfile.Key, plugin.GetType().ToString()); if (webData == null || webData.Length == 0) { // The document does not exist on the server. // This will happen only at the first synchro. CryptoXML xmlEncrypted = Encrypt(plugin); client.Save(CurrentProfile.Email, CurrentProfile.Key, plugin.GetType().ToString(), xmlEncrypted.OuterXml); continue; } CryptoXML xmlWeb = new CryptoXML(); xmlWeb.LoadXml(webData); string localData = plugin.Save(); CryptoXML xmlLocal = new CryptoXML(); if(localData != null) xmlLocal.LoadXml(localData); xmlWeb.Password = serverPassword; #if !NO_ENCRYPT xmlWeb.Decrypt(plugin.EncryptionXPath, true); #endif // At this point I have the two XML documents I want to synchronize // It is important to work on the XML document at this level because // we dont have access to the definition of the Item involved. The // plugin is not forced to inherit its item from the framework's // CryptoEditorPluginItem. // Step 1 // Update the nodes that are found in both documents with the most // recent version. UpdateItems(xmlLocal, xmlWeb); // Step 2 UpdateFolders(xmlLocal, xmlWeb); // Step 3 // Fins all items and folders that are not present in both documents UpdateMissingNodes(xmlLocal.DocumentElement, xmlWeb.DocumentElement); // Delete all inactive nodes DeleteInactiveNodes(xmlLocal.DocumentElement); // Load the resulting XML into the plugin plugin.Load(xmlLocal.OuterXml); // Publish the resulting xml on the web ... xmlWeb.Password = CurrentProfile.Password; xmlWeb.Encrypt(plugin.EncryptionXPath, true); client.Save(CurrentProfile.Email, CurrentProfile.Key, plugin.GetType().ToString(), xmlWeb.OuterXml); } finally { } } }
// Should restore the data from the web and then re-initiate plugins public void RestoreFromTheWeb() { BackupService client = new BackupService(); if (!CurrentProfile.PasswordValidated) return; foreach (ICryptoEditor plugin in plugins) { string data = client.Load(CurrentProfile.Email, CurrentProfile.Key, plugin.GetType().ToString()); if(data == null) { plugin.Load(data); continue; } CryptoXML xmlRoot = new CryptoXML(); xmlRoot.LoadXml(data); if (xmlRoot.DocumentElement.Name.Equals("CryptoEditorDefault")) { // This is text only data or a not well formed XML document xmlRoot.Decrypt("/*", true); plugin.Load(xmlRoot.DocumentElement.InnerText); continue; } xmlRoot.Password = CurrentProfile.Password; #if !NO_ENCRYPT xmlRoot.Decrypt(plugin.EncryptionXPath, true); #endif plugin.Load(xmlRoot.OuterXml); } }
public void Synchronize( string serverPassword, System.ComponentModel.BackgroundWorker backgroundWorker, out Dictionary<ICryptoEditor, string> newData) { // Create empty dict newData = new Dictionary<ICryptoEditor, string>(); if(CurrentProfile.Email.Length == 0 || CurrentProfile.Name.Length == 0) { MessageBox.Show( "You must provide both a profile name and an email address in order to use web synchronization", "Incomplete profile!", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } // BackupService client = new BackupService(); if (!CurrentProfile.PasswordValidated) return; for (int i = 0; i < plugins.Count; i++ ) { ICryptoEditor plugin = plugins[i] as ICryptoEditor; if (plugin == null) continue; try { if (!plugin.isPersistent()) continue; // Load the XML of that plugin from the web //string webData = client.Load(CurrentProfile.Email, CurrentProfile.Key, plugin.GetType().ToString()); string webData = ""; HttpServiceClient service = new HttpServiceClient(currentProfile); bool ret = service.Load(plugin.GetType().ToString(), ref webData); if (!ret) return; if (string.IsNullOrEmpty(webData)) { // The document does not exist on the server. // This will happen only at the first synchro. CryptoXML xmlEncrypted = Encrypt(plugin); //client.Save(CurrentProfile.Email, CurrentProfile.Key, plugin.GetType().ToString(), xmlEncrypted.OuterXml); ret = service.Save(plugin.GetType().ToString(), xmlEncrypted.OuterXml); if (!ret) return; continue; } CryptoXML xmlWeb = new CryptoXML(); xmlWeb.LoadXml(webData); string localData = plugin.Save(); CryptoXML xmlLocal = new CryptoXML(); if (localData != null) xmlLocal.LoadXml(localData); xmlWeb.Password = serverPassword; #if !NO_ENCRYPT xmlWeb.Decrypt(plugin.EncryptionXPath, true); #endif // At this point I have the two XML documents I want to synchronize // It is important to work on the XML document at this level because // we dont have access to the definition of the Items involved. The // plugin is not forced to inherit its item from the framework's // CryptoEditorPluginItem. // Step 1 // Update the nodes that are found in both documents with the most // recent version. UpdateItems(xmlLocal, xmlWeb); // Step 2 UpdateFolders(xmlLocal, xmlWeb); // Step 3 // Fins all items and folders that are not present in both documents UpdateMissingNodes(xmlLocal.DocumentElement, xmlWeb.DocumentElement); // Delete all inactive nodes in the local document DeleteInactiveNodes(xmlLocal.DocumentElement); // Remove data from the inactive server document nodes DeleteInactiveNodesContent(xmlWeb.DocumentElement); // Remove older inactive nodes ... // Load the resulting XML into the plugin //plugin.Load(xmlLocal.OuterXml); newData.Add(plugin, xmlLocal.OuterXml); backgroundWorker.ReportProgress((i+1)*100/plugins.Count, "Merging " + plugin.Text + " ..."); // Publish the resulting xml on the web ... xmlWeb.Password = CurrentProfile.Password; xmlWeb.Encrypt(plugin.EncryptionXPath, true); ret = service.Save(plugin.GetType().ToString(), xmlWeb.OuterXml); if (!ret) return; } finally { } } }