コード例 #1
0
        /// <summary>
        /// Exports all clipboarder entries to database
        /// </summary>
        public void ExportEntries()
        {
            List <TextContent>  textContents  = view.GetAllTextContent();
            List <ImageContent> imageContents = view.GetAllImageContent();

            if (textContents.Count == 0 && imageContents.Count == 0)
            {
                MessageBox.Show("No entries to save.", "Clipboarder",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                AskPasswordEncrypt askPassword = new AskPasswordEncrypt(this);
                DialogResult       result      = askPassword.ShowDialog();

                // Uses DatabaseOperations class object to connect and write to database Clipboarder.Extension
                DatabaseOperations dbOperations = new DatabaseOperations();

                if (result == DialogResult.OK)
                {
                    if (!File.Exists(System.IO.Path.Combine(Application.StartupPath, databaseName)))
                    {
                        //Creates new Database
                        DatabaseOperations.CreatesNewDatabase(databaseName);
                        Properties.Settings.Default.doesDatabaseExists = true;
                    }

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

                    User user = new User(dbOperations);
                    if (user.CurrentUserHasID())
                    {
                        user.GetCurrentUserID();
                    }
                    DatabaseReadWrite dbContents = new DatabaseReadWrite(dbOperations, user);
                    // Deletes existing content for current user if corresponding record exists in userTable

                    if (user.CurrentUserHasID())
                    {
                        try {
                            //dbContents.clearAllContent();
                            dbContents.DeleteAllRecordsForCurrentUsers();
                            user.DeleteEntry();
                            user.CreateEntry(BCrypt.HashPassword(password, BCrypt.GenerateSalt(10)));
                            user.GetCurrentUserID();
                        } catch (Exception ex) {
                            DialogResult messageResult =
                                MessageBox.Show("Error deleting existing records from database.\n"
                                                + "\n\nDo you want to continue?.", "Clipboarder Error",
                                                MessageBoxButtons.YesNo, MessageBoxIcon.Stop);

                            dbOperations.CloseConnection();
                            if (messageResult == DialogResult.No)
                            {
                                return;
                            }
                        }
                    }
                    else
                    {
                        // Hashes password using BCrypt Class and adds new record to userName table in database
                        try {
                            user.CreateEntry(BCrypt.HashPassword(password, BCrypt.GenerateSalt(10)));
                            user.GetCurrentUserID();
                        } catch (Exception ex) {
                            MessageBox.Show("Error adding current user record to database." + ex.ToString()
                                            + "\n\nOperation aborted.\n", "Clipboarder Error", MessageBoxButtons.OK,
                                            MessageBoxIcon.Stop);
                            dbOperations.CloseConnection();
                            return;
                        }
                    }

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

                    //-------------------------------------------------------------
                    // Exports text Entries

                    textContents.ForEach(content => {
                        // Sets task progress
                        view.TaskProgress = (100 / textContents.Count) * (content.index);
                        EncryptedTextContent encryptedContent =
                            ContentEncryption.EncryptTextContent(content, password);

                        // Inserts content to database
                        try {
                            dbContents.SetTextContent(encryptedContent);
                        } catch (Exception ex) {
                            MessageBox.Show("Error adding content to database. \n\nOperation aborted.\n"
                                            + ex.ToString(), "Clipboarder Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            dbOperations.CloseConnection();
                            return;
                        }
                    });

                    view.TaskProgress = 0;

                    //-------------------------------------------------------------
                    // Exports image Entries

                    imageContents.ForEach(content => {
                        // Sets task progress
                        view.TaskProgress = (100 / imageContents.Count) * (content.index);
                        EncryptedImageContent encryptedContent =
                            ContentEncryption.EncryptImageContent(content, password);

                        // Inserts content to database
                        try {
                            dbContents.SetImageContent(encryptedContent);
                        } catch (Exception ex) {
                            MessageBox.Show("Error adding content to database. \n\nOperation aborted.\n"
                                            + ex.ToString(), "Clipboarder Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            dbOperations.CloseConnection();
                            return;
                        }
                    });
                    view.TaskProgress       = 0;
                    view.ProgressVisibility = false;
                    view.status             = "Export Completed";
                    dbOperations.CloseConnection();
                }
                password = null;
            }
        }
コード例 #2
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 = "";
        }