2001-06-07 21:28:49 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/snglinst.h
|
|
|
|
// Purpose: wxSingleInstanceChecker can be used to restrict the number of
|
|
|
|
// simultaneously running copies of a program to one
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 08.06.01
|
|
|
|
// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
2001-06-07 21:28:49 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_SNGLINST_H_
|
|
|
|
#define _WX_SNGLINST_H_
|
|
|
|
|
|
|
|
#if wxUSE_SNGLINST_CHECKER
|
|
|
|
|
2009-09-16 08:38:00 -04:00
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/utils.h"
|
|
|
|
|
2001-06-07 21:28:49 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxSingleInstanceChecker
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2003-07-01 21:59:24 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxSingleInstanceChecker
|
2001-06-07 21:28:49 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// default ctor, use Create() after it
|
|
|
|
wxSingleInstanceChecker() { Init(); }
|
|
|
|
|
|
|
|
// like Create() but no error checking (dangerous!)
|
|
|
|
wxSingleInstanceChecker(const wxString& name,
|
|
|
|
const wxString& path = wxEmptyString)
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
Create(name, path);
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:38:00 -04:00
|
|
|
// notice that calling Create() is optional now, if you don't do it before
|
|
|
|
// calling IsAnotherRunning(), CreateDefault() is used automatically
|
|
|
|
//
|
|
|
|
// name it is used as the mutex name under Win32 and the lock file name
|
|
|
|
// under Unix so it should be as unique as possible and must be non-empty
|
2001-06-07 21:28:49 -04:00
|
|
|
//
|
|
|
|
// path is optional and is ignored under Win32 and used as the directory to
|
|
|
|
// create the lock file in under Unix (default is wxGetHomeDir())
|
|
|
|
//
|
2004-09-22 10:38:52 -04:00
|
|
|
// returns false if initialization failed, it doesn't mean that another
|
2001-06-07 21:28:49 -04:00
|
|
|
// instance is running - use IsAnotherRunning() to check it
|
|
|
|
bool Create(const wxString& name, const wxString& path = wxEmptyString);
|
|
|
|
|
2009-09-16 08:38:00 -04:00
|
|
|
// use the default name, which is a combination of wxTheApp->GetAppName()
|
|
|
|
// and wxGetUserId() for mutex/lock file
|
|
|
|
//
|
|
|
|
// this is called implicitly by IsAnotherRunning() if the checker hadn't
|
|
|
|
// been created until then
|
|
|
|
bool CreateDefault()
|
|
|
|
{
|
|
|
|
wxCHECK_MSG( wxTheApp, false, "must have application instance" );
|
|
|
|
return Create(wxTheApp->GetAppName() + '-' + wxGetUserId());
|
|
|
|
}
|
|
|
|
|
2001-06-07 21:28:49 -04:00
|
|
|
// is another copy of this program already running?
|
2009-09-16 08:38:00 -04:00
|
|
|
bool IsAnotherRunning() const
|
|
|
|
{
|
|
|
|
if ( !m_impl )
|
|
|
|
{
|
|
|
|
if ( !const_cast<wxSingleInstanceChecker *>(this)->CreateDefault() )
|
|
|
|
{
|
|
|
|
// if creation failed, return false as it's better to not
|
|
|
|
// prevent this instance from starting up if there is an error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoIsAnotherRunning();
|
|
|
|
}
|
2002-07-21 08:41:38 -04:00
|
|
|
|
2001-06-07 21:28:49 -04:00
|
|
|
// dtor is not virtual, this class is not meant to be used polymorphically
|
|
|
|
~wxSingleInstanceChecker();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// common part of all ctors
|
|
|
|
void Init() { m_impl = NULL; }
|
|
|
|
|
2009-09-16 08:38:00 -04:00
|
|
|
// do check if another instance is running, called only if m_impl != NULL
|
|
|
|
bool DoIsAnotherRunning() const;
|
|
|
|
|
2001-06-07 21:28:49 -04:00
|
|
|
// the implementation details (platform specific)
|
2007-07-09 06:09:52 -04:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl *m_impl;
|
2003-01-02 18:38:11 -05:00
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker);
|
2001-06-07 21:28:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // wxUSE_SNGLINST_CHECKER
|
|
|
|
|
|
|
|
#endif // _WX_SNGLINST_H_
|