Properly remove lc::Session instances of closed z-Trees

remotes/origin/HEAD
markuspg 10 years ago
parent 87480b47d8
commit 2898ff9132

@ -19,8 +19,6 @@
#include <memory> #include <memory>
#include <QDebug>
#include "session.h" #include "session.h"
#include "settings.h" #include "settings.h"
@ -29,7 +27,8 @@ extern std::unique_ptr< lc::Settings > settings;
lc::Session::Session( const QString &argZTreeDataTargetPath, const quint16 argZTreePort, lc::Session::Session( const QString &argZTreeDataTargetPath, const quint16 argZTreePort,
const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients, const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients,
const QString &argAnonymousReceiptsPlaceholder, const QString &argAnonymousReceiptsPlaceholder,
const QString &argLatexHeaderName ): const QString &argLatexHeaderName, QObject *argParent ):
QObject{ argParent },
zTreePort{ argZTreePort }, zTreePort{ argZTreePort },
anonymousReceiptsPlaceholder{ argAnonymousReceiptsPlaceholder }, anonymousReceiptsPlaceholder{ argAnonymousReceiptsPlaceholder },
latexHeaderName{ argLatexHeaderName }, latexHeaderName{ argLatexHeaderName },
@ -79,7 +78,9 @@ void lc::Session::InitializeClasses() {
zTreeDataTargetPath.append( "/" + date_string + "-" + QString::number( zTreePort ) ); zTreeDataTargetPath.append( "/" + date_string + "-" + QString::number( zTreePort ) );
qDebug() << "New session's chosen_zTree_data_target_path:" << zTreeDataTargetPath; qDebug() << "New session's chosen_zTree_data_target_path:" << zTreeDataTargetPath;
zTreeInstance = new ZTree{ zTreeDataTargetPath, zTreePort, zTreeVersionPath }; zTreeInstance = new ZTree{ zTreeDataTargetPath, zTreePort, zTreeVersionPath, this };
connect( zTreeInstance, &ZTree::ZTreeClosed,
this, &Session::OnzTreeClosed );
// Only create a 'Receipts_Handler' instance, if all neccessary variables were set // Only create a 'Receipts_Handler' instance, if all neccessary variables were set
if ( latexHeaderName != "None found" && !settings->dvipsCmd.isEmpty() if ( latexHeaderName != "None found" && !settings->dvipsCmd.isEmpty()
&& !settings->latexCmd.isEmpty() ) { && !settings->latexCmd.isEmpty() ) {
@ -92,6 +93,11 @@ void lc::Session::InitializeClasses() {
} }
} }
void lc::Session::OnzTreeClosed( int argExitCode ) {
qDebug() << "z-Tree running on port" << zTreePort << "closed with exit code" << argExitCode;
emit SessionFinished( this );
}
void lc::Session::RenameWindow() { void lc::Session::RenameWindow() {
// Example: wmctrl -r <window name> -T <new name> // Example: wmctrl -r <window name> -T <new name>

@ -39,8 +39,9 @@ public:
const int zTreePort = 7000; //! The port this session's zTree instance is running on const int zTreePort = 7000; //! The port this session's zTree instance is running on
Session( const QString &argZTreeDataTargetPath, const quint16 argZTreePort, Session( const QString &argZTreeDataTargetPath, const quint16 argZTreePort,
const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients, const QString &argAnonymousReceiptsPlaceholder, const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients,
const QString &argLatexHeaderName ); const QString &argAnonymousReceiptsPlaceholder,
const QString &argLatexHeaderName, QObject *argParent = nullptr );
~Session(); ~Session();
/*! Returns the data item with the given index /*! Returns the data item with the given index
@ -53,12 +54,13 @@ public:
class lcDataTargetPathCreationFailed {}; class lcDataTargetPathCreationFailed {};
signals: signals:
// void session_started(); void SessionFinished( Session *argSession );
private slots: private slots:
/*! Starts the session by creating instances of the relevant classes /*! Starts the session by creating instances of the relevant classes
*/ */
void InitializeClasses(); void InitializeClasses();
void OnzTreeClosed( int argExitCode );
/*! Changes zTree's window title to contain its port number to make zTree windows distinguishable /*! Changes zTree's window title to contain its port number to make zTree windows distinguishable
*/ */
void RenameWindow(); void RenameWindow();

@ -17,22 +17,17 @@
* along with Labcontrol. If not, see <http://www.gnu.org/licenses/>. * along with Labcontrol. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QDebug>
#include "sessionsmodel.h" #include "sessionsmodel.h"
lc::SessionsModel::SessionsModel( QObject *argParent ) : lc::SessionsModel::SessionsModel( QObject *argParent ) :
QAbstractTableModel{ argParent }, QAbstractTableModel{ argParent }
sessions_vector{ new QVector< Session* > }
{ {
} }
lc::SessionsModel::~SessionsModel() {
for (auto s: *sessions_vector)
delete s;
delete sessions_vector;
}
lc::Session *lc::SessionsModel::back() const { lc::Session *lc::SessionsModel::back() const {
return sessions_vector->back(); return sessionsList.back();
} }
int lc::SessionsModel::columnCount(const QModelIndex &parent) const { int lc::SessionsModel::columnCount(const QModelIndex &parent) const {
@ -44,11 +39,11 @@ QVariant lc::SessionsModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) if (!index.isValid())
return QVariant{}; return QVariant{};
if (index.row() >= sessions_vector->size() || index.row() < 0) if (index.row() >= sessionsList.size() || index.row() < 0)
return QVariant{}; return QVariant{};
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole)
return sessions_vector->at( index.row() )->GetDataItem( index.column() ); return sessionsList.at( index.row() )->GetDataItem( index.column() );
return QVariant{}; return QVariant{};
} }
@ -72,10 +67,20 @@ QVariant lc::SessionsModel::headerData(int section, Qt::Orientation orientation,
} }
void lc::SessionsModel::push_back( Session *argSession ) { void lc::SessionsModel::push_back( Session *argSession ) {
sessions_vector->push_back( argSession ); connect( argSession, &Session::SessionFinished,
this, &SessionsModel::RemoveSession );
argSession->setParent( this );
sessionsList.push_back( argSession );
}
void lc::SessionsModel::RemoveSession( Session *argSession ) {
if ( sessionsList.removeAll( argSession ) ) {
qDebug() << "Successfully removed" << argSession << "from lc::SessionsModel";
argSession->deleteLater();
}
} }
int lc::SessionsModel::rowCount(const QModelIndex &parent) const { int lc::SessionsModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent); Q_UNUSED(parent);
return sessions_vector->length(); return sessionsList.length();
} }

