diff options
author | Saturneric <[email protected]> | 2021-06-26 18:32:15 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-06-26 18:32:15 +0000 |
commit | 009fa10af30f3f08e7a3ee986c937f82ce00e38a (patch) | |
tree | 2465e9a252c2df481ce5c0e4b172059980d9a001 /src | |
parent | Reply work. (diff) | |
download | GpgFrontend-009fa10af30f3f08e7a3ee986c937f82ce00e38a.tar.gz GpgFrontend-009fa10af30f3f08e7a3ee986c937f82ce00e38a.zip |
Develop File Opera Functions
Diffstat (limited to 'src')
-rw-r--r-- | src/MainWindow.cpp | 4 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 17 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotUI.cpp | 4 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowUI.cpp | 4 | ||||
-rw-r--r-- | src/ui/widgets/EditorPage.cpp | 12 | ||||
-rw-r--r-- | src/ui/widgets/FilePage.cpp | 122 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 123 |
7 files changed, 214 insertions, 72 deletions
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 67274a65..0d8521e7 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -34,7 +34,9 @@ MainWindow::MainWindow() setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); - edit = new TextEdit(); + qDebug() << "Main Window" << this; + + edit = new TextEdit(this); setCentralWidget(edit); /* the list of Keys available*/ diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index dcfac10b..023b3d73 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -84,13 +84,13 @@ void MainWindow::slotSign() { mKeyList->getPrivateCheckedKeys(keys); if (keys.isEmpty()) { - QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected")); + QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); return; } for (const auto &key : keys) { if (!GpgME::GpgContext::checkIfKeyCanSign(key)) { - QMessageBox::information(nullptr, + QMessageBox::information(this, tr("Invalid Operation"), tr("The selected key contains a key that does not actually have a signature usage.<br/>") + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); @@ -255,7 +255,7 @@ void MainWindow::slotFileEncrypt() { QFileInfo fileInfo(path); if(!fileInfo.isFile()) { - QMessageBox::critical(this, tr("Error"), tr("Can only encrypt a file.")); + QMessageBox::critical(this, tr("Error"), tr("Select a file before doing it.")); return; } if(!fileInfo.isReadable()) { @@ -276,9 +276,14 @@ void MainWindow::slotFileEncrypt() { mKeyList->getCheckedKeys(keys); + if(keys.empty()) { + QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); + return; + } + for (const auto &key : keys) { if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) { - QMessageBox::information(nullptr, + QMessageBox::information(this, tr("Invalid Operation"), tr("The selected key contains a key that does not actually have a encrypt usage.<br/>") + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); @@ -403,3 +408,7 @@ void MainWindow::slotDecryptVerify() { delete resultAnalyseDecrypt; delete resultAnalyseVerify; } + +void MainWindow::slotOpenFile(QString &path) { + edit->slotOpenFile(path); +} diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index cad8669a..219d09b6 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -63,10 +63,8 @@ void MainWindow::slotCheckAttachmentFolder() { } void MainWindow::slotImportKeyFromEdit() { - if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == 0) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) return; - } - keyMgmt->slotImportKeys(edit->curTextPage()->toPlainText().toUtf8()); } diff --git a/src/ui/main_window/MainWindowUI.cpp b/src/ui/main_window/MainWindowUI.cpp index 5afdc263..fba2fe62 100644 --- a/src/ui/main_window/MainWindowUI.cpp +++ b/src/ui/main_window/MainWindowUI.cpp @@ -386,8 +386,8 @@ void MainWindow::createToolBars() { // Add dropdown menu for file encryption/decryption to crypttoolbar fileEncButton = new QToolButton(); - fileEncButton->setMenu(fileEncMenu); - // connect(fileEncButton, SIGNAL(clicked(bool)), this, SLOT(slotOpenFileTab())); + // fileEncButton->setMenu(fileEncMenu); + connect(fileEncButton, SIGNAL(clicked(bool)), this, SLOT(slotOpenFileTab())); fileEncButton->setPopupMode(QToolButton::InstantPopup); fileEncButton->setIcon(QIcon(":fileencryption.png")); fileEncButton->setToolTip(tr("Opera File")); diff --git a/src/ui/widgets/EditorPage.cpp b/src/ui/widgets/EditorPage.cpp index cb4ca5ef..8a51b721 100644 --- a/src/ui/widgets/EditorPage.cpp +++ b/src/ui/widgets/EditorPage.cpp @@ -27,7 +27,7 @@ #include <utility> EditorPage::EditorPage(QString filePath, QWidget *parent) : QWidget(parent), - fullFilePath(std::move(filePath)) { + fullFilePath(std::move(filePath)) { // Set the Textedit properties textPage = new QTextEdit(); textPage->setAcceptRichText(false); @@ -42,7 +42,7 @@ EditorPage::EditorPage(QString filePath, QWidget *parent) : QWidget(parent), setAttribute(Qt::WA_DeleteOnClose); textPage->setFocus(); - //connect(textPage, SIGNAL(textChanged()), this, SLOT(formatGpgHeader())); + connect(textPage, SIGNAL(textChanged()), this, SLOT(formatGpgHeader())); } const QString &EditorPage::getFilePath() const { @@ -64,11 +64,11 @@ void EditorPage::showNotificationWidget(QWidget *widget, const char *className) void EditorPage::closeNoteByClass(const char *className) { QList<QWidget *> widgets = findChildren<QWidget *>(); - foreach(QWidget *widget, widgets) { - if (widget->property(className) == true) { - widget->close(); - } + for (QWidget *widget: widgets) { + if (widget->property(className) == true) { + widget->close(); } + } } void EditorPage::slotFormatGpgHeader() { diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp index d1e04879..e4226bcf 100644 --- a/src/ui/widgets/FilePage.cpp +++ b/src/ui/widgets/FilePage.cpp @@ -24,7 +24,13 @@ #include "ui/widgets/FilePage.h" +#include "MainWindow.h" + FilePage::FilePage(QWidget *parent) : QWidget(parent) { + + qDebug() << "First Parent" << parent; + firstParent = parent; + qDebug() << "New File Page"; dirModel = new QFileSystemModel(); @@ -35,6 +41,10 @@ FilePage::FilePage(QWidget *parent) : QWidget(parent) { dirTreeView->setAnimated(true); dirTreeView->setIndentation(20); dirTreeView->setRootIndex(dirModel->index(QDir::currentPath())); + dirTreeView->setContextMenuPolicy(Qt::CustomContextMenu); + mPath = dirModel->rootPath(); + + createPopupMenu(); upLevelButton = new QPushButton("UP Level"); connect(upLevelButton, SIGNAL(clicked(bool)), this, SLOT(slotUpLevel())); @@ -43,7 +53,7 @@ FilePage::FilePage(QWidget *parent) : QWidget(parent) { connect(goPathButton, SIGNAL(clicked(bool)), this, SLOT(slotGoPath())); pathEdit = new QLineEdit(); - pathEdit->setFixedWidth(520); + pathEdit->setFixedWidth(500); pathEdit->setText(dirModel->rootPath()); auto *menuLayout = new QHBoxLayout(); @@ -60,6 +70,9 @@ FilePage::FilePage(QWidget *parent) : QWidget(parent) { connect(dirTreeView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(fileTreeViewItemClicked(const QModelIndex &))); connect(dirTreeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(fileTreeViewItemDoubleClicked(const QModelIndex &))); + connect(dirTreeView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenu(const QPoint &))); + + emit pathChanged(mPath); } @@ -77,6 +90,7 @@ void FilePage::slotUpLevel() { pathEdit->setText(mPath); } qDebug() << "Current Root mPath" << mPath; + emit pathChanged(mPath); } void FilePage::fileTreeViewItemDoubleClicked(const QModelIndex &index) { @@ -87,13 +101,11 @@ void FilePage::fileTreeViewItemDoubleClicked(const QModelIndex &index) { pathEdit->setText(mPath); } qDebug() << "Index mPath" << mPath; + emit pathChanged(mPath); } QString FilePage::getSelected() const { - QModelIndex index = dirTreeView->currentIndex(); - QVariant data = dirTreeView->model()->data(index); - qDebug() << "Target Path" << mPath; - return data.toString(); + return mPath; } void FilePage::slotGoPath() { @@ -105,4 +117,104 @@ void FilePage::slotGoPath() { } else { QMessageBox::critical(this, "Error", "The path is unprivileged or unreachable."); } + emit pathChanged(mPath); +} + +void FilePage::createPopupMenu() { + popUpMenu = new QMenu(); + + auto openItemAct = new QAction(tr("Open"), this); + connect(openItemAct, SIGNAL(triggered()), this, SLOT(slotOpenItem())); + auto deleteItemAct = new QAction(tr("Delete"), this); + connect(deleteItemAct, SIGNAL(triggered()), this, SLOT(slotDeleteItem())); + encryptItemAct = new QAction(tr("Encrypt File"), this); + connect(encryptItemAct, SIGNAL(triggered()), this, SLOT(slotEncryptItem())); + decryptItemAct = new QAction(tr("Decrypt File"), this); + connect(decryptItemAct, SIGNAL(triggered()), this, SLOT(slotDecryptItem())); + signItemAct = new QAction(tr("Sign File"), this); + connect(signItemAct, SIGNAL(triggered()), this, SLOT(slotSignItem())); + verifyItemAct = new QAction(tr("Verify File"), this); + connect(verifyItemAct, SIGNAL(triggered()), this, SLOT(slotVerifyItem())); + + popUpMenu->addAction(openItemAct); + popUpMenu->addAction(deleteItemAct); + popUpMenu->addSeparator(); + popUpMenu->addAction(encryptItemAct); + popUpMenu->addAction(decryptItemAct); + popUpMenu->addAction(signItemAct); + popUpMenu->addAction(verifyItemAct); + +} + +void FilePage::onCustomContextMenu(const QPoint &point) { + QModelIndex index = dirTreeView->indexAt(point); + mPath = dirModel->fileInfo(index).absoluteFilePath(); + qDebug() << "Right Click" << mPath; + if (index.isValid()) { + QFileInfo info(mPath); + encryptItemAct->setEnabled(info.isFile()); + decryptItemAct->setEnabled(info.isFile()); + signItemAct->setEnabled(info.isFile()); + verifyItemAct->setEnabled(info.isFile()); + + popUpMenu->exec(dirTreeView->viewport()->mapToGlobal(point)); + } +} + +void FilePage::slotOpenItem() { + QFileInfo info(mPath); + if(info.isDir()) { + qDebug() << "getSelected" << pathEdit->text(); + if(info.isReadable() && info.isExecutable()) { + qDebug() << "Set Path" << info.filePath(); + dirTreeView->setRootIndex(dirModel->index(info.filePath())); + } else { + QMessageBox::critical(this, "Error", "The path is unprivileged or unreachable."); + } + } else { + auto mainWindow = qobject_cast<MainWindow *>(firstParent); + qDebug() << "Open Item" << mPath; + if (mainWindow != nullptr) + mainWindow->slotOpenFile(mPath); + } + emit pathChanged(mPath); +} + +void FilePage::slotDeleteItem() { + QModelIndex index = dirTreeView->currentIndex(); + QVariant data = dirTreeView->model()->data(index); + + auto ret = QMessageBox::warning(this, + tr("Warning"), + tr("Are you sure you want to delete it?"), + QMessageBox::Ok | QMessageBox::Cancel); + + if(ret == QMessageBox::Cancel) + return; + + qDebug() << "Delete Item" << data.toString(); + + if(!dirModel->remove(index)){ + QMessageBox::critical(this, + tr("Error"), + tr("Unable to delete the file or folder.")); + } +} + +void FilePage::slotEncryptItem() { + auto mainWindow = qobject_cast<MainWindow *>(firstParent); + if(mainWindow != nullptr) + mainWindow->slotFileEncrypt(); +} + +void FilePage::slotDecryptItem() { + +} + +void FilePage::slotSignItem() { + +} + +void FilePage::slotVerifyItem() { + } diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index 00abe16f..2394b694 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -24,7 +24,7 @@ #include "ui/widgets/TextEdit.h" -TextEdit::TextEdit() { +TextEdit::TextEdit(QWidget *parent) : QWidget(parent) { countPage = 0; tabWidget = new QTabWidget(this); tabWidget->setMovable(true); @@ -66,45 +66,75 @@ void TextEdit::slotNewHelpTab(const QString &title, const QString &path) const { void TextEdit::slotNewFileTab() const { - auto *page = new FilePage(); + auto *page = new FilePage(qobject_cast<QWidget *>(parent())); tabWidget->addTab(page, "File"); tabWidget->setCurrentIndex(tabWidget->count() - 1); + connect(page, SIGNAL(pathChanged(QString)), this, SLOT(slotFilePagePathChanged(QString))); } +void TextEdit::slotOpenFile(QString &path) { + + QFile file(path); + + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + auto *page = new EditorPage(path); + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + page->getTextPage()->setPlainText(in.readAll()); + page->setFilePath(path); + QTextDocument *document = page->getTextPage()->document(); + document->setModified(false); + + tabWidget->addTab(page, strippedName(path)); + tabWidget->setCurrentIndex(tabWidget->count() - 1); + QApplication::restoreOverrideCursor(); + page->getTextPage()->setFocus(); + connect(page->getTextPage()->document(), SIGNAL(modificationChanged(bool)), this, + SLOT(slotShowModified())); + //enableAction(true) + file.close(); + } else { + QMessageBox::warning(this, tr("Warning"), + tr("Cannot read file %1:\n%2.") + .arg(path).arg(file.errorString())); + } +} + void TextEdit::slotOpen() { QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open file"), QDir::currentPath()); - foreach (QString fileName, fileNames) { - if (!fileName.isEmpty()) { - QFile file(fileName); - - if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { - auto *page = new EditorPage(fileName); - - QTextStream in(&file); - QApplication::setOverrideCursor(Qt::WaitCursor); - page->getTextPage()->setPlainText(in.readAll()); - page->setFilePath(fileName); - QTextDocument *document = page->getTextPage()->document(); - document->setModified(false); - - tabWidget->addTab(page, strippedName(fileName)); - tabWidget->setCurrentIndex(tabWidget->count() - 1); - QApplication::restoreOverrideCursor(); - page->getTextPage()->setFocus(); - connect(page->getTextPage()->document(), SIGNAL(modificationChanged(bool)), this, - SLOT(slotShowModified())); - //enableAction(true) - file.close(); - } else { - QMessageBox::warning(this, tr("Application"), - tr("Cannot read file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())); - } + for (const auto &fileName : fileNames) { + if (!fileName.isEmpty()) { + QFile file(fileName); + + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + auto *page = new EditorPage(fileName); + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + page->getTextPage()->setPlainText(in.readAll()); + page->setFilePath(fileName); + QTextDocument *document = page->getTextPage()->document(); + document->setModified(false); + + tabWidget->addTab(page, strippedName(fileName)); + tabWidget->setCurrentIndex(tabWidget->count() - 1); + QApplication::restoreOverrideCursor(); + page->getTextPage()->setFocus(); + connect(page->getTextPage()->document(), SIGNAL(modificationChanged(bool)), this, + SLOT(slotShowModified())); + //enableAction(true) + file.close(); + } else { + QMessageBox::warning(this, tr("Warning"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); } } + } } void TextEdit::slotSave() { @@ -170,7 +200,7 @@ bool TextEdit::slotSaveAs() { path = tabWidget->tabText(tabWidget->currentIndex()).remove(0, 2); } - QString fileName = QFileDialog::getSaveFileName(this, tr("Save file "), + QString fileName = QFileDialog::getSaveFileName(this, tr("Save file"), path); return saveFile(fileName); } @@ -299,7 +329,8 @@ bool TextEdit::maybeSaveAnyTab() { bool allsaved = true; QList<int> tabIdsToSave = dialog->getTabIdsToSave(); - foreach (int tabId, tabIdsToSave) { + foreach(int + tabId, tabIdsToSave) { tabWidget->setCurrentIndex(tabId); if (!maybeSaveCurrentTab(false)) { allsaved = false; @@ -544,26 +575,16 @@ void TextEdit::slotSelectAll() const { if (tabWidget->count() == 0 || curTextPage() == nullptr) { return; } - curTextPage()->selectAll(); } -/*void TextEdit::dragEnterEvent(QDragEnterEvent *event) -{ - if (event->mimeData()->hasFormat("text/plain")) - qDebug() << "enter textedit drag action"; - event->acceptProposedAction(); -} - -void TextEdit::dropEvent(QDropEvent* event) -{ - curTextPage()->setPlainText(event->mimeData()->text()); - - foreach (QUrl tmp, event->mimeData()->urls()) - { - qDebug() << tmp; +void TextEdit::slotFilePagePathChanged(const QString& path) { + int index = tabWidget->currentIndex(); + QString mPath; + if(path.size() > 8) { + mPath = path.mid(path.size()-8,8).prepend("..."); + } else { + mPath = path; } - - //event->acceptProposedAction(); -} -*/ + tabWidget->setTabText(index, mPath); +}
\ No newline at end of file |