Added RecentFiles C++ class to replace legacy C implementation of recent files.

This commit is contained in:
David Capello 2010-03-27 23:43:08 -03:00
parent d7f6b78119
commit 61c1e08be1
9 changed files with 158 additions and 162 deletions

View File

@ -163,7 +163,6 @@ COMMON_SOURCES = \
src/modules/gfx.cpp \
src/modules/gui.cpp \
src/modules/palettes.cpp \
src/modules/recent.cpp \
src/modules/rootmenu.cpp \
src/modules/skinneable_theme.cpp \
src/raster/algo.cpp \
@ -231,6 +230,7 @@ ASE_SOURCES = \
src/console.cpp \
src/context.cpp \
src/main.cpp \
src/recent_files.cpp \
src/ui_context.cpp \
src/undoable.cpp \
$(COMMON_SOURCES)

View File

@ -50,12 +50,12 @@
#include "modules/gfx.h"
#include "modules/gui.h"
#include "modules/palettes.h"
#include "modules/recent.h"
#include "modules/rootmenu.h"
#include "raster/image.h"
#include "raster/layer.h"
#include "raster/palette.h"
#include "raster/sprite.h"
#include "recent_files.h"
#include "tools/toolbox.h"
#include "ui_context.h"
#include "util/boundary.h"
@ -87,6 +87,7 @@ public:
RasterModule m_raster;
CommandsModule m_commands_modules;
UIContext m_ui_context;
RecentFiles m_recent_files_module;
};
App* App::m_instance = NULL;
@ -256,8 +257,8 @@ int App::run()
/* show it */
set_sprite_in_more_reliable_editor(context->get_first_sprite());
/* recent file */
recent_file(Vaca::convert_to<std::string>(option->data()).c_str());
// Recent file
RecentFiles::addRecentFile(Vaca::convert_to<std::string>(option->data()).c_str());
}
}
break;
@ -390,16 +391,18 @@ bool app_realloc_recent_list()
jwidget_free(submenu);
}
// Build the menu of recent files
submenu = jmenu_new();
jmenuitem_set_submenu(list_menuitem, submenu);
if (jlist_first(get_recent_files_list())) {
const char *filename;
Params params;
JLink link;
RecentFiles::const_iterator it = RecentFiles::begin();
RecentFiles::const_iterator end = RecentFiles::end();
JI_LIST_FOR_EACH(get_recent_files_list(), link) {
filename = reinterpret_cast<const char*>(link->data);
if (it != end) {
Params params;
for (; it != end; ++it) {
const char* filename = it->c_str();
params.set("filename", filename);

View File

@ -33,7 +33,7 @@
#include "raster/sprite.h"
#include "modules/editors.h"
#include "modules/gui.h"
#include "modules/recent.h"
#include "recent_files.h"
#include "widgets/statebar.h"
//////////////////////////////////////////////////////////////////////
@ -182,11 +182,14 @@ void OpenFileCommand::execute(Context* context)
if (!m_filename.empty()) {
FileOp *fop = fop_to_load_sprite(m_filename.c_str(), FILE_LOAD_SEQUENCE_ASK);
bool unrecent = false;
if (fop) {
if (fop->error) {
console.printf(fop->error);
fop_free(fop);
unrecent = true;
}
else {
JThread thread = jthread_new(openfile_bg, fop);
@ -200,7 +203,7 @@ void OpenFileCommand::execute(Context* context)
"<<Loading file:<<%s||&Cancel",
get_filename(m_filename.c_str()));
/* add a monitor to check the loading (FileOp) progress */
// Add a monitor to check the loading (FileOp) progress
data->monitor = add_gui_monitor(monitor_openfile_bg,
monitor_free, data);
@ -209,31 +212,27 @@ void OpenFileCommand::execute(Context* context)
if (data->monitor != NULL)
remove_gui_monitor(data->monitor);
/* stop the file-operation and wait the thread to exit */
// Stop the file-operation and wait the thread to exit
fop_stop(data->fop);
jthread_join(data->thread);
/* show any error */
// Show any error
if (fop->error) {
console.printf(fop->error);
unrecent = true;
}
else {
Sprite *sprite = fop->sprite;
if (sprite) {
UIContext* context = UIContext::instance();
recent_file(fop->filename);
RecentFiles::addRecentFile(fop->filename);
context->add_sprite(sprite);
set_sprite_in_more_reliable_editor(sprite);
}
/* if the sprite isn't NULL and the file-operation wasn't
stopped by the user... */
else if (!fop_is_stop(fop)) {
/* ...the file can't be loaded by errors, so we can remove it
from the recent-file list */
unrecent_file(fop->filename);
}
else if (!fop_is_stop(fop))
unrecent = true;
}
progress_free(data->progress);
@ -246,9 +245,15 @@ void OpenFileCommand::execute(Context* context)
fop_free(fop);
}
}
// The file was not found or was loaded loaded with errors,
// so we can remove it from the recent-file list
if (unrecent) {
RecentFiles::removeRecentFile(m_filename.c_str());
}
}
else {
/* do nothing (the user cancelled or something like that) */
// Do nothing (the user cancelled or something like that)
}
}
}

View File

@ -28,7 +28,7 @@
#include "dialogs/filesel.h"
#include "file/file.h"
#include "modules/gui.h"
#include "modules/recent.h"
#include "recent_files.h"
#include "raster/sprite.h"
#include "widgets/statebar.h"
#include "sprite_wrappers.h"
@ -123,7 +123,7 @@ static void save_sprite_in_background(Sprite* sprite, bool mark_as_saved)
}
/* no error? */
else {
recent_file(sprite->filename);
RecentFiles::addRecentFile(sprite->filename);
if (mark_as_saved)
sprite_mark_as_saved(sprite);

View File

@ -25,7 +25,6 @@
#include "modules/gfx.h"
#include "modules/gui.h"
#include "modules/palettes.h"
#include "modules/recent.h"
#include "modules/rootmenu.h"
#define DEF_MODULE(name, reqs) \
@ -49,7 +48,6 @@ static Module module[] =
DEF_MODULE(effect, 0),
DEF_MODULE(graphics, REQUIRE_INTERFACE),
DEF_MODULE(gui, REQUIRE_INTERFACE),
DEF_MODULE(recent, REQUIRE_INTERFACE),
DEF_MODULE(rootmenu, REQUIRE_INTERFACE),
DEF_MODULE(editors, REQUIRE_INTERFACE),
};

