edn/sources/appl/Buffer/BufferManager.cpp

555 lines
13 KiB
C++
Raw Normal View History

/**
* @author Edouard DUPIN
2012-11-25 11:55:06 +01:00
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license GPL v3 (see license file)
*/
2012-04-23 10:15:43 +02:00
#include <appl/Debug.h>
2012-04-24 09:42:14 +02:00
#include <appl/global.h>
#include <BufferManager.h>
2013-09-02 06:45:12 +02:00
#include <ewol/renderer/EObject.h>
#include <ewol/renderer/EObjectManager.h>
#undef __class__
#define __class__ "classBufferManager"
2012-02-29 18:06:08 +01:00
class classBufferManager: public ewol::EObject
{
public:
// Constructeur
classBufferManager(void);
~classBufferManager(void);
const char * const GetObjectType(void)
{
2012-07-24 17:48:18 +02:00
return "ApplBufferManager";
}
public:
2013-05-12 21:08:31 +02:00
virtual void OnReceiveMessage(const ewol::EMessage& _msg);
private:
// return the ID of the buffer allocated
// create a buffer with no element
int32_t Create(void);
// open curent filename
2012-11-01 10:47:36 +01:00
int32_t Open(etk::FSNode &myFile);
bool Remove(int32_t BufferID);
public:
int32_t GetSelected(void) { return m_idSelected;};
//void SetSelected(int32_t id) {m_idSelected = id;};
BufferText* Get(int32_t BufferID);
bool Exist(int32_t BufferID);
2012-11-01 10:47:36 +01:00
bool Exist(etk::FSNode &myFile);
int32_t GetId(etk::FSNode &myFile);
// return the number of buffer (open in the past) if 5 buffer open and 4 close ==> return 5
uint32_t Size(void);
uint32_t SizeOpen(void);
int32_t WitchBuffer(int32_t iEmeElement);
private:
etk::Vector<BufferText*> listBuffer; //!< element List of the char Elements
void RemoveAll(void); //!< remove all buffer
int32_t m_idSelected;
};
// Constructeur
classBufferManager::classBufferManager(void)
{
m_idSelected = -1;
2012-02-29 18:06:08 +01:00
RegisterMultiCast(ednMsgGuiNew);
RegisterMultiCast(ednMsgOpenFile);
RegisterMultiCast(ednMsgGuiClose);
RegisterMultiCast(ednMsgGuiSave);
RegisterMultiCast(ednMsgCodeViewSelectedId);
RegisterMultiCast(ednMsgBufferId);
}
classBufferManager::~classBufferManager(void)
{
//clean All Buffer
2012-04-23 10:15:43 +02:00
APPL_INFO("~classBufferManager::RemoveAll();");
RemoveAll();
// clear The list of Buffer
2012-04-23 10:15:43 +02:00
APPL_INFO("~classBufferManager::listBuffer.Clear();");
listBuffer.Clear();
}
2013-05-12 21:08:31 +02:00
void classBufferManager::OnReceiveMessage(const ewol::EMessage& _msg)
{
2013-05-12 21:08:31 +02:00
ewol::EObject::OnReceiveMessage(_msg);
2012-02-29 18:06:08 +01:00
2013-05-12 21:08:31 +02:00
if (_msg.GetMessage() == ednMsgBufferId) {
// select a new buffer ID :
2013-05-12 21:08:31 +02:00
if (_msg.GetData() == "") {
2012-04-23 10:15:43 +02:00
APPL_ERROR("Request select buffer ID = \"\" ");
} else {
int32_t newID = -1;
2013-05-12 21:08:31 +02:00
sscanf(_msg.GetData().c_str(), "%d", &newID);
if(true == Exist(newID)) {
m_idSelected = newID;
} else {
m_idSelected = -1;
2012-04-23 10:15:43 +02:00
APPL_ERROR("Request a non existant ID : " << newID << " reset to -1...");
}
}
2013-05-12 21:08:31 +02:00
} else if (_msg.GetMessage() == ednMsgGuiNew) {
int32_t newOne = Create();
if (-1 != newOne) {
m_idSelected = newOne;
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgBufferId, m_idSelected);
SendMultiCast(ednMsgBufferListChange);
}
2013-05-12 21:08:31 +02:00
} else if (_msg.GetMessage() == ednMsgOpenFile) {
if (_msg.GetData() != "" ) {
etk::FSNode myFile(_msg.GetData());
if (myFile.GetNodeType() == etk::FSN_FILE) {
APPL_DEBUG("request open file = \"" << _msg.GetData() << "\" ?= \"" << myFile << "\"");
int32_t newOne = Open(myFile);
if (-1 != newOne) {
m_idSelected = newOne;
SendMultiCast(ednMsgBufferId, m_idSelected);
SendMultiCast(ednMsgBufferListChange);
} else {
// TODO : notify user that we can not open the request file...
APPL_ERROR("Can not open the file : \"" << myFile << "\"");
}
2012-05-10 17:47:52 +02:00
} else {
APPL_ERROR("Request to open an Unknox element file : " << myFile << " type:" << myFile.GetNodeType());
2012-02-05 22:03:36 +01:00
}
}
2013-05-12 21:08:31 +02:00
} else if (_msg.GetMessage() == ednMsgGuiSave) {
if (_msg.GetData() == "") {
2012-04-23 10:15:43 +02:00
APPL_ERROR("Null data for close file ... ");
} else {
2013-05-12 21:08:31 +02:00
if (_msg.GetData() == "current") {
// Check buffer existence
if(true == Exist(m_idSelected)) {
// If no name ==> request a Gui display ...
if (Get(m_idSelected)->HaveName() == false) {
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgGuiSaveAs, "current");
} else {
Get(m_idSelected)->Save();
}
}
} else {
int32_t newId;
2013-05-12 21:08:31 +02:00
sscanf(_msg.GetData().c_str(), "%d", &newId);
if (false == Exist(newId)) {
2012-04-23 10:15:43 +02:00
APPL_ERROR("Request a save As with a non existant ID=" << newId);
} else {
// If no name ==> request a Gui display ...
if (Get(newId)->HaveName() == false) {
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgGuiSaveAs, newId);
} else {
Get(m_idSelected)->Save();
}
}
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgBufferState, "saved");
}
}
2013-05-12 21:08:31 +02:00
} else if (_msg.GetMessage() == ednMsgGuiClose) {
if (_msg.GetData() == "") {
2012-04-23 10:15:43 +02:00
APPL_ERROR("Null data for close file ... ");
} else {
2013-05-12 21:08:31 +02:00
if (_msg.GetData() == "All") {
} else {
int32_t closeID = -1;
2013-05-12 21:08:31 +02:00
if (_msg.GetData() == "current") {
closeID = m_idSelected;
2012-04-23 10:15:43 +02:00
APPL_DEBUG("Close specific buffer ID" << closeID);
} else {
// close specific buffer ...
2013-05-12 21:08:31 +02:00
sscanf(_msg.GetData().c_str(), "%d", &closeID);
2012-04-23 10:15:43 +02:00
APPL_DEBUG("Close specific buffer ID="<< closeID);
}
if(true == Exist(closeID)) {
// Get the new display buffer
if (m_idSelected == closeID) {
// Try previous buffer
int32_t destBuffer = -1;
for(int32_t ii=closeID-1; ii >= 0; ii--) {
if (true == Exist(ii) ) {
destBuffer = ii;
break;
}
}
// try next buffer
if (-1 == destBuffer) {
for(int32_t ii=closeID+1; ii < listBuffer.Size(); ii++) {
if (true == Exist(ii) ) {
destBuffer = ii;
break;
}
}
}
// set it to the currect display
m_idSelected = destBuffer;
SendMultiCast(ednMsgBufferId, destBuffer);
}
// Remove requested buffer
Remove(closeID);
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgBufferListChange);
} else {
2012-04-23 10:15:43 +02:00
APPL_ERROR("Request Close of a non existant ID : " << closeID);
}
}
}
2013-05-12 21:08:31 +02:00
} else if (_msg.GetMessage() == ednMsgCodeViewSelectedId) {
//Change the selected buffer
2013-05-12 21:08:31 +02:00
if (_msg.GetData() == "") {
2012-04-23 10:15:43 +02:00
APPL_ERROR("Null data for changing buffer ID file ... ");
} else {
int32_t newId;
2013-05-12 21:08:31 +02:00
sscanf(_msg.GetData().c_str(), "%d", &newId);
if (true == Exist(newId)) {
m_idSelected = newId;
} else {
2012-04-23 10:15:43 +02:00
APPL_ERROR("code biew request the selection of an non -existant buffer ==> reset to -1");
m_idSelected = -1;
}
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgBufferId, m_idSelected);
SendMultiCast(ednMsgBufferListChange);
}
}
/*
switch (id)
{
// Check buffer existence
if(true == Exist(dataID)) {
// If no name ==> request a Gui display ...
if (Get(dataID)->HaveName() == false) {
2012-04-23 10:15:43 +02:00
SendMessage(APPL_MSG__GUI_SHOW_SAVE_AS, dataID);
} else {
Get(dataID)->Save();
}
}
break;
}
*/
}
/**
* @brief Remove all buffer opened
*
* @param[in,out] ---
*
* @return ---
*
*/
void classBufferManager::RemoveAll(void)
{
int32_t i;
for (i=0; i<listBuffer.Size(); i++) {
Remove(i);
}
2012-02-29 18:06:08 +01:00
SendMultiCast(ednMsgGuiClose, "All");
}
/**
* @brief Create a new buffer with no name and empty
*
* @param[in,out] ---
*
* @return The ID of the curent buffer where the file is loaded
*
*/
int32_t classBufferManager::Create(void)
{
// allocate a new Buffer
BufferText *myBuffer = new BufferText();
// Add at the list of element
listBuffer.PushBack(myBuffer);
int32_t basicID = listBuffer.Size() - 1;
return basicID;
}
/**
* @brief open a file with the name set in parameters
*
* @param[in] filename curent filename
*
* @return The ID of the curent buffer where the file is loaded
*
* @todo : check if this file is not curently open and return the old ID
*
*/
2012-11-01 10:47:36 +01:00
int32_t classBufferManager::Open(etk::FSNode &myFile)
{
2012-05-10 17:47:52 +02:00
if (false == Exist(myFile)) {
// allocate a new Buffer
BufferText *myBuffer = new BufferText(myFile);
2012-05-10 17:47:52 +02:00
// Add at the list of element
listBuffer.PushBack(myBuffer);
return listBuffer.Size() - 1;
2012-05-10 17:47:52 +02:00
} else {
// the buffer already existed ==> we open it ...
return GetId(myFile);
}
}
BufferText * classBufferManager::Get(int32_t BufferID)
{
// possible special case : -1;
if (-1 >= BufferID) {
return NULL;
}
// check if the Buffer existed
if (BufferID < listBuffer.Size()) {
// check if the buffer already existed
if (NULL != listBuffer[BufferID]) {
return listBuffer[BufferID];
} else {
2012-04-23 10:15:43 +02:00
APPL_ERROR("non existing Buffer " << BufferID);
}
} else {
APPL_ERROR("call an non existing Buffer number too hight : " << BufferID << " > " << listBuffer.Size());
}
return NULL;
}
bool classBufferManager::Exist(int32_t BufferID)
{
if (-1 >= BufferID) {
return false;
}
// check if the Buffer existed
if (BufferID < listBuffer.Size()) {
// check if the buffer already existed
if (NULL != listBuffer[BufferID]) {
return true;
}
}
return false;
}
2012-11-01 10:47:36 +01:00
bool classBufferManager::Exist(etk::FSNode &myFile )
{
if (-1 == GetId(myFile)) {
return false;
}
return true;
}
2012-11-01 10:47:36 +01:00
int32_t classBufferManager::GetId(etk::FSNode &myFile)
{
int32_t iii;
// check if the Buffer existed
for (iii=0; iii < listBuffer.Size(); iii++) {
// check if the buffer already existed
if (NULL != listBuffer[iii]) {
if ( listBuffer[iii]->GetFileName() == myFile) {
return iii;
}
}
}
return -1;
}
// return the number of buffer (open in the past) if 5 buffer open and 4 close ==> return 5
uint32_t classBufferManager::Size(void)
{
return listBuffer.Size();
}
// nb of opens file Now ...
uint32_t classBufferManager::SizeOpen(void)
{
uint32_t jjj = 0;
// check if the Buffer existed
for (int32_t iii=0; iii<listBuffer.Size(); iii++) {
// check if the buffer already existed
if (NULL != listBuffer[iii]) {
jjj++;
}
}
return jjj;
}
/**
* @brief
*
* @param[in,out] ---
*
* @return ---
*
*/
bool classBufferManager::Remove(int32_t BufferID)
{
if (-1 >= BufferID) {
return false;
}
// check if the Buffer existed
if (BufferID < listBuffer.Size()) {
// check if the buffer already existed
if (NULL != listBuffer[BufferID]) {
// TODO : Check if it saved...
/*
if (false == IsSaved(BufferID) ) {
2012-04-23 10:15:43 +02:00
APPL_INFO("Buffer " << BufferID << " : Not Saved", BufferID);
}
*/
// Delete the Buffer
delete( listBuffer[BufferID] );
listBuffer[BufferID] = NULL;
2012-02-27 18:15:56 +01:00
/*
ewol::widgetMessageMultiCast::Send(GetWidgetId(), ednMsgBufferListChange);
2012-02-27 18:15:56 +01:00
*/
return true;
} else {
2012-04-23 10:15:43 +02:00
APPL_INFO("non existing Buffer " << BufferID);
return false;
}
} else {
APPL_INFO("call an non existing Buffer number too hight : " << BufferID << " > " << listBuffer.Size());
return false;
}
}
/**
* @brief to get the element 14 in the buffer
*
* @param[in,out] ---
*
* @return ---
*
*/
int32_t classBufferManager::WitchBuffer(int32_t iEmeElement)
{
int32_t i;
for (i=0; i<listBuffer.Size(); i++) {
if (NULL != listBuffer[i]) {
iEmeElement--;
// find the element :
if (0 == iEmeElement) {
return i;
}
}
}
return -1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Namespace part :
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static classBufferManager * localManager = NULL;
void BufferManager::Init(void)
{
if (NULL != localManager) {
EWOL_ERROR("classBufferManager ==> already exist, just unlink the previous ...");
localManager = NULL;
}
localManager = new classBufferManager();
if (NULL == localManager) {
EWOL_CRITICAL("Allocation of classBufferManager not done ...");
}
}
void BufferManager::UnInit(void)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return;
}
delete(localManager);
localManager = NULL;
}
int32_t BufferManager::GetSelected(void)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return -1;
}
return localManager->GetSelected();
}
BufferText * BufferManager::Get(int32_t BufferID)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return NULL;
}
return localManager->Get(BufferID);
}
bool BufferManager::Exist(int32_t BufferID)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return false;
}
return localManager->Exist(BufferID);
}
2012-11-01 10:47:36 +01:00
bool BufferManager::Exist(etk::FSNode &myFile)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return false;
}
return localManager->Exist(myFile);
}
2012-11-01 10:47:36 +01:00
int32_t BufferManager::GetId(etk::FSNode &myFile)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return -1;
}
return localManager->GetId(myFile);
}
uint32_t BufferManager::Size(void)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return 0;
}
return localManager->Size();
}
uint32_t BufferManager::SizeOpen(void)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return 0;
}
return localManager->SizeOpen();
}
int32_t BufferManager::WitchBuffer(int32_t iEmeElement)
{
if (NULL == localManager) {
EWOL_ERROR("classBufferManager ==> request UnInit, but does not exist ...");
return -1;
}
return localManager->WitchBuffer(iEmeElement);
}