Skip to content

Commit

Permalink
beta1.1indev1 - Centralized dbcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
410 committed Dec 16, 2021
1 parent 251ec80 commit 6312bd6
Show file tree
Hide file tree
Showing 14 changed files with 661 additions and 412 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ Vault is a secure, encrypted file database. It is written in Java, with combinat
- [x] Create SQLite3 DB on start if not exists

## Roadmap for Beta 1.1
- [ ] Restructure codes
- [ ] Fix bugs
- [x] Centralize DB controller
- [ ] Check DB compatibility

## Roadmap for Beta 2.0
- [ ] Searching System
- [ ] Large file support
10 changes: 5 additions & 5 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import screens.Frame;
import screens.views.Login;
import screens.views.Signup;
import utils.data.Static;
import utils.data.Constants;

public class Main {
public static void main(String[] args) throws Exception {
Expand All @@ -15,22 +15,22 @@ public static void main(String[] args) throws Exception {
for(int i = 0; i < args.length; i++) {
if (args[i].equals("-d")) {
if (args.length > i + 1) {
Static.DATABASE_NAME = args[i + 1];
Constants.DATABASE_NAME = args[i + 1];
}
}
}
}

SQLite3.dbFile = Static.DATABASE_NAME;
SQLite3.dbFile = Constants.DATABASE_NAME;
Frame.createFrame();

// Check if file exist
File db = new File(Static.DATABASE_NAME);
File db = new File(Constants.DATABASE_NAME);

// If exists, then login. Otherwise create new database and signup
if (db.isFile()) Frame.setContent(new Login());
else {
SQLite3.createDatabase();
SQLite3.initialSetup();
Frame.setContent(new Signup());
}

Expand Down
15 changes: 13 additions & 2 deletions src/database/SQLite3.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.sql.ResultSet;
import java.sql.Statement;

import screens.ColorScheme;
import utils.data.Constants;

public class SQLite3 {

private static Connection con = null;
Expand Down Expand Up @@ -51,10 +54,18 @@ public static ResultSet executeQuery(SQLStatementBuilder statement) throws Excep
return executeQuery(statement.build());
}

public static void createDatabase() throws Exception {
String sqlStatementForData = "CREATE TABLE \"data\" (\"id\" integer,\"data\" text,\"owner\" text,\"type\" text,\"addedDate\" text,\"readDate\" text,\"index\" text,\"tags\" text,\"modifiedDate\" text,\"title\" text, PRIMARY KEY (id))";
public static void initialSetup() throws Exception {
String sqlStatementForData = "CREATE TABLE \"data\" (\"id\" integer,\"data\" text,\"owner\" text,\"type\" text,\"addedDate\" text,\"readDate\" text,\"tags\" text,\"modifiedDate\" text,\"title\" text,\"file\" BLOB, PRIMARY KEY (id))";
String sqlStatementForUser = "CREATE TABLE \"users\" (\"id\" integerserial,\"user\" text NOT NULL, PRIMARY KEY (id))";
String sqlStatementForCompatibility = "CREATE TABLE \"compatibility\" (\"id\" integerserial,\"key\" text NOT NULL,\"value\" text NOT NULL, PRIMARY KEY (id))";
String sqlStatementForConfigurations = "CREATE TABLE \"configurations\" (\"id\" integerserial,\"key\" text NOT NULL,\"value\" text NOT NULL, PRIMARY KEY (id))";
executeQuery(sqlStatementForData);
executeQuery(sqlStatementForUser);
executeQuery(sqlStatementForCompatibility);
executeQuery(sqlStatementForConfigurations);

executeQuery("INSERT INTO compatibility (key, value) VALUES ('version', '" + Constants.VERSION + "');");
executeQuery("INSERT INTO compatibility (key, value) VALUES ('databaseType', '" + Constants.DBTYPE + "');");
executeQuery("INSERT INTO configurations (key, value) VALUES ('color', '" + ColorScheme.ID_WHITE + "');");
}
}
21 changes: 21 additions & 0 deletions src/screens/ColorScheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,25 @@ public class ColorScheme {
public static Color background = Color.WHITE;
public static Color text = Color.BLACK;
public static Color button = Color.GRAY;

public static String ID_BLACK = "black";
public static String ID_WHITE = "white";

public static void setColor(String id) {
if (id.equals(ID_BLACK)) {
background = Color.BLACK;
text = Color.WHITE;
button = Color.GRAY;
} else if (id.equals(ID_WHITE)) {
background = Color.WHITE;
text = Color.BLACK;
button = Color.GRAY;
}

repaintAllWindows();
}

private static void repaintAllWindows() {

}
}
4 changes: 2 additions & 2 deletions src/screens/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import javax.swing.JPanel;

import database.SQLite3;
import utils.data.Static;
import utils.data.Constants;

public class Frame {
public static JFrame frame;

public static void createFrame() {
frame = new JFrame("Vault (" + Static.VERSION + ")");
frame = new JFrame("Vault (" + Constants.VERSION + ")");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setMinimumSize(new java.awt.Dimension(400, 400));
Expand Down
79 changes: 20 additions & 59 deletions src/screens/views/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;

import java.awt.event.MouseEvent;
Expand All @@ -26,7 +25,6 @@
import screens.views.subviews.NewEntry;
import screens.views.subviews.ViewEntry;

import utils.CoreCryptography;
import utils.data.Entries;
import utils.data.Entry;
import utils.data.UserInfo;
Expand All @@ -40,7 +38,7 @@ public class Home extends JPanel implements UpdatableColor {
private static boolean fileListReady = false;
private static ViewDimension frameDimension = new ViewDimension();

private JList fileLists;
private JList<Object> fileLists;
private JButton addButton = new JButton("Add");
private JButton aboutButton = new JButton("About");
private JButton searchButton = new JButton("Search...");
Expand Down Expand Up @@ -148,20 +146,22 @@ public void run() {
ArrayList<String> entryNames = new ArrayList<>();
for (Entry entry : Entries.getEntries()) {
String tags = "";
for (String tag : entry.getTags()) {
tags += "#" + tag + " ";
}
// TODO: Add tags
// for (String tag : entry.getTags()) {
// tags += "#" + tag + " ";
// }
int prefixLength = entry.getType().length() + 2;
String typePrefix = "[" + entry.getType() + "]";
for(int i = prefixLength; i < 17; i++) {
typePrefix += " ";
}
entryNames.add(typePrefix + entry.getName() + " " + tags + " (" + entry.getAddedDate() + ")");
}
fileLists = new JList(entryNames.toArray());
fileLists = new JList<>(entryNames.toArray());
fileLists.addMouseListener(new MouseInputAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
@SuppressWarnings("unchecked")
JList<Object> list = (JList<Object>)evt.getSource();
if (evt.getClickCount() == 2) {
int index = list.locationToIndex(evt.getPoint());
if (index >= 0) {
Expand Down Expand Up @@ -196,9 +196,6 @@ public void mouseClicked(MouseEvent evt) {

private void buildUI() {
// Create components


// TODO: Add more buttons and functionalities
asyncFileListUpdate();

while(true) {
Expand All @@ -221,13 +218,7 @@ private void buildUI() {
addButton.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFrame frame = new JFrame("Add Entry");
frame.setResizable(true);
frame.setSize(300, 300); // TODO: Change the dimension
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

new NewEntry(frame, userInfo);
new NewEntry(new JFrame("Add Entry"), userInfo);
}
});

Expand Down Expand Up @@ -274,58 +265,28 @@ public void run() {
ResultSet rs = SQLite3.executeQuery(builder);

while(rs.next()) {
String id = rs.getInt("id") + "";
String data = rs.getString("data");
String type = rs.getString("type");
String addedDate = rs.getString("addedDate");
String modified = rs.getString("modifiedDate");
String readDate = rs.getString("readDate");
String tags = rs.getString("tags");
String title = rs.getString("title");

if (addedDate == null || addedDate.equals("") || addedDate.equals("null")) {
break;
}

addedDate = CoreCryptography.decrypt(addedDate, userInfo.getDecryptString(userInfo.getLoginToken()));
type = CoreCryptography.decrypt(type, userInfo.getDecryptString(userInfo.getLoginToken()));
modified = CoreCryptography.decrypt(modified, userInfo.getDecryptString(userInfo.getLoginToken()));
readDate = CoreCryptography.decrypt(readDate, userInfo.getDecryptString(userInfo.getLoginToken()));
tags = CoreCryptography.decrypt(tags, userInfo.getDecryptString(userInfo.getLoginToken()));
title = CoreCryptography.decrypt(title, userInfo.getDecryptString(userInfo.getLoginToken()));

ArrayList<String> tags_list = new ArrayList<>();
if(!tags.equals("")) {
String[] tags_array = tags.split(",");
for(String tag : tags_array) {
tags_list.add(tag);
}
Entry entry = new Entry(rs.getString("title"), rs.getInt("id") + "", rs.getString("data"), rs.getString("type"), rs.getString("addedDate"), rs.getString("modifiedDate"), rs.getString("readDate"), rs.getString("tags"), true);
int exit = entry.decrypt(userInfo, false);
switch (exit) {
case Entry.DECRYPT_FAILURE:
JOptionPane.showMessageDialog(null, "Error while decrypting data");
System.exit(1);
SQLite3.close();
break;
}

ArrayList<String> readDate_list = new ArrayList<>();
if(!readDate.equals("")) {
String[] readDate_array = readDate.split(",");
for(String date : readDate_array) {
readDate_list.add(date);
}
}

ArrayList<String> modified_list = new ArrayList<>();
if(!modified.equals("")) {
String[] modified_array = modified.split(",");
for(String date : modified_array) {
modified_list.add(date);
}
if (entry.getAddedDate() == null || entry.getAddedDate().equals("") || entry.getAddedDate().equals("null")) {
break;
}

Entry entry = new Entry(title, Long.parseLong(id), data, type, addedDate, readDate_list, modified_list, tags_list);
Entries.add(entry);
}

decryptionComplete = true;
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error while decrypting data");
JOptionPane.showMessageDialog(null, "Error while reading data");
System.exit(1);
SQLite3.close();
}
Expand Down
3 changes: 0 additions & 3 deletions src/screens/views/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public Login() {
buttonSignUp.setBackground(ColorScheme.button);
buttonLogin.setBackground(ColorScheme.button);


// Place components
// TODO: Set the components to be relative and neat
labelName.setBounds(Frame.frame.getWidth() / 2 - 100, Frame.frame.getHeight() / 2 - 100, 100, 20);
fieldName.setBounds(Frame.frame.getWidth() / 2 - 100, Frame.frame.getHeight() / 2 - 50, 200, 20);

Expand Down
1 change: 0 additions & 1 deletion src/screens/views/Signup.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public Signup() {
this.setLayout(null);

// Set bounds for components
// TODO: Set the components to be relative and neat
labelName.setBounds(Frame.frame.getWidth() / 2 - 100, Frame.frame.getHeight() / 2 - 100, 100, 20);
fieldName.setBounds(Frame.frame.getWidth() / 2 - 100, Frame.frame.getHeight() / 2 - 50, 200, 20);

Expand Down
9 changes: 4 additions & 5 deletions src/screens/views/subviews/About.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

import screens.ColorScheme;
import screens.UpdatableColor;
import utils.data.Static;
import utils.data.Constants;

public class About extends JPanel implements UpdatableColor {

public static boolean isOpen = false;

public About(JFrame frame) {
JLabel version = new JLabel("Version: " + Static.VERSION);
JLabel author = new JLabel("Author: " + Static.DEVELOPER);
JLabel copyright = new JLabel("Copyright (c) " + Static.DEVELOPER + " 2021-2022");
JLabel version = new JLabel("Version: " + Constants.VERSION);
JLabel author = new JLabel("Author: " + Constants.DEVELOPER);
JLabel copyright = new JLabel("Copyright (c) " + Constants.DEVELOPER + " 2021-2022");
JLabel disclaimer = new JLabel("Developer does not have any responsibility for any damages caused by this program.");
JLabel disclaimer2 = new JLabel("This program is currently in development.");

Expand Down Expand Up @@ -51,7 +51,6 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {

@Override
public void updateColor() {
// TODO Auto-generated method stub
this.setBackground(ColorScheme.background);
repaint();
}
Expand Down
Loading

0 comments on commit 6312bd6

Please sign in to comment.