Skip to content

Commit

Permalink
Introduce KeyFileUtil (#214)
Browse files Browse the repository at this point in the history
Some methods in DesktopFile are just wrappers of KeyFile and not related to DesktopFile itself.
  • Loading branch information
ryonakano authored Oct 13, 2024
1 parent 0775766 commit 1f28ebf
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 209 deletions.
239 changes: 31 additions & 208 deletions src/Model/DesktopFile.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,32 @@ public class Model.DesktopFile : Object {
public const string DESKTOP_SUFFIX = ".desktop";

/**
* A key under ``g_key_file_desktop_group``, whose value is a list of strings giving the keywords which may be used in
* addition to other metadata to describe this entry.
*
* Note: Using ``KeyFileDesktop.KEY_KEYWORDS`` will cause the cc failing with ``‘G_KEY_FILE_DESKTOP_KEY_KEYWORDS’ undeclared``
* error.<<BR>>
* This constant does not seem to be defined in the original glib and defined in the following patch.<<BR>>
* (and maybe valac uses glibc with this patch and thus it does not complain any error.)<<BR>>
* <<BR>>
* [[https://sources.debian.org/patches/glib2.0/2.78.3-2/01_gettext-desktopfiles.patch/]]<<BR>>
* <<BR>>
* We just keep to borrow the definition of KEY_KEYWORDS here instead of applying the patch,
* since it might have side effect.
* The absolute path to the desktop file.
*/
public const string KEY_KEYWORDS = "Keywords";
public string path { get; construct; }

/**
* The absolute path to the desktop file.
* Value of "Type" entry.
*/
public string path { get; construct; }
public string value_type {
set {
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_TYPE, value);
}
}

/**
* Value of "Name" entry.
*/
public string value_name {
owned get {
string? locale = get_locale_for_key (KeyFileDesktop.KEY_NAME, Application.preferred_language);
string name = get_locale_string (KeyFileDesktop.KEY_NAME, locale);
string? locale = Util.KeyFileUtil.get_locale_for_key (keyfile, KeyFileDesktop.KEY_NAME, Application.preferred_language);
string name = Util.KeyFileUtil.get_locale_string (keyfile, KeyFileDesktop.KEY_NAME, locale);

return name;
}

set {
set_string (KeyFileDesktop.KEY_NAME, value);
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_NAME, value);
}
}

Expand All @@ -54,11 +47,11 @@ public class Model.DesktopFile : Object {
*/
public string value_exec {
owned get {
return get_string (KeyFileDesktop.KEY_EXEC);
return Util.KeyFileUtil.get_string (keyfile, KeyFileDesktop.KEY_EXEC);
}

set {
set_string (KeyFileDesktop.KEY_EXEC, value);
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_EXEC, value);
}
}

Expand All @@ -67,11 +60,11 @@ public class Model.DesktopFile : Object {
*/
public string value_icon {
owned get {
return get_string (KeyFileDesktop.KEY_ICON);
return Util.KeyFileUtil.get_string (keyfile, KeyFileDesktop.KEY_ICON);
}

set {
set_string (KeyFileDesktop.KEY_ICON, value);
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_ICON, value);
}
}

Expand All @@ -80,14 +73,14 @@ public class Model.DesktopFile : Object {
*/
public string value_generic_name {
owned get {
string? locale = get_locale_for_key (KeyFileDesktop.KEY_GENERIC_NAME, Application.preferred_language);
string generic_name = get_locale_string (KeyFileDesktop.KEY_GENERIC_NAME, locale);
string? locale = Util.KeyFileUtil.get_locale_for_key (keyfile, KeyFileDesktop.KEY_GENERIC_NAME, Application.preferred_language);
string generic_name = Util.KeyFileUtil.get_locale_string (keyfile, KeyFileDesktop.KEY_GENERIC_NAME, locale);

return generic_name;
}

set {
set_string (KeyFileDesktop.KEY_GENERIC_NAME, value);
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_GENERIC_NAME, value);
}
}

Expand All @@ -96,14 +89,14 @@ public class Model.DesktopFile : Object {
*/
public string value_comment {
owned get {
string? locale = get_locale_for_key (KeyFileDesktop.KEY_COMMENT, Application.preferred_language);
string comment = get_locale_string (KeyFileDesktop.KEY_COMMENT, locale);
string? locale = Util.KeyFileUtil.get_locale_for_key (keyfile, KeyFileDesktop.KEY_COMMENT, Application.preferred_language);
string comment = Util.KeyFileUtil.get_locale_string (keyfile, KeyFileDesktop.KEY_COMMENT, locale);

return comment;
}

set {
set_string (KeyFileDesktop.KEY_COMMENT, value);
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_COMMENT, value);
}
}

