~ubuntu-branches/ubuntu/oneiric/notecase/oneiric

« back to all changes in this revision

Viewing changes to src/_unx/SingleInstance.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Soyez
  • Date: 2008-11-10 11:29:57 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20081110112957-9uu6p24w7i0c7ib2
Tags: 1.9.7-0ubuntu1
* New Upstream Release
* Updated Standards-Version to 3.8.0, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
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
 
////////////////////////////////////////////////////////////////////////////
9
 
 
10
 
#include "SingleInstance.h"
11
 
#include "../lib/FilePath.h"
12
 
#include "../interface.h"
13
 
#include "../lib/EnumDirectory.h"
14
 
#include <stdlib.h>
15
 
#include <string.h>
16
 
#include <vector>
17
 
 
18
 
#ifndef WIN32
19
 
 #include <libgen.h>    //basename
20
 
#endif
21
 
 
22
 
bool OnLockFileEnum(const char *szFile, void *data);
23
 
const char *get_config_dir();
24
 
 
25
 
CSingleInstance::CSingleInstance(const char *szName)
26
 
{
27
 
//calculate PID
28
 
        unsigned int nProcessID = GetCurrentPID();
29
 
        
30
 
        std::string strDir = get_config_dir();
31
 
        EnsureTerminated(strDir, '/');
32
 
 
33
 
        //create our own .lock file ASAP
34
 
        char szBuffer[1024];
35
 
        sprintf(szBuffer, "%snotecase_n%d.lock", strDir.c_str(), nProcessID);
36
 
        FILE *pTmp = fopen(szBuffer, "w");
37
 
        if(pTmp)
38
 
                fclose(pTmp);
39
 
 
40
 
        //list existing .lock files
41
 
        std::vector<std::string> lstFiles;
42
 
        EnumDirectory(strDir.c_str(), OnLockFileEnum, (void *)&lstFiles, ENUM_LST_FILES);
43
 
 
44
 
        int nCount = lstFiles.size();
45
 
        for(int i=0; i<nCount; i++)
46
 
        {
47
 
                        std::string strPath = lstFiles[i];
48
 
 
49
 
                #ifdef _WIN32
50
 
                        //calculate base name
51
 
                        std::string strFile(strPath);
52
 
                        int nPos1 = strFile.find_last_of('\\');
53
 
                        if(nPos1 > 0)
54
 
                                strFile = strFile.substr(nPos1+1);
55
 
                #else
56
 
                        std::string strFile(basename((char *)strPath.c_str()));
57
 
                #endif
58
 
 
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
66
 
                                {
67
 
                                        strPID = strPID.substr(1); // cut 'n' marker
68
 
 
69
 
                                        nPos = strPID.find('.');
70
 
                                        if(nPos != std::string::npos)
71
 
                                                strPID = strPID.substr(0, nPos);
72
 
 
73
 
                                        nPID = atoi(strPID.c_str());
74
 
                                }
75
 
                        }
76
 
                        
77
 
                        if(nPID <= 0){
78
 
                                remove(strPath.c_str());
79
 
                                continue;
80
 
                        }
81
 
                        if(nPID == nProcessID)
82
 
                                continue;       // our own PID, ignore
83
 
 
84
 
                        //check if PID belongs to a running application
85
 
                        if(IsPIDRunning(nPID))
86
 
                        {
87
 
                                m_bAlreadyExists = true;
88
 
                                return; // skip this file, app that created it is still running
89
 
                        }
90
 
                        else
91
 
                                remove(strPath.c_str());
92
 
                }
93
 
 
94
 
                m_bAlreadyExists = false;       //no lock files with running PIDs
95
 
}
96
 
 
97
 
CSingleInstance::~CSingleInstance()
98
 
{
99
 
        //remove .lock file on exit
100
 
 
101
 
        //calculate PID
102
 
        unsigned int nProcessID = GetCurrentPID();
103
 
        
104
 
        std::string strDir = get_config_dir();
105
 
        EnsureTerminated(strDir, '/');
106
 
 
107
 
        char szBuffer[1024];
108
 
        sprintf(szBuffer, "%snotecasepro_n%d.lock", strDir.c_str(), nProcessID);
109
 
        remove(szBuffer);
110
 
}
111
 
 
112
 
bool CSingleInstance::ProgramAlreadyStarted()
113
 
{
114
 
        return m_bAlreadyExists;
115
 
}
116
 
 
117
 
bool OnLockFileEnum(const char *szFile, void *data)
118
 
{
119
 
        std::vector<std::string> *lstFiles = (std::vector<std::string> *)data;
120
 
        if(data)
121
 
        {
122
 
        #ifdef _WIN32
123
 
                //calculate base name
124
 
                std::string strFile(szFile);
125
 
                int nPos = strFile.find_last_of('\\');
126
 
                if(nPos > 0)
127
 
                        strFile = strFile.substr(nPos+1);
128
 
        #else
129
 
                std::string strFile(basename((char *)szFile));
130
 
        #endif
131
 
 
132
 
                if(0 == strncmp(strFile.c_str(), "notecasepro_", strlen("notecasepro_")) &&
133
 
                         GetFileExt(strFile.c_str()) == ".lock")
134
 
                {
135
 
                        lstFiles->push_back(szFile);
136
 
                }
137
 
        }
138
 
 
139
 
        return true;    //keep enumerating
140
 
}
141
 
 
142
 
 
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
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
////////////////////////////////////////////////////////////////////////////
 
