Beispiel #1
0
        /// <summary>
        /// Import and populates CLipboarder grid with entries from database
        /// using DatabaseOperations, DatabaseReadWrite and User class
        /// </summary>
        private void ImportAndDisplayEntries()
        {
            if (!File.Exists(Path.Combine(Application.StartupPath, "contents.db")))
            {
                MessageBox.Show("No content to load." + "\n\n" + "Use Menu > Save Content to save entries.",
                                "Clipboarder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                // Creates DatabaseOperations object to perform operations on specified database
                DatabaseOperations dbOperations = new DatabaseOperations();

                // Connects to database and opens connection
                try {
                    dbOperations = new DatabaseOperations();
                    dbOperations.ConnectDatabase(databaseName);
                    dbOperations.OpenConnection();
                } catch (Exception ex) {
                    MessageBox.Show("Error connecting database, database does not exists or is unreachable."
                                    + ex.Message + "\n\nOperation aborted.\n", "Clipboarder Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    view.status = "Error";
                    dbOperations.CloseConnection();
                    return;
                }

                User user = new User(dbOperations);
                user.GetCurrentUserID();

                // Shows AskPasswordDecrypt form to ask user for password,
                // this call also passes hashed password from the database
                // to inform user for incorrect password.
                AskPasswordDecrypt askPassword = new AskPasswordDecrypt(this, user.GetUserPassword());
                DialogResult       result      = askPassword.ShowDialog();

                // If DialogResult.OK == true then AskPasswordDialog will provide password to
                // password field in MainFormPresenter instance earlier passed in constructor
                if (result != DialogResult.OK)
                {
                    dbOperations.CloseConnection();
                    return;
                }

                // Checks equality of password provided and stored
                if (!user.CurrentUserHasID())
                {
                    MessageBox.Show("Content for current user doesn't exists.\n\nOperation aborted.\n",
                                    "Clipboarder Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dbOperations.CloseConnection();
                    return;
                }
                else
                {
                    view.ClearAll();
                    DatabaseReadWrite dbContent = new DatabaseReadWrite(dbOperations, user);
                    try {
                        view.status = "Reading text from database";
                        List <EncryptedTextContent> encryptedList = dbContent.GetTextData();
                        List <TextContent>          outputList    = new List <TextContent>();

                        // Resets progress
                        view.ProgressVisibility = true;
                        view.TaskProgress       = 0;

                        // Decrypts and adds text content to textDataGrid
                        encryptedList.ForEach((encryptedContent) => {
                            view.TaskProgress        = (100 / encryptedList.Count) * encryptedContent.index;
                            TextContent contentToAdd =
                                ContentEncryption.DecryptTextContent(encryptedContent, password);
                            view.AddNewTextRow(contentToAdd);
                        });
                        view.TaskProgress = 0;
                    } catch (Exception ex) {
                        MessageBox.Show("Error filling table with values.\n\n\nOperation aborted." + Environment.NewLine,
                                        "Clipboarder Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        view.status = "Error";
                        dbOperations.CloseConnection();
                        return;
                    }

                    try {
                        view.status = "Reading images from database.";

                        List <EncryptedImageContent> encryptedList = dbContent.GetImageData();
                        List <ImageContent>          outputList    = new List <ImageContent>();

                        //progress bar values
                        view.TaskProgress       = 0;
                        view.ProgressVisibility = true;

                        // Decrypts image content
                        encryptedList.ForEach(encryptedContent => {
                            view.TaskProgress         = (100 / encryptedList.Count) * encryptedContent.index;
                            ImageContent contentToAdd =
                                ContentEncryption.DecryptImageContent(encryptedContent, password);
                            view.AddNewImageRow(contentToAdd);
                        });
                        view.TaskProgress = 0;
                    } catch (Exception) {
                        MessageBox.Show("Error filling table with values.\n\n\nOperation aborted.\n",
                                        "Clipboarder Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        view.status = "Error";
                        dbOperations.CloseConnection();
                        return;
                    }
                }
                view.status             = "Imported successfully";
                view.ProgressVisibility = false;
                dbOperations.CloseConnection();
            } // Else statement for check on file existence
            password = "";
        }