2016-05-02 22:01:55 +02:00
/** @file
2013-11-15 23:42:00 +01:00
* @ author Edouard DUPIN
* @ copyright 2010 , Edouard DUPIN , all right reserved
* @ license GPL v3 ( see license file )
*/
2013-12-13 21:50:40 +01:00
# include <ewol/context/Context.h>
2013-11-15 23:42:00 +01:00
# include <appl/debug.h>
# include <appl/Gui/WorkerCloseFile.h>
2013-11-17 20:37:06 +01:00
# include <ewol/widget/meta/StdPopUp.h>
2016-03-16 23:09:36 +01:00
# include <ewol/tools/message.h>
2013-11-15 23:42:00 +01:00
2014-08-07 23:41:48 +02:00
appl : : WorkerCloseFile : : WorkerCloseFile ( ) :
2016-03-02 21:51:44 +01:00
signalCloseDone ( this , " close-file-done " , " " ) ,
signalAbort ( this , " close-file-abort " , " " ) ,
2014-06-05 22:01:24 +02:00
m_buffer ( nullptr ) ,
m_worker ( nullptr ) ,
m_bufferManager ( nullptr ) {
2013-11-20 21:57:00 +01:00
addObjectType ( " appl::WorkerCloseFile " ) ;
2013-11-15 23:42:00 +01:00
// load buffer manager:
2014-08-07 23:41:48 +02:00
m_bufferManager = appl : : BufferManager : : create ( ) ;
}
2014-09-15 07:21:22 +02:00
void appl : : WorkerCloseFile : : init ( ) {
2014-09-12 21:52:03 +02:00
ewol : : object : : Worker : : init ( ) ;
2014-09-15 07:21:22 +02:00
}
void appl : : WorkerCloseFile : : startAction ( const std : : string & _bufferName ) {
2014-08-07 23:41:48 +02:00
m_bufferName = _bufferName ;
2014-06-05 22:01:24 +02:00
if ( m_bufferManager = = nullptr ) {
2013-11-15 23:42:00 +01:00
APPL_ERROR ( " can not call unexistant buffer manager ... " ) ;
2014-09-12 21:52:03 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
return ;
}
2013-11-17 20:37:06 +01:00
if ( m_bufferName = = " " ) {
// need to find the curent file ...
2016-07-19 22:03:39 +02:00
ememory : : SharedPtr < appl : : Buffer > tmpp = m_bufferManager - > getBufferSelected ( ) ;
2014-06-05 22:01:24 +02:00
if ( tmpp = = nullptr ) {
2013-11-17 20:37:06 +01:00
APPL_ERROR ( " No selected buffer now ... " ) ;
2014-09-15 07:21:22 +02:00
destroy ( ) ;
2013-11-17 20:37:06 +01:00
return ;
}
m_bufferName = tmpp - > getFileName ( ) ;
}
2013-11-15 23:42:00 +01:00
if ( m_bufferManager - > exist ( m_bufferName ) = = false ) {
APPL_ERROR ( " Try to close an non-existant file : " < < m_bufferName ) ;
2014-09-12 21:52:03 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
return ;
}
2013-11-19 21:43:43 +01:00
m_buffer = m_bufferManager - > get ( m_bufferName ) ;
2014-06-05 22:01:24 +02:00
if ( m_buffer = = nullptr ) {
2013-11-15 23:42:00 +01:00
APPL_ERROR ( " Error to get the buffer : " < < m_bufferName ) ;
2014-09-12 21:52:03 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
return ;
}
2013-11-19 21:43:43 +01:00
if ( m_buffer - > isModify ( ) = = false ) {
2014-08-22 05:21:10 +02:00
signalCloseDone . emit ( ) ;
2014-08-07 23:41:48 +02:00
m_buffer - > destroy ( ) ;
2014-09-15 07:21:22 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
return ;
}
2016-07-19 22:03:39 +02:00
ememory : : SharedPtr < ewol : : widget : : StdPopUp > tmpPopUp = ewol : : widget : : StdPopUp : : create ( ) ;
2014-06-05 22:01:24 +02:00
if ( tmpPopUp = = nullptr ) {
2013-11-15 23:42:00 +01:00
APPL_ERROR ( " Can not create a simple pop-up " ) ;
2014-09-15 07:21:22 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
return ;
}
2016-03-16 23:09:36 +01:00
tmpPopUp - > propertyTitle . set ( " <bold>_T{Close un-saved file:}</bold> " ) ;
tmpPopUp - > propertyComment . set ( " _T{The file named:} <i>' " + m_buffer - > getFileName ( ) + " '</i> _T{is curently modify.}<br/>_T{If you don't saves these modifications,}<br/>_T{they will be definitly lost...} " ) ;
2016-07-19 22:03:39 +02:00
ememory : : SharedPtr < ewol : : widget : : Button > bt = nullptr ;
2013-11-19 21:43:43 +01:00
if ( m_buffer - > hasFileName ( ) = = true ) {
2016-03-15 22:33:06 +01:00
bt = tmpPopUp - > addButton ( " _T{Save} " , true ) ;
2014-06-05 22:01:24 +02:00
if ( bt ! = nullptr ) {
2016-07-19 22:03:39 +02:00
bt - > signalPressed . connect ( sharedFromThis ( ) , & appl : : WorkerCloseFile : : onCallbackSaveValidate ) ;
2013-11-15 23:42:00 +01:00
}
}
2016-03-15 22:33:06 +01:00
bt = tmpPopUp - > addButton ( " _T{Save As} " , true ) ;
2014-06-05 22:01:24 +02:00
if ( bt ! = nullptr ) {
2016-07-19 22:03:39 +02:00
bt - > signalPressed . connect ( sharedFromThis ( ) , & appl : : WorkerCloseFile : : onCallbackSaveAsValidate ) ;
2013-11-15 23:42:00 +01:00
}
2016-03-15 22:33:06 +01:00
bt = tmpPopUp - > addButton ( " _T{Close} " , true ) ;
2014-06-05 22:01:24 +02:00
if ( bt ! = nullptr ) {
2016-07-19 22:03:39 +02:00
bt - > signalPressed . connect ( sharedFromThis ( ) , & appl : : WorkerCloseFile : : onCallbackClose ) ;
2013-11-15 23:42:00 +01:00
}
2016-03-15 22:33:06 +01:00
bt = tmpPopUp - > addButton ( " _T{Cancel} " , true ) ;
2014-09-12 21:52:03 +02:00
if ( bt ! = nullptr ) {
2016-07-19 22:03:39 +02:00
bt - > signalPressed . connect ( sharedFromThis ( ) , & appl : : WorkerCloseFile : : onCallbackCancel ) ;
2014-09-12 21:52:03 +02:00
}
2016-02-15 22:04:10 +01:00
tmpPopUp - > propertyCloseOutEvent . set ( true ) ;
2016-07-19 22:03:39 +02:00
ememory : : SharedPtr < ewol : : widget : : Windows > tmpWindows = ewol : : getContext ( ) . getWindows ( ) ;
2014-06-05 22:01:24 +02:00
if ( tmpWindows = = nullptr ) {
2013-11-15 23:42:00 +01:00
APPL_ERROR ( " Error to get the windows. " ) ;
2014-09-12 21:52:03 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
return ;
}
tmpWindows - > popUpWidgetPush ( tmpPopUp ) ;
}
2014-05-15 21:37:39 +02:00
appl : : WorkerCloseFile : : ~ WorkerCloseFile ( ) {
2014-09-12 21:52:03 +02:00
APPL_ERROR ( " Remove Worker " ) ;
2013-11-15 23:42:00 +01:00
}
2014-09-12 21:52:03 +02:00
void appl : : WorkerCloseFile : : onCallbackCancel ( ) {
APPL_VERBOSE ( " Cancel signal ... " ) ;
signalAbort . emit ( ) ;
destroy ( ) ;
}
2014-08-25 05:55:06 +02:00
void appl : : WorkerCloseFile : : onCallbackSaveAsValidate ( ) {
2014-06-05 22:01:24 +02:00
if ( m_bufferManager = = nullptr ) {
2013-11-15 23:42:00 +01:00
// nothing to do in this case ==> can do nothing ...
return ;
}
2016-03-10 22:37:37 +01:00
m_worker = appl : : WorkerSaveFile : : create ( " buffer-name " , m_bufferName ) ;
2014-08-25 05:55:06 +02:00
if ( m_worker ! = nullptr ) {
2016-07-19 22:03:39 +02:00
m_worker - > signalSaveDone . connect ( sharedFromThis ( ) , & appl : : WorkerCloseFile : : onCallbackClose ) ;
m_worker - > signalAbort . connect ( sharedFromThis ( ) , & appl : : WorkerCloseFile : : onCallbackCancel ) ;
2014-08-25 05:55:06 +02:00
}
}
void appl : : WorkerCloseFile : : onCallbackSaveValidate ( ) {
if ( m_bufferManager = = nullptr ) {
// nothing to do in this case ==> can do nothing ...
2014-09-12 21:52:03 +02:00
signalAbort . emit ( ) ;
destroy ( ) ;
2014-08-25 05:55:06 +02:00
return ;
}
if ( m_buffer = = nullptr ) {
APPL_ERROR ( " Error to get the buffer : oldName= " < < m_bufferName ) ;
2014-09-12 21:52:03 +02:00
signalAbort . emit ( ) ;
destroy ( ) ;
2014-08-25 05:55:06 +02:00
return ;
}
if ( m_buffer - > storeFile ( ) = = false ) {
2016-03-16 23:09:36 +01:00
ewol : : tools : : message : : displayWarning ( " We can not save the file : <br/><i> " + m_buffer - > getFileName ( ) + " </i> " ) ;
2014-09-12 21:52:03 +02:00
signalAbort . emit ( ) ;
2014-08-25 05:55:06 +02:00
} else {
2014-09-15 07:27:51 +02:00
m_buffer - > destroy ( ) ;
m_buffer . reset ( ) ;
2014-08-22 05:21:10 +02:00
signalCloseDone . emit ( ) ;
2013-11-15 23:42:00 +01:00
}
2014-09-12 21:52:03 +02:00
destroy ( ) ;
2013-11-15 23:42:00 +01:00
}
2014-08-25 05:55:06 +02:00
void appl : : WorkerCloseFile : : onCallbackClose ( ) {
if ( m_bufferManager = = nullptr ) {
// nothing to do in this case ==> can do nothing ...
2014-09-12 21:52:03 +02:00
signalAbort . emit ( ) ;
destroy ( ) ;
2014-08-25 05:55:06 +02:00
return ;
}
if ( m_buffer = = nullptr ) {
APPL_ERROR ( " Error to get the buffer : " < < m_bufferName ) ;
2014-09-12 21:52:03 +02:00
signalAbort . emit ( ) ;
destroy ( ) ;
2014-08-25 05:55:06 +02:00
return ;
}
m_buffer - > destroy ( ) ;
m_buffer . reset ( ) ;
2014-09-12 21:52:03 +02:00
signalCloseDone . emit ( ) ;
destroy ( ) ;
2014-08-25 05:55:06 +02:00
}