openplugins -- import and export thru settings save / restore settings

rockbox-gerrit-noreply--- via rockbox-cvs <[email protected]>
Newsgroups gmane.comp.systems.archos.rockbox.cvs
Message-ID <[email protected]>
commit 045d148697d0369d4382ab7fef2781e7506dc992
Author: William Wilgus <[email protected]>
Date:   Thu Jan 22 03:24:16 2026 -0500

    openplugins -- import and export thru settings save / restore settings
    
    saves openplugin entries to the config file for later import to the database
    
    lang_english_to_id() allows entries to be transfered between builds
    
    Change-Id: I47e01c5f75f8b0e69c203828e21759ef24bf125d

diff --git a/apps/open_plugin.c b/apps/open_plugin.c
index 9976e5b77c..bcb8b34604 100644
--- a/apps/open_plugin.c
+++ b/apps/open_plugin.c
@@ -27,6 +27,7 @@
 #include "splash.h"
 #include "lang.h"
 #include "filetypes.h"
+#include "language.h" /*lang_english_to_id*/
 
 /* Define LOGF_ENABLE to enable logf output in this file */
 /*#define LOGF_ENABLE*/
@@ -520,4 +521,86 @@ void open_plugin_cache_flush(void)
     op_update_dat(op_entry, true);
 }
 
+static bool op_entry_read(int fd, int selected_item, off_t data_sz, struct open_plugin_entry_t *op_entry)
+{
+    op_clear_entry(op_entry);
+    return ((selected_item >= 0) && (fd >= 0) &&
+        (lseek(fd, selected_item  * op_entry_sz, SEEK_SET) >= 0) &&
+        (read(fd, op_entry, data_sz) == data_sz) && op_entry_checksum(op_entry) > 0);
+}
+
+void open_plugin_import(char *strdat)
+{
+    /* Note: Destroys strdat */
+    char *vect[5];
+    /* expected openplugin strdat: "englishid", "name", "path", "param" */
+
+    if (split_string(strdat, ',', vect, 5) == 4)
+    {
+        /* Space for 5 values so we will fail if excess arguments or too few */
+        for(int i = 0; i < 4; i++)
+        {
+            vect[i] = skip_whitespace(vect[i]);
+            int eos = ((int)strlen(vect[i]))-2;
+            /* Failure if string is not quoted */
+            if (vect[i][0] != '"' || eos < 0 || vect[i][eos + 1] != '"')
+            {
+                logf("%s[%d] error: quoted string expected\n", __func__, i);
+                return;
+            }
+            vect[i]++; /* skip first " */
+            vect[i][eos] = '\0'; /* skip other " */
+        }
+        char *key = vect[1];
+        int lang_id = lang_english_to_id(vect[0]);
+        if (lang_id >= 0)
+            key = ID2P(lang_id);
+        /* if key exists it will be overwritten */
+        open_plugin_add_path(key, vect[2], vect[3]);
+        return;
+    }
+    logf("%s error: importing entries", __func__);
+}
+
+void open_plugin_export(int cfg_fd)
+{
+    /* NOTE: assumes cfg_fd is valid */
+    char *lang_name;
+    int  fd;
+    int index = 0;
+
+    struct open_plugin_entry_t *op_entry = open_plugin_get_entry();
+    /* if another entry is loaded; flush it to disk before we destroy it */
+    op_update_dat(op_entry, true);
+
+    fd = open(OPEN_PLUGIN_DAT, O_RDONLY, 0666);
+
+    if (fd < 0)
+    {
+        logf("%s error: opening %s", __func__, OPEN_PLUGIN_DAT);
+        return;  /* OPEN_PLUGIN_NOT_FOUND */
+    }
+    while (op_entry_read(fd, index++, op_entry_sz, op_entry))
+    {
+        /* don't save the LANG_OPEN_PLUGIN entry -- it is for internal use */
+        if (op_entry->lang_id == LANG_OPEN_PLUGIN)
+        {
+            continue;
+        }
+        if (op_entry->lang_id >=0)
+            lang_name = P2STR(ID2P(op_entry->lang_id));
+        else
+            lang_name = "[USER]"; /* needs to be an invalid lang string */
+
+        fdprintf(cfg_fd,"%s: \"%s\", \"%s\", \"%s\", \"%s\"\n",
+                 "openplugin", lang_name, op_entry->name, op_entry->path, op_entry->param);
+
+        logf("openplugin[%d]: \"%s\", \"%s\", \"%s\", \"%s\"\n lang id: %d hash: %x, csum: %x\n",
+                 index, lang_name, op_entry->name, op_entry->path, op_entry->param,
+                 op_entry->lang_id, op_entry->hash, op_entry->checksum);
+    }
+    close(fd);
+    logf("%s exported %d entries", __func__, index);
+}
+
 #endif /* ndef __PCTOOL__ */
