2011-07-20 10:33:24 +02:00
|
|
|
/**
|
|
|
|
* @author Edouard DUPIN
|
2012-11-25 11:55:06 +01:00
|
|
|
*
|
|
|
|
* @copyright 2010, Edouard DUPIN, all right reserved
|
|
|
|
*
|
|
|
|
* @license GPL v3 (see license file)
|
2011-07-20 10:33:24 +02:00
|
|
|
*/
|
|
|
|
|
2014-05-13 21:48:20 +02:00
|
|
|
#include <etk/types.h>
|
|
|
|
#include <etk/os/FSNode.h>
|
2013-10-25 20:49:26 +02:00
|
|
|
#include <appl/debug.h>
|
2012-04-24 09:42:14 +02:00
|
|
|
#include <appl/global.h>
|
2013-10-29 21:13:45 +01:00
|
|
|
#include <appl/BufferManager.h>
|
2013-12-13 21:50:40 +01:00
|
|
|
#include <ewol/object/Object.h>
|
|
|
|
#include <ewol/object/Manager.h>
|
|
|
|
#include <ewol/resource/Manager.h>
|
2011-07-20 10:33:24 +02:00
|
|
|
|
|
|
|
#undef __class__
|
2013-10-28 21:47:51 +01:00
|
|
|
#define __class__ "BufferManager"
|
|
|
|
|
2014-08-25 22:44:42 +02:00
|
|
|
appl::BufferManager::BufferManager() :
|
|
|
|
signalNewBuffer(*this, "new-buffer"),
|
|
|
|
signalSelectFile(*this, "select-buffer"),
|
2014-09-15 07:21:22 +02:00
|
|
|
signalTextSelectionChange(*this, "text-selection-change"),
|
|
|
|
signalRemoveBuffer(*this, "remove-buffer") {
|
2013-11-20 21:57:00 +01:00
|
|
|
addObjectType("appl::BufferManager");
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
|
|
|
|
2014-08-07 23:41:48 +02:00
|
|
|
void appl::BufferManager::init(const std::string& _uniqueName) {
|
|
|
|
ewol::Resource::init(_uniqueName);
|
|
|
|
}
|
|
|
|
|
2014-05-15 21:37:39 +02:00
|
|
|
appl::BufferManager::~BufferManager() {
|
2013-10-29 21:13:45 +01:00
|
|
|
m_list.clear();
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:42:00 +01:00
|
|
|
|
2014-08-07 23:41:48 +02:00
|
|
|
std::shared_ptr<appl::Buffer> appl::BufferManager::createNewBuffer() {
|
|
|
|
std::shared_ptr<appl::Buffer> tmp = appl::Buffer::create();
|
2014-06-05 22:01:24 +02:00
|
|
|
if (tmp == nullptr) {
|
2013-11-15 23:42:00 +01:00
|
|
|
APPL_ERROR("Can not allocate the Buffer (empty).");
|
2014-06-05 22:01:24 +02:00
|
|
|
return nullptr;
|
2013-11-15 23:42:00 +01:00
|
|
|
}
|
2014-09-15 07:21:22 +02:00
|
|
|
tmp->setParent(shared_from_this());
|
2013-11-15 23:42:00 +01:00
|
|
|
m_list.push_back(tmp);
|
2014-08-25 22:44:42 +02:00
|
|
|
APPL_INFO("Create a new Buffer");
|
|
|
|
signalNewBuffer.emit(tmp->getFileName());
|
|
|
|
APPL_INFO("Create a new Buffer (done)");
|
|
|
|
APPL_INFO("select Buffer");
|
|
|
|
signalSelectFile.emit(tmp->getFileName());
|
|
|
|
APPL_INFO("select Buffer (done)");
|
2013-11-15 23:42:00 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2014-08-07 23:41:48 +02:00
|
|
|
std::shared_ptr<appl::Buffer> appl::BufferManager::get(const std::string& _fileName, bool _createIfNeeded) {
|
2014-08-25 22:44:42 +02:00
|
|
|
APPL_INFO("get('" << _fileName << "'," << _createIfNeeded << ")");
|
2014-05-25 21:17:06 +02:00
|
|
|
for (auto &it : m_list) {
|
2014-06-05 22:01:24 +02:00
|
|
|
if (it == nullptr) {
|
2013-10-29 21:13:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-25 21:17:06 +02:00
|
|
|
if (it->getFileName() == _fileName) {
|
|
|
|
return it;
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_createIfNeeded == true) {
|
2013-11-25 21:20:13 +01:00
|
|
|
if (etk::FSNodeGetType(_fileName) == etk::FSN_FOLDER) {
|
2014-05-13 21:48:20 +02:00
|
|
|
APPL_WARNING("try open a folder : " << _fileName);
|
|
|
|
APPL_CRITICAL("plop");
|
2014-06-05 22:01:24 +02:00
|
|
|
return nullptr;
|
2013-11-25 21:20:13 +01:00
|
|
|
}
|
2014-08-07 23:41:48 +02:00
|
|
|
std::shared_ptr<appl::Buffer> tmp = appl::Buffer::create();
|
2014-06-05 22:01:24 +02:00
|
|
|
if (tmp == nullptr) {
|
2013-10-29 21:13:45 +01:00
|
|
|
APPL_ERROR("Can not allocate the Buffer class : " << _fileName);
|
2014-06-05 22:01:24 +02:00
|
|
|
return nullptr;
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
2014-09-12 22:10:32 +02:00
|
|
|
tmp->setParent(shared_from_this());
|
2013-10-29 21:13:45 +01:00
|
|
|
tmp->loadFile(_fileName);
|
2013-11-14 21:57:10 +01:00
|
|
|
m_list.push_back(tmp);
|
2014-08-25 22:44:42 +02:00
|
|
|
APPL_INFO("Creata a open Buffer");
|
|
|
|
signalNewBuffer.emit(tmp->getFileName());
|
|
|
|
APPL_INFO("Creata a open Buffer (done)");
|
2013-10-29 21:13:45 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
2014-06-05 22:01:24 +02:00
|
|
|
return nullptr;
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
2014-08-25 22:44:42 +02:00
|
|
|
|
2014-08-07 23:41:48 +02:00
|
|
|
void appl::BufferManager::setBufferSelected(std::shared_ptr<appl::Buffer> _bufferSelected) {
|
2013-11-07 21:08:57 +01:00
|
|
|
m_bufferSelected = _bufferSelected;
|
2014-08-25 22:44:42 +02:00
|
|
|
if (m_bufferSelected == nullptr) {
|
|
|
|
APPL_ERROR("select a NULL buffer ...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
APPL_INFO("Set buffer selected");
|
|
|
|
//signalSelectFile.emit(m_bufferSelected->getName());
|
|
|
|
APPL_INFO("Set buffer selected (done)");
|
2013-11-07 21:08:57 +01:00
|
|
|
}
|
2013-10-29 21:13:45 +01:00
|
|
|
|
2014-08-07 23:41:48 +02:00
|
|
|
std::shared_ptr<appl::Buffer> appl::BufferManager::get(int32_t _id) {
|
2014-05-25 21:17:06 +02:00
|
|
|
int32_t id = 0;
|
2014-06-02 21:04:35 +02:00
|
|
|
for (auto &it : m_list) {
|
2014-05-25 21:17:06 +02:00
|
|
|
if (id == _id) {
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
id++;
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
2014-05-25 21:17:06 +02:00
|
|
|
return m_list.back();
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
|
|
|
|
2013-11-14 21:57:10 +01:00
|
|
|
bool appl::BufferManager::exist(const std::string& _fileName) {
|
2014-06-02 21:04:35 +02:00
|
|
|
for (auto &it : m_list) {
|
2014-05-25 21:17:06 +02:00
|
|
|
if (it == nullptr) {
|
2013-10-30 21:16:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-25 21:17:06 +02:00
|
|
|
if (it->getFileName() == _fileName) {
|
2013-10-30 21:16:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-14 21:57:10 +01:00
|
|
|
void appl::BufferManager::open(const std::string& _fileName) {
|
2013-11-25 21:20:13 +01:00
|
|
|
if (exist(_fileName) == true) {
|
2014-09-12 21:36:20 +02:00
|
|
|
APPL_WARNING(" the element '" << _fileName << "' already exist ... just reselect it ...");
|
|
|
|
signalSelectFile.emit(_fileName);
|
2013-11-25 21:20:13 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-06-05 22:01:24 +02:00
|
|
|
if (get(_fileName, true) == nullptr) {
|
2014-09-12 21:36:20 +02:00
|
|
|
APPL_ERROR("Error get '" << _fileName << "' ... ");
|
2013-11-25 21:20:13 +01:00
|
|
|
return;
|
2013-10-30 21:16:38 +01:00
|
|
|
}
|
2014-08-25 22:44:42 +02:00
|
|
|
signalSelectFile.emit(_fileName);
|
2013-10-29 21:13:45 +01:00
|
|
|
}
|
|
|
|
|
2014-09-12 22:10:32 +02:00
|
|
|
|
|
|
|
void appl::BufferManager::requestDestroyFromChild(const std::shared_ptr<Object>& _child) {
|
|
|
|
APPL_WARNING("Buffer request a close...");
|
2014-09-15 07:21:22 +02:00
|
|
|
bool find = false;
|
|
|
|
int32_t newValue = -1;
|
2014-09-12 22:10:32 +02:00
|
|
|
auto it = m_list.begin();
|
|
|
|
while(it != m_list.end()) {
|
|
|
|
if (*it == nullptr) {
|
|
|
|
it = m_list.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*it == _child) {
|
|
|
|
it = m_list.erase(it);
|
2014-09-15 07:21:22 +02:00
|
|
|
find = true;
|
|
|
|
break;
|
2014-09-12 22:10:32 +02:00
|
|
|
}
|
2014-09-15 07:21:22 +02:00
|
|
|
newValue++;
|
2014-09-12 22:10:32 +02:00
|
|
|
++it;
|
|
|
|
}
|
2014-09-15 07:21:22 +02:00
|
|
|
if (find == true) {
|
|
|
|
signalRemoveBuffer.emit(std::dynamic_pointer_cast<appl::Buffer>(_child));
|
|
|
|
}
|
|
|
|
if (m_bufferSelected == _child) {
|
|
|
|
APPL_ERROR("is selected");
|
|
|
|
signalSelectFile.emit("");
|
|
|
|
m_bufferSelected = nullptr;
|
|
|
|
}
|
2014-09-12 22:10:32 +02:00
|
|
|
}
|