private bool importContacts(string iniPath) { try { var result = false; var size = 1024; var buffer = new string(' ', size); while (PNInterop.GetPrivateProfileString("contacts", null, null, buffer, size, iniPath) == size - 2) { // loop until sufficient buffer size size *= 2; buffer = new string(' ', size); } var names = buffer.Split('\0'); var keys = names.Where(n => n.Trim().Length > 0); var sqlList = new List<string>(); var contacts = new List<PNContact>(); int tempID = 0; if (PNStatic.Contacts.Count > 0) { tempID = PNStatic.Contacts.Max(c => c.ID) + 1; } foreach (var key in keys) { var cont = new PCONTPROP(); cont = PNInterop.ReadINIStructure(iniPath, "contacts", key, cont); if (cont.name != null && cont.name.Trim().Length > 0) { var pnc = new PNContact { ComputerName = cont.host, GroupID = cont.group, Name = cont.name, UseComputerName = cont.usename }; if (cont.address != 0) { pnc.IpAddress = buildAddressString(cont.address); } var temp = PNStatic.Contacts.FirstOrDefault(c => c.Name == pnc.Name); pnc.ID = temp != null ? temp.ID : tempID++; contacts.Add(pnc); } } // get contact froups size = 1024; buffer = new string(' ', size); while (PNInterop.GetPrivateProfileString("cont_groups", null, null, buffer, size, iniPath) == size - 2) { // loop until sufficient buffer size size *= 2; buffer = new string(' ', size); } names = buffer.Split('\0'); keys = names.Where(n => n.Trim().Length > 0); var groups = new List<PNContactGroup>(); tempID = 0; if (PNStatic.ContactGroups.Count > 0) { tempID = PNStatic.ContactGroups.Max(g => g.ID) + 1; } foreach (var key in keys) { var grp = new PCONTGROUP(); grp = PNInterop.ReadINIStructure(iniPath, "cont_groups", key, grp); if (grp.name == null || grp.name.Trim().Length <= 0) continue; var pg = new PNContactGroup { Name = grp.name }; var temp = PNStatic.ContactGroups.FirstOrDefault(g => g.Name == grp.name); pg.ID = temp != null ? temp.ID : tempID++; groups.Add(pg); } // build sql foreach (var pg in groups) { var sb = new StringBuilder(); sb.Append("INSERT OR REPLACE INTO CONTACT_GROUPS (ID, GROUP_NAME) VALUES("); sb.Append(pg.ID); sb.Append(", '"); sb.Append(pg.Name); sb.Append("')"); sqlList.Add(sb.ToString()); } foreach (var pc in contacts) { var sb = new StringBuilder(); sb.Append( "INSERT OR REPLACE INTO CONTACTS (ID, GROUP_ID, CONTACT_NAME, COMP_NAME, IP_ADDRESS, USE_COMP_NAME) VALUES("); sb.Append(pc.ID); sb.Append(", "); sb.Append(pc.GroupID); sb.Append(", '"); sb.Append(pc.Name); sb.Append("', '"); sb.Append(pc.ComputerName); sb.Append("', '"); sb.Append(pc.IpAddress); sb.Append("', "); sb.Append(Convert.ToInt32(pc.UseComputerName)); sb.Append(")"); sqlList.Add(sb.ToString()); } if (sqlList.Count > 0 && PNData.ExecuteTransactionForList(sqlList, PNData.ConnectionString)) { result = true; foreach (PNContactGroup pg in groups) { PNStatic.ContactGroups.RemoveAll(g => g.Name == pg.Name); PNStatic.ContactGroups.Add(pg); } foreach (PNContact pc in contacts) { PNStatic.Contacts.RemoveAll(c => c.Name == pc.Name); PNStatic.Contacts.Add(pc); } } return result; } catch (Exception ex) { PNStatic.LogException(ex); return false; } }
internal bool ContactAction(PNContact cn, AddEditMode mode) { try { if (mode == AddEditMode.Add) { if (_Contacts.Any(c => c.Name == cn.Name)) { var message = PNLang.Instance.GetMessageText("contact_exists", "Contact with this name already exists"); PNMessageBox.Show(message, PNStrings.PROG_NAME, MessageBoxButton.OK, MessageBoxImage.Information); return false; } _Contacts.Add(cn); } else { var c = _Contacts.FirstOrDefault(con => con.ID == cn.ID); if (c != null) { c.Name = cn.Name; c.ComputerName = cn.ComputerName; c.IpAddress = cn.IpAddress; c.UseComputerName = cn.UseComputerName; c.GroupID = cn.GroupID; } } fillContacts(false); fillGroups(false); return true; } catch (Exception ex) { PNStatic.LogException(ex); return false; } }
internal bool SendNotesViaNetwork(List<PNote> notes, PNContact cn) { try { var sb = new StringBuilder(); foreach (var note in notes) { if (PNStatic.Settings.Protection.PromptForPassword) if (!PNNotesOperations.LogIntoNoteOrGroup(note)) continue; string text, tempPath = ""; var newNote = false; var nc = new NoteConverter(); // decrypt note file to temp file if note is encrypted var path = Path.Combine(PNPaths.Instance.DataDir, note.ID); path += PNStrings.NOTE_EXTENSION; // save note first if (note.Dialog != null && note.Changed) { if (note.FromDB) { if (PNStatic.Settings.Network.SaveBeforeSending) { note.Dialog.ApplySaveNote(false); } } else { path = Path.Combine(Path.GetTempPath(), note.ID); path += PNStrings.NOTE_EXTENSION; note.Dialog.Edit.SaveFile(path, RichTextBoxStreamType.RichText); newNote = true; } } if (PNStatic.Settings.Protection.PasswordString.Length > 0 && PNStatic.Settings.Protection.StoreAsEncrypted && !newNote) { var fileName = Path.GetFileName(path); if (string.IsNullOrEmpty(fileName)) continue; tempPath = Path.Combine(Path.GetTempPath(), fileName); File.Copy(path, tempPath, true); using (var pne = new PNEncryptor(PNStatic.Settings.Protection.PasswordString)) { pne.DecryptTextFile(tempPath); } path = tempPath; } // read note file content using (var sr = new StreamReader(path)) { text = sr.ReadToEnd(); } // remove temp file if (tempPath != "") { File.Delete(tempPath); } //remove temporary file created for new note if (newNote) { File.Delete(path); } sb.Append(text); sb.Append(PNStrings.END_OF_TEXT); sb.Append(nc.ConvertToString(note)); sb.Append(PNStrings.END_OF_NOTE); } if (sb.Length <= 0) return false; string ipAddress; if (!cn.UseComputerName || !string.IsNullOrEmpty(cn.IpAddress)) { ipAddress = cn.IpAddress; } else { IPHostEntry ipHostInfo; try { ipHostInfo = Dns.GetHostEntry(cn.ComputerName); } catch (SocketException) { var msg = PNLang.Instance.GetMessageText("host_unknown", "Computer %PLACEHOLDER1% cannot be found on network"); msg = msg.Replace(PNStrings.PLACEHOLDER1, cn.ComputerName); PNMessageBox.Show(msg, PNStrings.PROG_NAME, MessageBoxButton.OK, MessageBoxImage.Error); return false; } var address = ipHostInfo.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); if (address == null) return false; ipAddress = address.ToString(); } //check whether contact's computer is on network if (PNConnections.CheckContactConnection(ipAddress) == ContactConnection.Disconnected) { var msg = PNLang.Instance.GetMessageText("contact_disconnected", "Computer of contact %PLACEHOLDER1% is not connected to network"); PNMessageBox.Show(msg.Replace("%PLACEHOLDER1%", cn.Name), PNStrings.PROG_NAME, MessageBoxButton.OK); return false; } var clientRunner = new PNWCFClientRunner(); clientRunner.PNDataError += WCFClient_PNDataError; clientRunner.NotesSent += WCFClient_NotesSent; if (ipAddress == PNSingleton.Instance.IpAddress.ToString()) //we are on intranet, so most probably this is the same computer { Task.Factory.StartNew(() => clientRunner.SendNotes(cn.Name, PNSingleton.Instance.IpAddress.ToString(), sb.ToString(), PNStatic.Settings.Network.ExchangePort.ToString(CultureInfo.InvariantCulture), notes)); //var t = new Thread(() => clientRunner.SendNotes(cn.Name, ipAddress.ToString(), sb.ToString(), PNStatic.Settings.Network.ExchangePort.ToString(CultureInfo.InvariantCulture), notes)); //t.Start(); return true; } else { return clientRunner.SendNotes(cn.Name, ipAddress, sb.ToString(), PNStatic.Settings.Network.ExchangePort.ToString(CultureInfo.InvariantCulture), notes); } } catch (Exception ex) { PNStatic.LogException(ex); return false; } }
internal static void UpdateContact(PNContact cn) { try { var sb = new StringBuilder("UPDATE CONTACTS SET GROUP_ID = "); sb.Append(cn.GroupID); sb.Append(", CONTACT_NAME = '"); sb.Append(cn.Name); sb.Append("', COMP_NAME = '"); sb.Append(cn.ComputerName); sb.Append("', IP_ADDRESS = '"); sb.Append(cn.IpAddress); sb.Append("', USE_COMP_NAME = "); sb.Append(Convert.ToInt32(cn.UseComputerName)); sb.Append(" WHERE ID = "); sb.Append(cn.ID); ExecuteTransactionForStringBuilder(sb, ConnectionString); } catch (Exception ex) { PNStatic.LogException(ex); } }
internal ContactChangedEventArgs(PNContact contact, AddEditMode mode) { _Contact = contact; _Mode = mode; Accepted = true; }