diff --git a/apps/open_plugin.h b/apps/open_plugin.h
index 847a834c0c..7038f419f3 100644
--- a/apps/open_plugin.h
+++ b/apps/open_plugin.h
@@ -84,6 +84,8 @@ int open_plugin_load_entry(const char *key);
 void open_plugin_browse(const char *key);
 int open_plugin_run(const char *key);
 void open_plugin_cache_flush(void); /* flush to disk */
+void open_plugin_import(char *strdat);
+void open_plugin_export(int cfg_fd);
 #endif
 
 #endif /*ndef __PCTOOL__ */
diff --git a/apps/settings.c b/apps/settings.c
index 56a9a8a425..9713a482da 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -293,11 +293,11 @@ bool copy_filename_setting(char *buf, size_t buflen, const char *input,
     return true;
 }
 
-void string_to_cfg(const char *name, char* value, bool *theme_changed)
+bool string_to_cfg(const char *name, char* value, bool *theme_changed)
 {
     const struct settings_list *setting = find_setting_by_cfgname(name);
     if (!setting)
-        return;
+        return false;
 
     uint32_t flags = setting->flags;
 
@@ -366,6 +366,7 @@ void string_to_cfg(const char *name, char* value, bool *theme_changed)
                 {
                     logf("Error: %s: Not Found! [%s]\r\n",
                                       setting->cfg_name, value);
+                    return false;
                 }
             }
         break;
@@ -394,6 +395,7 @@ void string_to_cfg(const char *name, char* value, bool *theme_changed)
         break;
     }
     }
+    return true;
 }
 
 bool settings_load_config(const char* file, bool apply)
@@ -412,7 +414,15 @@ bool settings_load_config(const char* file, bool apply)
         char *name, *value;
         if (!settings_parseline(line, &name, &value))
             continue;
-        string_to_cfg(name, value, &theme_changed);
+
+        if (!string_to_cfg(name, value, &theme_changed))
+        {
+            /* if we are here then name was not a valid setting */
+            if (!strcmp(name, "openplugin"))
+            {
+                open_plugin_import(value);
+            }
+        }
     } /* while(...) */
 
     close(fd);
@@ -630,6 +640,12 @@ static bool settings_write_config(const char* filename, int options)
 
         fdprintf(fd,"%s: %s\r\n",setting->cfg_name,value);
     } /* for(...) */
+
+    if (options == SETTINGS_SAVE_ALL)
+    {
+        /* add openplugin entries to the open settings file */
+        open_plugin_export(fd);
+    }
     close(fd);
     return true;
 }
diff --git a/apps/settings.h b/apps/settings.h
index a4b5a5832c..6a429f9d13 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -308,7 +308,7 @@ const struct settings_list* find_setting_by_cfgname(const char* name);
 bool cfg_int_to_string(const struct settings_list *setting, int val, char* buf, int buf_len);
 bool cfg_string_to_int(const struct settings_list *setting, int* out, const char* str);
 void cfg_to_string(const struct settings_list *setting, char* buf, int buf_len);
-void string_to_cfg(const char *name, char* value, bool *theme_changed);
+bool string_to_cfg(const char *name, char* value, bool *theme_changed);
 
 bool copy_filename_setting(char *buf, size_t buflen, const char *input,
                            const struct filename_setting *fs);
-- 
rockbox-cvs mailing list
[email protected]
https://lists.haxx.se/mailman/listinfo/rockbox-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.