diff options
Diffstat (limited to 'apps/plugins/shortcuts/shortcuts_append.c')
-rw-r--r-- | apps/plugins/shortcuts/shortcuts_append.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/apps/plugins/shortcuts/shortcuts_append.c b/apps/plugins/shortcuts/shortcuts_append.c new file mode 100644 index 0000000000..bc287234dd --- /dev/null +++ b/apps/plugins/shortcuts/shortcuts_append.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 Bryan Childs | ||
11 | * Copyright (c) 2007 Alexander Levin | ||
12 | * | ||
13 | * All files in this archive are subject to the GNU General Public License. | ||
14 | * See the file COPYING in the source tree root for full license agreement. | ||
15 | * | ||
16 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
17 | * KIND, either express or implied. | ||
18 | * | ||
19 | ****************************************************************************/ | ||
20 | |||
21 | #include "shortcuts.h" | ||
22 | |||
23 | PLUGIN_HEADER | ||
24 | |||
25 | |||
26 | bool append_entry_to_file(sc_file_t *file, char *path, bool is_dir) | ||
27 | { | ||
28 | sc_entry_t entry; | ||
29 | |||
30 | unsigned int required_len = rb->strlen(path); | ||
31 | if (is_dir) { | ||
32 | required_len += PATH_SEPARATOR_LEN; /* Add 1 for the trailing / */ | ||
33 | } | ||
34 | if (required_len >= sizeof(entry.path)) { | ||
35 | /* no attempt to print it: it will also be so too long for the splash */ | ||
36 | rb->splash(HZ*2, "Can't append shortcut, it's too long"); | ||
37 | return false; | ||
38 | } | ||
39 | entry.explicit_disp = false; | ||
40 | rb->strcpy(entry.path, path); | ||
41 | if (is_dir) { | ||
42 | rb->strcat(entry.path, PATH_SEPARATOR); | ||
43 | } | ||
44 | if (!append_entry(file, &entry)) { | ||
45 | rb->splash(HZ*2, "Too many entries!"); | ||
46 | return false; | ||
47 | } | ||
48 | return true; | ||
49 | } | ||
50 | |||
51 | |||
52 | enum plugin_status plugin_start(struct plugin_api* api, void* void_parameter) | ||
53 | { | ||
54 | rb = api; | ||
55 | bool found; | ||
56 | bool its_a_dir; | ||
57 | |||
58 | /* This is a viewer, so a parameter must have been specified */ | ||
59 | if (void_parameter == NULL) { | ||
60 | rb->splash(HZ*2, "No parameter specified!"); | ||
61 | return PLUGIN_ERROR; | ||
62 | } | ||
63 | char *parameter = (char*)void_parameter; | ||
64 | DEBUGF("Trying to append '%s' to the default link file '%s'...\n", | ||
65 | parameter, SHORTCUTS_FILENAME); | ||
66 | |||
67 | allocate_memory(&memory_buf, &memory_bufsize); | ||
68 | |||
69 | /* Determine if it's a file or a directory. First check | ||
70 | * if it's a dir and then file (not vice versa) since | ||
71 | * open() can also open a dir */ | ||
72 | found = true; | ||
73 | if (dir_exists(parameter)) { | ||
74 | its_a_dir = true; | ||
75 | } else if (file_exists(parameter)) { | ||
76 | its_a_dir = false; | ||
77 | } else { | ||
78 | found = false; | ||
79 | } | ||
80 | /* now we know if it's a file or a directory | ||
81 | * (or something went wrong) */ | ||
82 | |||
83 | if (!found) { | ||
84 | /* Something's gone properly pear shaped - | ||
85 | * we couldn't even find the entry */ | ||
86 | rb->splash(HZ*2, "File/Dir not found: %s", parameter); | ||
87 | return PLUGIN_ERROR; | ||
88 | } | ||
89 | |||
90 | DEBUGF("'%s' is a %s\n", parameter, (its_a_dir ? "dir" : "file")); | ||
91 | |||
92 | if (!load_sc_file(&sc_file, SHORTCUTS_FILENAME, false, | ||
93 | memory_buf, memory_bufsize)) { | ||
94 | DEBUGF("Couldn't load '%s'\n", SHORTCUTS_FILENAME); | ||
95 | return PLUGIN_ERROR; | ||
96 | } | ||
97 | |||
98 | if (!append_entry_to_file(&sc_file, parameter, its_a_dir)) { | ||
99 | DEBUGF("Couldn't append entry (too many entries?)\n"); | ||
100 | return PLUGIN_ERROR; | ||
101 | } | ||
102 | |||
103 | if (!dump_sc_file(&sc_file, SHORTCUTS_FILENAME)) { | ||
104 | DEBUGF("Couldn't write shortcuts to '%s'\n", SHORTCUTS_FILENAME); | ||
105 | return PLUGIN_ERROR; | ||
106 | } | ||
107 | |||
108 | return PLUGIN_OK; | ||
109 | } | ||