Пример #1
0
        private void othersInfoEditBoxInitialize()
        {
            this.othersInfoEditPanel        = new Panel();
            this.othersInfoEditPanel.Width  = this.Width;
            this.othersInfoEditPanel.Height = this.Height;

            Label settingsLabel = new Label();

            settingsLabel.Text      = "Account Settings";
            settingsLabel.Font      = CustomFonts.RegularBold;
            settingsLabel.Size      = settingsLabel.PreferredSize;
            settingsLabel.ForeColor = Color.FromArgb(77, 77, 77);
            this.othersInfoEditPanel.Controls.Add(settingsLabel);

            Label macAddressLabel = new Label();

            macAddressLabel.Text      = "Device MAC :";
            macAddressLabel.Font      = CustomFonts.Smaller;
            macAddressLabel.Size      = macAddressLabel.PreferredSize;
            macAddressLabel.ForeColor = Color.FromArgb(77, 77, 77);
            this.othersInfoEditPanel.Controls.Add(macAddressLabel);

            this.macAddressTextbox             = new TextBox();
            this.macAddressTextbox.Text        = Universal.SystemMACAddress;
            this.macAddressTextbox.ReadOnly    = true;
            this.macAddressTextbox.BorderStyle = BorderStyle.None;
            this.macAddressTextbox.Font        = CustomFonts.Small;
            this.macAddressTextbox.Width       = this.Width * 4 / 5;
            this.macAddressTextbox.ForeColor   = Color.FromArgb(77, 77, 77);
            this.macAddressTextbox.BackColor   = this.BackColor;
            this.othersInfoEditPanel.Controls.Add(macAddressTextbox);

            Label usernameLabel = new Label();

            usernameLabel.Text      = "Username :"******"@" + this.usernameTextbox.Text; };
            this.othersInfoEditPanel.Controls.Add(usernameTextbox);

            Label phoneLabel = new Label();

            phoneLabel.Text      = "Phone :";
            phoneLabel.Font      = CustomFonts.Smaller;
            phoneLabel.Size      = phoneLabel.PreferredSize;
            phoneLabel.ForeColor = Color.FromArgb(77, 77, 77);
            this.othersInfoEditPanel.Controls.Add(phoneLabel);

            this.phoneTextBox             = new TextBox();
            this.phoneTextBox.Text        = Consumer.LoggedIn.Phone;
            this.phoneTextBox.Font        = CustomFonts.Small;
            this.phoneTextBox.BorderStyle = BorderStyle.None;
            this.phoneTextBox.Width       = (this.Width * 5 / 6) - phoneLabel.Width;
            this.phoneTextBox.ForeColor   = Color.FromArgb(77, 77, 77);
            //this.phoneTextBox.KeyPress += (s, e) => { if(e.KeyChar >= '0' && e.KeyChar <= '9') };
            this.othersInfoEditPanel.Controls.Add(phoneTextBox);

            Label emailLabel = new Label();

            emailLabel.Text      = "Email";
            emailLabel.Font      = CustomFonts.Smaller;
            emailLabel.Size      = emailLabel.PreferredSize;
            emailLabel.ForeColor = Color.FromArgb(77, 77, 77);
            this.othersInfoEditPanel.Controls.Add(emailLabel);

            this.emailTextBox             = new TextBox();
            this.emailTextBox.Text        = Consumer.LoggedIn.Email;
            this.emailTextBox.ReadOnly    = true;               //changing email address is temporarily disabled
            this.emailTextBox.Font        = CustomFonts.Small;
            this.emailTextBox.BorderStyle = BorderStyle.None;
            this.emailTextBox.Width       = this.Width * 5 / 6;
            this.emailTextBox.ForeColor   = Color.FromArgb(77, 77, 77);
            this.othersInfoEditPanel.Controls.Add(emailTextBox);

            Label genderLabel = new Label();

            genderLabel.Text      = "Gender";
            genderLabel.Font      = CustomFonts.Smaller;
            genderLabel.ForeColor = Color.FromArgb(77, 77, 77);
            genderLabel.Size      = genderLabel.PreferredSize;
            this.othersInfoEditPanel.Controls.Add(genderLabel);

            genderChooser      = new ComboBox();
            genderChooser.Font = CustomFonts.Smallest;
            string[] itemList = new string[] { "", "Male", "Female", "Others" };
            foreach (string str in itemList)
            {
                genderChooser.Items.Add(str);
            }
            if (Consumer.LoggedIn.GenderIndex > 0)
            {
                genderChooser.SelectedIndex = (int)Consumer.LoggedIn.GenderIndex;
            }
            genderChooser.DropDownStyle = ComboBoxStyle.DropDownList;
            this.othersInfoEditPanel.Controls.Add(genderChooser);

            Label birthdateLabel = new Label();

            birthdateLabel.Text      = "Birth Date";
            birthdateLabel.Font      = CustomFonts.Smaller;
            birthdateLabel.Size      = birthdateLabel.PreferredSize;
            birthdateLabel.ForeColor = Color.FromArgb(77, 77, 77);
            this.othersInfoEditPanel.Controls.Add(birthdateLabel);

            birthdatePicker = new DateTimePicker();
            if (Consumer.LoggedIn.Birthdate != null)
            {
                birthdatePicker.Value = Convert.ToDateTime(Consumer.LoggedIn.Birthdate.DbFormat);
                birthdateLabel.Text   = "Birth Date";
            }
            else
            {
                birthdateLabel.Text = "Birth Date (not set)";
            }
            birthdateLabel.Size  = birthdateLabel.PreferredSize;
            birthdatePicker.Font = CustomFonts.Smaller;
            this.othersInfoEditPanel.Controls.Add(birthdatePicker);

            Label oldPasswordLabel = new Label();

            oldPasswordLabel.Text      = "Current Password";
            oldPasswordLabel.Font      = CustomFonts.Smaller;
            oldPasswordLabel.ForeColor = Color.FromArgb(77, 77, 77);
            oldPasswordLabel.Size      = oldPasswordLabel.PreferredSize;
            this.othersInfoEditPanel.Controls.Add(oldPasswordLabel);

            currentPasswordTextBox = new TextBox();
            currentPasswordTextBox.PasswordChar = '•';
            currentPasswordTextBox.ForeColor    = Color.FromArgb(77, 77, 77);
            currentPasswordTextBox.Font         = CustomFonts.Smallest;
            currentPasswordTextBox.Size         = currentPasswordTextBox.PreferredSize;
            this.othersInfoEditPanel.Controls.Add(currentPasswordTextBox);

            this.Controls.Add(othersInfoEditPanel);
            this.othersInfoEditPanel.Location = new Point(0, userViewPanel.Bottom + 4);

            //initializing sizes
            settingsLabel.Location = new Point((this.Width - settingsLabel.Width) / 2, settingsLabel.Height / 2);

            emailTextBox.Left           = (this.Width - emailTextBox.Width) / 2;
            usernameLabel.Left          = emailTextBox.Left - 3;
            usernameTextbox.Left        = usernameLabel.Right + 10;
            macAddressLabel.Left        = emailTextBox.Left - 3;
            macAddressTextbox.Left      = macAddressLabel.Right + 10;
            phoneLabel.Left             = emailTextBox.Left - 3;
            phoneTextBox.Left           = phoneLabel.Right + 10;
            emailLabel.Left             = emailTextBox.Left - 3;
            genderLabel.Left            = emailTextBox.Left - 3;
            genderChooser.Left          = genderLabel.Right + 10;
            birthdateLabel.Left         = emailTextBox.Left - 3;
            birthdatePicker.Left        = birthdateLabel.Right + 5;
            oldPasswordLabel.Left       = emailTextBox.Left - 3;
            currentPasswordTextBox.Left = oldPasswordLabel.Right + 5;

            macAddressLabel.Top   = settingsLabel.Bottom + 20;
            macAddressTextbox.Top = macAddressLabel.Top;

            usernameLabel.Top   = macAddressTextbox.Bottom + 15;
            usernameTextbox.Top = usernameLabel.Top;

            phoneLabel.Top   = usernameTextbox.Bottom + 15;
            phoneTextBox.Top = phoneLabel.Top;

            emailLabel.Top   = phoneTextBox.Bottom + 15;
            emailTextBox.Top = emailLabel.Bottom + 5;

            genderLabel.Top   = emailTextBox.Bottom + 15;
            genderChooser.Top = genderLabel.Top;

            birthdateLabel.Top  = genderChooser.Bottom + 15;
            birthdatePicker.Top = birthdateLabel.Top;

            oldPasswordLabel.Top         = birthdatePicker.Bottom + 15;
            currentPasswordTextBox.Top   = oldPasswordLabel.Top;
            currentPasswordTextBox.Width = emailTextBox.Right - oldPasswordLabel.Right;

            //buttons initialize

            saveButton             = new Button();
            saveButton.Text        = "Save";
            saveButton.BackColor   = Color.FromArgb(0, 0, 102);
            saveButton.ForeColor   = Color.FromArgb(234, 234, 234);
            saveButton.Font        = CustomFonts.SmallerBold;
            saveButton.Size        = saveButton.PreferredSize;
            saveButton.FlatStyle   = FlatStyle.Flat;
            saveButton.Left        = this.currentPasswordTextBox.Right - saveButton.Width;
            saveButton.MouseEnter += (s, e) => { saveButton.BackColor = Color.FromArgb(saveButton.BackColor.R + 50, saveButton.BackColor.G + 50, saveButton.BackColor.B + 50); };
            saveButton.MouseLeave += (s, e) => { saveButton.BackColor = Color.FromArgb(saveButton.BackColor.R - 50, saveButton.BackColor.G - 50, saveButton.BackColor.B - 50); };
            saveButton.Click      += (sender, eargs) =>
            {
                try
                {
                    string name = this.fullnameTextBox.Text, phone = phoneTextBox.Text, username = usernameTextbox.Text, email = emailTextBox.Text, inputPassword = currentPasswordTextBox.Text;
                    Time   birthdate = new Time(birthdatePicker.Value);
                    string nameError = null, usernameError = null, emailError = null, genderError = null, birthdateError = null, passwordError = null;
                    nameError = StandardAssuranceLibrary.Checker.CheckNameValidity(name);
                    if (username != Consumer.LoggedIn.Username)
                    {
                        usernameError = StandardAssuranceLibrary.Checker.CheckUsernameValidity(ref username);
                    }
                    if (email != Consumer.LoggedIn.Email)
                    {
                        emailError = StandardAssuranceLibrary.Checker.CheckEmailValidity(ref email);
                    }
                    if (genderChooser.SelectedIndex == -1 || genderChooser.SelectedIndex == 0)
                    {
                        genderError = "No gender is choosen.";
                    }
                    if (Time.TimeDistanceInMinute(birthdate, Time.CurrentTime) < 6837340)
                    {
                        birthdateError = "Less than 13 years old are not allowed.";
                    }

                    JObject consumerJson = new JObject();
                    consumerJson["id"]           = Consumer.LoggedIn.Id;
                    consumerJson["name"]         = name;
                    consumerJson["username"]     = username;
                    consumerJson["email"]        = email;
                    consumerJson["phone"]        = phone;
                    consumerJson["birthdate"]    = birthdate.TimeStampString;
                    consumerJson["gender_index"] = genderChooser.SelectedIndex;
                    if (Consumer.LoggedIn.ProfileImageId.Length > 5)
                    {
                        consumerJson["profile_img_id"] = Consumer.LoggedIn.ProfileImageId;
                    }

                    BackgroundWorker loaderWorker = new BackgroundWorker();
                    loaderWorker.DoWork += (s, e) =>
                    {
                        passwordError = StandardAssuranceLibrary.Checker.CheckOldPasswordMatch(inputPassword);
                        string errorMessage = passwordError + "\r\n" + nameError + "\r\n" + usernameError + "\r\n" + emailError + "\r\n" + genderError + "\r\n" + birthdateError + "\r\n";
                        string errorSum     = passwordError + usernameError + nameError + emailError + genderError + birthdateError;
                        if (errorSum.Length < 4)
                        {
                            Consumer consumer = new Consumer(consumerJson);
                            bool?    success  = ServerRequest.UpdateUserInfo(consumer);
                            if (success == true)
                            {
                                Consumer.LoggedIn = consumer;
                                Universal.ShowInfoMessage("The informations have been updated successful!", "Updated successfully!");
                                Universal.ParentForm.Invoke(new Action(() =>
                                {
                                    currentPasswordTextBox.Text = "";
                                }));
                            }
                            else
                            {
                                Universal.ShowErrorMessage("Error encountered while updating the informations.", "Update unsuccessful!");
                                Universal.ParentForm.Invoke(new Action(() =>
                                {
                                    currentPasswordTextBox.Text = "";
                                }));
                            }
                        }
                        else
                        {
                            Universal.ShowErrorMessage(errorMessage);
                        }
                    };
                    loaderWorker.RunWorkerAsync();
                    loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); };
                }
                catch (Exception ex) { Console.WriteLine("Exception in UpdateUser() => " + ex.Message); }
            };
            this.othersInfoEditPanel.Controls.Add(saveButton);

            deleteAccountButton             = new Button();
            deleteAccountButton.Text        = "Delete Account";
            deleteAccountButton.BackColor   = Color.FromArgb(153, 0, 0);
            deleteAccountButton.ForeColor   = Color.FromArgb(234, 234, 234);
            deleteAccountButton.Font        = CustomFonts.SmallerBold;
            deleteAccountButton.Size        = deleteAccountButton.PreferredSize;
            deleteAccountButton.FlatStyle   = FlatStyle.Flat;
            deleteAccountButton.Left        = oldPasswordLabel.Left;
            deleteAccountButton.MouseEnter += (s, e) => { deleteAccountButton.BackColor = Color.FromArgb(deleteAccountButton.BackColor.R + 50, deleteAccountButton.BackColor.G + 50, deleteAccountButton.BackColor.B + 50); };
            deleteAccountButton.MouseLeave += (s, e) => { deleteAccountButton.BackColor = Color.FromArgb(deleteAccountButton.BackColor.R - 50, deleteAccountButton.BackColor.G - 50, deleteAccountButton.BackColor.B - 50); };
            deleteAccountButton.Click      += (sender, eargs) =>
            {
                DialogResult result = MessageBox.Show("Are you sure to delete your account?\r\nAll your messages, conversations, friendlist and everything belongs to you will be deleted.\r\nAnd this can't be undone!", "Confirm deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly);
                if (result == DialogResult.Yes)
                {
                    if (this.currentPasswordTextBox.Text == null || this.currentPasswordTextBox.Text.Length <= 6)
                    {
                        Universal.ShowErrorMessage("Please enter your valid current password.", "Deletion unsuccessful!");
                        return;
                    }
                    BackgroundWorker loaderWorker = new BackgroundWorker();
                    loaderWorker.DoWork += (s, e) =>
                    {
                        try
                        {
                            string operationMessage = ServerRequest.DeleteConsumerAccount(Universal.SystemMACAddress, this.currentPasswordTextBox.Text);
                            if (operationMessage == null || operationMessage.Length == 0)
                            {
                                Universal.ShowErrorMessage("Connection to the server failed!", "Deletion unsuccessful!");
                            }
                            if (operationMessage == "success")
                            {
                                Universal.ShowInfoMessage("Your account has been deleted!", "Deletion successful!");
                                BackendManager.Logout();
                            }
                            else
                            {
                                Universal.ShowErrorMessage(operationMessage, "Deletion unsuccessful!");
                            }
                        }
                        catch { }
                    };
                    loaderWorker.RunWorkerAsync();
                    loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); };
                }
            };
            this.othersInfoEditPanel.Controls.Add(deleteAccountButton);

            Label cancelButtonLabel = new Label();

            cancelButtonLabel.Text        = "Cancel && Back";
            cancelButtonLabel.BackColor   = Color.FromArgb(this.BackColor.R - 20, this.BackColor.G - 20, this.BackColor.B - 20);
            cancelButtonLabel.TextAlign   = ContentAlignment.MiddleCenter;
            cancelButtonLabel.Font        = CustomFonts.SmallerBold;
            cancelButtonLabel.Width       = this.Width;
            cancelButtonLabel.Height      = cancelButtonLabel.PreferredHeight * 5 / 2;
            cancelButtonLabel.Top         = this.Bottom - this.userViewPanel.Height - cancelButtonLabel.Height;
            cancelButtonLabel.MouseEnter += (s, e) => { cancelButtonLabel.BackColor = Color.FromArgb(cancelButtonLabel.BackColor.R - 30, cancelButtonLabel.BackColor.G - 30, cancelButtonLabel.BackColor.B - 30); };
            cancelButtonLabel.MouseLeave += (s, e) => { cancelButtonLabel.BackColor = Color.FromArgb(cancelButtonLabel.BackColor.R + 30, cancelButtonLabel.BackColor.G + 30, cancelButtonLabel.BackColor.B + 30); };
            cancelButtonLabel.Click      += (s, e) =>
            {
                SlidebarPanel.MySidebarPanel.Controls.Remove(this);
                this.Dispose();
                SlidebarPanel.MySidebarPanel.Controls.Add(SlidebarPanel.MySidebarPanel.MainPanelInSidebar);
            };

            deleteAccountButton.Top = cancelButtonLabel.Top - deleteAccountButton.Height - 10;
            saveButton.Top          = deleteAccountButton.Top;

            LinkLabel changePasswordLink = new LinkLabel();

            changePasswordLink.Text         = "or, Change Password";
            changePasswordLink.Font         = CustomFonts.Smaller;
            changePasswordLink.LinkArea     = new LinkArea(4, changePasswordLink.Text.Length - 4);
            changePasswordLink.Size         = changePasswordLink.PreferredSize;
            changePasswordLink.Left         = oldPasswordLabel.Left;
            changePasswordLink.Top          = deleteAccountButton.Top - changePasswordLink.Height - 10;
            changePasswordLink.LinkClicked += (s, e) =>
            {
                if (ConversationPanel.CurrentDisplayedConversationPanel != null)
                {
                    ConversationPanel.CurrentDisplayedConversationPanel.Visible = false; SlidebarPanel.MySidebarPanel.Visible = false;
                }
                (new LoginPanel(Universal.ParentForm, true, true)).ShowPasswordSetupPanel();
            };
            this.othersInfoEditPanel.Controls.Add(changePasswordLink);

            LinkLabel UnbindDeviceLink = new LinkLabel();

            UnbindDeviceLink.Text         = "&Unbind Device";
            UnbindDeviceLink.Font         = CustomFonts.Smaller;
            UnbindDeviceLink.LinkArea     = new LinkArea(0, UnbindDeviceLink.Text.Length);
            UnbindDeviceLink.Size         = UnbindDeviceLink.PreferredSize;
            UnbindDeviceLink.Left         = this.othersInfoEditPanel.Right - UnbindDeviceLink.Width - (changePasswordLink.Left - this.othersInfoEditPanel.Left);
            UnbindDeviceLink.Top          = changePasswordLink.Top;
            UnbindDeviceLink.LinkClicked += (sender, eargs) =>
            {
                string passwordError = StandardAssuranceLibrary.Checker.CheckOldPasswordMatch(this.currentPasswordTextBox.Text);
                if (passwordError != null && passwordError.Length > 0)
                {
                    Universal.ShowErrorMessage(passwordError, "Error!");
                    return;
                }

                DialogResult result = MessageBox.Show("Are you sure to unbind this device from this account?", "Confirm deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly);
                if (result == DialogResult.Yes)
                {
                    BackgroundWorker loaderWorker = new BackgroundWorker();
                    loaderWorker.DoWork += (s, e) =>
                    {
                        bool?success = ServerRequest.UnbindDeviceFromAccount();
                        if (success == true)
                        {
                            Universal.ShowInfoMessage("This device has been unbound\r\nsuccessfullly from user account '" + User.LoggedIn.Username + "'!", "Unbound successfull!");
                            BackendManager.Logout();
                        }
                        else
                        {
                            string operationMessage = "Sorry! The device unbound process has been failed!";
                            if (success == null)
                            {
                                operationMessage = "Server Connection Failed!";
                            }
                            Universal.ShowErrorMessage(operationMessage, "Unbound unsuccessful!");
                        }
                    };
                    loaderWorker.RunWorkerAsync();
                    loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); };
                }
            };
            this.othersInfoEditPanel.Controls.Add(UnbindDeviceLink);

            this.othersInfoEditPanel.Controls.Add(cancelButtonLabel);
        }