@ -30,7 +30,6 @@ class SessionsModel : public QAbstractTableModel {
Q_OBJECT Q_OBJECT
public: public:
explicit SessionsModel(QObject *parent = 0); explicit SessionsModel(QObject *parent = 0);
~SessionsModel();
SessionsModel(const SessionsModel&) = delete; SessionsModel(const SessionsModel&) = delete;
Session *back() const; Session *back() const;
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const;
@ -39,13 +38,15 @@ public:
void push_back( Session *argSession ); void push_back( Session *argSession );
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
signals: signals:
public slots: public slots:
private: private:
QVector< Session* > *sessions_vector = nullptr; QList< Session* > sessionsList;
private slots:
void RemoveSession( Session *argSession );
}; };
} }

@ -27,8 +27,9 @@
extern std::unique_ptr< lc::Settings > settings; extern std::unique_ptr< lc::Settings > settings;
lc::ZTree::ZTree( const QString &argZTreeDataTargetPath, const int &argZTreePort, lc::ZTree::ZTree( const QString &argZTreeDataTargetPath, const int &argZTreePort,
const QString &argZTreeVersionPath ) { const QString &argZTreeVersionPath, QObject *argParent ) :
QString program{ settings->tasksetCmd }; QObject{ argParent }
{
QStringList arguments{ QStringList{} << "0x00000001" << settings->wineCmd QStringList arguments{ QStringList{} << "0x00000001" << settings->wineCmd
<< QString{ settings->zTreeInstDir + "/zTree_" << QString{ settings->zTreeInstDir + "/zTree_"
+ argZTreeVersionPath + "/ztree.exe" } + argZTreeVersionPath + "/ztree.exe" }
@ -41,14 +42,9 @@ lc::ZTree::ZTree( const QString &argZTreeDataTargetPath, const int &argZTreePort
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
zTreeInstance.setProcessEnvironment( env ); zTreeInstance.setProcessEnvironment( env );
zTreeInstance.startDetached( program, arguments, QDir::currentPath(), &pid ); zTreeInstance.start( settings->tasksetCmd, arguments, QIODevice::NotOpen );
connect( &zTreeInstance, SIGNAL( finished( int ) ), connect( &zTreeInstance, SIGNAL( finished( int ) ),
this, SLOT( ZTreeInstanceClosed() ) ); this, SIGNAL( ZTreeClosed( int ) ) );
// Output message via the debug messages tab qDebug() << settings->tasksetCmd << arguments.join( " " );
qDebug() << program << arguments.join( " " );
}
void lc::ZTree::ZTreeInstanceClosed() {
emit ZTreeClosed();
} }

@ -37,16 +37,13 @@ class ZTree: public QObject {
public: public:
ZTree( const QString &argZTreeDataTargetPath, ZTree( const QString &argZTreeDataTargetPath,
const int &argZTreePort, const QString &argZTreeVersionPath ); const int &argZTreePort, const QString &argZTreeVersionPath,
QObject *argParent = nullptr );
signals: signals:
void ZTreeClosed(); void ZTreeClosed( int argExitCode );
private slots:
void ZTreeInstanceClosed();
private: private:
qint64 pid = 0;
QProcess zTreeInstance; QProcess zTreeInstance;
}; };

Loading…
Cancel
Save