From f914cd0dec37be991ab4c0d7cae150ab4029704a Mon Sep 17 00:00:00 2001 From: Jacobo Aragunde Perez Date: Fri, 4 Jan 2013 13:06:50 +0100 Subject: [PATCH] Eeschema:commit patch with a small fix for windows. --- common/edaappl.cpp | 26 ++++++++++++++++++++++++++ eeschema/eeschema.cpp | 32 ++++++++++++++++++++++---------- eeschema/files-io.cpp | 24 ++++++++++++++++-------- include/appl_wxstruct.h | 10 ++++++++++ 4 files changed, 74 insertions(+), 18 deletions(-) diff --git a/common/edaappl.cpp b/common/edaappl.cpp index 66f3eade07..c3580aca02 100644 --- a/common/edaappl.cpp +++ b/common/edaappl.cpp @@ -273,6 +273,7 @@ static struct LANGUAGE_DESCR s_Language_List[] = EDA_APP::EDA_APP() { m_Checker = NULL; + m_oneInstancePerFileChecker = NULL; m_HtmlCtrl = NULL; m_settings = NULL; m_LanguageId = wxLANGUAGE_DEFAULT; @@ -298,6 +299,9 @@ EDA_APP::~EDA_APP() if( m_Checker ) delete m_Checker; + if( m_oneInstancePerFileChecker ) + delete m_oneInstancePerFileChecker; + 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; +} diff --git a/eeschema/eeschema.cpp b/eeschema/eeschema.cpp index 6cbe7c94c0..464c0451d1 100644 --- a/eeschema/eeschema.cpp +++ b/eeschema/eeschema.cpp @@ -89,18 +89,33 @@ bool EDA_APP::OnInit() { wxFileName filename; SCH_EDIT_FRAME* frame = NULL; + bool fileReady = false; InitEDA_Appl( wxT( "Eeschema" ), APP_EESCHEMA_T ); - if( m_Checker && m_Checker->IsAnotherRunning() ) - { - if( !IsOK( NULL, _( "Eeschema is already running, Continue?" ) ) ) - return false; - } - 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( !IsOK( NULL, _( "Eeschema is already running, Continue?" ) ) ) + return false; + } + // Give a default colour for all layers // (actual color will beinitialized by config) for( int ii = 0; ii < MAX_LAYERS; ii++ ) @@ -130,11 +145,8 @@ bool EDA_APP::OnInit() frame->Zoom_Automatique( true ); /* Load file specified in the command line. */ - if( filename.IsOk() ) + if( fileReady ) { - if( filename.GetExt() != SchematicFileExtension ) - filename.SetExt( SchematicFileExtension ); - wxSetWorkingDirectory( filename.GetPath() ); if( frame->LoadOneEEProject( filename.GetFullPath(), false ) ) diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index 552e1ccc57..18858d994d 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -204,14 +205,6 @@ bool SCH_EDIT_FRAME::LoadOneEEProject( const wxString& aFileName, bool aIsNew ) FullFileName = dlg.GetPath(); } - if( g_RootSheet ) - { - SAFE_DELETE( g_RootSheet ); - } - - CreateScreens(); - screen = GetScreen(); - wxFileName fn = FullFileName; if( fn.IsRelative() ) @@ -220,6 +213,21 @@ bool SCH_EDIT_FRAME::LoadOneEEProject( const wxString& aFileName, bool aIsNew ) 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 ); wxSetWorkingDirectory( fn.GetPath() ); diff --git a/include/appl_wxstruct.h b/include/appl_wxstruct.h index 8358d8c74f..5838249b7e 100644 --- a/include/appl_wxstruct.h +++ b/include/appl_wxstruct.h @@ -67,6 +67,9 @@ protected: /// Used to prevent multiple instances of an application from being run at the same time. wxSingleInstanceChecker* m_Checker; + /// Used to prevent opening the same file multiple times. + wxSingleInstanceChecker* m_oneInstancePerFileChecker; + wxString m_Project; /// The application specific configuration settings. @@ -410,6 +413,13 @@ public: */ 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 ); }; /*