Пример #2
0
        public static void LoginProcessRun()                                                    //works as expected
        {
            bool?macExists = ServerRequest.MacAddressExists(Universal.SystemMACAddress);

            if (macExists == false)
            {
                if (Universal.ParentForm.InvokeRequired)
                {
                    Universal.ParentForm.Invoke(new MethodInvoker(delegate
                    {
                        Universal.ParentForm.Controls.Add(new SignupPanel(Universal.ParentForm));
                        SplashScreen.Instance.Hide();
                    }));
                }
                else
                {
                    Universal.ParentForm.Controls.Add(new SignupPanel(Universal.ParentForm));
                    SplashScreen.Instance.Hide();
                }
                return;
            }
            else if (macExists == null)
            {
                if (Universal.ParentForm.InvokeRequired)
                {
                    Universal.ParentForm.Invoke(new MethodInvoker(delegate
                    {
                        MessageBox.Show(Universal.ParentForm, "Server connection failed", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }));
                }
                else
                {
                    MessageBox.Show(Universal.ParentForm, "Server connection failed", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }

            JObject localCookieJson = Checker.ValidLoginCookieData(Universal.SystemMACAddress);

            if (localCookieJson != null && (Time.TimeDistanceInMinute(new Time(localCookieJson["last_login_time"].ToString()), Time.CurrentTime) > 4320 || localCookieJson["mac_address"].ToString() != Universal.SystemMACAddress))
            {
                LocalDataFileAccess.EraseCurrentLoginCookie();
                localCookieJson = null;
            }

            if (localCookieJson == null)
            {
                int?userVerifiedOrPasswordIsSet = ServerRequest.UserVerifiedOrPasswordIsSet(Universal.SystemMACAddress);
                if (userVerifiedOrPasswordIsSet == 0)            //0 means user is not verified
                {
                    if (Universal.ParentForm.InvokeRequired)
                    {
                        Universal.ParentForm.Invoke(new MethodInvoker(ShowEmailVerificationInputPanel));
                    }
                    else
                    {
                        ShowEmailVerificationInputPanel();
                    }
                }
                else if (userVerifiedOrPasswordIsSet == 1)          //1 means user verified but password is not set
                {
                    if (Universal.ParentForm.InvokeRequired)
                    {
                        Universal.ParentForm.Invoke(new MethodInvoker(ShowPasswordSetupPanel));
                    }
                    else
                    {
                        ShowPasswordSetupPanel();
                    }
                }
                else if (userVerifiedOrPasswordIsSet == 2)           //2 means user is verified and password is set
                {
                    if (Universal.ParentForm.InvokeRequired)
                    {
                        Universal.ParentForm.Invoke(new MethodInvoker(ShowPasswordEnterPanel));
                    }
                    else
                    {
                        ShowPasswordEnterPanel();
                    }
                }
                else if (userVerifiedOrPasswordIsSet == null)
                {
                    Universal.ShowErrorMessage("Server connection failed!");
                }
            }
            else
            {
                bool?loginSuccess = ServerRequest.LoginWithCookie(localCookieJson);
                if (loginSuccess == true)
                {
                    BackendManager.Password = localCookieJson["password"].ToString();
                    BackendManager.SaveLoginCookie();
                    BackendManager.LoginNow(User.LoggedIn);
                    SplashScreen.Instance.Hide();
                }
                else if (loginSuccess == false)
                {
                    LocalDataFileAccess.EraseCurrentLoginCookie();
                    LoginProcessRun();
                }
                else
                {
                    MessageBox.Show("Server connection failed!");
                }
            }
        }
Пример #3
0
 internal static void SetChangedPassword(string newPassword)
 {
     BackendManager.Password = newPassword;
     DatabaseAccess.Instance.ChangeLocalDataBasePassword(BackendManager.GenerateLocalDbPassword());
 }
        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();
            }
        }
Пример #5
0
 private void EventListener(object sender, EventArgs ee)
 {
     if (sender == this.bindButton)
     {
         string errorMessage = "";
         bool   allOk        = true;
         if (macAddressBox.Text == null || macAddressBox.Text.Length == 0)
         {
             errorMessage += "Invalid MAC Address Input!";
             allOk         = false;
         }
         if (usernameTextBox.Text == null || usernameTextBox.Text.Length == 0)
         {
             if (errorMessage.Length > 0)
             {
                 errorMessage += "\r\n";
             }
             errorMessage += "Username is empty!";
             allOk         = false;
         }
         if (passwordTextBox.Text == null || passwordTextBox.Text.Length == 0)
         {
             if (errorMessage.Length > 0)
             {
                 errorMessage += "\r\n";
             }
             errorMessage += "Password is empty!";
             allOk         = false;
         }
         if (allOk)
         {
             VisualizingTools.ShowWaitingAnimation(new Point(this.bindButton.Left, this.bindButton.Bottom + 20), new Size(this.bindButton.Width, this.bindButton.Height / 2), this);
             BackgroundWorker loaderWorker = new BackgroundWorker();
             loaderWorker.DoWork += (s, e) =>
             {
                 try
                 {
                     bool?success = ServerRequest.BindDeviceAndLogin(macAddressBox.Text, usernameTextBox.Text, passwordTextBox.Text);
                     if (success == true)
                     {
                         if (this.loginCheckbox.Checked)
                         {
                             BackendManager.SaveLoginCookie();
                         }
                         BackendManager.LoginNow(User.LoggedIn);
                         this.Invoke(new Action(() =>
                         {
                             VisualizingTools.HideWaitingAnimation();
                             this.Visible        = false;
                             this.parent.Visible = false;
                             this.parent.Dispose();
                             this.Dispose();
                         }
                                                ));
                     }
                     else if (success == false)
                     {
                         errorMessage = "Invalid username or password!";
                         this.Invoke(new Action(() =>
                         {
                             ShowErrorMessage(errorMessage);
                         }
                                                ));
                     }
                     else
                     {
                         errorMessage = "Server connection failed!";
                         this.Invoke(new Action(() =>
                         {
                             ShowErrorMessage(errorMessage);
                         }
                                                ));
                     }
                 }
                 catch { }
             };
             loaderWorker.RunWorkerAsync();
             loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); };
         }
         else
         {
             ShowErrorMessage(errorMessage);
         }
     }
     else if (sender == this.backButton)
     {
         this.parent.Visible = true;
         this.Dispose();
     }
 }
 private void EventListener(object sender, EventArgs ee)
 {
     if (sender == this.verifyButton)
     {
         string errorMessage = "";
         if (verificationCodeTextBox.Text == null || verificationCodeTextBox.Text.Length == 0)
         {
             errorMessage += "Verification Code Field Empty!";
             ShowErrorMessage(errorMessage);
             return;
         }
         VisualizingTools.ShowWaitingAnimation(new Point(this.verifyButton.Left, this.verifyButton.Bottom + 20), new Size(this.verifyButton.Width, this.verifyButton.Height / 2), this);
         BackgroundWorker loaderWorker = new BackgroundWorker();
         loaderWorker.DoWork += (s, e) =>
         {
             try
             {
                 int?status = null;
                 status = ServerRequest.VerifyVerificationCode(verificationCodeTextBox.Text, this.purpose);
                 if (status == 1)        //1 means verification code is verified successfully
                 {
                     this.Invoke(new Action(() =>
                     {
                         VisualizingTools.HideWaitingAnimation();
                         this.Visible = false;
                         this.Dispose();
                         if (this.parent != null)
                         {
                             this.parent.Hide();
                             this.parent.Dispose();
                         }
                     }
                                            ));
                     if (this.purpose == "email_verify")
                     {
                         BackendManager.LoginProcessRun();
                     }
                     else if (this.purpose == "password_reset")
                     {
                         Universal.ParentForm.Invoke(new MethodInvoker(BackendManager.ShowPasswordSetupPanel));
                     }
                 }
                 else
                 {
                     if (status == 2)       //2 means verification code is not valid
                     {
                         errorMessage = "Verification Code is invalid!";
                     }
                     else if (status == 3)   //3 means verification code is expried
                     {
                         errorMessage = "Verification Code is expired!\r\nA new verification code is sent.";
                     }
                     else if (status == 4)   //4 means too many wrong (more than 5) attempts
                     {
                         errorMessage = "Too many unsuccessful attempts!\r\nA new verification code is sent.";
                     }
                     else
                     {
                         errorMessage = "Server connection failed!";
                     }
                     this.Invoke(new Action(() =>
                     {
                         ShowErrorMessage(errorMessage);
                     }
                                            ));
                 }
             }
             catch (Exception ex) { Console.WriteLine("Exception in UserVerificationPanel.cs = > " + ex.Message); }
         };
         loaderWorker.RunWorkerAsync();
         loaderWorker.RunWorkerCompleted += (s, e) => { loaderWorker.Dispose(); };
     }
     else if (sender == this.backButton)
     {
         if (this.parent != null)
         {
             this.parent.Visible = true;
         }
         else
         {
             ServerRequest.DeleteConsumerAccount(Universal.SystemMACAddress, "");
             BackendManager.Logout();
         }
         this.Dispose();
     }
 }