View File

@ -1,126 +0,0 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2010 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "jinete/jlist.h"
#include "app.h"
#include "core/cfg.h"
#include "modules/gui.h"
#include "modules/recent.h"
static int installed = false;
static JList recent_files;
static int recent_files_limit = 16;
int init_module_recent()
{
const char *filename;
char buf[512];
int c;
installed = true;
recent_files = jlist_new();
for (c=recent_files_limit-1; c>=0; c--) {
sprintf(buf, "Filename%02d", c);
filename = get_config_string("RecentFiles", buf, NULL);
if ((filename) && (*filename))
recent_file(filename);
}
return 0;
}
void exit_module_recent()
{
char buf[512], *filename;
JLink link;
int c = 0;
/* remove list of recent files */
JI_LIST_FOR_EACH(recent_files, link) {
filename = reinterpret_cast<char*>(link->data);
sprintf(buf, "Filename%02d", c);
set_config_string("RecentFiles", buf, filename);
jfree(filename);
c++;
}
jlist_free(recent_files);
installed = false;
}
JList get_recent_files_list()
{
return recent_files;
}
void recent_file(const char *filename)
{
if (installed) {
char *filename_it;
int count = 0;
JLink link;
JI_LIST_FOR_EACH(recent_files, link) {
filename_it = reinterpret_cast<char*>(link->data);
if (strcmp(filename, filename_it) == 0) {
jlist_remove(recent_files, filename_it);
jlist_prepend(recent_files, filename_it);
schedule_rebuild_recent_list();
return;
}
count++;
}
if (count == recent_files_limit) {
/* remove the last recent-file entry */
link = jlist_last(recent_files);
jfree(link->data);
jlist_delete_link(recent_files, link);
}
jlist_prepend(recent_files, jstrdup(filename));
schedule_rebuild_recent_list();
}
}
void unrecent_file(const char *filename)
{
if (installed) {
char *filename_it;
JLink link;
JI_LIST_FOR_EACH(recent_files, link) {
filename_it = reinterpret_cast<char*>(link->data);
if (strcmp(filename, filename_it) == 0) {
jfree(filename_it);
jlist_delete_link(recent_files, link);
schedule_rebuild_recent_list();
break;
}
}
}
}

