edn/sources/appl/Gui/WorkerCloseFile.cpp

173 lines
4.8 KiB
C++
Raw Normal View History

/**
* @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>
#include <appl/debug.h>
#include <appl/Gui/WorkerCloseFile.h>
#include <ewol/widget/meta/StdPopUp.h>
#undef __class__
#define __class__ "WorkerCloseFile"
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) {
addObjectType("appl::WorkerCloseFile");
// load buffer manager:
m_bufferManager = appl::BufferManager::create();
}
void appl::WorkerCloseFile::init() {
2014-09-12 21:52:03 +02:00
ewol::object::Worker::init();
}
void appl::WorkerCloseFile::startAction(const std::string& _bufferName) {
m_bufferName = _bufferName;
2014-06-05 22:01:24 +02:00
if (m_bufferManager == nullptr) {
APPL_ERROR("can not call unexistant buffer manager ... ");
2014-09-12 21:52:03 +02:00
destroy();
return;
}
if (m_bufferName == "") {
// need to find the curent file ...
std::shared_ptr<appl::Buffer> tmpp = m_bufferManager->getBufferSelected();
2014-06-05 22:01:24 +02:00
if (tmpp == nullptr) {
APPL_ERROR("No selected buffer now ...");
destroy();
return;
}
m_bufferName = tmpp->getFileName();
}
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();
return;
}
m_buffer = m_bufferManager->get(m_bufferName);
2014-06-05 22:01:24 +02:00
if (m_buffer == nullptr) {
APPL_ERROR("Error to get the buffer : " << m_bufferName);
2014-09-12 21:52:03 +02:00
destroy();
return;
}
if (m_buffer->isModify() == false) {
2014-08-22 05:21:10 +02:00
signalCloseDone.emit();
m_buffer->destroy();
destroy();
return;
}
std::shared_ptr<ewol::widget::StdPopUp> tmpPopUp = ewol::widget::StdPopUp::create();
2014-06-05 22:01:24 +02:00
if (tmpPopUp == nullptr) {
APPL_ERROR("Can not create a simple pop-up");
destroy();
return;
}
tmpPopUp->setTitle("<bold>Close un-saved file:</bold>");
tmpPopUp->setComment("The file named : <i>\"" + m_buffer->getFileName() + "\"</i> is curently modify. <br/>If you don't saves these modifications,<br/>they will be definitly lost...");
std::shared_ptr<ewol::widget::Button> bt = nullptr;
if (m_buffer->hasFileName() == true) {
bt = tmpPopUp->addButton("Save", true);
2014-06-05 22:01:24 +02:00
if (bt != nullptr) {
2016-02-19 23:33:00 +01:00
bt->signalPressed.connect(shared_from_this(), &appl::WorkerCloseFile::onCallbackSaveValidate);
}
}
bt = tmpPopUp->addButton("Save As", true);
2014-06-05 22:01:24 +02:00
if (bt != nullptr) {
2016-02-19 23:33:00 +01:00
bt->signalPressed.connect(shared_from_this(), &appl::WorkerCloseFile::onCallbackSaveAsValidate);
}
bt = tmpPopUp->addButton("Close", true);
2014-06-05 22:01:24 +02:00
if (bt != nullptr) {
2016-02-19 23:33:00 +01:00
bt->signalPressed.connect(shared_from_this(), &appl::WorkerCloseFile::onCallbackClose);
}
2014-09-12 21:52:03 +02:00
bt = tmpPopUp->addButton("Cancel", true);
if (bt != nullptr) {
2016-02-19 23:33:00 +01:00
bt->signalPressed.connect(shared_from_this(), &appl::WorkerCloseFile::onCallbackCancel);
2014-09-12 21:52:03 +02:00
}
2016-02-15 22:04:10 +01:00
tmpPopUp->propertyCloseOutEvent.set(true);
std::shared_ptr<ewol::widget::Windows> tmpWindows = ewol::getContext().getWindows();
2014-06-05 22:01:24 +02:00
if (tmpWindows == nullptr) {
APPL_ERROR("Error to get the windows.");
2014-09-12 21:52:03 +02:00
destroy();
return;
}
tmpWindows->popUpWidgetPush(tmpPopUp);
}
appl::WorkerCloseFile::~WorkerCloseFile() {
2014-09-12 21:52:03 +02:00
APPL_ERROR("Remove Worker");
}
2014-09-12 21:52:03 +02:00
void appl::WorkerCloseFile::onCallbackCancel() {
APPL_VERBOSE("Cancel signal ...");
signalAbort.emit();
destroy();
}
void appl::WorkerCloseFile::onCallbackSaveAsValidate() {
2014-06-05 22:01:24 +02:00
if (m_bufferManager == nullptr) {
// nothing to do in this case ==> can do nothing ...
return;
}
m_worker = appl::WorkerSaveFile::create(m_bufferName);
if (m_worker != nullptr) {
2016-02-19 23:33:00 +01:00
m_worker->signalSaveDone.connect(shared_from_this(), &appl::WorkerCloseFile::onCallbackClose);
m_worker->signalAbort.connect(shared_from_this(), &appl::WorkerCloseFile::onCallbackCancel);
}
}
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();
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();
return;
}
if (m_buffer->storeFile() == false) {
std::shared_ptr<ewol::widget::Windows> tmpWindows = ewol::getContext().getWindows();
if (tmpWindows == nullptr) {
return;
}
tmpWindows->displayWarningMessage("We can not save the file : <br/><i>" + m_buffer->getFileName() + "</i>");
2014-09-12 21:52:03 +02:00
signalAbort.emit();
} else {
m_buffer->destroy();
m_buffer.reset();
2014-08-22 05:21:10 +02:00
signalCloseDone.emit();
}
2014-09-12 21:52:03 +02:00
destroy();
}
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();
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();
return;
}
m_buffer->destroy();
m_buffer.reset();
2014-09-12 21:52:03 +02:00
signalCloseDone.emit();
destroy();
}