Ejemplo n.º 1
0
        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;
            }
        }