Skip to content

Commit

Permalink
desktop: Add support for user-defined bookmarks
Browse files Browse the repository at this point in the history
Now they are kept in a separate list using a different interface, since
otherwise it'll be harder to keep track of which bookmarks are
user-defined and which aren't, in the file dialog.
  • Loading branch information
irydacea committed Oct 12, 2016
1 parent eb3bc1f commit 71f69a8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
55 changes: 51 additions & 4 deletions src/desktop/paths.cpp
Expand Up @@ -20,6 +20,7 @@
#include "filesystem.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"

Expand Down Expand Up @@ -132,6 +133,17 @@ inline std::string pretty_path(const std::string& path)
return filesystem::normalize_path(path, true, true);
}

inline config get_bookmarks_config()
{
const config& cfg = preferences::get_child("dir_bookmarks");
return cfg ? cfg : config{};
}

inline void commit_bookmarks_config(config& cfg)
{
preferences::set_child("dir_bookmarks", cfg);
}

} // unnamed namespace

std::string user_profile_dir()
Expand Down Expand Up @@ -193,10 +205,6 @@ std::vector<path_info> game_paths(unsigned path_types)
res.push_back({{ N_("filesystem_path_game^User preferences"), GETTEXT_DOMAIN }, "", game_user_pref_dir});
}

if(path_types & GAME_USER_BOOKMARKS) {
// TODO
}

return res;
}

Expand All @@ -223,4 +231,43 @@ std::vector<path_info> system_paths(unsigned path_types)
return res;
}

unsigned add_user_bookmark(const std::string& label, const std::string& path)
{
config cfg = get_bookmarks_config();

config& bookmark_cfg = cfg.add_child("bookmark");
bookmark_cfg["label"] = label;
bookmark_cfg["path"] = path;

commit_bookmarks_config(cfg);

return cfg.child_count("bookmark");
}

void remove_user_bookmark(unsigned index)
{
config cfg = get_bookmarks_config();
const unsigned prev_size = cfg.child_count("bookmark");

if(index < prev_size) {
cfg.remove_child("bookmark", index);
}

commit_bookmarks_config(cfg);
}

std::vector<bookmark_info> user_bookmarks()
{
const config& cfg = get_bookmarks_config();
std::vector<bookmark_info> res;

if(cfg.has_child("bookmark")) {
for(const config& bookmark_cfg : cfg.child_range("bookmark")) {
res.push_back({ bookmark_cfg["label"], bookmark_cfg["path"] });
}
}

return res;
}

} // namespace desktop
17 changes: 15 additions & 2 deletions src/desktop/paths.hpp
Expand Up @@ -58,7 +58,6 @@ enum GAME_PATH_TYPES
GAME_CORE_DATA_DIR = 0x2, /**< Game data dir. */
GAME_USER_PREFS_DIR = 0x4, /**< User preferences dir. */
GAME_USER_DATA_DIR = 0x8, /**< User data dir. */
GAME_USER_BOOKMARKS = 0x10 /**< User-defined bookmarked paths. */
};

enum SYSTEM_PATH_TYPES
Expand All @@ -74,7 +73,7 @@ enum SYSTEM_PATH_TYPES
* These paths are guaranteed to be their canonical forms (with links and dot
* entries resolved) and using the platform's preferred path delimiter.
*/
std::vector<path_info> game_paths(unsigned path_types = GAME_CORE_DATA_DIR | GAME_USER_DATA_DIR | GAME_USER_BOOKMARKS);
std::vector<path_info> game_paths(unsigned path_types = GAME_CORE_DATA_DIR | GAME_USER_DATA_DIR);

/**
* Returns a list of system-defined paths.
Expand All @@ -89,6 +88,20 @@ std::vector<path_info> game_paths(unsigned path_types = GAME_CORE_DATA_DIR | GAM
*/
std::vector<path_info> system_paths(unsigned path_types = SYSTEM_ALL_DRIVES | SYSTEM_USER_PROFILE | SYSTEM_ROOTFS);

struct bookmark_info
{
/** User defined label. */
std::string label;
/** Real path. */
std::string path;
};

unsigned add_user_bookmark(const std::string& label, const std::string& path);

void remove_user_bookmark(unsigned index);

std::vector<bookmark_info> user_bookmarks();

} // namespace desktop

#endif

0 comments on commit 71f69a8

Please sign in to comment.