Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Etwas
Monday, August 27th, 2007 at 8:18:23pm UTC 

  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.  
  24. enum sc_list_action_type {
  25.     SCLA_NONE,
  26.     SCLA_SELECT,
  27.     SCLA_DELETE,
  28. };
  29.  
  30.  
  31. static char str_dirname[MAX_PATH+1];
  32. static int gselected_item = 0;
  33.  
  34.  
  35. enum sc_list_action_type draw_sc_list(struct gui_synclist gui_sc);
  36. char* build_sc_list(int selected_item, void* data, char* buffer);
  37. void append_entry_to_the_list(sc_entry_t *entry);
  38.  
  39. void delete_sc(int sc_num);
  40. enum plugin_status list_sc(void);
  41. bool exists(char* filename);
  42. bool goto_dir(char *path);
  43.  
  44.  
  45. bool exists(char* filename) {
  46.     int fd = 0;
  47.    
  48.     get_dir_name(filename, str_dirname);
  49.  
  50.     fd = rb->open(str_dirname,O_RDONLY);
  51.     if (fd < 0) {
  52.         return false;
  53.     }
  54.     rb->close(fd);
  55.     return true;
  56. }
  57.  
  58. enum sc_list_action_type draw_sc_list(struct gui_synclist gui_sc) {
  59.     int button;
  60.  
  61.     rb->gui_synclist_draw(&gui_sc);
  62.  
  63.     while (true) {
  64.         /* draw the statusbar, should be done often */
  65.         rb->gui_syncstatusbar_draw(rb->statusbars, true);
  66.         /* user input */
  67.         button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
  68.         if (rb->gui_synclist_do_button(&gui_sc,button,
  69.                                             LIST_WRAP_UNLESS_HELD)) {
  70.             /* automatic handling of user input.
  71.             * _UNLESS_HELD can be _ON or _OFF also
  72.             * selection changed, so redraw */
  73.             continue;
  74.         }
  75.         switch (button) { /* process the user input */
  76.             case ACTION_STD_OK:
  77.                 gselected_item = rb->gui_synclist_get_sel_pos(&gui_sc);
  78.                 return SCLA_SELECT;
  79.             case ACTION_STD_MENU:
  80.                 /* Only allow delete entries in the default file */
  81.                 if (!user_file) {
  82.                     gselected_item = rb->gui_synclist_get_sel_pos(&gui_sc);
  83.                     return SCLA_DELETE;
  84.                 }
  85.                 break;
  86.             case ACTION_STD_CANCEL:
  87.                 return SCLA_NONE;
  88.         }
  89.     }
  90. }
  91.  
  92. char* build_sc_list(int selected_item, void* data, char* buffer) {
  93.     int i;
  94.     sc_entry_t* temp_node = (sc_entry_t*)data;
  95.     char text_buffer[MAX_PATH];
  96.  
  97.     for (i=0; i<selected_item && temp_node != NULL;i++){
  98.         temp_node = temp_node->next;
  99.     }
  100.     if (temp_node == NULL){
  101.         return NULL;
  102.     }
  103.     rb->snprintf(text_buffer, sizeof(text_buffer), "%s", temp_node->disp);
  104.     rb->strcpy(buffer, text_buffer);
  105.     return buffer;
  106. }
  107.  
  108.  
  109. void delete_sc(int sc_num){
  110.     /* Note: This function is a nasty hack and I should probably
  111.      * be shot for doing it this way.*/
  112.     int i;
  113.     sc_entry_t* current = shortcuts;
  114.     sc_entry_t* previous = shortcuts;
  115.  
  116.     if (total_entries==1) {
  117.         /* This is the only item in the file
  118.          * so just set the whole shortcuts list
  119.          * to zero */
  120.         shortcuts = NULL;
  121.     } else {
  122.         if (sc_num!=0) {
  123.             for (i=0; i<sc_num && current != NULL; i++){
  124.                 /* keep previous pointing at the prior
  125.                 * item in the list */
  126.                 if (previous != current) {
  127.                     previous = current;
  128.                 }
  129.                 current = current->next;
  130.             }
  131.             /* current should now be pointing at the item
  132.             * to be deleted, so update the previous item
  133.             * to point to whatever current is pointing to
  134.             * as next item */
  135.             if (current) {
  136.                 previous->next = current->next;
  137.             } else {
  138.                 previous->next = 0;
  139.             }
  140.         } else {
  141.             shortcuts = shortcuts->next;
  142.         }
  143.     }
  144. }
  145.  
  146. enum plugin_status list_sc(void) {
  147.     int selected_item = 0;
  148.     char selected_dir[MAX_PATH];
  149.     enum sc_list_action_type action = SCLA_NONE;
  150.     struct gui_synclist gui_sc;
  151.     sc_entry_t *selected_entry;
  152.  
  153.     rb->memset(selected_dir,0,MAX_PATH);
  154.  
  155.     /* Setup the GUI list object, draw it to the screen,
  156.      * and then handle the user input to it */
  157.     rb->gui_synclist_init(&gui_sc,&build_sc_list,shortcuts,false,1);
  158.     rb->gui_synclist_set_title(&gui_sc,"Shortcuts",NOICON);
  159.     rb->gui_synclist_set_nb_items(&gui_sc,total_entries);
  160.     rb->gui_synclist_limit_scroll(&gui_sc,false);
  161.     rb->gui_synclist_select_item(&gui_sc,0);
  162.  
  163.     /* Draw the prepared widget to the LCD now */
  164.     action = draw_sc_list(gui_sc);
  165.  
  166.     /* which item do we action? */
  167.     selected_item = gselected_item;
  168.  
  169.     /* Find out which option the user selected.
  170.      * Handily, the callback function which is used
  171.      * to populate the list is equally useful here! */
  172.     build_sc_list(selected_item,(void*)shortcuts,selected_dir);
  173.  
  174.     /* perform the following actions if the user "selected"
  175.      * the item in the list (i.e. they want to go there
  176.      * in the filebrowser tree */
  177.     switch(action) {
  178.         case SCLA_SELECT:
  179.             selected_entry = get_entry(selected_item);
  180.             if (selected_entry != NULL) {
  181.                 if (!goto_dir(selected_entry->path)) {
  182.                     return PLUGIN_ERROR;
  183.                 }
  184.                 return PLUGIN_OK;
  185.             }
  186.             break;
  187.         case SCLA_DELETE:
  188.             rb->splash(HZ, "Deleting item");
  189.             delete_sc(selected_item);
  190.             return write_sc_file(link_filename);
  191.         case SCLA_NONE:
  192.             return PLUGIN_OK;
  193.     }
  194.     return PLUGIN_OK;
  195. }
  196.  
  197. bool goto_dir(char *path) {
  198.     if (!exists(path)) {
  199.         rb->splash(HZ*2, "File / Directory no longer exists on disk");
  200.         return false;
  201.     }
  202.     /* Set the browsers dirfilter to the global setting
  203.      * This is required in case the plugin was launched
  204.      * from the plugins browser, in which case the
  205.      * dirfilter is set to only display .rock files */
  206.     rb->set_dirfilter(rb->global_settings->dirfilter);
  207.  
  208.     /* Change directory to the entry selected by the user */
  209.     rb->set_current_file(path);
  210.     return true;
  211. }
  212.  
  213. enum plugin_status plugin_start(struct plugin_api* api, void* void_parameter) {
  214.     rb = api;
  215.    
  216.     /* This is a viewer, so a parameter must have been specified */
  217.     if (void_parameter == NULL) {
  218.         rb->splash(HZ*2, "No parameter specified!");
  219.         return PLUGIN_ERROR;
  220.     }
  221.     char *parameter = (char*)void_parameter;
  222.  
  223.     /* Initialise the plugin buffer */
  224.     sc_alloc_init();
  225.  
  226.     if (!load_sc_file(parameter, true)) {
  227.         rb->debugf("Could not load %s\n", parameter);
  228.         return PLUGIN_ERROR;
  229.     }
  230.     if (total_entries==0) {
  231.         rb->splash(HZ*2, "No shortcuts in the file!");
  232.         return PLUGIN_OK;
  233.     } else if ((total_entries==1) && user_file) {
  234.         /* if there's only one entry in the user .link file,
  235.          * go straight to it without displaying the menu */
  236.         if (!goto_dir(shortcuts->path)) {
  237.             return PLUGIN_ERROR;
  238.         }
  239.         return PLUGIN_OK;
  240.     }
  241.     /* Display a menu to choose between the entries */
  242.     return list_sc();
  243. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right