2022-04-30 18:12:14 +02:00
// Copyright (C) 2022 Jakub Melka
//
// This file is part of PDF4QT.
//
// PDF4QT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// PDF4QT is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
2022-06-12 15:45:32 +02:00
# include "pdfcertificatemanagerdialog.h"
# include "ui_pdfcertificatemanagerdialog.h"
# include "pdfcreatecertificatedialog.h"
2022-04-30 18:12:14 +02:00
# include "pdfwidgetutils.h"
# include <QAction>
# include <QPushButton>
2022-05-07 17:36:19 +02:00
# include <QFileSystemModel>
# include <QDesktopServices>
# include <QMessageBox>
# include <QFileDialog>
2022-04-30 18:12:14 +02:00
2022-06-12 15:45:32 +02:00
namespace pdf
2022-04-30 18:12:14 +02:00
{
2022-06-12 15:45:32 +02:00
PDFCertificateManagerDialog : : PDFCertificateManagerDialog ( QWidget * parent ) :
2022-04-30 18:12:14 +02:00
QDialog ( parent ) ,
2022-06-12 15:45:32 +02:00
ui ( new Ui : : PDFCertificateManagerDialog ) ,
2022-04-30 18:12:14 +02:00
m_newCertificateButton ( nullptr ) ,
2022-05-07 17:36:19 +02:00
m_openCertificateDirectoryButton ( nullptr ) ,
m_deleteCertificateButton ( nullptr ) ,
m_importCertificateButton ( nullptr ) ,
m_certificateFileModel ( nullptr )
2022-04-30 18:12:14 +02:00
{
ui - > setupUi ( this ) ;
2022-06-12 15:45:32 +02:00
QDir : : root ( ) . mkpath ( PDFCertificateManager : : getCertificateDirectory ( ) ) ;
2022-05-07 17:36:19 +02:00
m_certificateFileModel = new QFileSystemModel ( this ) ;
2022-06-12 15:45:32 +02:00
QModelIndex rootIndex = m_certificateFileModel - > setRootPath ( PDFCertificateManager : : getCertificateDirectory ( ) ) ;
2022-05-07 17:36:19 +02:00
ui - > fileView - > setModel ( m_certificateFileModel ) ;
ui - > fileView - > setRootIndex ( rootIndex ) ;
m_newCertificateButton = ui - > buttonBox - > addButton ( tr ( " Create " ) , QDialogButtonBox : : ActionRole ) ;
m_openCertificateDirectoryButton = ui - > buttonBox - > addButton ( tr ( " Open Directory " ) , QDialogButtonBox : : ActionRole ) ;
m_deleteCertificateButton = ui - > buttonBox - > addButton ( tr ( " Delete " ) , QDialogButtonBox : : ActionRole ) ;
m_importCertificateButton = ui - > buttonBox - > addButton ( tr ( " Import " ) , QDialogButtonBox : : ActionRole ) ;
2022-04-30 18:12:14 +02:00
2022-06-12 15:45:32 +02:00
connect ( m_newCertificateButton , & QPushButton : : clicked , this , & PDFCertificateManagerDialog : : onNewCertificateClicked ) ;
connect ( m_openCertificateDirectoryButton , & QPushButton : : clicked , this , & PDFCertificateManagerDialog : : onOpenCertificateDirectoryClicked ) ;
connect ( m_deleteCertificateButton , & QPushButton : : clicked , this , & PDFCertificateManagerDialog : : onDeleteCertificateClicked ) ;
connect ( m_importCertificateButton , & QPushButton : : clicked , this , & PDFCertificateManagerDialog : : onImportCertificateClicked ) ;
2022-04-30 18:12:14 +02:00
setMinimumSize ( pdf : : PDFWidgetUtils : : scaleDPI ( this , QSize ( 640 , 480 ) ) ) ;
}
2022-06-12 15:45:32 +02:00
PDFCertificateManagerDialog : : ~ PDFCertificateManagerDialog ( )
2022-04-30 18:12:14 +02:00
{
delete ui ;
}
2022-06-12 15:45:32 +02:00
void PDFCertificateManagerDialog : : onNewCertificateClicked ( )
2022-04-30 18:12:14 +02:00
{
2022-06-12 15:45:32 +02:00
PDFCreateCertificateDialog dialog ( this ) ;
if ( dialog . exec ( ) = = PDFCreateCertificateDialog : : Accepted )
2022-04-30 18:12:14 +02:00
{
2022-06-12 15:45:32 +02:00
const PDFCertificateManager : : NewCertificateInfo info = dialog . getNewCertificateInfo ( ) ;
2022-04-30 18:12:14 +02:00
m_certificateManager . createCertificate ( info ) ;
}
}
2022-06-12 15:45:32 +02:00
void PDFCertificateManagerDialog : : onOpenCertificateDirectoryClicked ( )
2022-05-07 17:36:19 +02:00
{
2022-06-12 15:45:32 +02:00
QDesktopServices : : openUrl ( QString ( " file:///%1 " ) . arg ( PDFCertificateManager : : getCertificateDirectory ( ) , QUrl : : TolerantMode ) ) ;
2022-05-07 17:36:19 +02:00
}
2022-06-12 15:45:32 +02:00
void PDFCertificateManagerDialog : : onDeleteCertificateClicked ( )
2022-05-07 17:36:19 +02:00
{
QFileInfo fileInfo = m_certificateFileModel - > fileInfo ( ui - > fileView - > currentIndex ( ) ) ;
if ( fileInfo . exists ( ) )
{
if ( QMessageBox : : question ( this , tr ( " Confirm delete " ) , tr ( " Do you want to delete certificate '%1'? " ) . arg ( fileInfo . fileName ( ) ) , QMessageBox : : No , QMessageBox : : Yes ) = = QMessageBox : : Yes )
{
QFile file ( fileInfo . filePath ( ) ) ;
if ( ! file . remove ( ) )
{
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " Cannot delete certificate '%1' " ) . arg ( fileInfo . fileName ( ) ) ) ;
}
}
}
}
2022-06-12 15:45:32 +02:00
void PDFCertificateManagerDialog : : onImportCertificateClicked ( )
2022-05-07 17:36:19 +02:00
{
QString selectedFile = QFileDialog : : getOpenFileName ( this , tr ( " Import Certificate " ) , QStandardPaths : : writableLocation ( QStandardPaths : : DocumentsLocation ) , tr ( " Certificate file (*.pfx);;All files (*.*) " ) ) ;
if ( selectedFile . isEmpty ( ) )
{
return ;
}
QFile file ( selectedFile ) ;
if ( file . exists ( ) )
{
2022-06-12 15:45:32 +02:00
QString path = PDFCertificateManager : : getCertificateDirectory ( ) ;
2022-05-07 17:36:19 +02:00
QString targetFile = QString ( " %1/%2 " ) . arg ( path , QFileInfo ( file ) . fileName ( ) ) ;
if ( QFile : : exists ( targetFile ) )
{
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " Target file exists. Please rename the certificate file to import. " ) ) ;
}
else
{
if ( file . copy ( targetFile ) )
{
QMessageBox : : information ( this , tr ( " Import Certificate " ) , tr ( " Certificate '%1' was successfully imported. " ) . arg ( file . fileName ( ) ) ) ;
}
else
{
QMessageBox : : critical ( this , tr ( " Import Certificate " ) , tr ( " Error occured during certificate '%1' import. " ) . arg ( file . fileName ( ) ) ) ;
}
}
}
}
2022-06-12 15:45:32 +02:00
} // namespace pdf