예제 #1
0
        private void ShowNuntiasTasks(Nuntias nuntias, bool playConversationInnerSound, string processesedText)
        {
            bool nuntiasIsImage = (nuntias.ContentFileId != null && nuntias.ContentFileId.Length > 0 && nuntias.Text.Length >= 5 && nuntias.Text.Substring(0, 5) == "Image");     //Nuntias.Text format for image file is 'Image: image_file_name'

            SyncAssets.NuntiasSortedList[nuntias.Id] = nuntias;
            if (playConversationInnerSound)
            {
                ConversationPanel.newMessageSound.Play();
            }
            bool      newTimeStampShowed = this.ShowTimeStampIfNecessary(nuntias);
            bool      profileImageShowed = this.ShowProfileImageIfNecessary(nuntias, newTimeStampShowed);
            LinkLabel nuntiasLabel       = SyncAssets.ShowedNuntiasLabelSortedList[nuntias.Id] = new LinkLabel();

            nuntiasLabel.Name     = nuntias.Id.ToString();
            nuntiasLabel.LinkArea = new LinkArea(0, 0);
            if (nuntias.ContentFileId != null && nuntias.ContentFileId.Length > 0)
            {
                try
                {
                    if (nuntias.ContentFileId == "deleted")
                    {
                        nuntiasLabel.Text      = processesedText;
                        nuntiasLabel.ForeColor = Color.FromArgb(95, 95, 95);
                        nuntiasLabel.Padding   = new Padding(7, 7, 7, 7);
                        nuntiasLabel.Font      = CustomFonts.New(CustomFonts.SmallerSize, 'i');
                        nuntiasLabel.Size      = nuntiasLabel.PreferredSize;
                    }
                    else
                    {
                        string contentFilePath = LocalDataFileAccess.GetContentPathFromLocalData(nuntias.ContentFileId);
                        if (nuntiasIsImage)
                        {
                            if (contentFilePath != null)
                            {
                                using (FileStream imgStream = new FileStream(contentFilePath, FileMode.Open))
                                {
                                    Image img = Image.FromStream(imgStream);
                                    nuntiasLabel.Image = new Bitmap(img, new Size((int)((150.0 / img.Height) * img.Width), 150));
                                    img.Dispose();
                                    imgStream.Close();
                                    nuntiasLabel.Size = nuntiasLabel.Image.Size;
                                }
                            }
                            nuntiasLabel.Padding = new Padding(7, 7, 7, 7);
                        }
                        else
                        {
                            nuntiasLabel.Text         = processesedText;
                            nuntiasLabel.LinkArea     = new LinkArea(6, nuntiasLabel.Text.Length - 6);
                            nuntiasLabel.LinkColor    = Color.FromArgb(94, 92, 0);
                            nuntiasLabel.LinkClicked += delegate(Object sender, LinkLabelLinkClickedEventArgs e)
                            {
                                try
                                {
                                    Process.Start(contentFilePath);
                                    nuntiasLabel.LinkVisited = true;
                                }
                                catch
                                {
                                    Universal.ShowErrorMessage("Failed to open the file!");
                                }
                            };
                            nuntiasLabel.ForeColor = Color.FromArgb(95, 95, 95);
                            nuntiasLabel.Padding   = new Padding(7, 7, 7, 7);
                            nuntiasLabel.Font      = CustomFonts.New(CustomFonts.SmallerSize, 'i');
                            nuntiasLabel.Size      = nuntiasLabel.PreferredSize;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception occured in conversation panel ShowNuntias() method due to: " + e.Message + " exception type " + e.GetType());
                }
            }
            else
            {
                nuntiasLabel.Text = processesedText;
                Universal.SetLinkAreaIfLinkFound(nuntiasLabel);
                nuntiasLabel.LinkClicked += delegate(Object sender, LinkLabelLinkClickedEventArgs e)
                {
                    try
                    {
                        Process.Start(e.Link.LinkData.ToString());; e.Link.Visited = true;
                    }
                    catch
                    {
                        Universal.ShowErrorMessage("Failed to open the link!");
                    }
                };
                nuntiasLabel.Font    = CustomFonts.Smaller;
                nuntiasLabel.Padding = new Padding(7, 7, 7, 7);
                nuntiasLabel.Size    = nuntiasLabel.PreferredSize;
            }

            if (!nuntiasIsImage && nuntias.ContentFileId != "deleted")
            {
                if (nuntias.SenderId == Consumer.LoggedIn.Id)
                {
                    nuntiasLabel.BackColor = Color.FromArgb(231, 255, 234);
                }
                else
                {
                    nuntiasLabel.BackColor = Color.FromArgb(229, 231, 255);
                }
            }
            if (profileImageShowed)
            {
                nuntiasLabel.Top = this.lastShowedLabel.Bottom - this.profileImageSize.Height + 5;
            }
            else if (this.lastShowedNuntias == null || this.lastShowedNuntias.SenderId != nuntias.SenderId)
            {
                nuntiasLabel.Top = this.lastShowedLabel.Bottom + 25;
            }
            else
            {
                nuntiasLabel.Top = this.lastShowedLabel.Bottom + 5;
            }
            if (nuntias.SenderId == Consumer.LoggedIn.Id)
            {
                nuntiasLabel.Left = this.selfProfileImageLocation.X - nuntiasLabel.Width - this.profileImageSize.Width / 4;
            }
            else
            {
                nuntiasLabel.Left = this.othersProfileImageLocation.X + this.profileImageSize.Width + this.profileImageSize.Width / 4;
            }
            this.nuntiasBossPanel.Controls.Add(nuntiasLabel);
            this.lastShowedNuntias = nuntias;
            this.lastShowedLabel   = nuntiasLabel;

            int   x = 0;
            Timer makeXzeroTimer = new Timer()
            {
                Interval = 500
            };

            makeXzeroTimer.Tick += delegate(Object sender, EventArgs e) { x = 0; };

            NuntiasInfoPanel    respectiveInfoPanel   = null;
            NuntiasOptionsPanel respectiveOptionPanel = null;

            if (nuntias.ContentFileId != "deleted")
            {
                respectiveInfoPanel = SyncAssets.NuntiasInfoPanelSortedList[nuntias.Id] = new NuntiasInfoPanel(nuntias.Id);
                this.nuntiasBossPanel.Controls.Add(respectiveInfoPanel);

                respectiveOptionPanel = SyncAssets.NuntiasOptionPanelSortedList[nuntias.Id] = new NuntiasOptionsPanel(nuntias.Id);
                this.nuntiasBossPanel.Controls.Add(respectiveOptionPanel);
            }

            nuntiasLabel.MouseEnter += (s, me) =>
            {
                if (nuntiasIsImage)
                {
                    return;
                }
                Color current = ((Label)s).BackColor;
                if (current == Color.Transparent)
                {
                    return;
                }
                ((Label)s).BackColor = Color.FromArgb(current.R - 25, current.G - 25, current.B - 25);
            };
            nuntiasLabel.MouseLeave += (s, me) =>
            {
                if (nuntiasIsImage)
                {
                    return;
                }
                Color current = ((Label)s).BackColor;
                if (current == Color.Transparent)
                {
                    return;
                }
                ((Label)s).BackColor = Color.FromArgb(current.R + 25, current.G + 25, current.B + 25);
            };
            nuntiasLabel.MouseClick += delegate(Object sender, MouseEventArgs e)
            {
                if (respectiveOptionPanel == null)
                {
                    return;
                }
                if (respectiveInfoPanel == null)
                {
                    return;
                }
                if (e.Button == MouseButtons.Left)
                {
                    if (nuntias.ContentFileId != null && nuntias.ContentFileId.Length > 0)
                    {
                        makeXzeroTimer.Start();
                        x++;
                    }
                    if (x == 2)
                    {
                        if (nuntias.ContentFileId != null && nuntias.ContentFileId.Length > 0)
                        {
                            try
                            {
                                Process.Start(LocalDataFileAccess.GetFilePathInLocalData(nuntias.ContentFileId));
                            }
                            catch
                            {
                                Universal.ShowErrorMessage("Failed to open the file!");
                            }
                        }
                        makeXzeroTimer.Stop();
                        x = 0;
                    }
                    if (respectiveOptionPanel.Visible)
                    {
                        respectiveOptionPanel.ChangeNuntiasOptionPanelState();
                    }
                    respectiveInfoPanel.ChangeNuntiasInfoPanelState();
                    this.OnClick(sender, e);
                }
                else
                {
                    if (respectiveInfoPanel.Visible)
                    {
                        respectiveInfoPanel.ChangeNuntiasInfoPanelState();
                    }
                    respectiveOptionPanel.ChangeNuntiasOptionPanelState();
                    this.OnClick(sender, e);
                }
            };

            Label keepSpaceAtBottom = new Label();

            keepSpaceAtBottom.Location = new Point(0, this.lastShowedLabel.Bottom + 25);
            keepSpaceAtBottom.Size     = new Size(0, 0);
            this.nuntiasBossPanel.Controls.Add(keepSpaceAtBottom);
            FilePreviewPanelHeight(0);
            this.nuntiasBossPanel.VerticalScroll.Value = this.nuntiasBossPanel.VerticalScroll.Maximum;
        }
        private void SendChoosenFile(string choosenSafeFileName, string localPath, string extension)
        {
            if (File.ReadAllBytes(localPath).Length > 1024 * 1024 * 5)
            {
                Universal.ShowErrorMessage("Please choose a file below 5 MB.");
                return;
            }
            this.sendFileButton.Visible   = false;
            this.cancelFileButton.Visible = false;
            int animationLeft = 0, animationPadding = 40;

            if (imgPreview != null)
            {
                animationLeft = imgPreview.Right + animationPadding;
            }
            else
            {
                animationLeft = Math.Max(animationLeft, fileNameLabel.Right + animationPadding);
            }
            VisualizingTools.ShowWaitingAnimation(new Point(animationLeft, (filePreviewPanel.Height - sendButton.Height / 2) / 2), new Size(this.filePreviewPanel.Right - animationLeft - animationPadding - 20, sendButton.Height / 2), filePreviewPanel);
            BackgroundWorker loaderWorker = new BackgroundWorker();

            loaderWorker.DoWork += (s, e) =>
            {
                try
                {
                    using (FileStream gotFileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        string       fileIdName          = this.theConversation.ConversationID + "_" + Universal.SystemMACAddress + "_" + Time.CurrentTime.TimeStampString + "-" + choosenSafeFileName;
                        MemoryStream gotFileMemoryStream = new MemoryStream();
                        gotFileStream.CopyTo(gotFileMemoryStream);
                        bool storeToLocalSucceed = LocalDataFileAccess.SaveNuntiasContentToLocal(gotFileMemoryStream, fileIdName);
                        if (storeToLocalSucceed)
                        {
                            if (this.theConversation == null)
                            {
                                long?conversationId = ServerRequest.GetDuetConversationId(User.LoggedIn, this.receiver);
                                if (conversationId == null)
                                {
                                    MessageBox.Show("Server connection failed!\r\nPlease retry.");
                                    return;
                                }
                                this.TheConversation = new DuetConversation(Consumer.LoggedIn, this.receiver);
                                this.TheConversation.ConversationID = (long)conversationId;
                            }
                            Nuntias newNuntias = new Nuntias("File: " + choosenSafeFileName, Consumer.LoggedIn.Id, Time.CurrentTime, this.theConversation.ConversationID);
                            newNuntias.ContentFileId = fileIdName;
                            if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
                            {
                                newNuntias.Text = "Image : " + choosenSafeFileName;
                            }
                            long?nuntiasTmpID = NuntiasRepository.Instance.StoreTmpNuntias(newNuntias);
                            if (nuntiasTmpID == null)
                            {
                                newNuntias = null;
                            }
                            else
                            {
                                newNuntias.Id = nuntiasTmpID ?? 0;
                            }
                            if (newNuntias == null)
                            {
                                return;
                            }

                            this.ShowNuntias(newNuntias, true);
                            BackendManager.SendPendingNuntii();

                            string localStoredPath = LocalDataFileAccess.GetContentPathFromLocalData(fileIdName);

                            Universal.ParentForm.Invoke(new Action(() =>
                            {
                                VisualizingTools.HideWaitingAnimation();
                                if (localStoredPath != null)
                                {
                                    this.FilePreviewPanelHeight(0);
                                }
                                else
                                {
                                    Universal.ShowErrorMessage("Failed to send the file!");
                                }
                            }));

                            gotFileStream.Close();
                            gotFileMemoryStream.Dispose();
                            loaderWorker.Dispose();
                            this.DisposeUnmanaged();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error occured while sending a file: " + ex.Message);
                };
            };
            loaderWorker.RunWorkerAsync();
            loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); };
            if (ConversationListPanel.MyConversationListPanel != null)
            {
                ConversationListPanel.MyConversationListPanel.RefreshConversationList();
            }
        }
예제 #3
0
        public NuntiasOptionsPanel(long nuntiasId)
        {
            this.nuntiasId = nuntiasId;
            Nuntias nuntias = SyncAssets.NuntiasSortedList[this.nuntiasId];

            this.parentNuntiasLabel = SyncAssets.ShowedNuntiasLabelSortedList[nuntias.Id];
            this.BackColor          = Color.FromArgb(51, 51, 51);
            this.ForeColor          = Color.FromArgb(220, 220, 220);
            if (nuntias.SenderId == Consumer.LoggedIn.Id)
            {
                this.Name = "own";
            }
            else
            {
                this.Name = "other";
            }

            copyNuntiasTextLabel = new Label();
            deleteNuntiasLabel   = new Label();

            this.Controls.Add(copyNuntiasTextLabel);
            this.Controls.Add(deleteNuntiasLabel);

            copyNuntiasTextLabel.Text        = "";
            copyNuntiasTextLabel.Image       = copyIcon;
            copyNuntiasTextLabel.ImageAlign  = ContentAlignment.MiddleLeft;
            copyNuntiasTextLabel.TextAlign   = ContentAlignment.MiddleRight;
            copyNuntiasTextLabel.Font        = CustomFonts.Smallest;
            copyNuntiasTextLabel.Size        = labelSize;
            copyNuntiasTextLabel.MouseEnter += (s, e) => { copyNuntiasTextLabel.BackColor = Color.FromArgb(128, 128, 128); };
            copyNuntiasTextLabel.MouseLeave += (s, e) => { copyNuntiasTextLabel.BackColor = Color.Transparent; };
            copyNuntiasTextLabel.Click      += (s, e) =>
            {
                if (nuntias.ContentFileId == null || nuntias.ContentFileId.Length == 0)
                {
                    Clipboard.SetText(nuntias.Text);
                }
                else if (nuntias.Text.Length >= 5 && nuntias.Text.Substring(0, 5) == "Image")
                {
                    Clipboard.SetImage(Image.FromFile(LocalDataFileAccess.GetContentPathFromLocalData(nuntias.ContentFileId)));
                }
                else if (nuntias.Text.Length >= 4 && nuntias.Text.Substring(0, 4) == "File")
                {
                    StringCollection paths = new StringCollection();
                    paths.Add(LocalDataFileAccess.GetContentPathFromLocalData(nuntias.ContentFileId));
                    Clipboard.SetFileDropList(paths);
                }
                this.ChangeNuntiasOptionPanelState();
            };
            copyNuntiasTextLabel.Visible = false;

            deleteNuntiasLabel.Text        = "";
            deleteNuntiasLabel.Image       = deleteIcon;
            deleteNuntiasLabel.ImageAlign  = ContentAlignment.MiddleLeft;
            deleteNuntiasLabel.TextAlign   = ContentAlignment.MiddleRight;
            deleteNuntiasLabel.Font        = CustomFonts.Smallest;
            deleteNuntiasLabel.Size        = labelSize;
            deleteNuntiasLabel.MouseEnter += (s, e) => { deleteNuntiasLabel.BackColor = Color.FromArgb(128, 128, 128); };
            deleteNuntiasLabel.MouseLeave += (s, e) => { deleteNuntiasLabel.BackColor = Color.Transparent; };
            deleteNuntiasLabel.Click      += (s, e) =>
            {
                if (nuntias.SenderId == Consumer.LoggedIn.Id && Time.TimeDistanceInSecond(Time.CurrentTime, nuntias.SentTime) <= 300)
                {
                    //server allows deletion of a message that was sent 5 minutes ago or later.
                    DialogResult result = MessageBox.Show("Delete the message from both side?\r\nClick Yes. Else click No.", "Message Deletion", MessageBoxButtons.YesNoCancel);
                    if (result == DialogResult.Yes || result == DialogResult.No)
                    {
                        BackgroundWorker bworker = new BackgroundWorker();
                        bworker.DoWork += (ss, ee) =>
                        {
                            bool success = ServerRequest.DeleteNuntias(Consumer.LoggedIn.Id, this.NuntiasId, result == DialogResult.Yes);
                            if (success && result == DialogResult.No)
                            {
                                SyncAssets.DeleteNuntiasAssets(this.NuntiasId);
                            }
                        };
                        bworker.RunWorkerAsync();
                        bworker.RunWorkerCompleted += (ss, ee) => { bworker.Dispose(); };
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    DialogResult result = MessageBox.Show("Delete the message for you?", "Message Deletion", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        BackgroundWorker bworker = new BackgroundWorker();
                        bworker.DoWork += (ss, ee) =>
                        {
                            bool success = ServerRequest.DeleteNuntias(Consumer.LoggedIn.Id, this.NuntiasId, false);
                            if (success)
                            {
                                SyncAssets.DeleteNuntiasAssets(this.NuntiasId);
                            }
                        };
                        bworker.RunWorkerAsync();
                        bworker.RunWorkerCompleted += (ss, ee) => { bworker.Dispose(); };
                    }
                    else
                    {
                        return;
                    }
                }
                this.ChangeNuntiasOptionPanelState();
            };
            deleteNuntiasLabel.Visible = false;

            this.UpdateOptionPanel();
        }