summaryrefslogtreecommitdiff
path: root/apps/plugins/shortcuts/shortcuts_append.c
blob: 0c4534a6b9de566065e93b1281502a298c05408e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Bryan Childs
 * Copyright (c) 2007 Alexander Levin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "shortcuts.h"




static bool append_entry_to_file(sc_file_t *file, char *path, bool is_dir)
{
    sc_entry_t entry;

    unsigned int required_len = rb->strlen(path);
    if (is_dir) {
        required_len += PATH_SEPARATOR_LEN; /* Add 1 for the trailing / */
    }
    if (required_len >= sizeof(entry.path)) {
        /* no attempt to print it: it will also be so too long for the splash */
        rb->splash(HZ*2, "Can't append shortcut, it's too long");
        return false;
    }
    entry.explicit_disp = false;
    rb->strcpy(entry.path, path);
    if (is_dir) {
        rb->strcat(entry.path, PATH_SEPARATOR);
    }
    if (!append_entry(file, &entry)) {
        rb->splash(HZ*2, "Too many entries!");
        return false;
    }
    return true;
}


enum plugin_status plugin_start(const void* void_parameter)
{
    bool found;
    bool its_a_dir;
    
    /* This is a viewer, so a parameter must have been specified */
    if (void_parameter == NULL) {
        rb->splash(HZ*2, "No parameter specified!");
        return PLUGIN_ERROR;
    }
    char *parameter = (char*)void_parameter;
    DEBUGF("Trying to append '%s' to the default link file '%s'...\n",
            parameter, SHORTCUTS_FILENAME);

    allocate_memory(&memory_buf, &memory_bufsize);

    /* Determine if it's a file or a directory. First check
     * if it's a dir and then file (not vice versa) since
     * open() can also open a dir */
    found = true;
    if (rb->dir_exists(parameter)) {
        its_a_dir = true;
    } else if (rb->file_exists(parameter)) {
        its_a_dir = false;
    } else {
        found = false;
    }
    /* now we know if it's a file or a directory
     * (or something went wrong) */

    if (!found) {
        /* Something's gone properly pear shaped -
         * we couldn't even find the entry */
        rb->splashf(HZ*2, "File/Dir not found: %s", parameter);
        return PLUGIN_ERROR;
    }

    DEBUGF("'%s' is a %s\n", parameter, (its_a_dir ? "dir" : "file"));

    if (!load_sc_file(&sc_file, SHORTCUTS_FILENAME, false,
                memory_buf, memory_bufsize)) {
        DEBUGF("Couldn't load '%s'\n", SHORTCUTS_FILENAME);
        return PLUGIN_ERROR;
    }
    
    if (!append_entry_to_file(&sc_file, parameter, its_a_dir)) {
        DEBUGF("Couldn't append entry (too many entries?)\n");
        return PLUGIN_ERROR;
    }
    
    if (!dump_sc_file(&sc_file, SHORTCUTS_FILENAME)) {
        DEBUGF("Couldn't write shortcuts to '%s'\n", SHORTCUTS_FILENAME);
        return PLUGIN_ERROR;
    }

    return PLUGIN_OK;
}