2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2016-04-12 06:17:39 +08:00
|
|
|
// Copyright (C) 2001-2016 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2010-03-28 10:43:08 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-03-28 10:43:08 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2010-03-28 10:43:08 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/recent_files.h"
|
2010-03-28 10:43:08 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ini_file.h"
|
2013-01-19 10:39:19 +08:00
|
|
|
#include "base/fs.h"
|
2011-06-30 09:51:46 +08:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2012-02-12 06:09:50 +08:00
|
|
|
#include <set>
|
2010-03-28 10:43:08 +08:00
|
|
|
|
2015-03-12 04:48:28 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct compare_path {
|
|
|
|
std::string a;
|
|
|
|
compare_path(const std::string& a) : a(a) { }
|
|
|
|
bool operator()(const std::string& b) const {
|
|
|
|
return base::compare_filenames(a, b) == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
|
|
|
|
2010-03-28 10:43:08 +08:00
|
|
|
RecentFiles::RecentFiles()
|
2012-02-12 06:04:52 +08:00
|
|
|
: m_files(16)
|
|
|
|
, m_paths(16)
|
2010-03-28 10:43:08 +08:00
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
2012-02-12 06:04:52 +08:00
|
|
|
for (int c=m_files.limit()-1; c>=0; c--) {
|
2010-03-28 10:43:08 +08:00
|
|
|
sprintf(buf, "Filename%02d", c);
|
|
|
|
|
2012-02-12 06:04:52 +08:00
|
|
|
const char* filename = get_config_string("RecentFiles", buf, NULL);
|
2015-03-12 04:48:28 +08:00
|
|
|
if (filename && *filename && base::is_file(filename)) {
|
2015-08-27 22:12:30 +08:00
|
|
|
std::string fn = normalizePath(filename);
|
2015-03-12 04:48:28 +08:00
|
|
|
m_files.addItem(fn, compare_path(fn));
|
|
|
|
}
|
2012-02-12 06:04:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int c=m_paths.limit()-1; c>=0; c--) {
|
|
|
|
sprintf(buf, "Path%02d", c);
|
|
|
|
|
|
|
|
const char* path = get_config_string("RecentPaths", buf, NULL);
|
2015-03-12 04:48:28 +08:00
|
|
|
if (path && *path) {
|
2015-08-27 22:12:30 +08:00
|
|
|
std::string p = normalizePath(path);
|
2015-03-12 04:48:28 +08:00
|
|
|
m_paths.addItem(p, compare_path(p));
|
2012-02-12 06:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
2010-03-28 10:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RecentFiles::~RecentFiles()
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
2012-02-12 06:04:52 +08:00
|
|
|
int c = 0;
|
2015-03-12 04:48:28 +08:00
|
|
|
for (auto const& filename : m_files) {
|
2010-03-28 10:43:08 +08:00
|
|
|
sprintf(buf, "Filename%02d", c);
|
2015-03-12 04:48:28 +08:00
|
|
|
set_config_string("RecentFiles", buf, filename.c_str());
|
2010-03-28 10:43:08 +08:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2012-02-12 06:04:52 +08:00
|
|
|
c = 0;
|
2015-03-12 04:48:28 +08:00
|
|
|
for (auto const& path : m_paths) {
|
2012-02-12 06:04:52 +08:00
|
|
|
sprintf(buf, "Path%02d", c);
|
2015-03-12 04:48:28 +08:00
|
|
|
set_config_string("RecentPaths", buf, path.c_str());
|
2012-02-12 06:04:52 +08:00
|
|
|
c++;
|
|
|
|
}
|
2010-03-28 10:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecentFiles::addRecentFile(const char* filename)
|
|
|
|
{
|
2015-08-27 22:12:30 +08:00
|
|
|
std::string fn = normalizePath(filename);
|
2015-03-12 04:48:28 +08:00
|
|
|
m_files.addItem(fn, compare_path(fn));
|
|
|
|
|
|
|
|
std::string path = base::get_file_path(fn);
|
|
|
|
m_paths.addItem(path, compare_path(path));
|
2012-07-10 00:20:58 +08:00
|
|
|
|
2015-03-02 22:18:33 +08:00
|
|
|
Changed();
|
2010-03-28 10:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecentFiles::removeRecentFile(const char* filename)
|
|
|
|
{
|
2015-08-27 22:12:30 +08:00
|
|
|
std::string fn = normalizePath(filename);
|
2015-03-12 04:48:28 +08:00
|
|
|
m_files.removeItem(fn, compare_path(fn));
|
|
|
|
|
|
|
|
std::string path = base::get_file_path(filename);
|
|
|
|
m_paths.removeItem(path, compare_path(path));
|
2012-07-10 00:20:58 +08:00
|
|
|
|
2015-03-02 22:18:33 +08:00
|
|
|
Changed();
|
2010-03-28 10:43:08 +08:00
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2015-08-27 22:12:30 +08:00
|
|
|
std::string RecentFiles::normalizePath(std::string fn)
|
|
|
|
{
|
2016-04-12 06:17:39 +08:00
|
|
|
return base::normalize_path(fn);
|
2015-08-27 22:12:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
} // namespace app
|