2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-03-14 23:08:02 +08:00
|
|
|
// Copyright (C) 2001-2018 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"
|
2018-03-14 23:08:02 +08:00
|
|
|
#include "fmt/format.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 {
|
|
|
|
|
2018-03-14 23:08:02 +08:00
|
|
|
const char* kRecentFilesSection = "RecentFiles";
|
|
|
|
const char* kRecentFoldersSection = "RecentPaths";
|
|
|
|
|
2015-03-12 04:48:28 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-14 23:08:02 +08:00
|
|
|
std::string format_filename_var(const int i) {
|
|
|
|
return fmt::format("Filename{0:02d}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string format_folder_var(const int i) {
|
|
|
|
return fmt::format("Path{0:02d}", i);
|
|
|
|
}
|
|
|
|
|
2015-03-12 04:48:28 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-02-12 06:04:52 +08:00
|
|
|
for (int c=m_files.limit()-1; c>=0; c--) {
|
2018-03-14 23:08:02 +08:00
|
|
|
const char* filename = get_config_string(kRecentFilesSection,
|
|
|
|
format_filename_var(c).c_str(),
|
|
|
|
nullptr);
|
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--) {
|
2018-03-14 23:08:02 +08:00
|
|
|
const char* path = get_config_string(kRecentFoldersSection,
|
|
|
|
format_folder_var(c).c_str(),
|
|
|
|
nullptr);
|
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()
|
|
|
|
{
|
2018-03-14 23:08:02 +08:00
|
|
|
// Save recent files
|
2010-03-28 10:43:08 +08:00
|
|
|
|
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) {
|
2018-03-14 23:08:02 +08:00
|
|
|
set_config_string(kRecentFilesSection,
|
|
|
|
format_filename_var(c).c_str(),
|
|
|
|
filename.c_str());
|
|
|
|
++c;
|
2010-03-28 10:43:08 +08:00
|
|
|
}
|
2018-03-14 23:08:02 +08:00
|
|
|
for (; c<m_files.limit(); ++c) {
|
|
|
|
del_config_value(kRecentFilesSection,
|
|
|
|
format_filename_var(c).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save recent folders
|
2010-03-28 10:43:08 +08:00
|
|
|
|
2012-02-12 06:04:52 +08:00
|
|
|
c = 0;
|
2015-03-12 04:48:28 +08:00
|
|
|
for (auto const& path : m_paths) {
|
2018-03-14 23:08:02 +08:00
|
|
|
set_config_string(kRecentFoldersSection,
|
|
|
|
format_folder_var(c).c_str(),
|
|
|
|
path.c_str());
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
for (; c<m_files.limit(); ++c) {
|
|
|
|
del_config_value(kRecentFoldersSection,
|
|
|
|
format_folder_var(c).c_str());
|
2012-02-12 06:04:52 +08:00
|
|
|
}
|
2010-03-28 10:43:08 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 23:08:02 +08:00
|
|
|
void RecentFiles::addRecentFile(const std::string& filename)
|
2010-03-28 10:43:08 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2018-03-14 23:08:02 +08:00
|
|
|
void RecentFiles::removeRecentFile(const std::string& filename)
|
2010-03-28 10:43:08 +08:00
|
|
|
{
|
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));
|
|
|
|
|
2018-03-14 23:08:02 +08:00
|
|
|
std::string dir = base::get_file_path(fn);
|
|
|
|
if (!base::is_directory(dir))
|
|
|
|
removeRecentFolder(dir);
|
|
|
|
|
|
|
|
Changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecentFiles::removeRecentFolder(const std::string& dir)
|
|
|
|
{
|
|
|
|
std::string fn = normalizePath(dir);
|
|
|
|
m_paths.removeItem(fn, compare_path(fn));
|
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
|
|
|
|
2018-03-14 23:08:02 +08:00
|
|
|
std::string RecentFiles::normalizePath(const std::string& filename)
|
2015-08-27 22:12:30 +08:00
|
|
|
{
|
2018-03-14 23:08:02 +08:00
|
|
|
return base::normalize_path(filename);
|
2015-08-27 22:12:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
} // namespace app
|