diff --git a/src/Lib/receipts_handler.cpp b/src/Lib/receipts_handler.cpp index e4ac3ea..2582daa 100644 --- a/src/Lib/receipts_handler.cpp +++ b/src/Lib/receipts_handler.cpp @@ -27,28 +27,23 @@ extern std::unique_ptr< lc::Settings > settings; lc::ReceiptsHandler::ReceiptsHandler( const QString &argZTreeDataTargetPath, - const bool &argPrintReceiptsForLocalClients, + bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, const QString &argLatexHeaderName, QObject *argParent ) : QObject{ argParent }, anonymousReceiptsPlaceholder{ argAnonymousReceiptsPlaceholder }, + dateString{ QDateTime::currentDateTime().toString( "yyMMdd_hhmm" ) }, + expectedPaymentFileName{ dateString + ".pay" }, + expectedPaymentFilePath{ argZTreeDataTargetPath + "/" + dateString + ".pay" }, latexHeaderName{ argLatexHeaderName }, - printReceiptsForLocalClients{ new bool { argPrintReceiptsForLocalClients } }, + paymentFile{ expectedPaymentFilePath }, + printReceiptsForLocalClients{ argPrintReceiptsForLocalClients }, timer{ new QTimer{ this } }, zTreeDataTargetPath{ argZTreeDataTargetPath } { - // Guess the name of the payment file - QDateTime currentDate; - currentDate = QDateTime::currentDateTime(); - dateString = currentDate.toString( "yyMMdd_hhmm" ); - expectedPaymentFileName = QString{ dateString + ".pay" }; - expectedPaymentFilePath = QString{ zTreeDataTargetPath + "/" + dateString + ".pay" }; qDebug() << "Expected payment file name is:" << expectedPaymentFilePath; - // Create a new file object representing the payment file - paymentFile = new QFile( expectedPaymentFilePath ); - // Create a QTimer regularly checking if the payment file was created and print it if so connect( timer, &QTimer::timeout, this, &ReceiptsHandler::PrintReceipts ); @@ -56,36 +51,28 @@ lc::ReceiptsHandler::ReceiptsHandler( const QString &argZTreeDataTargetPath, } lc::ReceiptsHandler::ReceiptsHandler( const QString &argZTreeDataTargetPath, - const bool &argPrintReceiptsForLocalClients, + bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, const QString &argLatexHeaderName, const QString * const argDateString, QObject *argParent ) : QObject{ argParent }, anonymousReceiptsPlaceholder{ argAnonymousReceiptsPlaceholder }, dateString{ *argDateString }, + expectedPaymentFileName{ *argDateString + ".pay" }, + expectedPaymentFilePath{ argZTreeDataTargetPath + "/" + *argDateString + ".pay" }, latexHeaderName{ argLatexHeaderName }, - printReceiptsForLocalClients{ new bool { argPrintReceiptsForLocalClients } }, + paymentFile{ expectedPaymentFilePath }, + printReceiptsForLocalClients{ argPrintReceiptsForLocalClients }, zTreeDataTargetPath{ argZTreeDataTargetPath } { - expectedPaymentFileName = QString{ dateString + ".pay" }; - expectedPaymentFilePath = QString{ zTreeDataTargetPath + "/" + *argDateString + ".pay" }; qDebug() << "Expected payment file name is:" << expectedPaymentFilePath; - // Create a new file object representing the payment file - paymentFile = new QFile( expectedPaymentFilePath ); - PrintReceipts(); } -lc::ReceiptsHandler::~ReceiptsHandler() { - delete paymentFile; - delete printReceiptsForLocalClients; - delete receiptsPrinter; -} - void lc::ReceiptsHandler::PrintReceipts() { // If the payment file exists, print it - if ( paymentFile->exists() ) { + if ( paymentFile.exists() ) { qDebug() << "The payment file has been created and will be printed"; if ( timer ) { timer->stop(); @@ -113,7 +100,7 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() { // Split the lines containing the participants' data into their inidivual parts QStringList temp_participant_data = it->split('\t', QString::KeepEmptyParts); qDebug() << temp_participant_data.join( " - " ); - if ( !( *printReceiptsForLocalClients ) && temp_participant_data.at( 3 ).contains( "local" ) ) { + if ( !printReceiptsForLocalClients && temp_participant_data.at( 3 ).contains( "local" ) ) { qDebug() << "Receipt for local client" << temp_participant_data.at( 1 ) << "will not be printed."; } else { @@ -210,7 +197,7 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() { delete latexText; latexText = nullptr; - receiptsPrinter = new ReceiptsPrinter{ dateString, zTreeDataTargetPath }; + receiptsPrinter = new ReceiptsPrinter{ dateString, zTreeDataTargetPath, this }; receiptsPrinter->start(); connect( receiptsPrinter, &ReceiptsPrinter::PrintingFinished, this, &ReceiptsHandler::DeleteReceiptsPrinterInstance ); @@ -225,7 +212,7 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() { void lc::ReceiptsHandler::DeleteReceiptsPrinterInstance() { receiptsPrinter->quit(); receiptsPrinter->wait(); - delete receiptsPrinter; + receiptsPrinter->deleteLater(); receiptsPrinter = nullptr; qDebug() << "Deleted 'ReceiptsPrinter' instance."; @@ -244,8 +231,8 @@ QVector *lc::ReceiptsHandler::GetParticipantsDataFromPaymentFile() { QVector *participantsData = new QVector; // Open the payment file for reading and create a QTextStream - paymentFile->open( QIODevice::ReadOnly | QIODevice::Text ); - QTextStream in( paymentFile ); + paymentFile.open( QIODevice::ReadOnly | QIODevice::Text ); + QTextStream in( &paymentFile ); in.setCodec( "ISO 8859-1" ); // Read the file line by line and store them in the vector @@ -261,7 +248,7 @@ QVector *lc::ReceiptsHandler::GetParticipantsDataFromPaymentFile() { participantsData->erase( participantsData->begin() ); // Close the file afterwards - paymentFile->close(); + paymentFile.close(); return participantsData; } diff --git a/src/Lib/receipts_handler.h b/src/Lib/receipts_handler.h index 53b971d..eb0b1b3 100644 --- a/src/Lib/receipts_handler.h +++ b/src/Lib/receipts_handler.h @@ -47,15 +47,14 @@ class ReceiptsHandler : public QObject { public: explicit ReceiptsHandler( const QString &argZTreeDataTargetPath, - const bool &argPrintReceiptsForLocalClients, + bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, const QString &argLatexHeaderName, QObject *argParent = nullptr ); explicit ReceiptsHandler( const QString &argZTreeDataTargetPath, - const bool &argPrintReceiptsForLocalClients, + bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, const QString &argLatexHeaderName, const QString * const argDateString, QObject *argParent = nullptr ); - ~ReceiptsHandler(); signals: void PrintingFinished(); @@ -78,12 +77,12 @@ private: void MakeReceiptsAnonymous( QVector *argDataVector, bool argAlsoAnonymizeClients ); const QString anonymousReceiptsPlaceholder; //!< Placeholder which shall be inserted for participant names if anonymous printing is desired (QString != "") - QString dateString; //!< The expected date string of the payment file in form 'yyMMdd_hhmm' - QString expectedPaymentFileName; //!< The name of the expected payment file - QString expectedPaymentFilePath; //!< The path of the expected payment file + const QString dateString; //!< The expected date string of the payment file in form 'yyMMdd_hhmm' + const QString expectedPaymentFileName; //!< The name of the expected payment file + const QString expectedPaymentFilePath; //!< The path of the expected payment file const QString latexHeaderName; //!< The name of the chosen LaTeX header template - QFile *paymentFile = nullptr; //!< A pointer to the '*.pay' file being watched for existance and starting the printing process - const bool * const printReceiptsForLocalClients; //!< Stores if receipts shall be printed for local clients + QFile paymentFile; //!< A pointer to the '*.pay' file being watched for existance and starting the printing process + const bool printReceiptsForLocalClients; //!< Stores if receipts shall be printed for local clients ReceiptsPrinter *receiptsPrinter = nullptr; //!< Creates new thread for receipts printing QTimer *timer = nullptr; //!< Used for regular checking if the payment file was created const QString zTreeDataTargetPath; //!< A reference to the data target path stored in the session class instance diff --git a/src/Lib/session.cpp b/src/Lib/session.cpp index ff5e5ca..0e79879 100644 --- a/src/Lib/session.cpp +++ b/src/Lib/session.cpp @@ -59,7 +59,6 @@ lc::Session::~Session() { client->SetSessionPort( 0 ); client->SetzLeafVersion( "" ); } - delete receiptsHandler; } QVariant lc::Session::GetDataItem( int argIndex ) { @@ -89,10 +88,8 @@ void lc::Session::InitializeClasses() { // Only create a 'Receipts_Handler' instance, if all neccessary variables were set if ( latexHeaderName != "None found" && !settings->dvipsCmd.isEmpty() && !settings->latexCmd.isEmpty() ) { - receiptsHandler = new ReceiptsHandler{ zTreeDataTargetPath, - printReceiptsForLocalClients, - anonymousReceiptsPlaceholder, - latexHeaderName }; + new ReceiptsHandler{ zTreeDataTargetPath, printReceiptsForLocalClients, + anonymousReceiptsPlaceholder, latexHeaderName, this }; } else { qDebug() << "No 'ReceiptsHandler' instance was created."; } diff --git a/src/Lib/session.h b/src/Lib/session.h index 73ea2c9..18c27e7 100644 --- a/src/Lib/session.h +++ b/src/Lib/session.h @@ -73,7 +73,6 @@ private: const QVector< Client* > assocClients; const QString latexHeaderName; //! The name of the chosen LaTeX header const bool printReceiptsForLocalClients = true; //! True if receipts shall be printed for local clients - ReceiptsHandler *receiptsHandler = nullptr; //! For automatic creation and printing of the receipts QString zTreeDataTargetPath; //! The path were the data of this zTree instance's session will be saved ZTree *zTreeInstance= nullptr; //! The session's zTree instance const QString zTreeVersionPath; //! The path to the version of zTree used by this session's instance