Ejemplo n.º 1
0
        private void receiveNote(string data)
        {
            try
            {
                var temp = data.Split(PNStrings.END_OF_ADDRESS);
                var receivedFrom = PNStatic.Contacts.ContactNameByComputerName(temp[0]);
                var addresses = Dns.GetHostAddresses(temp[0]);
                // because we are on intranet, sender's ip which is equal to ourself ip is most probably ip of our computer
                var recIp = (addresses.Any(ip => ip.Equals(PNSingleton.Instance.IpAddress)))
                    ? PNSingleton.Instance.IpAddress
                    : addresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
                var sb = new StringBuilder();
                _ReceivedNotes = new List<string>();

                var rawData = temp[1].Split(PNStrings.END_OF_NOTE);
                //rawData[rawData.Length - 1] = rawData[rawData.Length - 1].Substring(0, rawData[rawData.Length - 1].IndexOf(PNStrings.END_OF_FILE));
                for (var i = 0; i < rawData.Length - 1; i++)
                {
                    temp = rawData[i].Split(PNStrings.END_OF_TEXT);
                    var nc = new NoteConverter();
                    var note = (PNote)nc.ConvertFromString(temp[1]);
                    if (note == null) continue;
                    note.ID = DateTime.Now.ToString("yyMMddHHmmssfff");
                    //note.NoteLocation = new Point(0, 0);
                    note.GroupID = (int)SpecialGroups.Incoming;
                    note.PrevGroupID = note.GroupID;
                    note.SentReceived = SendReceiveStatus.Received;
                    note.DateReceived = DateTime.Now;
                    note.ReceivedFrom = receivedFrom;
                    note.ReceivedIp = recIp != null ? recIp.ToString() : "";
                    note.NoteLocation =
                        new Point(
                            (Screen.GetWorkingArea(new System.Drawing.Point((int)Left,
                                (int)Top)).Width - note.NoteSize.Width) / 2,
                            (Screen.GetWorkingArea(new System.Drawing.Point((int)Left,
                                (int)Top)).Height - note.NoteSize.Height) / 2);

                    if (PNStatic.Settings.Network.ReceivedOnTop)
                    {
                        note.Topmost = true;
                    }

                    _ReceivedNotes.Add(note.ID);
                    sb.Append(note.Name);
                    sb.Append(";");
                    //sb.AppendLine();

                    if (!PNStatic.Settings.Network.ShowAfterArrive)
                    {
                        note.Visible = false;
                    }

                    var path = Path.Combine(PNPaths.Instance.DataDir, note.ID) + PNStrings.NOTE_EXTENSION;
                    using (var sw = new StreamWriter(path, false))
                    {
                        sw.Write(temp[0]);
                    }
                    if (PNStatic.Settings.Protection.PasswordString.Length > 0 && PNStatic.Settings.Protection.StoreAsEncrypted)
                    {
                        using (var pne = new PNEncryptor(PNStatic.Settings.Protection.PasswordString))
                        {
                            pne.EncryptTextFile(path);
                        }
                    }
                    if (note.Visible)
                    {
                        note.Dialog = new WndNote(note, note.ID, NewNoteMode.Identificator);
                        note.Dialog.Show();
                    }
                    PNStatic.Notes.Add(note);
                    if (NewNoteCreated != null)
                    {
                        NewNoteCreated(this, new NewNoteCreatedEventArgs(note));
                    }
                    subscribeToNoteEvents(note);

                    // save received note
                    PNNotesOperations.SaveNewNote(note);
                    PNNotesOperations.SaveNoteTags(note);
                    if (note.Schedule != null)
                    {
                        PNNotesOperations.SaveNoteSchedule(note);
                    }
                    note.Changed = false;
                }
                if (!PNStatic.Settings.Network.NoSoundOnArrive)
                {
                    PNSound.PlayMailSound();
                }

                if (!PNStatic.Settings.Network.NoNotificationOnArrive)
                {
                    var sbb = new StringBuilder(PNLang.Instance.GetCaptionText("received", "New notes received"));
                    sbb.Append(": ");
                    sbb.Append(sb);
                    if (sbb.Length > 1) sbb.Length -= 1;
                    sbb.AppendLine();
                    sbb.Append(PNLang.Instance.GetMessageText("sender", "Sender:"));
                    sbb.Append(" ");
                    sbb.Append(receivedFrom);
                    var baloon = new Baloon(BaloonMode.NoteReceived);
                    if (PNStatic.Settings.Network.ShowReceivedOnClick || PNStatic.Settings.Network.ShowIncomingOnClick)
                    {
                        baloon.BaloonLink = sbb.ToString();
                    }
                    else
                    {
                        baloon.BaloonText = sbb.ToString();
                    }
                    baloon.BaloonLinkClicked += baloon_BaloonLinkClicked;
                    ntfPN.ShowCustomBalloon(baloon, PopupAnimation.Slide, 10000);
                }
                if (NotesReceived != null)
                {
                    NotesReceived(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                PNStatic.LogException(ex);
            }
        }