/// <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; } }