Changeset 84d29a2 in mainline


Ignore:
Timestamp:
2023-11-27T13:09:59Z (5 months ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, topic/simplify-dev-export
Children:
ef4d684
Parents:
8a43f01
Message:

Libtbarcfg needs work

Add tbarcfg_create(), smenu_entry_create(), smenu_entry_destroy().
Fill in and improve unit tests.

Location:
uspace/lib/tbarcfg
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/tbarcfg/include/tbarcfg/tbarcfg.h

    r8a43f01 r84d29a2  
    3838
    3939#include <errno.h>
     40#include <sif.h>
    4041#include <types/tbarcfg/tbarcfg.h>
    4142
     43extern errno_t tbarcfg_create(const char *, tbarcfg_t **);
    4244extern errno_t tbarcfg_open(const char *, tbarcfg_t **);
    4345extern void tbarcfg_close(tbarcfg_t *);
     
    4951extern errno_t smenu_entry_set_cmd(smenu_entry_t *, const char *);
    5052extern errno_t smenu_entry_save(smenu_entry_t *);
     53extern errno_t smenu_entry_create(tbarcfg_t *, const char *, const char *);
     54extern errno_t smenu_entry_destroy(smenu_entry_t *);
    5155
    5256#endif
  • uspace/lib/tbarcfg/private/tbarcfg.h

    r8a43f01 r84d29a2  
    4848        /** List of start menu entries (smenu_entry_t) */
    4949        list_t entries;
     50        /** Entries SIF node */
     51        sif_node_t *nentries;
    5052};
    5153
  • uspace/lib/tbarcfg/src/tbarcfg.c

    r8a43f01 r84d29a2  
    4141#include "../private/tbarcfg.h"
    4242
     43/** Create taskbar configuration.
     44 *
     45 * @param repopath Pathname of the new menu repository
     46 * @param rtbcfg Place to store pointer to taskbar configuration
     47 * @return EOK on success or an error code
     48 */
     49errno_t tbarcfg_create(const char *repopath, tbarcfg_t **rtbcfg)
     50{
     51        tbarcfg_t *tbcfg;
     52        sif_sess_t *repo = NULL;
     53        sif_node_t *rnode;
     54        errno_t rc;
     55        sif_trans_t *trans = NULL;
     56
     57        tbcfg = calloc(1, sizeof(tbarcfg_t));
     58        if (tbcfg == NULL) {
     59                rc = ENOMEM;
     60                goto error;
     61        }
     62
     63        list_initialize(&tbcfg->entries);
     64
     65        rc = sif_create(repopath, &repo);
     66        if (rc != EOK)
     67                goto error;
     68
     69        tbcfg->repo = repo;
     70
     71        rnode = sif_get_root(repo);
     72
     73        rc = sif_trans_begin(repo, &trans);
     74        if (rc != EOK)
     75                goto error;
     76
     77        rc = sif_node_append_child(trans, rnode, "entries", &tbcfg->nentries);
     78        if (rc != EOK)
     79                goto error;
     80
     81        rc = sif_trans_end(trans);
     82        if (rc != EOK)
     83                goto error;
     84
     85        *rtbcfg = tbcfg;
     86        return EOK;
     87error:
     88        if (trans != NULL)
     89                sif_trans_abort(trans);
     90        if (repo != NULL)
     91                sif_close(repo);
     92        if (tbcfg != NULL)
     93                free(tbcfg);
     94        return rc;
     95}
     96
    4397/** Open taskbar configuration.
    4498 *
     
    52106        sif_sess_t *repo = NULL;
    53107        sif_node_t *rnode;
    54         sif_node_t *nentries;
    55108        sif_node_t *nentry;
    56109        const char *ntype;
     
    74127
    75128        rnode = sif_get_root(repo);
    76         nentries = sif_node_first_child(rnode);
    77         ntype = sif_node_get_type(nentries);
     129        tbcfg->nentries = sif_node_first_child(rnode);
     130        ntype = sif_node_get_type(tbcfg->nentries);
    78131        if (str_cmp(ntype, "entries") != 0) {
    79132                rc = EIO;
     
    81134        }
    82135
    83         nentry = sif_node_first_child(nentries);
     136        nentry = sif_node_first_child(tbcfg->nentries);
    84137        while (nentry != NULL) {
    85138                ntype = sif_node_get_type(nentry);
     
    238291errno_t smenu_entry_save(smenu_entry_t *entry)
    239292{
    240         sif_trans_t *trans;
     293        sif_trans_t *trans = NULL;
    241294        errno_t rc;
    242295
     
    329382}
    330383
     384/** Create new start menu entry.
     385 *
     386 * @param smenu Start menu
     387 * @param nentry Backing SIF node
     388 * @param caption Caption
     389 * @param cmd Command to run
     390 */
     391errno_t smenu_entry_create(tbarcfg_t *smenu, const char *caption,
     392    const char *cmd)
     393{
     394        sif_node_t *nentry;
     395        errno_t rc;
     396        sif_trans_t *trans = NULL;
     397
     398        rc = sif_trans_begin(smenu->repo, &trans);
     399        if (rc != EOK)
     400                goto error;
     401
     402        rc = sif_node_append_child(trans, smenu->nentries, "entry",
     403            &nentry);
     404        if (rc != EOK)
     405                goto error;
     406
     407        rc = smenu_entry_new(smenu, nentry, caption, cmd);
     408        if (rc != EOK)
     409                goto error;
     410
     411        rc = sif_trans_end(trans);
     412        if (rc != EOK)
     413                goto error;
     414
     415        return EOK;
     416error:
     417        if (trans != NULL)
     418                sif_trans_abort(trans);
     419        return rc;
     420}
     421
     422/** Destroy start menu entry..
     423 *
     424 * @param entry Start menu entry
     425 * @return EOK on success or an error code
     426 */
     427errno_t smenu_entry_destroy(smenu_entry_t *entry)
     428{
     429        errno_t rc;
     430        sif_trans_t *trans = NULL;
     431
     432        rc = sif_trans_begin(entry->smenu->repo, &trans);
     433        if (rc != EOK)
     434                goto error;
     435
     436        sif_node_destroy(trans, entry->nentry);
     437
     438        rc = sif_trans_end(trans);
     439        if (rc != EOK)
     440                goto error;
     441
     442        smenu_entry_delete(entry);
     443error:
     444        if (trans != NULL)
     445                sif_trans_abort(trans);
     446        return rc;
     447}
     448
    331449/** @}
    332450 */
  • uspace/lib/tbarcfg/test/tbarcfg.c

    r8a43f01 r84d29a2  
    3636PCUT_TEST_SUITE(tbarcfg);
    3737
    38 /** Opening and closing taskbar configuration */
    39 PCUT_TEST(open_close)
    40 {
    41         errno_t rc;
    42         tbarcfg_t *tbcfg;
    43         FILE *f;
    44         int rv;
    45 
    46         f = fopen("/tmp/test", "wt");
    47         PCUT_ASSERT_NOT_NULL(f);
    48 
    49         rv = fputs("[sif](){[entries](){}}", f);
    50         PCUT_ASSERT_TRUE(rv >= 0);
    51 
    52         rv = fclose(f);
    53         PCUT_ASSERT_INT_EQUALS(0, rv);
    54 
    55         rc = tbarcfg_open("/tmp/test", &tbcfg);
    56         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    57 
    58         tbarcfg_close(tbcfg);
     38/** Creating, opening and closing taskbar configuration */
     39PCUT_TEST(create_open_close)
     40{
     41        errno_t rc;
     42        tbarcfg_t *tbcfg;
     43        char fname[L_tmpnam], *p;
     44
     45        p = tmpnam(fname);
     46        PCUT_ASSERT_NOT_NULL(p);
     47
     48        /* Create new repository */
     49        rc = tbarcfg_create(fname, &tbcfg);
     50        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     51
     52        tbarcfg_close(tbcfg);
     53
     54        /* Re-open the repository */
     55
     56        rc = tbarcfg_open(fname, &tbcfg);
     57        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     58
     59        tbarcfg_close(tbcfg);
     60        remove(fname);
    5961}
    6062
     
    6466        errno_t rc;
    6567        tbarcfg_t *tbcfg;
    66         smenu_entry_t *e;
    67         FILE *f;
    68         int rv;
    69 
    70         f = fopen("/tmp/test", "wt");
    71         PCUT_ASSERT_NOT_NULL(f);
    72 
    73         rv = fputs("[sif](){[entries](){"
    74             "[entry]([caption]=[A][cmd]=[a]){}"
    75             "[entry]([caption]=[B][cmd]=[b]){}"
    76             "}}", f);
    77         PCUT_ASSERT_TRUE(rv >= 0);
    78 
    79         rv = fclose(f);
    80         PCUT_ASSERT_INT_EQUALS(0, rv);
    81 
    82         rc = tbarcfg_open("/tmp/test", &tbcfg);
     68        char fname[L_tmpnam], *p;
     69        smenu_entry_t *e;
     70
     71        p = tmpnam(fname);
     72        PCUT_ASSERT_NOT_NULL(p);
     73
     74        rc = tbarcfg_create(fname, &tbcfg);
     75        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     76
     77        rc = smenu_entry_create(tbcfg, "A", "a");
     78        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     79
     80        rc = smenu_entry_create(tbcfg, "B", "b");
    8381        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    8482
     
    9189
    9290        tbarcfg_close(tbcfg);
     91        remove(fname);
    9392}
    9493
     
    9897        errno_t rc;
    9998        tbarcfg_t *tbcfg;
     99        char fname[L_tmpnam], *p;
    100100        smenu_entry_t *e;
    101101        const char *caption;
    102102        const char *cmd;
    103         FILE *f;
    104         int rv;
    105 
    106         f = fopen("/tmp/test", "wt");
    107         PCUT_ASSERT_NOT_NULL(f);
    108 
    109         rv = fputs("[sif](){[entries](){"
    110             "[entry]([caption]=[A][cmd]=[a]){}"
    111             "}}", f);
    112         PCUT_ASSERT_TRUE(rv >= 0);
    113 
    114         rv = fclose(f);
    115         PCUT_ASSERT_INT_EQUALS(0, rv);
    116 
    117         rc = tbarcfg_open("/tmp/test", &tbcfg);
     103
     104        p = tmpnam(fname);
     105        PCUT_ASSERT_NOT_NULL(p);
     106
     107        rc = tbarcfg_create(fname, &tbcfg);
     108        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     109
     110        rc = smenu_entry_create(tbcfg, "A", "a");
    118111        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    119112
     
    127120
    128121        tbarcfg_close(tbcfg);
     122        remove(fname);
     123}
     124
     125/** Setting menu entry properties */
     126PCUT_TEST(set_caption_cmd)
     127{
     128        errno_t rc;
     129        tbarcfg_t *tbcfg;
     130        char fname[L_tmpnam], *p;
     131        smenu_entry_t *e;
     132        const char *caption;
     133        const char *cmd;
     134
     135        p = tmpnam(fname);
     136        PCUT_ASSERT_NOT_NULL(p);
     137
     138        rc = tbarcfg_create(fname, &tbcfg);
     139        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     140
     141        rc = smenu_entry_create(tbcfg, "A", "a");
     142        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     143
     144        e = tbarcfg_smenu_first(tbcfg);
     145        PCUT_ASSERT_NOT_NULL(e);
     146
     147        caption = smenu_entry_get_caption(e);
     148        PCUT_ASSERT_STR_EQUALS("A", caption);
     149        cmd = smenu_entry_get_cmd(e);
     150        PCUT_ASSERT_STR_EQUALS("a", cmd);
     151
     152        /* Set properties */
     153        rc = smenu_entry_set_caption(e, "B");
     154        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     155        rc = smenu_entry_set_cmd(e, "b");
     156        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     157
     158        rc = smenu_entry_save(e);
     159        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     160
     161        /* Check that properties have been set */
     162        caption = smenu_entry_get_caption(e);
     163        PCUT_ASSERT_STR_EQUALS("B", caption);
     164        cmd = smenu_entry_get_cmd(e);
     165        PCUT_ASSERT_STR_EQUALS("b", cmd);
     166
     167        tbarcfg_close(tbcfg);
     168
     169        /* Re-open repository */
     170
     171        rc = tbarcfg_open(fname, &tbcfg);
     172        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     173
     174        e = tbarcfg_smenu_first(tbcfg);
     175        PCUT_ASSERT_NOT_NULL(e);
     176
     177        /* Check that new values of properties have persisted */
     178        caption = smenu_entry_get_caption(e);
     179        PCUT_ASSERT_STR_EQUALS("B", caption);
     180        cmd = smenu_entry_get_cmd(e);
     181        PCUT_ASSERT_STR_EQUALS("b", cmd);
     182
     183        tbarcfg_close(tbcfg);
     184        remove(fname);
     185}
     186
     187/** Create start menu entry */
     188PCUT_TEST(entry_create)
     189{
     190        errno_t rc;
     191        tbarcfg_t *tbcfg;
     192        char fname[L_tmpnam], *p;
     193        smenu_entry_t *e;
     194        const char *caption;
     195        const char *cmd;
     196
     197        p = tmpnam(fname);
     198        PCUT_ASSERT_NOT_NULL(p);
     199
     200        rc = tbarcfg_create(fname, &tbcfg);
     201        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     202
     203        rc = smenu_entry_create(tbcfg, "A", "a");
     204        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     205
     206        e = tbarcfg_smenu_first(tbcfg);
     207        PCUT_ASSERT_NOT_NULL(e);
     208
     209        caption = smenu_entry_get_caption(e);
     210        PCUT_ASSERT_STR_EQUALS("A", caption);
     211        cmd = smenu_entry_get_cmd(e);
     212        PCUT_ASSERT_STR_EQUALS("a", cmd);
     213
     214        tbarcfg_close(tbcfg);
     215        remove(fname);
    129216}
    130217
Note: See TracChangeset for help on using the changeset viewer.