| 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> |
| 10 | // Licence: wxWindows licence |
| 11 | /////////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #ifndef _WX_SNGLINST_H_ |
| 14 | #define _WX_SNGLINST_H_ |
| 15 | |
| 16 | #if wxUSE_SNGLINST_CHECKER |
| 17 | |
| 18 | #include "wx/app.h" |
| 19 | #include "wx/utils.h" |
| 20 | |
| 21 | // ---------------------------------------------------------------------------- |
| 22 | // wxSingleInstanceChecker |
| 23 | // ---------------------------------------------------------------------------- |
| 24 | |
| 25 | class WXDLLIMPEXP_BASE wxSingleInstanceChecker |
| 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 | |
| 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 |
| 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 | // |
| 48 | // returns false if initialization failed, it doesn't mean that another |
| 49 | // instance is running - use IsAnotherRunning() to check it |
| 50 | bool Create(const wxString& name, const wxString& path = wxEmptyString); |
| 51 | |
| 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 | |
| 63 | // is another copy of this program already running? |
| 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 | } |
| 78 | |
| 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 | |
| 86 | // do check if another instance is running, called only if m_impl != NULL |
| 87 | bool DoIsAnotherRunning() const; |
| 88 | |
| 89 | // the implementation details (platform specific) |
| 90 | class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl *m_impl; |
| 91 | |
| 92 | wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker); |
| 93 | }; |
| 94 | |
| 95 | #endif // wxUSE_SNGLINST_CHECKER |
| 96 | |
| 97 | #endif // _WX_SNGLINST_H_ |