PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/zuluMount-gui/oneinstance.cpp

https://gitlab.com/m.schmidt/zuluCrypt
C++ | 135 lines | 83 code | 33 blank | 19 comment | 4 complexity | 58a2186ef56394b4348aee6ec062f4c0 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, BSD-2-Clause
  1. /*
  2. *
  3. * Copyright (c) 2012-2015
  4. * name : Francis Banyikwa
  5. * email: mhogomchungu@gmail.com
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "oneinstance.h"
  20. #include <QDebug>
  21. #include "../zuluCrypt-gui/utility.h"
  22. #include <memory>
  23. #include <utility>
  24. oneinstance::oneinstance( QObject * parent,const char * socketPath,const char * methodName,
  25. const QString& device,std::function<void( QObject * )> start ) : m_firstInstance( std::move( start ) )
  26. {
  27. m_device = device ;
  28. this->setParent( parent ) ;
  29. m_serverPath = utility::homePath() + "/.zuluCrypt-socket/" ;
  30. m_methodName = methodName ;
  31. QDir d ;
  32. d.mkdir( m_serverPath ) ;
  33. m_serverPath += socketPath ;
  34. if( QFile::exists( m_serverPath ) ){
  35. m_localSocket = new QLocalSocket() ;
  36. connect( m_localSocket,SIGNAL( destroyed( QObject * ) ),this,SLOT( Exit( QObject * ) ) ) ;
  37. connect( m_localSocket,SIGNAL( connected() ),this,SLOT( connected() ) ) ;
  38. connect( m_localSocket,SIGNAL( error( QLocalSocket::LocalSocketError ) ),
  39. this,SLOT( errorOnConnect( QLocalSocket::LocalSocketError ) ) ) ;
  40. m_localSocket->connectToServer( m_serverPath ) ;
  41. }else{
  42. this->startInstance() ;
  43. }
  44. }
  45. void oneinstance::startInstance()
  46. {
  47. m_firstInstance( this ) ;
  48. QMetaObject::invokeMethod( this->parent(),m_methodName,Qt::QueuedConnection ) ;
  49. m_localServer = new QLocalServer() ;
  50. connect( m_localServer,SIGNAL( newConnection() ),this,SLOT( gotConnection() ) ) ;
  51. m_localServer->listen( QString( m_serverPath ) ) ;
  52. }
  53. void oneinstance::Exit( QObject * e )
  54. {
  55. Q_UNUSED( e ) ;
  56. //QCoreApplication::exit( 200 ) ;
  57. exit( 0 ) ;
  58. }
  59. void oneinstance::setDevice( QString device )
  60. {
  61. m_device = device ;
  62. }
  63. void oneinstance::killProcess()
  64. {
  65. QMetaObject::invokeMethod( this,"Exit",Qt::QueuedConnection ) ;
  66. }
  67. void oneinstance::gotConnection()
  68. {
  69. std::unique_ptr<QLocalSocket> s( m_localServer->nextPendingConnection() ) ;
  70. s->waitForReadyRead() ;
  71. QByteArray data = s->readAll() ;
  72. if( data.isEmpty() ){
  73. emit raise() ;
  74. }else{
  75. emit raiseWithDevice( data ) ;
  76. }
  77. }
  78. void oneinstance::errorOnConnect( QLocalSocket::LocalSocketError e )
  79. {
  80. Q_UNUSED( e ) ;
  81. qDebug() << tr( "Previous instance seem to have crashed,trying to clean up before starting" ) ;
  82. QFile::remove( m_serverPath ) ;
  83. this->startInstance() ;
  84. }
  85. void oneinstance::connected()
  86. {
  87. qDebug() << tr( "There seem to be another instance running,exiting this one" ) ;
  88. if( !m_device.isEmpty() ){
  89. m_localSocket->write( m_device.toLatin1() ) ;
  90. m_localSocket->waitForBytesWritten() ;
  91. }
  92. m_localSocket->close() ;
  93. m_localSocket->deleteLater() ;
  94. }
  95. oneinstance::~oneinstance()
  96. {
  97. if( m_localServer ){
  98. m_localServer->close() ;
  99. delete m_localServer ;
  100. QFile::remove( m_serverPath ) ;
  101. }
  102. }