1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: object of this class should be alive as long as program lives
7
// so that instance can be detected (borowed from gnome-volume-manager)
8
////////////////////////////////////////////////////////////////////////////
10
#include "SingleInstance.h"
11
#include "../lib/FilePath.h"
12
#include "../interface.h"
13
#include "../lib/EnumDirectory.h"
19
#include <libgen.h> //basename
22
bool OnLockFileEnum(const char *szFile, void *data);
23
const char *get_config_dir();
25
CSingleInstance::CSingleInstance(const char *szName)
28
unsigned int nProcessID = GetCurrentPID();
30
std::string strDir = get_config_dir();
31
EnsureTerminated(strDir, '/');
33
//create our own .lock file ASAP
35
sprintf(szBuffer, "%snotecase_n%d.lock", strDir.c_str(), nProcessID);
36
FILE *pTmp = fopen(szBuffer, "w");
40
//list existing .lock files
41
std::vector<std::string> lstFiles;
42
EnumDirectory(strDir.c_str(), OnLockFileEnum, (void *)&lstFiles, ENUM_LST_FILES);
44
int nCount = lstFiles.size();
45
for(int i=0; i<nCount; i++)
47
std::string strPath = lstFiles[i];
51
std::string strFile(strPath);
52
int nPos1 = strFile.find_last_of('\\');
54
strFile = strFile.substr(nPos1+1);
56
std::string strFile(basename((char *)strPath.c_str()));
59
//try to extract PID from file name (if exists)
60
unsigned int nPID = 0;
61
std::string strPID = strFile;
62
std::string::size_type nPos = strPID.rfind('_');
63
if(nPos != std::string::npos){
64
strPID = strPID.substr(nPos+1);
65
if(!strPID.empty() && strPID.at(0) == 'n') //correct segment with PID
67
strPID = strPID.substr(1); // cut 'n' marker
69
nPos = strPID.find('.');
70
if(nPos != std::string::npos)
71
strPID = strPID.substr(0, nPos);
73
nPID = atoi(strPID.c_str());
78
remove(strPath.c_str());
81
if(nPID == nProcessID)
82
continue; // our own PID, ignore
84
//check if PID belongs to a running application
85
if(IsPIDRunning(nPID))
87
m_bAlreadyExists = true;
88
return; // skip this file, app that created it is still running
91
remove(strPath.c_str());
94
m_bAlreadyExists = false; //no lock files with running PIDs
97
CSingleInstance::~CSingleInstance()
99
//remove .lock file on exit
102
unsigned int nProcessID = GetCurrentPID();
104
std::string strDir = get_config_dir();
105
EnsureTerminated(strDir, '/');
108
sprintf(szBuffer, "%snotecasepro_n%d.lock", strDir.c_str(), nProcessID);
112
bool CSingleInstance::ProgramAlreadyStarted()
114
return m_bAlreadyExists;
117
bool OnLockFileEnum(const char *szFile, void *data)
119
std::vector<std::string> *lstFiles = (std::vector<std::string> *)data;
123
//calculate base name
124
std::string strFile(szFile);
125
int nPos = strFile.find_last_of('\\');
127
strFile = strFile.substr(nPos+1);
129
std::string strFile(basename((char *)szFile));
132
if(0 == strncmp(strFile.c_str(), "notecasepro_", strlen("notecasepro_")) &&
133
GetFileExt(strFile.c_str()) == ".lock")
135
lstFiles->push_back(szFile);
139
return true; //keep enumerating
1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: object of this class should be alive as long as program lives
7
// so that instance can be detected (borowed from gnome-volume-manager)
8
////////////////////////////////////////////////////////////////////////////
10
#include "SingleInstance.h"
11
#include "../lib/FilePath.h"
12
#include "../interface.h"
13
#include "../lib/EnumDirectory.h"
19
#include <libgen.h> //basename
22
bool OnLockFileEnum(const char *szFile, void *data);
23
const char *get_config_dir();
25
CSingleInstance::CSingleInstance(const char *szName)
28
unsigned int nProcessID = GetCurrentPID();
30
std::string strDir = get_config_dir();
31
EnsureTerminated(strDir, '/');
33
//create our own .lock file ASAP
35
sprintf(szBuffer, "%snotecase_n%d.lock", strDir.c_str(), nProcessID);
36
FILE *pTmp = fopen(szBuffer, "w");
40
//list existing .lock files
41
std::vector<std::string> lstFiles;
42
EnumDirectory(strDir.c_str(), OnLockFileEnum, (void *)&lstFiles, ENUM_LST_FILES);
44
int nCount = lstFiles.size();
45
for(int i=0; i<nCount; i++)
47
std::string strPath = lstFiles[i];
51
std::string strFile(strPath);
52
int nPos1 = strFile.find_last_of('\\');
54
strFile = strFile.substr(nPos1+1);
56
std::string strFile(basename((char *)strPath.c_str()));
59
//try to extract PID from file name (if exists)
60
unsigned int nPID = 0;
61
std::string strPID = strFile;
62
std::string::size_type nPos = strPID.rfind('_');
63
if(nPos != std::string::npos){
64
strPID = strPID.substr(nPos+1);
65
if(!strPID.empty() && strPID.at(0) == 'n') //correct segment with PID
67
strPID = strPID.substr(1); // cut 'n' marker
69
nPos = strPID.find('.');
70
if(nPos != std::string::npos)
71
strPID = strPID.substr(0, nPos);
73
nPID = atoi(strPID.c_str());
78
remove(strPath.c_str());
81
if(nPID == nProcessID)
82
continue; // our own PID, ignore
84
//check if PID belongs to a running application
85
if(IsPIDRunning(nPID))
87
m_bAlreadyExists = true;
88
return; // skip this file, app that created it is still running
91
remove(strPath.c_str());
94
m_bAlreadyExists = false; //no lock files with running PIDs
97
CSingleInstance::~CSingleInstance()
99
//remove .lock file on exit
102
unsigned int nProcessID = GetCurrentPID();
104
std::string strDir = get_config_dir();
105
EnsureTerminated(strDir, '/');
108
sprintf(szBuffer, "%snotecase_n%d.lock", strDir.c_str(), nProcessID);
112
bool CSingleInstance::ProgramAlreadyStarted()
114
return m_bAlreadyExists;
117
bool OnLockFileEnum(const char *szFile, void *data)
119
std::vector<std::string> *lstFiles = (std::vector<std::string> *)data;
123
//calculate base name
124
std::string strFile(szFile);
125
int nPos = strFile.find_last_of('\\');
127
strFile = strFile.substr(nPos+1);
129
std::string strFile(basename((char *)szFile));
132
if(0 == strncmp(strFile.c_str(), "notecase_", strlen("notecase_")) &&
133
GetFileExt(strFile.c_str()) == ".lock")
135
lstFiles->push_back(szFile);
139
return true; //keep enumerating