Expand All @@ -112,11 +105,11 @@ public class Model.DesktopFile : Object {
*/
public string[] value_categories {
owned get {
return get_string_list (KeyFileDesktop.KEY_CATEGORIES);
return Util.KeyFileUtil.get_string_list (keyfile, KeyFileDesktop.KEY_CATEGORIES);
}

set {
set_string_list (KeyFileDesktop.KEY_CATEGORIES, value);
Util.KeyFileUtil.set_string_list (keyfile, KeyFileDesktop.KEY_CATEGORIES, value);
}
}

Expand All @@ -125,11 +118,11 @@ public class Model.DesktopFile : Object {
*/
public string[] value_keywords {
owned get {
return get_string_list (KEY_KEYWORDS);
return Util.KeyFileUtil.get_string_list (keyfile, Util.KeyFileUtil.KEY_KEYWORDS);
}

set {
set_string_list (KEY_KEYWORDS, value);
Util.KeyFileUtil.set_string_list (keyfile, Util.KeyFileUtil.KEY_KEYWORDS, value);
}
}

Expand All @@ -138,11 +131,11 @@ public class Model.DesktopFile : Object {
*/
public string value_startup_wm_class {
owned get {
return get_string (KeyFileDesktop.KEY_STARTUP_WM_CLASS);
return Util.KeyFileUtil.get_string (keyfile, KeyFileDesktop.KEY_STARTUP_WM_CLASS);
}

set {
set_string (KeyFileDesktop.KEY_STARTUP_WM_CLASS, value);
Util.KeyFileUtil.set_string (keyfile, KeyFileDesktop.KEY_STARTUP_WM_CLASS, value);
}
}

Expand All @@ -151,11 +144,11 @@ public class Model.DesktopFile : Object {
*/
public bool value_terminal {
get {
return get_boolean (KeyFileDesktop.KEY_TERMINAL);
return Util.KeyFileUtil.get_boolean (keyfile, KeyFileDesktop.KEY_TERMINAL);
}

set {
set_boolean (KeyFileDesktop.KEY_TERMINAL, value);
Util.KeyFileUtil.set_boolean (keyfile, KeyFileDesktop.KEY_TERMINAL, value);
}
}

Expand Down Expand Up @@ -204,132 +197,6 @@ public class Model.DesktopFile : Object {
return dest.load_from_data (data);
}

////////////////////////////////////////////////////////////////////////////
//
// Key Opearations
//
////////////////////////////////////////////////////////////////////////////

/**
* Return the value associated with ``key`` as a boolean.
*
* @param key a key
* @return the value associated with the key as a boolean, or false if the key was not found or could not be parsed
* @see GLib.KeyFile.get_boolean
*/
public bool get_boolean (string key) {
bool val = false;

if (!has_key (key)) {
return val;
}

try {
val = keyfile.get_boolean (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_boolean: key=%s: %s", key, err.message);
}

return val;
}

public string get_string (string key) {
string val = "";

if (!has_key (key)) {
return val;
}

try {
val = keyfile.get_string (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_string: key=%s: %s", key, err.message);
}

return val;
}

public bool has_key (string key) {
bool ret = false;

// Maybe keyfile is new and has no key yet
if (!keyfile.has_group (KeyFileDesktop.GROUP)) {
return ret;
}

try {
ret = keyfile.has_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.has_key: key=%s: %s", key, err.message);
}

return ret;
}

public string[] get_string_list (string key) {
string[] val = {};

if (!has_key (key)) {
return val;
}

try {
val = keyfile.get_string_list (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_string_list: key=%s: %s", key, err.message);
}

return val;
}

public void set_boolean (string key, bool val) {
if (val) {
// Update the value when the corresponding entry has some value.
keyfile.set_boolean (KeyFileDesktop.GROUP, key, val);
} else {
// Remove the key when it exists and the corresponding entry has no value.
if (has_key (key)) {
try {
keyfile.remove_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, err.message);
}
}
}
}

public void set_string (string key, string val) {
if (val != "") {
// Update the value when the corresponding entry has some value.
keyfile.set_string (KeyFileDesktop.GROUP, key, val);
} else {
// Remove the key when it exists and the corresponding entry has no value.
if (has_key (key)) {
try {
keyfile.remove_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, err.message);
}
}
}
}

public void set_string_list (string key, string[] list) {
if (list.length > 0) {
// Update the value when the corresponding entry has some value.
keyfile.set_string_list (KeyFileDesktop.GROUP, key, list);
} else {
// Remove the key when it exists and the corresponding entry has no value.
if (has_key (key)) {
try {
keyfile.remove_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, err.message);
}
}
}
}

public string to_data () {
return keyfile.to_data ();
}
Expand All @@ -346,62 +213,18 @@ public class Model.DesktopFile : Object {
return ret;
}

////////////////////////////////////////////////////////////////////////////
//
// Localized Key Opearations
//
////////////////////////////////////////////////////////////////////////////

public string? get_locale_for_key (string key, string? locale = null) {
return keyfile.get_locale_for_key (KeyFileDesktop.GROUP, key, locale);
}

public string get_locale_string (string key, string? locale = null) {
string val = "";

if (!has_key (key)) {
return val;
}

try {
val = keyfile.get_locale_string (KeyFileDesktop.GROUP, key, locale);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_locale_string: key=%s: %s", key, err.message);
}

return val;
}

////////////////////////////////////////////////////////////////////////////
//
// File Opearations
//
////////////////////////////////////////////////////////////////////////////

public bool load_file () {
bool ret = false;

try {
ret = keyfile.load_from_file (path, KeyFileFlags.KEEP_TRANSLATIONS);
} catch (FileError err) {
warning ("Failed to load from file. path=%s: %s", path, err.message);
} catch (KeyFileError err) {
warning ("Invalid keyfile. path=%s: %s", path, err.message);
}

return ret;
return Util.KeyFileUtil.load_file (keyfile, path, KeyFileFlags.KEEP_TRANSLATIONS);
}

public bool save_file () {
bool ret = false;

try {
ret = keyfile.save_to_file (path);
} catch (FileError err) {
warning ("Failed to save file. path=%s: %s", path, err.message);
}

return ret;
return Util.KeyFileUtil.save_file (keyfile, path);
}

public bool delete_file () {
Expand Down
Loading

0 comments on commit 1f28ebf

Please sign in to comment.