diff --git a/src/app.cpp b/src/app.cpp index c9bbbe187..a37455c25 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -439,8 +439,8 @@ bool app_rebuild_recent_list() submenu = new Menu(); list_menuitem->setSubmenu(submenu); - RecentFiles::const_iterator it = App::instance()->getRecentFiles()->begin(); - RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end(); + RecentFiles::const_iterator it = App::instance()->getRecentFiles()->files_begin(); + RecentFiles::const_iterator end = App::instance()->getRecentFiles()->files_end(); if (it != end) { Params params; diff --git a/src/base/recent_items.h b/src/base/recent_items.h new file mode 100644 index 000000000..daac81d93 --- /dev/null +++ b/src/base/recent_items.h @@ -0,0 +1,63 @@ +// ASEPRITE base library +// Copyright (C) 2001-2012 David Capello +// +// This source file is ditributed under a BSD-like license, please +// read LICENSE.txt for more information. + +#ifndef BASE_RECENT_ITEMS_H_INCLUDED +#define BASE_RECENT_ITEMS_H_INCLUDED + +#include +#include + +namespace base { + +template +class RecentItems { +public: + typedef std::list Items; + typedef typename Items::iterator iterator; + typedef typename Items::const_iterator const_iterator; + + RecentItems(size_t limit) : m_limit(limit) { } + + const_iterator begin() { return m_items.begin(); } + const_iterator end() { return m_items.end(); } + + size_t size() const { return m_items.size(); } + size_t limit() const { return m_limit; } + + void addItem(const T& item) { + iterator it = std::find(m_items.begin(), m_items.end(), item); + + // If the item already exist in the list... + if (it != m_items.end()) { + // Move it to the first position + m_items.erase(it); + m_items.insert(m_items.begin(), item); + return; + } + + // Does the list is full? + if (m_items.size() == m_limit) { + // Remove the last entry + m_items.erase(--m_items.end()); + } + + m_items.insert(m_items.begin(), item); + } + + void removeItem(const T& item) { + iterator it = std::find(m_items.begin(), m_items.end(), item); + if (it != m_items.end()) + m_items.erase(it); + } + +private: + Items m_items; + size_t m_limit; +}; + +} // namespace base + +#endif diff --git a/src/dialogs/filesel.cpp b/src/dialogs/filesel.cpp index 445c32133..bf293684b 100644 --- a/src/dialogs/filesel.cpp +++ b/src/dialogs/filesel.cpp @@ -450,22 +450,10 @@ static void update_location(Widget* window) newItem = location->addItem(""); newItem = location->addItem("-------- Recent Paths --------"); - std::set included; - - // For each recent file... - RecentFiles::const_iterator it = App::instance()->getRecentFiles()->begin(); - RecentFiles::const_iterator end = App::instance()->getRecentFiles()->end(); - for (; it != end; ++it) { - // Get the path of the recent file - base::string path = base::get_file_path(*it); - - // Check if the path was not already included in the list - if (included.find(path) == included.end()) { - included.insert(path); - - location->addItem(path); - } - } + RecentFiles::const_iterator it = App::instance()->getRecentFiles()->paths_begin(); + RecentFiles::const_iterator end = App::instance()->getRecentFiles()->paths_end(); + for (; it != end; ++it) + location->addItem(*it); } // Select the location diff --git a/src/recent_files.cpp b/src/recent_files.cpp index 4ccb72c40..c02b57482 100644 --- a/src/recent_files.cpp +++ b/src/recent_files.cpp @@ -21,85 +21,66 @@ #include "recent_files.h" #include "app.h" +#include "base/path.h" #include "ini_file.h" -#include #include #include RecentFiles::RecentFiles() - : m_limit(16) + : m_files(16) + , m_paths(16) { - const char* filename; char buf[512]; - int c; - for (c=m_limit-1; c>=0; c--) { + for (int c=m_files.limit()-1; c>=0; c--) { sprintf(buf, "Filename%02d", c); - filename = get_config_string("RecentFiles", buf, NULL); - if ((filename) && (*filename)) - addRecentFile(filename); + const char* filename = get_config_string("RecentFiles", buf, NULL); + if (filename && *filename) + m_files.addItem(filename); + } + + for (int c=m_paths.limit()-1; c>=0; c--) { + sprintf(buf, "Path%02d", c); + + const char* path = get_config_string("RecentPaths", buf, NULL); + if (path && *path) + m_paths.addItem(path); } } RecentFiles::~RecentFiles() { char buf[512]; - int c = 0; - for (const_iterator it = begin(); it != end(); ++it) { + int c = 0; + for (const_iterator it = files_begin(); it != files_end(); ++it) { const char* filename = it->c_str(); sprintf(buf, "Filename%02d", c); set_config_string("RecentFiles", buf, filename); c++; } - m_files.clear(); -} - -RecentFiles::const_iterator RecentFiles::begin() -{ - return m_files.begin(); -} - -RecentFiles::const_iterator RecentFiles::end() -{ - return m_files.end(); + c = 0; + for (const_iterator it = paths_begin(); it != paths_end(); ++it) { + const char* path = it->c_str(); + sprintf(buf, "Path%02d", c); + set_config_string("RecentPaths", buf, path); + c++; + } } void RecentFiles::addRecentFile(const char* filename) { - iterator it = std::find(m_files.begin(), m_files.end(), filename); - - // If the filename already exist in the list... - if (it != m_files.end()) { - // Move it to the first position - m_files.erase(it); - m_files.insert(m_files.begin(), filename); - - app_rebuild_recent_list(); - return; - } - - // If the filename does not exist... - - // Does the list is full? - if ((int)m_files.size() == m_limit) { - // Remove the last entry - m_files.erase(--m_files.end()); - } - - m_files.insert(m_files.begin(), filename); + m_files.addItem(filename); + m_paths.addItem(base::get_file_path(filename)); app_rebuild_recent_list(); } void RecentFiles::removeRecentFile(const char* filename) { - iterator it = std::find(m_files.begin(), m_files.end(), filename); - - if (it != m_files.end()) { - m_files.erase(it); - app_rebuild_recent_list(); - } + m_files.removeItem(filename); + m_paths.removeItem(base::get_file_path(filename)); + app_rebuild_recent_list(); } diff --git a/src/recent_files.h b/src/recent_files.h index a2fc12930..b1c5655c0 100644 --- a/src/recent_files.h +++ b/src/recent_files.h @@ -19,18 +19,24 @@ #ifndef RECENT_FILES_H_INCLUDED #define RECENT_FILES_H_INCLUDED -#include +#include "base/recent_items.h" + #include class RecentFiles { public: - typedef std::list FilesList; - typedef FilesList::iterator iterator; - typedef FilesList::const_iterator const_iterator; + typedef base::RecentItems List; + typedef List::iterator iterator; + typedef List::const_iterator const_iterator; - const_iterator begin(); - const_iterator end(); + // Iterate through recent files. + const_iterator files_begin() { return m_files.begin(); } + const_iterator files_end() { return m_files.end(); } + + // Iterate through recent paths. + const_iterator paths_begin() { return m_paths.begin(); } + const_iterator paths_end() { return m_paths.end(); } RecentFiles(); ~RecentFiles(); @@ -39,8 +45,8 @@ public: void removeRecentFile(const char* filename); private: - RecentFiles::FilesList m_files; - int m_limit; + List m_files; + List m_paths; }; #endif