9
 
 
10
#include "SingleInstance.h"
 
11
#include "../lib/FilePath.h"
 
12
#include "../interface.h"
 
13
#include "../lib/EnumDirectory.h"
 
14
#include <stdlib.h>
 
15
#include <string.h>
 
16
#include <vector>
 
17
 
 
18
#ifndef WIN32
 
19
 #include <libgen.h>    //basename
 
20
#endif
 
21
 
 
22
bool OnLockFileEnum(const char *szFile, void *data);
 
23
const char *get_config_dir();
 
24
 
 
25
CSingleInstance::CSingleInstance(const char *szName)
 
26
{
 
27
//calculate PID
 
28
        unsigned int nProcessID = GetCurrentPID();
 
29
 
 
30
        std::string strDir = get_config_dir();
 
31
        EnsureTerminated(strDir, '/');
 
32
 
 
33
        //create our own .lock file ASAP
 
34
        char szBuffer[1024];
 
35
        sprintf(szBuffer, "%snotecase_n%d.lock", strDir.c_str(), nProcessID);
 
36
        FILE *pTmp = fopen(szBuffer, "w");
 
37
        if(pTmp)
 
38
                fclose(pTmp);
 
39
 
 
40
        //list existing .lock files
 
41
        std::vector<std::string> lstFiles;
 
42
        EnumDirectory(strDir.c_str(), OnLockFileEnum, (void *)&lstFiles, ENUM_LST_FILES);
 
43
 
 
44
        int nCount = lstFiles.size();
 
45
        for(int i=0; i<nCount; i++)
 
46
        {
 
47
                        std::string strPath = lstFiles[i];
 
48
 
 
49
                #ifdef _WIN32
 
50
                        //calculate base name
 
51
                        std::string strFile(strPath);
 
52
                        int nPos1 = strFile.find_last_of('\\');
 
53
                        if(nPos1 > 0)
 
54
                                strFile = strFile.substr(nPos1+1);
 
55
                #else
 
56
                        std::string strFile(basename((char *)strPath.c_str()));
 
57
                #endif
 
58
 
 
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
 
66
                                {
 
67
                                        strPID = strPID.substr(1); // cut 'n' marker
 
68
 
 
69
                                        nPos = strPID.find('.');
 
70
                                        if(nPos != std::string::npos)
 
71
                                                strPID = strPID.substr(0, nPos);
 
72
 
 
73
                                        nPID = atoi(strPID.c_str());
 
74
                                }
 
75
                        }
 
76
 
 
77
                        if(nPID <= 0){
 
78
                                remove(strPath.c_str());
 
79
                                continue;
 
80
                        }
 
81
                        if(nPID == nProcessID)
 
82
                                continue;       // our own PID, ignore
 
83
 
 
84
                        //check if PID belongs to a running application
 
85
                        if(IsPIDRunning(nPID))
 
86
                        {
 
87
                                m_bAlreadyExists = true;
 
88
                                return; // skip this file, app that created it is still running
 
89
                        }
 
90
                        else
 
91
                                remove(strPath.c_str());
 
92
                }
 
93
 
 
94
                m_bAlreadyExists = false;       //no lock files with running PIDs
 
95
}
 
96
 
 
97
CSingleInstance::~CSingleInstance()
 
98
{
 
99
        //remove .lock file on exit
 
100
 
 
101
        //calculate PID
 
102
        unsigned int nProcessID = GetCurrentPID();
 
103
 
 
104
        std::string strDir = get_config_dir();
 
105
        EnsureTerminated(strDir, '/');
 
106
 
 
107
        char szBuffer[1024];
 
108
        sprintf(szBuffer, "%snotecase_n%d.lock", strDir.c_str(), nProcessID);
 
109
        remove(szBuffer);
 
110
}
 
111
 
 
112
bool CSingleInstance::ProgramAlreadyStarted()
 
113
{
 
114
        return m_bAlreadyExists;
 
115
}
 
116
 
 
117
bool OnLockFileEnum(const char *szFile, void *data)
 
118
{
 
119
        std::vector<std::string> *lstFiles = (std::vector<std::string> *)data;
 
120
        if(data)
 
121
        {
 
122
        #ifdef _WIN32
 
123
                //calculate base name
 
124
                std::string strFile(szFile);
 
125
                int nPos = strFile.find_last_of('\\');
 
126
                if(nPos > 0)
 
127
                        strFile = strFile.substr(nPos+1);
 
128
        #else
 
129
                std::string strFile(basename((char *)szFile));
 
130
        #endif
 
131
 
 
132
                if(0 == strncmp(strFile.c_str(), "notecase_", strlen("notecase_")) &&
 
133
                         GetFileExt(strFile.c_str()) == ".lock")
 
134
                {
 
135
                        lstFiles->push_back(szFile);
 
136
                }
 
137
        }
 
138
 
 
139
        return true;    //keep enumerating
 
140
}