Changeset 233a3a06 in mainline


Ignore:
Timestamp:
2018-08-02T12:03:39Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
42964a7
Parents:
7b87e1d
git-author:
Jiri Svoboda <jiri@…> (2018-08-01 18:03:17)
git-committer:
Jiri Svoboda <jiri@…> (2018-08-02 12:03:39)
Message:

Contacts listing and unmarshalling.

File:
1 edited

Legend:

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

    r7b87e1d r233a3a06  
    5858/** Contact entry */
    5959typedef struct {
    60         /** Link to contacts_t.entries */
     60        /** Containing contacts */
     61        contacts_t *contacts;
     62        /** Link to contacts->entries */
    6163        link_t lentries;
     64        /** SIF node for this entry */
     65        sif_node_t *nentry;
     66        /** Contact name */
     67        char *name;
    6268} contacts_entry_t;
    6369
     
    7177        ac_exit
    7278} contact_action_t;
     79
     80static errno_t contacts_unmarshal(sif_node_t *, contacts_t *);
    7381
    7482/** Open contacts repo or create it if it does not exist.
     
    139147                }
    140148
     149                rc = contacts_unmarshal(node, contacts);
     150                if (rc != EOK)
     151                        goto error;
     152
    141153                contacts->nentries = node;
    142154        }
     
    156168}
    157169
     170/** Unmarshal contact entries from SIF repository.
     171 *
     172 * @param nentries Entries node
     173 * @param contacts Contacts object to unmarshal to
     174 * @return EOK on success or error code
     175 */
     176static errno_t contacts_unmarshal(sif_node_t *nentries, contacts_t *contacts)
     177{
     178        sif_node_t *nentry;
     179        contacts_entry_t *entry;
     180        const char *name;
     181
     182        contacts->nentries = nentries;
     183
     184        nentry = sif_node_first_child(nentries);
     185        while (nentry != NULL) {
     186                if (str_cmp(sif_node_get_type(nentry), "entry") != 0)
     187                        return EIO;
     188
     189                entry = calloc(1, sizeof(contacts_entry_t));
     190                if (entry == NULL)
     191                        return ENOMEM;
     192
     193                name = sif_node_get_attr(nentry, "name");
     194                if (name == NULL) {
     195                        free(entry);
     196                        return EIO;
     197                }
     198
     199                entry->name = str_dup(name);
     200                if (entry->name == NULL) {
     201                        free(entry);
     202                        return ENOMEM;
     203                }
     204
     205                entry->contacts = contacts;
     206                entry->nentry = nentry;
     207                list_append(&entry->lentries, &contacts->entries);
     208
     209                nentry = sif_node_next_child(nentry);
     210        }
     211
     212        return EOK;
     213}
     214
     215
    158216/** Interaction to create new contact.
    159217 *
     
    165223        sif_trans_t *trans = NULL;
    166224        sif_node_t *nentry;
     225        contacts_entry_t *entry = NULL;
    167226        errno_t rc;
    168227        char *cname = NULL;
     
    182241                goto error;
    183242
     243        entry = calloc(1, sizeof(contacts_entry_t));
     244        if (entry == NULL) {
     245                rc = ENOMEM;
     246                goto error;
     247        }
     248
    184249        rc = sif_trans_begin(contacts->repo, &trans);
    185250        if (rc != EOK)
     
    199264
    200265        trans = NULL;
    201 
    202         free(cname);
     266        entry->contacts = contacts;
     267        entry->nentry = nentry;
     268        entry->name = cname;
     269        list_append(&entry->lentries, &contacts->entries);
     270
    203271        tinput_destroy(tinput);
    204272        return EOK;
     
    208276        if (cname != NULL)
    209277                free(cname);
     278        if (entry != NULL)
     279                free(entry);
    210280        return rc;
    211281}
     
    229299        sif_close(contacts->repo);
    230300        free(contacts);
     301}
     302
     303/** Get first contacts entry.
     304 *
     305 * @param contacts Contacts
     306 * @return First entry or @c NULL if there is none
     307 */
     308static contacts_entry_t *contacts_first(contacts_t *contacts)
     309{
     310        link_t *link;
     311
     312        link = list_first(&contacts->entries);
     313        if (link == NULL)
     314                return NULL;
     315
     316        return list_get_instance(link, contacts_entry_t, lentries);
     317}
     318
     319/** Get next contacts entry.
     320 *
     321 * @param cur Current entry
     322 * @return Next entry or @c NULL if there is none
     323 */
     324static contacts_entry_t *contacts_next(contacts_entry_t *cur)
     325{
     326        link_t *link;
     327
     328        link = list_next(&cur->lentries, &cur->contacts->entries);
     329        if (link == NULL)
     330                return NULL;
     331
     332        return list_get_instance(link, contacts_entry_t, lentries);
     333}
     334
     335/** List all contacts.
     336 *
     337 * @param contacts Contacts
     338 */
     339static void contacts_list_all(contacts_t *contacts)
     340{
     341        contacts_entry_t *entry;
     342
     343        entry = contacts_first(contacts);
     344        while (entry != NULL) {
     345                printf(" * %s\n", entry->name);
     346                entry = contacts_next(entry);
     347        }
    231348}
    232349
     
    282399
    283400        while (!quit) {
     401                contacts_list_all(contacts);
     402
    284403                rc = nchoice_get(choice, &sel);
    285404                if (rc != EOK) {
Note: See TracChangeset for help on using the changeset viewer.