Changes in uspace/lib/tbarcfg/src/tbarcfg.c [e63e74a:f87ff8e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/tbarcfg/src/tbarcfg.c
re63e74a rf87ff8e 1 1 /* 2 * Copyright (c) 202 4Jiri Svoboda2 * Copyright (c) 2023 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 108 108 sif_node_t *nentry; 109 109 const char *ntype; 110 const char *separator;111 110 const char *caption; 112 111 const char *cmd; 113 const char *terminal = NULL;114 112 errno_t rc; 115 113 … … 144 142 } 145 143 146 separator = sif_node_get_attr(nentry, "separator");147 if ( separator != NULL && str_cmp(separator, "y") != 0) {144 caption = sif_node_get_attr(nentry, "caption"); 145 if (caption == NULL) { 148 146 rc = EIO; 149 147 goto error; 150 148 } 151 149 152 if (separator == NULL) { 153 caption = sif_node_get_attr(nentry, "caption"); 154 if (caption == NULL) { 155 rc = EIO; 156 goto error; 157 } 158 159 cmd = sif_node_get_attr(nentry, "cmd"); 160 if (cmd == NULL) { 161 rc = EIO; 162 goto error; 163 } 164 165 terminal = sif_node_get_attr(nentry, "terminal"); 166 if (terminal == NULL) 167 terminal = "n"; 168 169 rc = smenu_entry_new(tbcfg, nentry, caption, cmd, 170 str_cmp(terminal, "y") == 0, NULL); 171 if (rc != EOK) 172 goto error; 173 } else { 174 rc = smenu_entry_sep_new(tbcfg, nentry, NULL); 175 if (rc != EOK) 176 goto error; 150 cmd = sif_node_get_attr(nentry, "cmd"); 151 if (cmd == NULL) { 152 rc = EIO; 153 goto error; 177 154 } 155 156 rc = smenu_entry_new(tbcfg, nentry, caption, cmd); 157 if (rc != EOK) 158 goto error; 178 159 179 160 nentry = sif_node_next_child(nentry); … … 240 221 } 241 222 242 /** Get last start menu entry.243 *244 * @param tbcfg Taskbar configuration245 * @return Previous entry or @c NULL if the menu is empty246 */247 smenu_entry_t *tbarcfg_smenu_last(tbarcfg_t *tbcfg)248 {249 link_t *link;250 251 link = list_last(&tbcfg->entries);252 if (link == NULL)253 return NULL;254 255 return list_get_instance(link, smenu_entry_t, lentries);256 }257 258 /** Get previous start menu entry.259 *260 * @param cur Current entry261 * @return Previous entry or @c NULL if @a cur is the last entry262 */263 smenu_entry_t *tbarcfg_smenu_prev(smenu_entry_t *cur)264 {265 link_t *link;266 267 link = list_prev(&cur->lentries, &cur->smenu->entries);268 if (link == NULL)269 return NULL;270 271 return list_get_instance(link, smenu_entry_t, lentries);272 }273 274 223 /** Get start menu entry caption. 275 224 * … … 279 228 const char *smenu_entry_get_caption(smenu_entry_t *entry) 280 229 { 281 assert(!entry->separator);282 230 return entry->caption; 283 231 } … … 285 233 /** Get start menu entry command. 286 234 * 287 * @param entr yStart menu entry235 * @param entr Start menu entry 288 236 * @return Command to run 289 237 */ 290 238 const char *smenu_entry_get_cmd(smenu_entry_t *entry) 291 239 { 292 assert(!entry->separator);293 240 return entry->cmd; 294 }295 296 /** Get start menu entry start in terminal flag.297 *298 * @param entry Start menu entry299 * @return Start in terminal flag300 */301 bool smenu_entry_get_terminal(smenu_entry_t *entry)302 {303 assert(!entry->separator);304 return entry->terminal;305 }306 307 /** Get start menu entry separator flag.308 *309 * @param entry Start menu entry310 * @return Separator flag311 */312 bool smenu_entry_get_separator(smenu_entry_t *entry)313 {314 return entry->separator;315 241 } 316 242 … … 327 253 { 328 254 char *dcap; 329 330 assert(!entry->separator);331 255 332 256 dcap = str_dup(caption); … … 352 276 char *dcmd; 353 277 354 assert(!entry->separator);355 356 278 dcmd = str_dup(cmd); 357 279 if (dcmd == NULL) … … 363 285 } 364 286 365 /** Set start menu entry start in terminal flag.366 *367 * Note: To make the change visible to others and persistent,368 * you must call @c smenu_entry_save()369 *370 * @param entry Start menu entry371 * @param terminal Start in terminal flag372 */373 void smenu_entry_set_terminal(smenu_entry_t *entry, bool terminal)374 {375 assert(!entry->separator);376 entry->terminal = terminal;377 }378 379 /** Save start menu entry using transaction.380 *381 * @param entry Start menu entry382 * @param trans Transaction383 */384 static errno_t smenu_entry_save_trans(smenu_entry_t *entry, sif_trans_t *trans)385 {386 errno_t rc;387 388 if (entry->separator) {389 rc = sif_node_set_attr(trans, entry->nentry, "separator", "y");390 if (rc != EOK)391 goto error;392 } else {393 sif_node_unset_attr(trans, entry->nentry, "separator");394 395 rc = sif_node_set_attr(trans, entry->nentry, "cmd", entry->cmd);396 if (rc != EOK)397 goto error;398 399 rc = sif_node_set_attr(trans, entry->nentry, "caption",400 entry->caption);401 if (rc != EOK)402 goto error;403 404 rc = sif_node_set_attr(trans, entry->nentry, "terminal",405 entry->terminal ? "y" : "n");406 if (rc != EOK)407 goto error;408 }409 410 return EOK;411 error:412 return rc;413 }414 415 287 /** Save any changes to start menu entry. 416 288 * … … 426 298 goto error; 427 299 428 rc = smenu_entry_save_trans(entry, trans); 300 rc = sif_node_set_attr(trans, entry->nentry, "cmd", entry->cmd); 301 if (rc != EOK) 302 goto error; 303 304 rc = sif_node_set_attr(trans, entry->nentry, "caption", entry->caption); 429 305 if (rc != EOK) 430 306 goto error; … … 449 325 * @param caption Caption 450 326 * @param cmd Command to run 451 * @param terminal Start in terminal452 * @param rentry Place to store pointer to new entry or @c NULL453 327 */ 454 328 errno_t smenu_entry_new(tbarcfg_t *smenu, sif_node_t *nentry, 455 const char *caption, const char *cmd , bool terminal, smenu_entry_t **rentry)329 const char *caption, const char *cmd) 456 330 { 457 331 smenu_entry_t *entry; … … 478 352 } 479 353 480 entry->terminal = terminal;481 482 354 entry->smenu = smenu; 483 355 list_append(&entry->lentries, &smenu->entries); 484 if (rentry != NULL)485 *rentry = entry;486 356 return EOK; 487 357 error: … … 497 367 } 498 368 499 /** Allocate a start menu separator entry and append it to the start menu500 * (internal).501 *502 * This only creates the entry in memory, but does not update the repository.503 *504 * @param smenu Start menu505 * @param nentry Backing SIF node506 * @param rentry Place to store pointer to new entry or @c NULL507 */508 errno_t smenu_entry_sep_new(tbarcfg_t *smenu, sif_node_t *nentry,509 smenu_entry_t **rentry)510 {511 smenu_entry_t *entry;512 errno_t rc;513 514 entry = calloc(1, sizeof(smenu_entry_t));515 if (entry == NULL) {516 rc = ENOMEM;517 goto error;518 }519 520 entry->nentry = nentry;521 entry->separator = true;522 523 entry->smenu = smenu;524 list_append(&entry->lentries, &smenu->entries);525 if (rentry != NULL)526 *rentry = entry;527 528 return EOK;529 error:530 return rc;531 }532 533 369 /** Delete start menu entry. 534 370 * … … 541 377 { 542 378 list_remove(&entry->lentries); 543 if (entry->caption != NULL) 544 free(entry->caption); 545 if (entry->cmd != NULL) 546 free(entry->cmd); 379 free(entry->caption); 380 free(entry->cmd); 547 381 free(entry); 548 382 } … … 554 388 * @param caption Caption 555 389 * @param cmd Command to run 556 * @param terminal Start in terminal557 * @param rentry Place to store pointer to new entry or @c NULL558 390 */ 559 391 errno_t smenu_entry_create(tbarcfg_t *smenu, const char *caption, 560 const char *cmd , bool terminal, smenu_entry_t **rentry)392 const char *cmd) 561 393 { 562 394 sif_node_t *nentry; 563 smenu_entry_t *entry;564 395 errno_t rc; 565 396 sif_trans_t *trans = NULL; … … 582 413 goto error; 583 414 584 rc = sif_node_set_attr(trans, nentry, "terminal", terminal ? "y" : "n"); 585 if (rc != EOK) 586 goto error; 587 588 rc = smenu_entry_new(smenu, nentry, caption, cmd, terminal, &entry); 415 rc = smenu_entry_new(smenu, nentry, caption, cmd); 589 416 if (rc != EOK) 590 417 goto error; … … 594 421 goto error; 595 422 596 if (rentry != NULL)597 *rentry = entry;598 423 return EOK; 599 424 error: … … 603 428 } 604 429 605 /** Create new start menu separator entry. 606 * 607 * @param smenu Start menu 608 * @param nentry Backing SIF node 609 * @param rentry Place to store pointer to new entry or @c NULL 610 */ 611 errno_t smenu_entry_sep_create(tbarcfg_t *smenu, smenu_entry_t **rentry) 612 { 613 sif_node_t *nentry; 614 smenu_entry_t *entry; 430 /** Destroy start menu entry.. 431 * 432 * @param entry Start menu entry 433 * @return EOK on success or an error code 434 */ 435 errno_t smenu_entry_destroy(smenu_entry_t *entry) 436 { 615 437 errno_t rc; 616 438 sif_trans_t *trans = NULL; 617 439 618 rc = sif_trans_begin(smenu->repo, &trans); 619 if (rc != EOK) 620 goto error; 621 622 rc = sif_node_append_child(trans, smenu->nentries, "entry", 623 &nentry); 624 if (rc != EOK) 625 goto error; 626 627 rc = sif_node_set_attr(trans, nentry, "separator", "y"); 628 if (rc != EOK) 629 goto error; 630 631 rc = smenu_entry_sep_new(smenu, nentry, &entry); 632 if (rc != EOK) 633 goto error; 440 rc = sif_trans_begin(entry->smenu->repo, &trans); 441 if (rc != EOK) 442 goto error; 443 444 sif_node_destroy(trans, entry->nentry); 634 445 635 446 rc = sif_trans_end(trans); … … 637 448 goto error; 638 449 639 if (rentry != NULL) 640 *rentry = entry; 450 smenu_entry_delete(entry); 641 451 return EOK; 642 452 error: … … 646 456 } 647 457 648 /** Destroy start menu entry.649 *650 * @param entry Start menu entry651 * @return EOK on success or an error code652 */653 errno_t smenu_entry_destroy(smenu_entry_t *entry)654 {655 errno_t rc;656 sif_trans_t *trans = NULL;657 658 rc = sif_trans_begin(entry->smenu->repo, &trans);659 if (rc != EOK)660 goto error;661 662 sif_node_destroy(trans, entry->nentry);663 664 rc = sif_trans_end(trans);665 if (rc != EOK)666 goto error;667 668 smenu_entry_delete(entry);669 return EOK;670 error:671 if (trans != NULL)672 sif_trans_abort(trans);673 return rc;674 }675 676 /** Move start menu entry up.677 *678 * @param entry Start menu entry679 * @return EOK on success or an error code680 */681 errno_t smenu_entry_move_up(smenu_entry_t *entry)682 {683 errno_t rc;684 sif_trans_t *trans = NULL;685 sif_node_t *nnode = NULL;686 sif_node_t *old_node;687 smenu_entry_t *prev;688 689 rc = sif_trans_begin(entry->smenu->repo, &trans);690 if (rc != EOK)691 goto error;692 693 prev = tbarcfg_smenu_prev(entry);694 if (prev == NULL) {695 /* Entry is already at first position, nothing to do. */696 return EOK;697 }698 699 rc = sif_node_insert_before(trans, prev->nentry, "entry", &nnode);700 if (rc != EOK)701 goto error;702 703 old_node = entry->nentry;704 entry->nentry = nnode;705 706 rc = smenu_entry_save_trans(entry, trans);707 if (rc != EOK) {708 entry->nentry = old_node;709 goto error;710 }711 712 sif_node_destroy(trans, old_node);713 714 rc = sif_trans_end(trans);715 if (rc != EOK) {716 entry->nentry = old_node;717 goto error;718 }719 720 list_remove(&entry->lentries);721 list_insert_before(&entry->lentries, &prev->lentries);722 return EOK;723 error:724 if (nnode != NULL)725 sif_node_destroy(trans, nnode);726 if (trans != NULL)727 sif_trans_abort(trans);728 return rc;729 }730 731 /** Move start menu entry down.732 *733 * @param entry Start menu entry734 * @return EOK on success or an error code735 */736 errno_t smenu_entry_move_down(smenu_entry_t *entry)737 {738 errno_t rc;739 sif_trans_t *trans = NULL;740 sif_node_t *nnode = NULL;741 sif_node_t *old_node;742 smenu_entry_t *next;743 744 rc = sif_trans_begin(entry->smenu->repo, &trans);745 if (rc != EOK)746 goto error;747 748 next = tbarcfg_smenu_next(entry);749 if (next == NULL) {750 /* Entry is already at last position, nothing to do. */751 return EOK;752 }753 754 rc = sif_node_insert_after(trans, next->nentry, "entry", &nnode);755 if (rc != EOK)756 goto error;757 758 old_node = entry->nentry;759 entry->nentry = nnode;760 761 rc = smenu_entry_save_trans(entry, trans);762 if (rc != EOK) {763 entry->nentry = old_node;764 goto error;765 }766 767 sif_node_destroy(trans, old_node);768 769 rc = sif_trans_end(trans);770 if (rc != EOK) {771 entry->nentry = old_node;772 goto error;773 }774 775 list_remove(&entry->lentries);776 list_insert_after(&entry->lentries, &next->lentries);777 return EOK;778 error:779 if (nnode != NULL)780 sif_node_destroy(trans, nnode);781 if (trans != NULL)782 sif_trans_abort(trans);783 return rc;784 }785 786 458 /** @} 787 459 */
Note:
See TracChangeset
for help on using the changeset viewer.