fix red.
[kugel-rb.git] / apps / plugins / shortcuts / shortcuts_append.c
blob69b2a445f37fa6408a2d7e76dcb63629cc92402a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Bryan Childs
11 * Copyright (c) 2007 Alexander Levin
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "shortcuts.h"
25 PLUGIN_HEADER
28 bool append_entry_to_file(sc_file_t *file, char *path, bool is_dir)
30 sc_entry_t entry;
32 unsigned int required_len = rb->strlen(path);
33 if (is_dir) {
34 required_len += PATH_SEPARATOR_LEN; /* Add 1 for the trailing / */
36 if (required_len >= sizeof(entry.path)) {
37 /* no attempt to print it: it will also be so too long for the splash */
38 rb->splash(HZ*2, "Can't append shortcut, it's too long");
39 return false;
41 entry.explicit_disp = false;
42 rb->strcpy(entry.path, path);
43 if (is_dir) {
44 rb->strcat(entry.path, PATH_SEPARATOR);
46 if (!append_entry(file, &entry)) {
47 rb->splash(HZ*2, "Too many entries!");
48 return false;
50 return true;
54 enum plugin_status plugin_start(const void* void_parameter)
56 bool found;
57 bool its_a_dir;
59 /* This is a viewer, so a parameter must have been specified */
60 if (void_parameter == NULL) {
61 rb->splash(HZ*2, "No parameter specified!");
62 return PLUGIN_ERROR;
64 char *parameter = (char*)void_parameter;
65 DEBUGF("Trying to append '%s' to the default link file '%s'...\n",
66 parameter, SHORTCUTS_FILENAME);
68 allocate_memory(&memory_buf, &memory_bufsize);
70 /* Determine if it's a file or a directory. First check
71 * if it's a dir and then file (not vice versa) since
72 * open() can also open a dir */
73 found = true;
74 if (rb->dir_exists(parameter)) {
75 its_a_dir = true;
76 } else if (rb->file_exists(parameter)) {
77 its_a_dir = false;
78 } else {
79 found = false;
81 /* now we know if it's a file or a directory
82 * (or something went wrong) */
84 if (!found) {
85 /* Something's gone properly pear shaped -
86 * we couldn't even find the entry */
87 rb->splashf(HZ*2, "File/Dir not found: %s", parameter);
88 return PLUGIN_ERROR;
91 DEBUGF("'%s' is a %s\n", parameter, (its_a_dir ? "dir" : "file"));
93 if (!load_sc_file(&sc_file, SHORTCUTS_FILENAME, false,
94 memory_buf, memory_bufsize)) {
95 DEBUGF("Couldn't load '%s'\n", SHORTCUTS_FILENAME);
96 return PLUGIN_ERROR;
99 if (!append_entry_to_file(&sc_file, parameter, its_a_dir)) {
100 DEBUGF("Couldn't append entry (too many entries?)\n");
101 return PLUGIN_ERROR;
104 if (!dump_sc_file(&sc_file, SHORTCUTS_FILENAME)) {
105 DEBUGF("Couldn't write shortcuts to '%s'\n", SHORTCUTS_FILENAME);
106 return PLUGIN_ERROR;
109 return PLUGIN_OK;