107
src/recent_files.cpp Normal file
View File

@ -0,0 +1,107 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2010 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <cstdio>
#include <cstring>
#include <algorithm>
#include "app.h"
#include "core/cfg.h"
#include "modules/gui.h"
#include "recent_files.h"
static RecentFiles::FilesList recent_files;
static int recent_files_limit = 16;
RecentFiles::RecentFiles()
{
const char *filename;
char buf[512];
int c;
for (c=recent_files_limit-1; c>=0; c--) {
sprintf(buf, "Filename%02d", c);
filename = get_config_string("RecentFiles", buf, NULL);
if ((filename) && (*filename))
addRecentFile(filename);
}
}
RecentFiles::~RecentFiles()
{
char buf[512];
int c = 0;
for (const_iterator it = begin(); it != end(); ++it) {
const char* filename = it->c_str();
sprintf(buf, "Filename%02d", c);
set_config_string("RecentFiles", buf, filename);
c++;
}
recent_files.clear();
}
RecentFiles::const_iterator RecentFiles::begin()
{
return recent_files.begin();
}
RecentFiles::const_iterator RecentFiles::end()
{
return recent_files.end();
}
void RecentFiles::addRecentFile(const char* filename)
{
iterator it = std::find(recent_files.begin(), recent_files.end(), filename);
// If the filename already exist in the list...
if (it != recent_files.end()) {
// Move it to the first position
recent_files.erase(it);
recent_files.insert(recent_files.begin(), filename);
schedule_rebuild_recent_list();
return;
}
// If the filename does not exist...
// Does the list is full?
if (recent_files.size() == recent_files_limit) {
// Remove the last entry
recent_files.erase(--recent_files.end());
}
recent_files.insert(recent_files.begin(), filename);
schedule_rebuild_recent_list();
}
void RecentFiles::removeRecentFile(const char* filename)
{
iterator it = std::find(recent_files.begin(), recent_files.end(), filename);
if (it != recent_files.end()) {
recent_files.erase(it);
schedule_rebuild_recent_list();
}
}

View File

@ -16,18 +16,28 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef MODULES_RECENT_H_INCLUDED
#define MODULES_RECENT_H_INCLUDED
#ifndef RECENT_FILES_H_INCLUDED
#define RECENT_FILES_H_INCLUDED
#include "jinete/jbase.h"
#include <list>
#include <string>
int init_module_recent();
void exit_module_recent();
class RecentFiles
{
public:
typedef std::list<std::string> FilesList;
typedef FilesList::iterator iterator;
typedef FilesList::const_iterator const_iterator;
JList get_recent_files_list();
static const_iterator begin();
static const_iterator end();
void recent_file(const char *filename);
void unrecent_file(const char *filename);
RecentFiles();
~RecentFiles();
static void addRecentFile(const char* filename);
static void removeRecentFile(const char* filename);
};
#endif

View File

@ -33,7 +33,6 @@
#include "modules/gfx.h"
#include "modules/gui.h"
#include "modules/palettes.h"
#include "modules/recent.h"
#include "raster/image.h"
#include "raster/palette.h"
#include "raster/rotate.h"