Changeset b336bfd8 in mainline


Ignore:
Timestamp:
2025-02-06T20:42:14Z (6 days ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master
Children:
a7a16a2f
Parents:
accdf882
Message:

Start text editor if open action is used on .txt file.

Location:
uspace/app/nav
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nav/nav.c

    raccdf882 rb336bfd8  
    7070
    7171static void navigator_panel_activate_req(void *, panel_t *);
     72static void navigator_panel_file_open(void *, panel_t *, const char *);
    7273
    7374static panel_cb_t navigator_panel_cb = {
    74         .activate_req = navigator_panel_activate_req
     75        .activate_req = navigator_panel_activate_req,
     76        .file_open = navigator_panel_file_open
    7577};
    7678
     
    328330/** Open file in text editor.
    329331 *
    330  * @param panel Panel
     332 * @param navigator Navigator
    331333 * @param fname File name
    332334 *
     
    367369}
    368370
     371/** Execute file entry.
     372 *
     373 * @param navigator Navigator
     374 * @param fname File name
     375 *
     376 * @return EOK on success or an error code
     377 */
     378static errno_t navigator_exec_file(navigator_t *navigator, const char *fname)
     379{
     380        task_id_t id;
     381        task_wait_t wait;
     382        task_exit_t texit;
     383        int retval;
     384        errno_t rc;
     385
     386        /* Free up and clean console for the child task. */
     387        rc = ui_suspend(navigator->ui);
     388        if (rc != EOK)
     389                return rc;
     390
     391        rc = task_spawnl(&id, &wait, fname, fname, NULL);
     392        if (rc != EOK)
     393                goto error;
     394
     395        rc = task_wait(&wait, &texit, &retval);
     396        if ((rc != EOK) || (texit != TASK_EXIT_NORMAL))
     397                goto error;
     398
     399        /* Resume UI operation */
     400        rc = ui_resume(navigator->ui);
     401        if (rc != EOK)
     402                return rc;
     403
     404        (void) ui_paint(navigator->ui);
     405        return EOK;
     406error:
     407        (void) ui_resume(navigator->ui);
     408        (void) ui_paint(navigator->ui);
     409        return rc;
     410}
     411
     412/** Open panel file entry.
     413 *
     414 * Perform Open action on a file entry (based on extension).
     415 *
     416 * @param navigator Navigator
     417 * @param fname File name
     418 *
     419 * @return EOK on success or an error code
     420 */
     421static errno_t navigator_open_file(navigator_t *navigator, const char *fname)
     422{
     423        const char *ext;
     424
     425        ext = str_rchr(fname, '.');
     426        if (ext != NULL) {
     427                if (str_casecmp(ext, ".txt") == 0)
     428                        return navigator_edit_file(navigator, fname);
     429        }
     430
     431        return navigator_exec_file(navigator, fname);
     432}
     433
    369434/** File / Edit menu entry selected */
    370435static void navigator_file_edit(void *arg)
     
    403468}
    404469
     470/** Panel callback requesting file open.
     471 *
     472 * @param arg Argument (navigator_t *)
     473 * @param panel Panel
     474 * @param fname File name
     475 */
     476void navigator_panel_file_open(void *arg, panel_t *panel, const char *fname)
     477{
     478        navigator_t *navigator = (navigator_t *)arg;
     479
     480        (void)panel;
     481        navigator_open_file(navigator, fname);
     482}
     483
    405484/** @}
    406485 */
  • uspace/app/nav/panel.c

    raccdf882 rb336bfd8  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    377377}
    378378
    379 /** Open panel file entry.
    380  *
    381  * Perform Open action on a file entry (i.e. try running it).
    382  *
    383  * @param panel Panel
    384  * @param fname File name
    385  *
    386  * @return EOK on success or an error code
    387  */
    388 static errno_t panel_open_file(panel_t *panel, const char *fname)
    389 {
    390         task_id_t id;
    391         task_wait_t wait;
    392         task_exit_t texit;
    393         int retval;
    394         errno_t rc;
    395         ui_t *ui;
    396 
    397         ui = ui_window_get_ui(panel->window);
    398 
    399         /* Free up and clean console for the child task. */
    400         rc = ui_suspend(ui);
    401         if (rc != EOK)
    402                 return rc;
    403 
    404         rc = task_spawnl(&id, &wait, fname, fname, NULL);
    405         if (rc != EOK)
    406                 goto error;
    407 
    408         rc = task_wait(&wait, &texit, &retval);
    409         if ((rc != EOK) || (texit != TASK_EXIT_NORMAL))
    410                 goto error;
    411 
    412         /* Resume UI operation */
    413         rc = ui_resume(ui);
    414         if (rc != EOK)
    415                 return rc;
    416 
    417         (void) ui_paint(ui_window_get_ui(panel->window));
    418         return EOK;
    419 error:
    420         (void) ui_resume(ui);
    421         (void) ui_paint(ui_window_get_ui(panel->window));
    422         return rc;
    423 }
    424 
    425379/** File list in panel requests activation.
    426380 *
     
    446400        panel_t *panel = (panel_t *)arg;
    447401
    448         (void) panel_open_file(panel, fname);
     402        if (panel->cb != NULL && panel->cb->file_open != NULL)
     403                panel->cb->file_open(panel->cb_arg, panel, fname);
    449404}
    450405
  • uspace/app/nav/types/panel.h

    raccdf882 rb336bfd8  
    11/*
    2  * Copyright (c) 2022 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    8080        /** Request panel activation */
    8181        void (*activate_req)(void *, panel_t *);
     82        /** Open file */
     83        void (*file_open)(void *, panel_t *, const char *);
    8284} panel_cb_t;
    8385
Note: See TracChangeset for help on using the changeset viewer.