]>
Commit | Line | Data |
---|---|---|
68a602fc VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/snglinst.h | |
3 | // Purpose: wxSingleInstanceChecker can be used to restrict the number of | |
4 | // simultaneously running copies of a program to one | |
5 | // Author: Vadim Zeitlin | |
6 | // Modified by: | |
7 | // Created: 08.06.01 | |
68a602fc | 8 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
65571936 | 9 | // Licence: wxWindows licence |
68a602fc VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_SNGLINST_H_ | |
13 | #define _WX_SNGLINST_H_ | |
14 | ||
68a602fc VZ |
15 | #if wxUSE_SNGLINST_CHECKER |
16 | ||
956b3d92 VZ |
17 | #include "wx/app.h" |
18 | #include "wx/utils.h" | |
19 | ||
68a602fc VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // wxSingleInstanceChecker | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
bddd7a8d | 24 | class WXDLLIMPEXP_BASE wxSingleInstanceChecker |
68a602fc VZ |
25 | { |
26 | public: | |
27 | // default ctor, use Create() after it | |
28 | wxSingleInstanceChecker() { Init(); } | |
29 | ||
30 | // like Create() but no error checking (dangerous!) | |
31 | wxSingleInstanceChecker(const wxString& name, | |
32 | const wxString& path = wxEmptyString) | |
33 | { | |
34 | Init(); | |
35 | Create(name, path); | |
36 | } | |
37 | ||
956b3d92 VZ |
38 | // notice that calling Create() is optional now, if you don't do it before |
39 | // calling IsAnotherRunning(), CreateDefault() is used automatically | |
40 | // | |
41 | // name it is used as the mutex name under Win32 and the lock file name | |
42 | // under Unix so it should be as unique as possible and must be non-empty | |
68a602fc VZ |
43 | // |
44 | // path is optional and is ignored under Win32 and used as the directory to | |
45 | // create the lock file in under Unix (default is wxGetHomeDir()) | |
46 | // | |
d775fa82 | 47 | // returns false if initialization failed, it doesn't mean that another |
68a602fc VZ |
48 | // instance is running - use IsAnotherRunning() to check it |
49 | bool Create(const wxString& name, const wxString& path = wxEmptyString); | |
50 | ||
956b3d92 VZ |
51 | // use the default name, which is a combination of wxTheApp->GetAppName() |
52 | // and wxGetUserId() for mutex/lock file | |
53 | // | |
54 | // this is called implicitly by IsAnotherRunning() if the checker hadn't | |
55 | // been created until then | |
56 | bool CreateDefault() | |
57 | { | |
58 | wxCHECK_MSG( wxTheApp, false, "must have application instance" ); | |
59 | return Create(wxTheApp->GetAppName() + '-' + wxGetUserId()); | |
60 | } | |
61 | ||
68a602fc | 62 | // is another copy of this program already running? |
956b3d92 VZ |
63 | bool IsAnotherRunning() const |
64 | { | |
65 | if ( !m_impl ) | |
66 | { | |
67 | if ( !const_cast<wxSingleInstanceChecker *>(this)->CreateDefault() ) | |
68 | { | |
69 | // if creation failed, return false as it's better to not | |
70 | // prevent this instance from starting up if there is an error | |
71 | return false; | |
72 | } | |
73 | } | |
74 | ||
75 | return DoIsAnotherRunning(); | |
76 | } | |
04354c1d | 77 | |
68a602fc VZ |
78 | // dtor is not virtual, this class is not meant to be used polymorphically |
79 | ~wxSingleInstanceChecker(); | |
80 | ||
81 | private: | |
82 | // common part of all ctors | |
83 | void Init() { m_impl = NULL; } | |
84 | ||
956b3d92 VZ |
85 | // do check if another instance is running, called only if m_impl != NULL |
86 | bool DoIsAnotherRunning() const; | |
87 | ||
68a602fc | 88 | // the implementation details (platform specific) |
b5dbe15d | 89 | class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl *m_impl; |
22f3361e | 90 | |
c0c133e1 | 91 | wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker); |
68a602fc VZ |
92 | }; |
93 | ||
94 | #endif // wxUSE_SNGLINST_CHECKER | |
95 | ||
96 | #endif // _WX_SNGLINST_H_ |