Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/tbarcfg/src/tbarcfg.c

    rbff8619 ree3b28a9  
    4747
    4848static void tbarcfg_notify_conn(ipc_call_t *, void *);
    49 static errno_t smenu_entry_save(smenu_entry_t *, sif_node_t *);
    5049
    5150/** Create taskbar configuration.
     
    5857{
    5958        tbarcfg_t *tbcfg;
    60         sif_doc_t *doc = NULL;
     59        sif_sess_t *repo = NULL;
    6160        sif_node_t *rnode;
    62         sif_node_t *nentries;
    63         errno_t rc;
     61        errno_t rc;
     62        sif_trans_t *trans = NULL;
    6463
    6564        tbcfg = calloc(1, sizeof(tbarcfg_t));
     
    7069
    7170        list_initialize(&tbcfg->entries);
    72         tbcfg->cfgpath = str_dup(repopath);
    73         if (tbcfg->cfgpath == NULL) {
    74                 rc = ENOMEM;
    75                 goto error;
    76         }
    77 
    78         rc = sif_new(&doc);
    79         if (rc != EOK)
    80                 goto error;
    81 
    82         rnode = sif_get_root(doc);
    83 
    84         rc = sif_node_append_child(rnode, "entries", &nentries);
    85         if (rc != EOK)
    86                 goto error;
    87 
    88         (void)nentries;
    89 
    90         rc = sif_save(doc, repopath);
    91         if (rc != EOK)
    92                 goto error;
    93 
    94         sif_delete(doc);
     71
     72        rc = sif_create(repopath, &repo);
     73        if (rc != EOK)
     74                goto error;
     75
     76        tbcfg->repo = repo;
     77
     78        rnode = sif_get_root(repo);
     79
     80        rc = sif_trans_begin(repo, &trans);
     81        if (rc != EOK)
     82                goto error;
     83
     84        rc = sif_node_append_child(trans, rnode, "entries", &tbcfg->nentries);
     85        if (rc != EOK)
     86                goto error;
     87
     88        rc = sif_trans_end(trans);
     89        if (rc != EOK)
     90                goto error;
    9591
    9692        *rtbcfg = tbcfg;
    9793        return EOK;
    9894error:
    99         if (doc != NULL)
    100                 sif_delete(doc);
    101         if (tbcfg != NULL && tbcfg->cfgpath != NULL)
    102                 free(tbcfg->cfgpath);
     95        if (trans != NULL)
     96                sif_trans_abort(trans);
     97        if (repo != NULL)
     98                sif_close(repo);
    10399        if (tbcfg != NULL)
    104100                free(tbcfg);
     
    115111{
    116112        tbarcfg_t *tbcfg;
    117         sif_doc_t *doc = NULL;
    118         sif_node_t *nentries;
     113        sif_sess_t *repo = NULL;
    119114        sif_node_t *rnode;
    120115        sif_node_t *nentry;
     
    133128
    134129        list_initialize(&tbcfg->entries);
    135         tbcfg->cfgpath = str_dup(repopath);
    136         if (tbcfg->cfgpath == NULL) {
    137                 rc = ENOMEM;
    138                 goto error;
    139         }
    140 
    141         rc = sif_load(repopath, &doc);
    142         if (rc != EOK)
    143                 goto error;
    144 
    145         rnode = sif_get_root(doc);
    146         nentries = sif_node_first_child(rnode);
    147         ntype = sif_node_get_type(nentries);
     130
     131        rc = sif_open(repopath, &repo);
     132        if (rc != EOK)
     133                goto error;
     134
     135        tbcfg->repo = repo;
     136
     137        rnode = sif_get_root(repo);
     138        tbcfg->nentries = sif_node_first_child(rnode);
     139        ntype = sif_node_get_type(tbcfg->nentries);
    148140        if (str_cmp(ntype, "entries") != 0) {
    149141                rc = EIO;
     
    151143        }
    152144
    153         nentry = sif_node_first_child(nentries);
     145        nentry = sif_node_first_child(tbcfg->nentries);
    154146        while (nentry != NULL) {
    155147                ntype = sif_node_get_type(nentry);
     
    182174                                terminal = "n";
    183175
    184                         rc = smenu_entry_create(tbcfg, caption, cmd,
     176                        rc = smenu_entry_new(tbcfg, nentry, caption, cmd,
    185177                            str_cmp(terminal, "y") == 0, NULL);
    186178                        if (rc != EOK)
    187179                                goto error;
    188180                } else {
    189                         rc = smenu_entry_sep_create(tbcfg, NULL);
     181                        rc = smenu_entry_sep_new(tbcfg, nentry, NULL);
    190182                        if (rc != EOK)
    191183                                goto error;
     
    195187        }
    196188
    197         sif_delete(doc);
    198189        *rtbcfg = tbcfg;
    199190        return EOK;
    200191error:
    201         if (doc != NULL)
    202                 sif_delete(doc);
    203         if (tbcfg != NULL && tbcfg->cfgpath != NULL)
    204                 free(tbcfg->cfgpath);
     192        if (repo != NULL)
     193                sif_close(repo);
    205194        if (tbcfg != NULL)
    206195                free(tbcfg);
     
    218207        entry = tbarcfg_smenu_first(tbcfg);
    219208        while (entry != NULL) {
    220                 smenu_entry_destroy(entry);
     209                smenu_entry_delete(entry);
    221210                entry = tbarcfg_smenu_first(tbcfg);
    222211        }
    223212
    224         free(tbcfg->cfgpath);
     213        (void)sif_close(tbcfg->repo);
    225214        free(tbcfg);
    226 }
    227 
    228 /** Synchronize taskbar configuration to config file.
    229  *
    230  * @param repopath Pathname of the menu repository
    231  * @param rtbcfg Place to store pointer to taskbar configuration
    232  * @return EOK on success or an error code
    233  */
    234 errno_t tbarcfg_sync(tbarcfg_t *tbcfg)
    235 {
    236         sif_doc_t *doc = NULL;
    237         sif_node_t *nentries;
    238         sif_node_t *rnode;
    239         smenu_entry_t *entry;
    240         errno_t rc;
    241 
    242         rc = sif_new(&doc);
    243         if (rc != EOK)
    244                 goto error;
    245 
    246         rnode = sif_get_root(doc);
    247 
    248         rc = sif_node_append_child(rnode, "entries", &nentries);
    249         if (rc != EOK)
    250                 goto error;
    251 
    252         entry = tbarcfg_smenu_first(tbcfg);
    253         while (entry != NULL) {
    254                 rc = smenu_entry_save(entry, nentries);
    255                 if (rc != EOK)
    256                         goto error;
    257 
    258                 entry = tbarcfg_smenu_next(entry);
    259         }
    260 
    261         rc = sif_save(doc, tbcfg->cfgpath);
    262         if (rc != EOK)
    263                 goto error;
    264 
    265         sif_delete(doc);
    266         return EOK;
    267 error:
    268         if (doc != NULL)
    269                 sif_delete(doc);
    270         if (tbcfg != NULL && tbcfg->cfgpath != NULL)
    271                 free(tbcfg->cfgpath);
    272         if (tbcfg != NULL)
    273                 free(tbcfg);
    274         return rc;
    275215}
    276216
     
    385325 *
    386326 * Note: To make the change visible to others and persistent,
    387  * you must call @c tbarcfg_sync()
     327 * you must call @c smenu_entry_save()
    388328 *
    389329 * @param entry Start menu entry
     
    409349 *
    410350 * Note: To make the change visible to others and persistent,
    411  * you must call @c tbarcfg_sync()
     351 * you must call @c smenu_entry_save()
    412352 *
    413353 * @param entry Start menu entry
     
    433373 *
    434374 * Note: To make the change visible to others and persistent,
    435  * you must call @c tbarcfg_sync()
     375 * you must call @c smenu_entry_save()
    436376 *
    437377 * @param entry Start menu entry
     
    447387 *
    448388 * @param entry Start menu entry
    449  * @param nentries Entries node
    450  */
    451 static errno_t smenu_entry_save(smenu_entry_t *entry, sif_node_t *nentries)
    452 {
    453         sif_node_t *nentry = NULL;
    454         errno_t rc;
    455 
    456         rc = sif_node_append_child(nentries, "entry", &nentry);
    457         if (rc != EOK)
    458                 goto error;
     389 * @param trans Transaction
     390 */
     391static errno_t smenu_entry_save_trans(smenu_entry_t *entry, sif_trans_t *trans)
     392{
     393        errno_t rc;
    459394
    460395        if (entry->separator) {
    461                 rc = sif_node_set_attr(nentry, "separator", "y");
     396                rc = sif_node_set_attr(trans, entry->nentry, "separator", "y");
    462397                if (rc != EOK)
    463398                        goto error;
    464399        } else {
    465                 rc = sif_node_set_attr(nentry, "cmd", entry->cmd);
     400                sif_node_unset_attr(trans, entry->nentry, "separator");
     401
     402                rc = sif_node_set_attr(trans, entry->nentry, "cmd", entry->cmd);
    466403                if (rc != EOK)
    467404                        goto error;
    468405
    469                 rc = sif_node_set_attr(nentry, "caption",
     406                rc = sif_node_set_attr(trans, entry->nentry, "caption",
    470407                    entry->caption);
    471408                if (rc != EOK)
    472409                        goto error;
    473410
    474                 rc = sif_node_set_attr(nentry, "terminal",
     411                rc = sif_node_set_attr(trans, entry->nentry, "terminal",
    475412                    entry->terminal ? "y" : "n");
    476413                if (rc != EOK)
     
    480417        return EOK;
    481418error:
    482         if (nentry != NULL)
    483                 sif_node_destroy(nentry);
    484         return rc;
    485 }
    486 
    487 /** Create new start menu entry and append it to the start menu (internal).
     419        return rc;
     420}
     421
     422/** Save any changes to start menu entry.
     423 *
     424 * @param entry Start menu entry
     425 */
     426errno_t smenu_entry_save(smenu_entry_t *entry)
     427{
     428        sif_trans_t *trans = NULL;
     429        errno_t rc;
     430
     431        rc = sif_trans_begin(entry->smenu->repo, &trans);
     432        if (rc != EOK)
     433                goto error;
     434
     435        rc = smenu_entry_save_trans(entry, trans);
     436        if (rc != EOK)
     437                goto error;
     438
     439        rc = sif_trans_end(trans);
     440        if (rc != EOK)
     441                goto error;
     442
     443        return EOK;
     444error:
     445        if (trans != NULL)
     446                sif_trans_abort(trans);
     447        return rc;
     448}
     449
     450/** Allocate a start menu entry and append it to the start menu (internal).
    488451 *
    489452 * This only creates the entry in memory, but does not update the repository.
    490453 *
    491454 * @param smenu Start menu
     455 * @param nentry Backing SIF node
    492456 * @param caption Caption
    493457 * @param cmd Command to run
     
    495459 * @param rentry Place to store pointer to new entry or @c NULL
    496460 */
    497 errno_t smenu_entry_create(tbarcfg_t *smenu, const char *caption,
    498     const char *cmd, bool terminal, smenu_entry_t **rentry)
     461errno_t smenu_entry_new(tbarcfg_t *smenu, sif_node_t *nentry,
     462    const char *caption, const char *cmd, bool terminal, smenu_entry_t **rentry)
    499463{
    500464        smenu_entry_t *entry;
     
    506470                goto error;
    507471        }
     472
     473        entry->nentry = nentry;
    508474
    509475        entry->caption = str_dup(caption);
     
    538504}
    539505
    540 /** Create new start menu separator entry and append it to the start menu
     506/** Allocate a start menu separator entry and append it to the start menu
    541507 * (internal).
    542508 *
     
    544510 *
    545511 * @param smenu Start menu
     512 * @param nentry Backing SIF node
    546513 * @param rentry Place to store pointer to new entry or @c NULL
    547514 */
    548 errno_t smenu_entry_sep_create(tbarcfg_t *smenu, smenu_entry_t **rentry)
     515errno_t smenu_entry_sep_new(tbarcfg_t *smenu, sif_node_t *nentry,
     516    smenu_entry_t **rentry)
    549517{
    550518        smenu_entry_t *entry;
     
    557525        }
    558526
     527        entry->nentry = nentry;
    559528        entry->separator = true;
    560529
     
    569538}
    570539
    571 /** Destroy start menu entry.
     540/** Delete start menu entry.
    572541 *
    573542 * This only deletes the entry from, but does not update the
     
    576545 * @param entry Start menu entry
    577546 */
    578 void smenu_entry_destroy(smenu_entry_t *entry)
     547void smenu_entry_delete(smenu_entry_t *entry)
    579548{
    580549        list_remove(&entry->lentries);
     
    586555}
    587556
     557/** Create new start menu entry.
     558 *
     559 * @param smenu Start menu
     560 * @param nentry Backing SIF node
     561 * @param caption Caption
     562 * @param cmd Command to run
     563 * @param terminal Start in terminal
     564 * @param rentry Place to store pointer to new entry or @c NULL
     565 */
     566errno_t smenu_entry_create(tbarcfg_t *smenu, const char *caption,
     567    const char *cmd, bool terminal, smenu_entry_t **rentry)
     568{
     569        sif_node_t *nentry;
     570        smenu_entry_t *entry;
     571        errno_t rc;
     572        sif_trans_t *trans = NULL;
     573
     574        rc = sif_trans_begin(smenu->repo, &trans);
     575        if (rc != EOK)
     576                goto error;
     577
     578        rc = sif_node_append_child(trans, smenu->nentries, "entry",
     579            &nentry);
     580        if (rc != EOK)
     581                goto error;
     582
     583        rc = sif_node_set_attr(trans, nentry, "cmd", cmd);
     584        if (rc != EOK)
     585                goto error;
     586
     587        rc = sif_node_set_attr(trans, nentry, "caption", caption);
     588        if (rc != EOK)
     589                goto error;
     590
     591        rc = sif_node_set_attr(trans, nentry, "terminal", terminal ? "y" : "n");
     592        if (rc != EOK)
     593                goto error;
     594
     595        rc = smenu_entry_new(smenu, nentry, caption, cmd, terminal, &entry);
     596        if (rc != EOK)
     597                goto error;
     598
     599        rc = sif_trans_end(trans);
     600        if (rc != EOK)
     601                goto error;
     602
     603        if (rentry != NULL)
     604                *rentry = entry;
     605        return EOK;
     606error:
     607        if (trans != NULL)
     608                sif_trans_abort(trans);
     609        return rc;
     610}
     611
     612/** Create new start menu separator entry.
     613 *
     614 * @param smenu Start menu
     615 * @param nentry Backing SIF node
     616 * @param rentry Place to store pointer to new entry or @c NULL
     617 */
     618errno_t smenu_entry_sep_create(tbarcfg_t *smenu, smenu_entry_t **rentry)
     619{
     620        sif_node_t *nentry;
     621        smenu_entry_t *entry;
     622        errno_t rc;
     623        sif_trans_t *trans = NULL;
     624
     625        rc = sif_trans_begin(smenu->repo, &trans);
     626        if (rc != EOK)
     627                goto error;
     628
     629        rc = sif_node_append_child(trans, smenu->nentries, "entry",
     630            &nentry);
     631        if (rc != EOK)
     632                goto error;
     633
     634        rc = sif_node_set_attr(trans, nentry, "separator", "y");
     635        if (rc != EOK)
     636                goto error;
     637
     638        rc = smenu_entry_sep_new(smenu, nentry, &entry);
     639        if (rc != EOK)
     640                goto error;
     641
     642        rc = sif_trans_end(trans);
     643        if (rc != EOK)
     644                goto error;
     645
     646        if (rentry != NULL)
     647                *rentry = entry;
     648        return EOK;
     649error:
     650        if (trans != NULL)
     651                sif_trans_abort(trans);
     652        return rc;
     653}
     654
     655/** Destroy start menu entry.
     656 *
     657 * @param entry Start menu entry
     658 * @return EOK on success or an error code
     659 */
     660errno_t smenu_entry_destroy(smenu_entry_t *entry)
     661{
     662        errno_t rc;
     663        sif_trans_t *trans = NULL;
     664
     665        rc = sif_trans_begin(entry->smenu->repo, &trans);
     666        if (rc != EOK)
     667                goto error;
     668
     669        sif_node_destroy(trans, entry->nentry);
     670
     671        rc = sif_trans_end(trans);
     672        if (rc != EOK)
     673                goto error;
     674
     675        smenu_entry_delete(entry);
     676        return EOK;
     677error:
     678        if (trans != NULL)
     679                sif_trans_abort(trans);
     680        return rc;
     681}
     682
    588683/** Move start menu entry up.
    589684 *
    590685 * @param entry Start menu entry
    591  */
    592 void smenu_entry_move_up(smenu_entry_t *entry)
    593 {
     686 * @return EOK on success or an error code
     687 */
     688errno_t smenu_entry_move_up(smenu_entry_t *entry)
     689{
     690        errno_t rc;
     691        sif_trans_t *trans = NULL;
     692        sif_node_t *nnode = NULL;
     693        sif_node_t *old_node;
    594694        smenu_entry_t *prev;
     695
     696        rc = sif_trans_begin(entry->smenu->repo, &trans);
     697        if (rc != EOK)
     698                goto error;
    595699
    596700        prev = tbarcfg_smenu_prev(entry);
    597701        if (prev == NULL) {
    598702                /* Entry is already at first position, nothing to do. */
    599                 return;
     703                return EOK;
     704        }
     705
     706        rc = sif_node_insert_before(trans, prev->nentry, "entry", &nnode);
     707        if (rc != EOK)
     708                goto error;
     709
     710        old_node = entry->nentry;
     711        entry->nentry = nnode;
     712
     713        rc = smenu_entry_save_trans(entry, trans);
     714        if (rc != EOK) {
     715                entry->nentry = old_node;
     716                goto error;
     717        }
     718
     719        sif_node_destroy(trans, old_node);
     720
     721        rc = sif_trans_end(trans);
     722        if (rc != EOK) {
     723                entry->nentry = old_node;
     724                goto error;
    600725        }
    601726
    602727        list_remove(&entry->lentries);
    603728        list_insert_before(&entry->lentries, &prev->lentries);
     729        return EOK;
     730error:
     731        if (nnode != NULL)
     732                sif_node_destroy(trans, nnode);
     733        if (trans != NULL)
     734                sif_trans_abort(trans);
     735        return rc;
    604736}
    605737
     
    607739 *
    608740 * @param entry Start menu entry
    609  */
    610 void smenu_entry_move_down(smenu_entry_t *entry)
    611 {
     741 * @return EOK on success or an error code
     742 */
     743errno_t smenu_entry_move_down(smenu_entry_t *entry)
     744{
     745        errno_t rc;
     746        sif_trans_t *trans = NULL;
     747        sif_node_t *nnode = NULL;
     748        sif_node_t *old_node;
    612749        smenu_entry_t *next;
     750
     751        rc = sif_trans_begin(entry->smenu->repo, &trans);
     752        if (rc != EOK)
     753                goto error;
    613754
    614755        next = tbarcfg_smenu_next(entry);
    615756        if (next == NULL) {
    616757                /* Entry is already at last position, nothing to do. */
    617                 return;
     758                return EOK;
     759        }
     760
     761        rc = sif_node_insert_after(trans, next->nentry, "entry", &nnode);
     762        if (rc != EOK)
     763                goto error;
     764
     765        old_node = entry->nentry;
     766        entry->nentry = nnode;
     767
     768        rc = smenu_entry_save_trans(entry, trans);
     769        if (rc != EOK) {
     770                entry->nentry = old_node;
     771                goto error;
     772        }
     773
     774        sif_node_destroy(trans, old_node);
     775
     776        rc = sif_trans_end(trans);
     777        if (rc != EOK) {
     778                entry->nentry = old_node;
     779                goto error;
    618780        }
    619781
    620782        list_remove(&entry->lentries);
    621783        list_insert_after(&entry->lentries, &next->lentries);
     784        return EOK;
     785error:
     786        if (nnode != NULL)
     787                sif_node_destroy(trans, nnode);
     788        if (trans != NULL)
     789                sif_trans_abort(trans);
     790        return rc;
    622791}
    623792
Note: See TracChangeset for help on using the changeset viewer.