After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 615950 - Mixed use of Glib::ustring and std::string for content type (and MIME type)
Mixed use of Glib::ustring and std::string for content type (and MIME type)
Status: RESOLVED FIXED
Product: glibmm
Classification: Bindings
Component: giomm
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkmm-forge
gtkmm-forge
Depends on:
Blocks:
 
 
Reported: 2010-04-16 11:53 UTC by Debarshi Ray
Modified: 2016-12-18 11:38 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Debarshi Ray 2010-04-16 11:53:55 UTC
These interfaces use Glib::ustring for content type (and MIME type):
Gio::content_type_can_be_executable
Gio::content_type_equals
Gio::content_type_from_mime_type
Gio::content_type_get_description
Gio::content_type_get_icon
Gio::content_type_get_mime_type
Gio::content_type_guess
Gio::content_type_is_a
Gio::content_type_is_unknown
Gio::content_type_get_registered

These interface use std::string for content type:
Gio::FileInfo::get_content_type
Gio::FileInfo::set_content_type
Comment 1 Murray Cumming 2010-06-19 08:39:59 UTC
Thanks. Do you have a suggestion for what should be used?
Comment 2 Kjell Ahlstedt 2011-02-03 09:30:39 UTC
According to documentation in glib/gio, the strings in
g_file_info_[get|set]_content_type() are UTF-8 strings.
Thus Glib::ustring is most appropriate.
See gfileinfo.h, gfileattribute.c, gioenums.h.

There are many methods in Gio::FileInfo that get or set a file attribute
of type G_FILE_ATTRIBUTE_TYPE_STRING (UTF-8 encoded strings). Most of them
return or take as input parameter an std::string. They are

[get|set]_attribute_string
get_attribute_as_string     (returns a Glib::ustring)
[get|set]_display_name
[get|set]_edit_name
[get|set]_content_type
get_etag

The following methods get and set byte-strings (for instance encoding used for
filenames). They ought to continue to use std::string.

[get|set]_attribute_byte_string
[get|set]_name
[get|set]_symlink_target

All these methods, and others, take a "const std::string& attribute" parameter.
I don't know if const Glib::ustring& would be better.
All predefined attribute keys consist of only ASCII lowercase letters, hyphens
and colons. It's possible to define new attributes with the set_attribute_*
methods. I think they accept any nul-terminated byte string as attribute key,
without making any assumption about a particular character encoding.
Comment 3 Murray Cumming 2011-02-21 10:13:31 UTC
(In reply to comment #2)
> According to documentation in glib/gio, the strings in
> g_file_info_[get|set]_content_type() are UTF-8 strings.

What documentation? URL, please.

> Thus Glib::ustring is most appropriate.
> See gfileinfo.h, gfileattribute.c, gioenums.h.
>
> There are many methods in Gio::FileInfo that get or set a file attribute
> of type G_FILE_ATTRIBUTE_TYPE_STRING (UTF-8 encoded strings).

Yes:
http://library.gnome.org/devel/gio/stable/gio-GFileAttribute.html#GFileAttributeType

> Most of them
> return or take as input parameter an std::string. They are
> 
> [get|set]_attribute_string
> get_attribute_as_string     (returns a Glib::ustring)
> [get|set]_display_name
> [get|set]_edit_name
> [get|set]_content_type
> get_etag
> 
> The following methods get and set byte-strings (for instance encoding used for
> filenames). They ought to continue to use std::string.
> 
> [get|set]_attribute_byte_string
> [get|set]_name
> [get|set]_symlink_target
> 
> All these methods, and others, take a "const std::string& attribute" parameter.
> I don't know if const Glib::ustring& would be better.
> All predefined attribute keys consist of only ASCII lowercase letters, hyphens
> and colons. It's possible to define new attributes with the set_attribute_*
> methods. I think they accept any nul-terminated byte string as attribute key,
> without making any assumption about a particular character encoding.
Comment 4 Kjell Ahlstedt 2011-02-21 14:38:51 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > According to documentation in glib/gio, the strings in
> > g_file_info_[get|set]_content_type() are UTF-8 strings.
> 
> What documentation? URL, please.

g_file_info_set_content_type() is described at
http://library.gnome.org/devel/gio/stable/GFileInfo.html#g-file-info-set-content-type

with a link to the description of G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE at
http://library.gnome.org/devel/gio/stable/GFileInfo.html#G-FILE-ATTRIBUTE-STANDARD-CONTENT-TYPE:CAPS

which is of type G_FILE_ATTRIBUTE_TYPE_STRING, described at
http://library.gnome.org/devel/gio/stable/gio-GFileAttribute.html#G-FILE-ATTRIBUTE-TYPE-STRING:CAPS

  G_FILE_ATTRIBUTE_TYPE_STRING      a null terminated UTF8 string.
  G_FILE_ATTRIBUTE_TYPE_BYTE_STRING a zero terminated string of non-zero bytes. 

Similar links can be followed starting from the other g_file_info_set_xxx()
functions.
Comment 5 Murray Cumming 2011-03-09 13:21:01 UTC
Thanks. For Gio::FileInfo, I have added TODOs, so we can fix that when we can break ABI one day:
http://git.gnome.org/browse/glibmm/commit/?id=158d2cb186fda90faa097ffa4e401c199c44660b

For the Gio::content_type_*() API, I still need documentation for the C API saying that it is UTF-8. At the moment I don't see that. For instance:
  http://library.gnome.org/devel/gio/unstable/gio-GContentType.html#g-content-type-can-be-executable
Comment 6 Kjell Ahlstedt 2011-03-09 19:41:48 UTC
(In reply to comment #5)
> For Gio::FileInfo, I have added TODOs, so we can fix that when we can
> break ABI one day:

I noticed that you did not add TODOs at [get|set]_attribute_string. Did you
forget, or is it because the C API documentation is not as clear as for the
other functions? It seems to be implicit that "string" means UTF-8 string,
"byte string" means a string with unspecified character encoding.
From the source code it's clear that the return value of
g_file_info_get_attribute_string() and the parameter 'value' in
g_file_info_set_attribute_string() are of type G_FILE_ATTRIBUTE_TYPE_STRING.

> For the Gio::content_type_*() API, I still need documentation for the C API
> saying that it is UTF-8. At the moment I don't see that.

I haven't found any unambiguous documentation for g_content_type_*(). Perhaps
it's implicit here too that "string" means UTF-8 string, but I can't find
a statement to that effect.
Comment 7 Murray Cumming 2011-03-11 09:06:25 UTC
(In reply to comment #6)
> From the source code it's clear that the return value of
> g_file_info_get_attribute_string() and the parameter 'value' in
> g_file_info_set_attribute_string() are of type G_FILE_ATTRIBUTE_TYPE_STRING.

Yes, I agree. So I improved the C documentation (they let me make simple fixes):
http://git.gnome.org/browse/glib/commit/?id=c1a75ca783f602d3edf465c28918dac7ea57a1e7

And I have added the TODOs for glibmm:
http://git.gnome.org/browse/glibmm/commit/?id=4c4096d2901c4cbdbffac472fb032ad956f53fa8
Comment 8 Kjell Ahlstedt 2016-12-18 11:38:17 UTC
glibmm-2.52: I've changed to Glib::ustring in the Gio::FileInfo methods
listed in comment 2. I've also changed
Gio::File::set_attribute_string()
Gio::FileIOStream::get_etag()
Gio::FileOutputStream::get_etag()