Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "editor.h"
- #include "ui_editor.h"
- #include <QFileDialog>
- #include <QDebug>
- #include <QListWidget>
- #include <String.h>
- #include <iostream>
- #include <stdio.h>
- #include <QDesktopServices>
- #include "editrecent.h"
- #include "settings.h"
- #include "usernamepasswordgenerator.h"
- #include "global.h"
- #include "thisapp.h"
- #include <QMessageBox>
- #include "getkey.h"
- #include <QFileSystemModel>
- #include <QStandardItem>
- #include <QTreeWidgetItem>
- #include <QHeaderView>
- #include <AES/qaesencryption.h>
- #include <QCryptographicHash>
- #include <filecreationwizard.h>
- #include <newfiletreewizard.h>
- //change this shit later
- QString Editor::openedDir = "C:\\Users\\Full_Nitrous\\Desktop\\TestFolder";
- QString Editor::key = "";
- QString Editor::iv = "";
- QFileSystemModel Editor::*globalDirModel;
- QMenu Editor::*commandFileContextMenu;
- QPoint Editor::lastCommandFilePoint;
- Editor::Editor(QWidget *parent) : QMainWindow(parent), ui(new Ui::Editor)
- {
- ui->setupUi(this);
- readRecentDotDat();
- ui->actionFind->setEnabled(false);
- ui->actionUndo->setEnabled(false);
- ui->actionRedo->setEnabled(false);
- ui->actionSave_Selected_File->setEnabled(false);
- ui->actionFullscreen->setEnabled(false);
- ui->actionSettings->setEnabled(false);
- ui->actionCustom->setEnabled(false);
- ui->actionNew_Command_File->setEnabled(false);
- ui->actionNew_password_file->setEnabled(false);
- ui->actionNew->setEnabled(false);
- ui->actionNew_Text_File->setEnabled(false);
- ui->actionStart_Menu->setEnabled(false);
- ui->label->setPixmap(QPixmap(startPagePicture));
- ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
- ui->tableWidget->setColumnWidth(1, 60);
- ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
- connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested(QPoint)));
- commandFileContextMenu = new QMenu(this);
- commandFileContextMenu->addAction(tr("Add New Command"));
- commandFileContextMenu->addAction(tr("Remove Command"));
- connect(commandFileContextMenu, SIGNAL(triggered(QAction*)), SLOT(editCommandRows(QAction*)));
- readDirectoryToEditor();
- return;
- }
- Editor::~Editor()
- {
- delete ui;
- return;
- }
- void Editor::editCommandRows(QAction* action)
- {
- QString actiontodo = action->text();
- if(actiontodo == "Add New Command")
- {
- ui->tableWidget->insertRow(ui->tableWidget->rowCount());
- ui->tableWidget->verticalHeader()->resizeSection(ui->tableWidget->rowCount() - 1, 20);
- }
- else if(actiontodo == "Remove Command")
- {
- QModelIndex index = ui->tableWidget->indexAt(lastCommandFilePoint);
- ui->tableWidget->removeRow(index.row());
- }
- return;
- }
- void Editor::customMenuRequested(QPoint pos)
- {
- lastCommandFilePoint = pos;
- commandFileContextMenu->popup(ui->tableWidget->viewport()->mapToGlobal(pos));
- return;
- }
- void Editor::enterEditor(QString directory)
- {
- qDebug() << "entered editor with: " << directory;
- if(directory == "" || !QDir(directory).exists())
- {
- QMessageBox messageBox;
- messageBox.critical(0,"Error","Invalid directory");
- messageBox.setFixedSize(500,200);
- }
- else
- {
- openedDir = directory;
- promptKey();
- }
- return;
- }
- QString Editor::promptKey()
- {
- QString returnValue = "";
- getKey gk;
- gk.setModal(true);
- gk.exec();
- QObject::connect(&gk, SIGNAL(gotKey(QString)), this, SLOT(continueToEnteringEditor(QString)));
- return returnValue;
- }
- void Editor::continueToEnteringEditor(QString keyInput)
- {
- bool wrongCharacters = false;
- if(keyInput == "" || wrongCharacters)
- {
- QMessageBox messageBox;
- messageBox.critical(0,"Input Error","Invalid characters and or no characters were used.");
- messageBox.setFixedSize(500,200);
- }
- else
- { key = keyInput;
- ui->actionNew_password_file->setEnabled(true);
- ui->actionNew_Command_File->setEnabled(true);
- ui->actionNew_Text_File->setEnabled(true);
- ui->actionStart_Menu->setEnabled(true);
- ui->actionFind->setEnabled(true);
- ui->actionStart_Menu->setEnabled(true);
- ui->actionNew->setEnabled(true);
- this->setWindowTitle("Editor - " + openedDir);
- ui->stackedWidget->setCurrentIndex(1);
- readDirectoryToEditor();
- }
- return;
- }
- void Editor::readDirectoryToEditor(void)
- {
- globalDirModel = new QFileSystemModel(this);
- ui->treeView->setModel(globalDirModel);
- ui->treeView->setRootIndex(globalDirModel->setRootPath(openedDir + "/DRT/"));
- ui->treeView->setSelectionMode(QAbstractItemView::SingleSelection);
- ui->treeView->setDragDropMode(QAbstractItemView::InternalMove);
- ui->treeView->hideColumn(1);
- ui->treeView->hideColumn(2);
- ui->treeView->hideColumn(3);
- return;
- }
- void Editor::on_treeView_clicked(const QModelIndex &index)
- {
- QFileSystemModel *dirModel = new QFileSystemModel(this);
- QString path = dirModel->fileInfo(index).absoluteFilePath();
- QString extension = dirModel->fileInfo(index).suffix();
- bool isDir = dirModel->fileInfo(index).isDir();
- if(isDir)
- {
- ui->stackedWidget_2->setCurrentIndex(3);
- ui->actionSave_Selected_File->setEnabled(false);
- }
- else if(!isDir)
- {
- ui->actionSave_Selected_File->setEnabled(true);
- if(extension == "ea1")
- {
- QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC);
- QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
- QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Sha256);
- QFile file(path);
- file.open(QIODevice::ReadOnly);
- QByteArray encryptedData = file.readAll();
- QByteArray decodeText = encryption.decode(encryptedData, hashKey, hashIV);
- int last;
- QString container = "";
- for(int i = 0; i < decodeText.length(); i++)
- {
- if(decodeText[i] == '\n')
- {
- ui->lineEdit_2->setText(container);
- container = "";
- last = i + 1;
- break;
- }
- else
- {
- container += decodeText[i];
- }
- }
- for(int i = last; i < decodeText.length(); i++)
- {
- if(decodeText[i] == '\n')
- {
- ui->lineEdit->setText(container);
- container = "";
- last = i + 1;
- break;
- }
- else
- {
- container += decodeText[i];
- }
- }
- for(int i = last; i < decodeText.length(); i++)
- {
- if(decodeText[i] == '\0')
- {
- break;
- }
- container += decodeText[i];
- }
- ui->plainTextEdit->setPlainText(container);
- ui->stackedWidget_2->setCurrentIndex(2);
- }
- else if(extension == "cmd")
- {
- ui->tableWidget->setRowCount(0);
- QString data = "";
- int delay = 0;
- char c;
- FILE *file;
- file = fopen(path.toStdString().c_str(), "r");
- while((c = getc(file)) != EOF)
- {
- ungetc(c, file);
- delay = getc(file);
- delay = (delay << 8) | getc(file);
- while((c = getc(file)) != EOF)
- {
- if(c == '\n')
- {
- ui->tableWidget->insertRow (ui->tableWidget->rowCount());
- ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 0, new QTableWidgetItem(data));
- ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 1, new QTableWidgetItem(QString::number(delay)));
- ui->tableWidget->verticalHeader()->resizeSection(ui->tableWidget->rowCount() - 1, 20);
- data = "";
- delay = 0;
- break;
- }
- else
- {
- data += c;
- }
- }
- }
- fclose(file);
- ui->stackedWidget_2->setCurrentIndex(1);
- }
- else if(extension == "txt")
- {
- ui->stackedWidget_2->setCurrentIndex(0);
- QString data = "";
- char c;
- FILE *file;
- file = fopen(path.toStdString().c_str(), "r");
- while((c = fgetc(file)) != EOF)
- {
- data += c;
- }
- ui->textEdit_2->setText(data);
- fclose(file);
- }
- else
- {
- ui->stackedWidget_2->setCurrentIndex(3);
- }
- }
- delete dirModel;
- return;
- }
- void Editor::readRecentDotDat(void)
- {
- FILE *file;
- file = fopen(recentDirectoriesFilePath, "r");
- char c;
- std::string container = "";
- while((c = getc(file)) != EOF)
- {
- if(c == '\n')
- {
- ui->label_3->setText(container.c_str());
- container = "";
- break;
- }
- else
- {
- container += c;
- }
- }
- rewind(file);
- while((c = getc(file)) != EOF)
- {
- if(c == '\n')
- {
- ui->recentDirectoriesList->addItem(container.c_str());
- container = "";
- }
- else
- {
- container += c;
- }
- }
- fclose(file);
- return;
- }
- void Editor::on_recentDirectoriesList_itemClicked(QListWidgetItem *item)
- {
- ui->label_3->setText(item->text());
- return;
- }
- void Editor::on_recentDirectoriesList_itemDoubleClicked(QListWidgetItem *item)
- {
- enterEditor(item->text());
- return;
- }
- void Editor::on_actionAdd_To_Recent_Directories_triggered()
- {
- EditRecent er;
- QObject::connect(&er, SIGNAL(recentDirectoriesChange()), this, SLOT(reloadRecentDirectories()));
- er.setModal(true);
- er.exec();
- return;
- }
- void Editor::reloadRecentDirectories(void)
- {
- ui->recentDirectoriesList->clear();
- readRecentDotDat();
- return;
- }
- void Editor::on_pushButton_clicked()
- {
- QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
- if(dir != "")
- {
- std::string addThis = dir.toStdString() + '\n';
- bool exists = false;
- FILE *file;
- file = fopen(recentDirectoriesFilePath, "r");
- std::string container = "";
- char c;
- while((c = getc(file)) != EOF)
- {
- if(c == '\n')
- {
- if(container + '\n' == addThis)
- {
- exists = true;
- }
- container = "";
- }
- else
- {
- container += c;
- }
- }
- fclose(file);
- if(!exists)
- {
- file = fopen(recentDirectoriesFilePath, "a");
- fwrite(addThis.c_str(), sizeof(char), addThis.length(), file);
- fclose(file);
- }
- emit reloadRecentDirectories();
- }
- enterEditor(dir);
- return;
- }
- void Editor::on_pushButton_2_clicked()
- {
- QString a = ui->label_3->text();
- enterEditor(a);
- return;
- }
- void Editor::on_actionQuit_triggered()
- {
- qApp->exit();
- return;
- }
- void Editor::on_actionDark_triggered()
- {
- setStyleSheet(
- "QMainWindow "
- "{"
- " background: 'yellow';"
- "}"
- "QPushButton"
- "{"
- " background: 'black';"
- "}"
- "");
- return;
- }
- void Editor::on_actionLight_triggered()
- {
- setStyleSheet("");
- return;
- }
- void Editor::on_actionFullscreen_triggered()
- {
- if(ui->actionFullscreen->text() == "Fullscreen")
- {
- Editor::showFullScreen();
- ui->actionFullscreen->setText("Normalize");
- }
- else if(ui->actionFullscreen->text() == "Normalize")
- {
- Editor::showNormal();
- ui->actionFullscreen->setText("Fullscreen");
- }
- return;
- }
- void Editor::on_actionSettings_triggered()
- {
- Settings settings;
- settings.setTab(0);
- settings.setModal(true);
- settings.exec();
- return;
- }
- void Editor::on_actionUsername_and_Password_Generator_triggered()
- {
- UsernamePasswordGenerator UPGen;
- UPGen.setModal(true);
- UPGen.exec();
- return;
- }
- void Editor::on_actionWebsite_triggered()
- {
- QString link = "http://www.voidzehn.com";
- QDesktopServices::openUrl(QUrl(link));
- return;
- }
- void Editor::on_actionThis_Application_triggered()
- {
- ThisApp ta;
- ta.setModal(true);
- ta.exec();
- return;
- }
- void Editor::on_actionCustom_triggered()
- {
- Settings settings;
- settings.setTab(3);
- settings.setModal(true);
- settings.exec();
- return;
- }
- void Editor::on_actionOffline_Manuals_triggered()
- {
- QString link = "file:///C://Users//Full_Nitrous//Downloads//pepe_baloon_1_2.pdf";
- QDesktopServices::openUrl(QUrl(link));
- return;
- }
- void Editor::on_actionOnline_manuals_triggered()
- {
- QString link = "http://www.voidzehn.com/pdfsomething";
- QDesktopServices::openUrl(QUrl(link));
- return;
- }
- void Editor::on_actionContact_triggered()
- {
- QString link = "http://www.voidzehn.com/contact";
- QDesktopServices::openUrl(QUrl(link));
- return;
- }
- //this function has to get moved to a context menu instead of being static control
- /*
- void Editor::on_pushButton_4_clicked()
- {
- QModelIndex index = ui->treeView->currentIndex();
- QFileSystemModel *dirModel = new QFileSystemModel(this);
- QString path = dirModel->fileInfo(index).absoluteFilePath();
- QString dir = dirModel->fileInfo(index).dir().path();
- QString extension = dirModel->fileInfo(index).suffix();
- bool isDir = dirModel->fileInfo(index).isDir();
- if(ui->lineEdit_3->text() == "" || path == "")
- {
- QMessageBox messageBox;
- messageBox.information(0, "Information", "Invalid path and or no new name was given.");
- messageBox.setFixedSize(500,200);
- }
- else
- {
- if(isDir)
- {
- QString newPath = dir + '/' + ui->lineEdit_3->text();
- QDir currentDirectory(path);
- currentDirectory.rename(currentDirectory.path(), newPath);
- }
- else
- {
- QString newPath = dir + '/' + ui->lineEdit_3->text() + '.' + extension;
- QFile file(newPath);
- if(file.exists())
- {
- QMessageBox messageBox;
- messageBox.information(0, "Information", "File already exists.");
- messageBox.setFixedSize(500,200);
- file.close();
- }
- else
- {
- QFile file(path);
- if(file.exists())
- {
- file.rename(newPath);
- }
- file.close();
- ui->lineEdit_3->setText("");
- }
- }
- }
- delete dirModel;
- return;
- }
- */
- void Editor::on_actionSave_Selected_File_triggered()
- {
- //get what file is selected
- qDebug() << "save";
- QModelIndex index = ui->treeView->currentIndex();
- QFileSystemModel *dirModel = new QFileSystemModel(this);
- QString path = dirModel->fileInfo(index).absoluteFilePath();
- QString extension = dirModel->fileInfo(index).suffix();
- if(extension == "ea1")
- {
- QString username = ui->lineEdit_2->text();
- QString password = ui->lineEdit->text();
- QString additionalNotes = ui->plainTextEdit->toPlainText();
- QString combinedData = username + '\n' + password + '\n' + additionalNotes;
- QAESEncryption encryption2(QAESEncryption::AES_256, QAESEncryption::CBC);
- QByteArray hashKey2 = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
- QByteArray hashIV2 = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Sha256);
- QByteArray encoded = encryption2.encode(combinedData.toLocal8Bit(), hashKey2, hashIV2);
- QFile file(path);
- file.open(QIODevice::WriteOnly);
- file.write(encoded);
- file.close();
- }
- else if(extension == "txt")
- {
- QByteArray text = ui->textEdit_2->toPlainText().toLocal8Bit();
- QFile file(path);
- file.open(QIODevice::WriteOnly);
- file.write(text);
- file.close();
- }
- else if(extension == "cmd")
- {
- QByteArray command;
- int delay;
- QByteArray delayAsBytes;
- //QFile file(path);
- //file.open(QIODevice::WriteOnly);
- int rows = ui->tableWidget->rowCount();
- qDebug() << rows;
- bool convertOK;
- QFile file(path);
- file.open(QIODevice::WriteOnly);
- for(int i = 0; i < rows; i++)
- {
- command = ui->tableWidget->item(i, 0)->text().toLocal8Bit();
- delay = ui->tableWidget->item(i, 1)->text().toInt(&convertOK);
- delayAsBytes.append((char)((delay >> 8) & 0xFF));
- delayAsBytes.append((char)(delay & 0xFF));
- file.write(delayAsBytes);
- file.write(command.append('\n'));
- qDebug() << "wrote this: " << delayAsBytes;
- qDebug() << "wrote this: " << command;
- delayAsBytes.clear();
- }
- file.close();
- }
- delete dirModel;
- return;
- }
- void Editor::on_pushButton_3_clicked()
- {
- NewFileTreeWizard nftwiz;
- nftwiz.setModal(true);
- nftwiz.exec();
- return;
- }
- void Editor::on_actionNew_password_file_triggered()
- {
- createFile(".ea1", "new");
- return;
- }
- void Editor::on_actionNew_Command_File_triggered()
- {
- createFile(".cmd", "new");
- return;
- }
- void Editor::on_actionNew_Text_File_triggered()
- {
- createFile(".txt", "new");
- return;
- }
- void Editor::on_actionNew_triggered()
- {
- FileCreationWizard fwiz;
- QObject::connect(&fwiz, SIGNAL(fileCreated(QString, QString)), this, SLOT(createFileFromWizard(QString,QString)));
- fwiz.setModal(true);
- fwiz.exec();
- return;
- }
- void Editor::createFileFromWizard(QString fileType, QString fileName)
- {
- if(fileType == "AES 256 Encrypted Password and Username file (*.ea1)")
- {
- createFile(".ea1", fileName);
- }
- else if(fileType == "Text File (*.txt)")
- {
- createFile(".txt", fileName);
- }
- else if(fileType == "Delayed Command File (*.cmd)")
- {
- createFile(".cmd", fileName);
- }
- return;
- }
- void Editor::createFile(QString type, QString name)
- {
- QModelIndex index = ui->treeView->currentIndex();
- QFileSystemModel *dirModel = new QFileSystemModel(this);
- QString dir = dirModel->fileInfo(index).dir().path();
- QString fileName = name;
- QString extension = type;
- //sort of half ass fix for selecting the first file in the treeView
- if(dir == ".")
- {
- dir = openedDir + "/DRT/";
- }
- //sort of half ass fix for selecting the first file in the treeView
- int i = 0;
- QFile file(dir + '/' + fileName + extension);
- while(file.exists())
- {
- i++;
- file.setFileName(dir + '/' + fileName + QString::number(i) + extension);
- if(i == INT_MAX)
- {
- break;
- }
- }
- file.open(QIODevice::WriteOnly);
- file.close();
- delete dirModel;
- return;
- }
- void Editor::on_actionStart_Menu_triggered()
- {
- delete globalDirModel;
- ui->stackedWidget->setCurrentIndex(0);
- ui->stackedWidget_2->setCurrentIndex(3);
- ui->actionStart_Menu->setEnabled(false);
- ui->actionNew_Command_File->setEnabled(false);
- ui->actionNew_password_file->setEnabled(false);
- ui->actionNew->setEnabled(false);
- ui->actionNew_Text_File->setEnabled(false);
- ui->actionSave_Selected_File->setEnabled(false);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement