Merge lp:~3v1n0/unity/desktop-utilities-unescape-paths into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Michal Hruby
Approved revision: no longer in the source branch.
Merged at revision: 2801
Proposed branch: lp:~3v1n0/unity/desktop-utilities-unescape-paths
Merge into: lp:unity
Diff against target: 97 lines (+33/-9)
2 files modified
UnityCore/DesktopUtilities.cpp (+18/-6)
tests/test_desktop_utilities.cpp (+15/-3)
To merge this branch: bzr merge lp:~3v1n0/unity/desktop-utilities-unescape-paths
Reviewer Review Type Date Requested Status
Michal Hruby (community) Approve
Review via email: mp+127590@code.launchpad.net

Commit message

DesktopUtilities: unescape the desktop paths and ids before processing them

Description of the change

When computing the DesktopID or the file path from a DesktopID, we need to make sure that the processed file has been escaped.

Unit tests added.

To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

Is this really what we want to do? We should properly distinguish methods that accept paths vs methods that work with URIs. Otherwise the next bug we'll get is "Unable to add desktop files with [insert special symbol here] in their name".

Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Mh, in effect this seems to happen now if a file name has contains a '%', while dragging it from dash... But at this point I would say that the lens should use the same format of the file managers and so always escaping these file URIs, or it won't be always possible to distinguish them.

Revision history for this message
Daniel van Vugt (vanvugt) wrote :

Note: This is already committed to lp:unity/6.0 before lp:unity!

Revision history for this message
Michal Hruby (mhr3) wrote :

I have my doubts about this, but let's at least keep trunk in sync with 6.0 and perhaps fix it up later.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'UnityCore/DesktopUtilities.cpp'
--- UnityCore/DesktopUtilities.cpp 2012-09-07 14:32:44 +0000
+++ UnityCore/DesktopUtilities.cpp 2012-10-02 21:09:22 +0000
@@ -87,6 +87,12 @@
87 if (desktop_path.empty())87 if (desktop_path.empty())
88 return "";88 return "";
8989
90 glib::String unescaped(g_uri_unescape_string(desktop_path.c_str(), NULL));
91 std::string const& desktop_file = unescaped.Str();
92
93 if (desktop_file.empty())
94 return "";
95
90 for (auto dir : default_paths)96 for (auto dir : default_paths)
91 {97 {
92 if (!dir.empty())98 if (!dir.empty())
@@ -100,11 +106,11 @@
100 dir.append(G_DIR_SEPARATOR_S "applications" G_DIR_SEPARATOR_S);106 dir.append(G_DIR_SEPARATOR_S "applications" G_DIR_SEPARATOR_S);
101 }107 }
102108
103 if (desktop_path.find(dir) == 0)109 if (desktop_file.find(dir) == 0)
104 {110 {
105 // if we are in a subdirectory of system path, the store name should111 // if we are in a subdirectory of system path, the store name should
106 // be subdir-filename.desktop112 // be subdir-filename.desktop
107 std::string desktop_suffix = desktop_path.substr(dir.size());113 std::string desktop_suffix = desktop_file.substr(dir.size());
108 std::replace(desktop_suffix.begin(), desktop_suffix.end(), G_DIR_SEPARATOR, '-');114 std::replace(desktop_suffix.begin(), desktop_suffix.end(), G_DIR_SEPARATOR, '-');
109115
110 return desktop_suffix;116 return desktop_suffix;
@@ -112,7 +118,7 @@
112 }118 }
113 }119 }
114120
115 return desktop_path;121 return desktop_file;
116}122}
117123
118std::string DesktopUtilities::GetDesktopID(std::string const& desktop_path)124std::string DesktopUtilities::GetDesktopID(std::string const& desktop_path)
@@ -126,12 +132,18 @@
126 if (desktop_id.empty())132 if (desktop_id.empty())
127 return "";133 return "";
128134
135 glib::String unescaped_id(g_uri_unescape_string(desktop_id.c_str(), NULL));
136 std::string const& id = unescaped_id.Str();
137
138 if (id.empty())
139 return "";
140
129 glib::Object<GDesktopAppInfo> info;141 glib::Object<GDesktopAppInfo> info;
130142
131 if (desktop_id.find(G_DIR_SEPARATOR_S) != std::string::npos)143 if (id.find(G_DIR_SEPARATOR_S) != std::string::npos)
132 info = g_desktop_app_info_new_from_filename(desktop_id.c_str());144 info = g_desktop_app_info_new_from_filename(id.c_str());
133 else145 else
134 info = g_desktop_app_info_new(desktop_id.c_str());146 info = g_desktop_app_info_new(id.c_str());
135147
136 if (info)148 if (info)
137 {149 {
138150
=== modified file 'tests/test_desktop_utilities.cpp'
--- tests/test_desktop_utilities.cpp 2012-09-18 15:47:49 +0000
+++ tests/test_desktop_utilities.cpp 2012-10-02 21:09:22 +0000
@@ -36,9 +36,6 @@
36TEST(TestDesktopUtilitiesDesktopID, TestEmptyValues)36TEST(TestDesktopUtilitiesDesktopID, TestEmptyValues)
37{37{
38 std::vector<std::string> empty_list;38 std::vector<std::string> empty_list;
39
40 EXPECT_THAT(DesktopUtilities::GetDesktopID(empty_list, "/some/path/to.desktop"),
41 Eq("/some/path/to.desktop"));
42 EXPECT_THAT(DesktopUtilities::GetDesktopID(empty_list, "/some/path/to.desktop"),39 EXPECT_THAT(DesktopUtilities::GetDesktopID(empty_list, "/some/path/to.desktop"),
43 Eq("/some/path/to.desktop"));40 Eq("/some/path/to.desktop"));
4441
@@ -77,6 +74,21 @@
77 Eq("/some/path/applications/to.desktop"));74 Eq("/some/path/applications/to.desktop"));
78}75}
7976
77TEST(TestDesktopUtilitiesDesktopID, TestUnescapePath)
78{
79 std::vector<std::string> dirs;
80
81 dirs.push_back("/this/ path");
82 dirs.push_back("/that/path /");
83
84 EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/this/%20path/applications/to%20file.desktop"),
85 Eq("to file.desktop"));
86 EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/that/path%20/applications/to%20file.desktop"),
87 Eq("to file.desktop"));
88 EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/some/path/applications/to%20file.desktop"),
89 Eq("/some/path/applications/to file.desktop"));
90}
91
80TEST(TestDesktopUtilitiesDesktopID, TestSubdirectory)92TEST(TestDesktopUtilitiesDesktopID, TestSubdirectory)
81{93{
82 std::vector<std::string> dirs;94 std::vector<std::string> dirs;