diff options
| author | Alexander Valavanis <valavanisalex@gmail.com> | 2017-07-10 08:19:49 +0000 |
|---|---|---|
| committer | Alexander Valavanis <valavanisalex@gmail.com> | 2017-07-10 08:19:49 +0000 |
| commit | 9bb10ed9ea07d21c926f3ab1ca5db8027be29378 (patch) | |
| tree | ce610e17ab62bb84f01bbb4d8cef50fed685382a /src | |
| parent | Update popup menu usage (diff) | |
| parent | Shortcuts: Fix for 7d248fbba5b0c24d9a24cda8c3f2e79f96395553 (is_user_set was ... (diff) | |
| download | inkscape-9bb10ed9ea07d21c926f3ab1ca5db8027be29378.tar.gz inkscape-9bb10ed9ea07d21c926f3ab1ca5db8027be29378.zip | |
Merge branch 'master' of gitlab.com:inkscape/inkscape
Diffstat (limited to 'src')
| -rw-r--r-- | src/inkscape.cpp | 14 | ||||
| -rw-r--r-- | src/io/resource.cpp | 13 | ||||
| -rw-r--r-- | src/shortcuts.cpp | 69 |
3 files changed, 66 insertions, 30 deletions
diff --git a/src/inkscape.cpp b/src/inkscape.cpp index 22f7230a0..24a744bbd 100644 --- a/src/inkscape.cpp +++ b/src/inkscape.cpp @@ -30,20 +30,6 @@ #include "debug/simple-event.h" #include "debug/event-tracker.h" -#ifndef WIN32 -# define HAS_PROC_SELF_EXE //to get path of executable -#else - -#if !defined(_WIN32_IE) || (_WIN32_IE < 0x0400) -# undef _WIN32_IE -# define _WIN32_IE 0x0400 -#endif -//#define HAS_SHGetSpecialFolderPath -#define HAS_SHGetSpecialFolderLocation -#define HAS_GetModuleFileName -# include <shlobj.h> -#endif - #include <glib/gstdio.h> #include <glibmm/i18n.h> #include <glibmm/miscutils.h> diff --git a/src/io/resource.cpp b/src/io/resource.cpp index 7ecfceb81..dfb851d58 100644 --- a/src/io/resource.cpp +++ b/src/io/resource.cpp @@ -20,6 +20,10 @@ #include "config.h" #endif +#ifdef WIN32 +#include <shlobj.h> // for SHGetSpecialFolderLocation +#endif + #include <glibmm/miscutils.h> #include <glibmm/stringutils.h> #include <glibmm/fileutils.h> @@ -271,12 +275,13 @@ char *profile_path(const char *filename) prefdir = g_strdup(userenv); } -#ifdef HAS_SHGetSpecialFolderLocation - // prefer c:\Documents and Settings\UserName\Application Data\ to - // c:\Documents and Settings\userName\; +#ifdef WIN32 + // prefer c:\Documents and Settings\UserName\Application Data\ to c:\Documents and Settings\userName\; + // TODO: CSIDL_APPDATA is C:\Users\UserName\AppData\Roaming these days + // should we migrate to AppData\Local? Then we can simply use the portable g_get_user_config_dir() if (!prefdir) { ITEMIDLIST *pidl = 0; - if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) { + if ( SHGetFolderLocation( NULL, CSIDL_APPDATA, NULL, 0, &pidl ) == S_OK ) { gchar * utf8Path = NULL; { diff --git a/src/shortcuts.cpp b/src/shortcuts.cpp index e7343c5e4..52248b81f 100644 --- a/src/shortcuts.cpp +++ b/src/shortcuts.cpp @@ -50,7 +50,7 @@ using namespace Inkscape; using namespace Inkscape::IO::Resource; -static void try_shortcuts_file(char const *filename); +static bool try_shortcuts_file(char const *filename, bool const is_user_set=false); static void read_shortcuts_file(char const *filename, bool const is_user_set=false); unsigned int sp_shortcut_get_key(unsigned int const shortcut); @@ -78,28 +78,49 @@ static std::map<Inkscape::Verb *, unsigned int> *user_shortcuts = NULL; void sp_shortcut_init() { - verbs = new std::map<unsigned int, Inkscape::Verb * >(); primary_shortcuts = new std::map<Inkscape::Verb *, unsigned int>(); user_shortcuts = new std::map<Inkscape::Verb *, unsigned int>(); + // try to load shortcut file as set in preferences + // if preference is unset or loading fails fallback to share/keys/default.xml and finally share/keys/inkscape.xml Inkscape::Preferences *prefs = Inkscape::Preferences::get(); Glib::ustring shortcutfile = prefs->getString("/options/kbshortcuts/shortcutfile"); + bool success = false; + gchar const *reason; if (shortcutfile.empty()) { - shortcutfile = Glib::ustring(get_path(SYSTEM, KEYS, "default.xml")); + reason = "No key file set in preferences"; + } else { + success = try_shortcuts_file(shortcutfile.c_str()); + reason = "Unable to read key file set in preferences"; + } + if (!success) { + g_info("%s. Falling back to 'default.xml'.", reason); + success = try_shortcuts_file(get_path(SYSTEM, KEYS, "default.xml")); + } + if (!success) { + g_info("Could not load 'default.xml' either. Falling back to 'inkscape.xml'."); + success = try_shortcuts_file(get_path(SYSTEM, KEYS, "inkscape.xml")); + } + if (!success) { + g_warning("Could not load any keyboard shortcut file (including fallbacks to 'default.xml' and 'inkscape.xml')."); } - read_shortcuts_file(shortcutfile.c_str()); - try_shortcuts_file(get_path(USER, KEYS, "default.xml")); + // load shortcuts adjusted by user + try_shortcuts_file(get_path(USER, KEYS, "default.xml"), true); } -static void try_shortcuts_file(char const *filename) { +static bool try_shortcuts_file(char const *filename, bool const is_user_set) { using Inkscape::IO::file_test; /* ah, if only we had an exception to catch... (permission, forgiveness) */ if (file_test(filename, G_FILE_TEST_EXISTS)) { - read_shortcuts_file(filename, true); + read_shortcuts_file(filename, is_user_set); + return true; } + + g_info("Unable to read keyboard shortcuts from %s (does not exist)", filename); + return false; } /* @@ -217,7 +238,12 @@ Inkscape::XML::Document *sp_shortcut_create_template_file(char const *filename) void sp_shortcut_get_file_names(std::vector<Glib::ustring> *names, std::vector<Glib::ustring> *paths) { using namespace Inkscape::IO::Resource; - for(auto &filename: get_filenames(KEYS, {".xml"}, {"default.xml", "inkscape.xml"})) { + std::vector<Glib::ustring> filenames = get_filenames(SYSTEM, KEYS, {".xml"}); + std::vector<Glib::ustring> filenames_user = get_filenames(USER, KEYS, {".xml"}, {"default.xml"}); // exclude default.xml as it only includes the user's modifications + filenames.insert(filenames.end(), filenames_user.begin(), filenames_user.end()); + + std::vector<std::pair<Glib::ustring, Glib::ustring>> names_and_paths; + for(auto &filename: filenames) { Glib::ustring label = Glib::path_get_basename(filename); XML::Document *doc = sp_repr_read_file(filename.c_str(), NULL); @@ -226,21 +252,40 @@ void sp_shortcut_get_file_names(std::vector<Glib::ustring> *names, std::vector<G continue; } - // Get the "key name" from the root element of each files + // Get the "key name" from the root element of each file XML::Node *root=doc->root(); if (!strcmp(root->name(), "keys")) { gchar const *name=root->attribute("name"); if (name) { label = Glib::ustring(name) + " (" + label + ")"; } - paths->push_back(filename.c_str()); - names->push_back(label.c_str()); + std::pair<Glib::ustring, Glib::ustring> name_and_path; + name_and_path = std::make_pair(label, filename); + names_and_paths.push_back(name_and_path); } else { g_warning("Not a shortcut keys file %s", filename.c_str()); } Inkscape::GC::release(doc); } - + + // sort by name + std::sort(names_and_paths.begin(), names_and_paths.end(), + [](std::pair<Glib::ustring, Glib::ustring>& pair1, std::pair<Glib::ustring, Glib::ustring>& pair2) { + return Glib::path_get_basename(pair1.first).compare(Glib::path_get_basename(pair2.first)) < 0; + }); + auto it_default = std::find_if(names_and_paths.begin(), names_and_paths.end(), + [](std::pair<Glib::ustring, Glib::ustring>& pair) { + return !Glib::path_get_basename(pair.second).compare("default.xml"); + }); + if (it_default != names_and_paths.end()) { + std::rotate(names_and_paths.begin(), it_default, it_default+1); + } + + // transform pairs to output vectors + std::transform(names_and_paths.begin(),names_and_paths.end(), std::back_inserter(*names), + [](const std::pair<Glib::ustring, Glib::ustring>& pair) { return pair.first; }); + std::transform(names_and_paths.begin(),names_and_paths.end(), std::back_inserter(*paths), + [](const std::pair<Glib::ustring, Glib::ustring>& pair) { return pair.second; }); } Glib::ustring sp_shortcut_get_file_path() |
