Commit f914cd0d authored by Jacobo Aragunde Perez's avatar Jacobo Aragunde Perez Committed by jean-pierre charras
Browse files

Eeschema:commit patch <Prevent Eeschema from opening the same file twice> with...

Eeschema:commit patch <Prevent Eeschema from opening the same file twice> with a small fix for windows.
parent 16402d81
Loading
Loading
Loading
Loading
+26 −0
Original line number Original line Diff line number Diff line
@@ -273,6 +273,7 @@ static struct LANGUAGE_DESCR s_Language_List[] =
EDA_APP::EDA_APP()
EDA_APP::EDA_APP()
{
{
    m_Checker = NULL;
    m_Checker = NULL;
    m_oneInstancePerFileChecker = NULL;
    m_HtmlCtrl = NULL;
    m_HtmlCtrl = NULL;
    m_settings = NULL;
    m_settings = NULL;
    m_LanguageId = wxLANGUAGE_DEFAULT;
    m_LanguageId = wxLANGUAGE_DEFAULT;
@@ -298,6 +299,9 @@ EDA_APP::~EDA_APP()
    if( m_Checker )
    if( m_Checker )
        delete m_Checker;
        delete m_Checker;


    if( m_oneInstancePerFileChecker )
        delete m_oneInstancePerFileChecker;

    delete m_Locale;
    delete m_Locale;
}
}


@@ -1124,3 +1128,25 @@ void EDA_APP::InsertLibraryPath( const wxString& aPaths, size_t aIndex )
        }
        }
    }
    }
}
}

bool EDA_APP::LockFile( const wxString& fileName )
{
    // semaphore to protect the edition of the file by more than one instance
    if( m_oneInstancePerFileChecker != NULL )
    {
        // it means that we had an open file and we are opening a different one
        delete m_oneInstancePerFileChecker;
    }
    wxString lockFileName = fileName + wxT( ".lock" );
    lockFileName.Replace( wxT( "/" ), wxT( "_" ) );
    // We can have filenames coming from Windows, so also convert Windows separator
    lockFileName.Replace( wxT( "\\" ), wxT( "_" ) );
    m_oneInstancePerFileChecker = new wxSingleInstanceChecker( lockFileName );
    if( m_oneInstancePerFileChecker &&
        m_oneInstancePerFileChecker->IsAnotherRunning() )
    {
        return false;
    }

    return true;
}
+20 −8
Original line number Original line Diff line number Diff line
@@ -89,18 +89,33 @@ bool EDA_APP::OnInit()
{
{
    wxFileName      filename;
    wxFileName      filename;
    SCH_EDIT_FRAME* frame = NULL;
    SCH_EDIT_FRAME* frame = NULL;
    bool fileReady = false;


    InitEDA_Appl( wxT( "Eeschema" ), APP_EESCHEMA_T );
    InitEDA_Appl( wxT( "Eeschema" ), APP_EESCHEMA_T );


    if( argc > 1 )
        filename = argv[1];

    if( filename.IsOk() )
    {
        if( filename.GetExt() != SchematicFileExtension )
            filename.SetExt( SchematicFileExtension );

        if( !wxGetApp().LockFile( filename.GetFullPath() ) )
        {
            DisplayError( NULL, _( "This file is already open." ) );
            return false;
        }

        fileReady = true;
    }

    if( m_Checker && m_Checker->IsAnotherRunning() )
    if( m_Checker && m_Checker->IsAnotherRunning() )
      {
      {
          if( !IsOK( NULL, _( "Eeschema is already running, Continue?" ) ) )
          if( !IsOK( NULL, _( "Eeschema is already running, Continue?" ) ) )
              return false;
              return false;
      }
      }


    if( argc > 1 )
        filename = argv[1];

    // Give a default colour for all layers
    // Give a default colour for all layers
    // (actual color will beinitialized by config)
    // (actual color will beinitialized by config)
    for( int ii = 0; ii < MAX_LAYERS; ii++ )
    for( int ii = 0; ii < MAX_LAYERS; ii++ )
@@ -130,11 +145,8 @@ bool EDA_APP::OnInit()
    frame->Zoom_Automatique( true );
    frame->Zoom_Automatique( true );


    /* Load file specified in the command line. */
    /* Load file specified in the command line. */
    if( filename.IsOk() )
    if( fileReady )
    {
    {
        if( filename.GetExt() != SchematicFileExtension )
            filename.SetExt( SchematicFileExtension );

        wxSetWorkingDirectory( filename.GetPath() );
        wxSetWorkingDirectory( filename.GetPath() );


        if( frame->LoadOneEEProject( filename.GetFullPath(), false ) )
        if( frame->LoadOneEEProject( filename.GetFullPath(), false ) )
+16 −8
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@
#include <confirm.h>
#include <confirm.h>
#include <gestfich.h>
#include <gestfich.h>
#include <wxEeschemaStruct.h>
#include <wxEeschemaStruct.h>
#include <appl_wxstruct.h>


#include <general.h>
#include <general.h>
#include <protos.h>
#include <protos.h>
@@ -204,14 +205,6 @@ bool SCH_EDIT_FRAME::LoadOneEEProject( const wxString& aFileName, bool aIsNew )
        FullFileName = dlg.GetPath();
        FullFileName = dlg.GetPath();
    }
    }


    if( g_RootSheet )
    {
        SAFE_DELETE( g_RootSheet );
    }

    CreateScreens();
    screen = GetScreen();

    wxFileName fn = FullFileName;
    wxFileName fn = FullFileName;


    if( fn.IsRelative() )
    if( fn.IsRelative() )
@@ -220,6 +213,21 @@ bool SCH_EDIT_FRAME::LoadOneEEProject( const wxString& aFileName, bool aIsNew )
        FullFileName = fn.GetFullPath();
        FullFileName = fn.GetFullPath();
    }
    }


    if( !wxGetApp().LockFile( FullFileName ) )
    {
        DisplayError( this, _( "This file is already open." ) );
        return false;
    }

    // Clear the screen before open a new file
    if( g_RootSheet )
    {
        SAFE_DELETE( g_RootSheet );
    }

    CreateScreens();
    screen = GetScreen();

    wxLogDebug( wxT( "Loading schematic " ) + FullFileName );
    wxLogDebug( wxT( "Loading schematic " ) + FullFileName );
    wxSetWorkingDirectory( fn.GetPath() );
    wxSetWorkingDirectory( fn.GetPath() );


+10 −0
Original line number Original line Diff line number Diff line
@@ -67,6 +67,9 @@ protected:
    /// Used to prevent multiple instances of an application from being run at the same time.
    /// Used to prevent multiple instances of an application from being run at the same time.
    wxSingleInstanceChecker* m_Checker;
    wxSingleInstanceChecker* m_Checker;


    /// Used to prevent opening the same file multiple times.
    wxSingleInstanceChecker* m_oneInstancePerFileChecker;

    wxString m_Project;
    wxString m_Project;


    /// The application specific configuration settings.
    /// The application specific configuration settings.
@@ -410,6 +413,13 @@ public:
     */
     */
    void InsertLibraryPath( const wxString& aPaths, size_t aIndex );
    void InsertLibraryPath( const wxString& aPaths, size_t aIndex );


    /**
     * Function LockFile
     * Locks the access to a file.
     * @param fileName = full path to the file.
     * @return false if the file was already locked, true otherwise.
     */
    bool LockFile( const wxString& fileName );
};
};


/*
/*