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