ctb/src/menu.c

Go to the documentation of this file.
00001 /*
00002  * File Name: menu.c
00003  */
00004 
00005 /*
00006  * This file is part of ctb.
00007  *
00008  * ctb is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * ctb is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021  
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 #include "config.h"
00032 
00033 #include "ctb_log.h"
00034 #include "fileview.h"
00035 #include "i18n.h"
00036 #include "ipc.h"
00037 #include "menu.h"
00038 
00039 
00040 //----------------------------------------------------------------------------
00041 // Type Declarations
00042 //----------------------------------------------------------------------------
00043 
00044 
00045 //----------------------------------------------------------------------------
00046 // Constants
00047 //----------------------------------------------------------------------------
00048 
00049 // menu groups                              
00050 static const char *GROUP_VIEWS              = "ctb_view";
00051 static const char *GROUP_SORT               = "ctb_sort";
00052 
00053 static const char *GROUP_ACTIONS            = "ctb_actions";
00054                                             
00055 // menu items                               
00056 //   group "Views"                          
00057 static const char *ITEM_VIEW_ICON           = "view_small";
00058 static const char *ITEM_VIEW_DETAIL         = "view_detail";
00059 static const char *ITEM_VIEW_CONTENT        = "view_content";
00060 
00061 //   group "Sort"                           
00062 static const char *ITEM_SORT_NAME           = "sort_name";
00063 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00064 static const char *ITEM_SORT_TYPE           = "sort_type";
00065 static const char *ITEM_SORT_SIZE           = "sort_size";
00066 #endif
00067 static const char *ITEM_SORT_AUTHOR         = "sort_author";
00068 static const char *ITEM_SORT_DATE_ADDED     = "sort_date_added";
00069 static const char *ITEM_SORT_DATE_READ      = "sort_date_read";
00070 //   group "Actions"                        
00071 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00072 static const char *ITEM_ACTION_SHORTCUT     = "create_shortcut";
00073 #endif
00074 static const char *ITEM_ACTION_DELETE       = "delete_mode";
00075                                             
00076 // item states                              
00077 static const char *STATE_NORMAL             = "normal";
00078 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00079 static const char *STATE_DISABLED           = "disabled";
00080 #endif
00081 static const char *STATE_SELECTED           = "selected";
00082 
00083 // stylus states
00084 static const char *STYLUS_NONE              = "none";
00085 
00086 static const char* BACK_TO_LIBRARY          = "back_to_library";
00087 static const char* SYSTEM_TOP               = "system_top";
00088 
00089 //----------------------------------------------------------------------------
00090 // Static Variables
00091 //----------------------------------------------------------------------------
00092 
00093 static GString  *g_sort_item            = NULL;
00094 static ctb_viewmodes_t g_viewmode = BROWSE_MODE;
00095 static gchar* g_category = NULL;
00096 
00097 //============================================================================
00098 // Functions Implementation
00099 //============================================================================
00100 
00101 void menu_init()
00102 {
00103     g_sort_item = g_string_new("");
00104 
00105     // add groups
00106     const char *group = GROUP_VIEWS;
00107     ipc_menu_add_group( group,                  "",    "folder"          );
00108     ipc_menu_add_item ( ITEM_VIEW_ICON,         group, "view_small"      );
00109 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00110     ipc_menu_add_item ( ITEM_VIEW_DETAIL,         group, "view_detail"     );
00111 #endif
00112     ipc_menu_add_item ( ITEM_VIEW_CONTENT,      group, "view_content"     );
00113 
00114     group = GROUP_SORT;                         
00115     ipc_menu_add_group( group,                  "",    "folder"          );                                                  
00116     ipc_menu_add_item ( ITEM_SORT_NAME,         group, "sort_name"       );
00117     ipc_menu_add_item ( ITEM_SORT_AUTHOR,       group, "sort_author"     );
00118 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00119     ipc_menu_add_item ( ITEM_SORT_TYPE,         group, "sort_type"       );
00120     ipc_menu_add_item ( ITEM_SORT_SIZE,         group, "sort_size"       );
00121 #endif
00122     ipc_menu_add_item ( ITEM_SORT_DATE_ADDED,   group, "sort_date_added" );
00123     ipc_menu_add_item ( ITEM_SORT_DATE_READ,    group, "sort_date_read" );
00124 
00125     group = GROUP_ACTIONS;
00126     ipc_menu_add_group( group,                  "",    "folder"          );
00127 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00128     ipc_menu_add_item ( ITEM_ACTION_SHORTCUT,   group, "create_shortcut" );
00129 #endif
00130     ipc_menu_add_item ( ITEM_ACTION_DELETE,     group, "delete"          );
00131 
00132     // add menus
00133     ipc_menu_add_menu(MENU_CONTENT_MEDIA,       GROUP_VIEWS,   GROUP_SORT,    GROUP_ACTIONS);
00134     ipc_menu_add_menu(MENU_CONTENT,             GROUP_VIEWS,   NULL,          NULL);
00135     ipc_menu_add_menu(MENU_CONTENT_DELETE_MODE, GROUP_ACTIONS, NULL,          NULL);
00136     ipc_menu_add_menu(MENU_CONTENT_RECENT_MODE, GROUP_VIEWS,   GROUP_ACTIONS, NULL);
00137 
00138     ipc_menu_set_item_state(BACK_TO_LIBRARY, SYSTEM_TOP, STATE_NORMAL);
00139 
00140     menu_set_text();
00141 }
00142 
00143 
00144 static void update_viewmode_text()
00145 {
00146     const char *group = GROUP_ACTIONS;
00147 
00148     switch (g_viewmode) {
00149         case BROWSE_MODE:
00150             ipc_menu_set_item_label(ITEM_ACTION_DELETE, group, _("Delete..."));
00151 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00152             ipc_menu_set_item_state(ITEM_ACTION_SHORTCUT, group, STATE_NORMAL);
00153 #endif
00154             break;
00155         case DELETE_MODE:
00156             ipc_menu_set_item_label(ITEM_ACTION_DELETE, group, _("Cancel Delete"));
00157 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00158             ipc_menu_set_item_state(ITEM_ACTION_SHORTCUT, group, STATE_DISABLED);
00159 #endif
00160             break;
00161     }
00162 }
00163 
00164 
00165 static void update_category()
00166 {
00167     if (filemodel_window_is_on_top()) {
00168         ipc_menu_set_item_label(BACK_TO_LIBRARY, SYSTEM_TOP, "");
00169     } else {
00170         char buffer[128];
00171         buffer[0] = 0;
00172         if (g_category) {
00173             /* TRANSLATORS: %s is replaced by the current category
00174             Example: 'Back to Books' */
00175             snprintf(buffer, 127, _("Back to %s"), _(g_category));
00176         }
00177         ipc_menu_set_item_label(BACK_TO_LIBRARY, SYSTEM_TOP, buffer);
00178     }
00179 }
00180 
00181 
00182 void menu_set_category(const char* category)
00183 {
00184     g_free(g_category);
00185     g_category = g_strdup(category);
00186     update_category();
00187 }
00188 
00189 
00190 // initialise popup menu
00191 void menu_set_text()
00192 {
00193     const char *group;
00194 
00195     // set groups + items
00196     group = GROUP_VIEWS;
00197     ipc_menu_set_group_label( group,                         _("View")              );
00198 #if MACHINE_IS_DR800S || MACHINE_IS_DR800SG || MACHINE_IS_DR800SW
00199     ipc_menu_set_item_label ( ITEM_VIEW_ICON,         group, _("View as Covers")    );
00200 #elif MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00201     ipc_menu_set_item_label ( ITEM_VIEW_ICON,         group, _("View as Thumbnails"));
00202     ipc_menu_set_item_label ( ITEM_VIEW_DETAIL,       group, _("View as Details")   );
00203 #else
00204 #error Unhandled machine type
00205 #endif
00206     ipc_menu_set_item_label ( ITEM_VIEW_CONTENT,      group, _("View as List")      );
00207     //
00208     group = GROUP_SORT;
00209     ipc_menu_set_group_label( group,                         _("Sort by")           );
00210     ipc_menu_set_item_label ( ITEM_SORT_NAME,         group, _("Sort by Title")     );
00211     ipc_menu_set_item_label ( ITEM_SORT_AUTHOR,       group, _("Sort by Author")    );
00212 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00213     ipc_menu_set_item_label ( ITEM_SORT_TYPE,         group, _("Sort by Type")      );
00214     ipc_menu_set_item_label ( ITEM_SORT_SIZE,         group, _("Sort by Size")      );
00215 #endif
00216     ipc_menu_set_item_label ( ITEM_SORT_DATE_ADDED,   group, _("Sort by Date Added")  );
00217     ipc_menu_set_item_label ( ITEM_SORT_DATE_READ,    group, _("Sort by Recently Opened"));
00218     //
00219     group = GROUP_ACTIONS;
00220     ipc_menu_set_group_label( group,                         _("Actions")           );
00221 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00222     ipc_menu_set_item_label ( ITEM_ACTION_SHORTCUT,   group, _("Add Shortcut")      );
00223 #endif
00224 
00225     update_viewmode_text();
00226     update_category();
00227 }
00228 
00229 
00230 void menu_show()
00231 {
00232     if (!filemodel_window_is_on_top()) return;
00233 
00234     ipc_status_set_stylus( STYLUS_NONE );
00235     update_category();
00236     ipc_menu_show_menu(filemodel_get_menu_content());
00237     filemodel_update_pagecounter();
00238 }
00239 
00240 
00241 void menu_hide()
00242 {
00243     if (!filemodel_current_is_desktop()) {
00244         update_category();
00245         ipc_menu_updates_finished();
00246     }
00247 }
00248 
00249 
00250 void menu_select_view_type (ctb_viewtypes_t view_type)
00251 {
00252     static const char *item_old = "";
00253     const char *item_new = "";
00254 
00255     LOGPRINTF("entry: view_type [%d]", view_type);
00256 
00257     switch (view_type)
00258     {
00259         case CTB_ICONVIEW:
00260             item_new = ITEM_VIEW_ICON;
00261             break;
00262         case CTB_LISTVIEW:
00263             item_new = ITEM_VIEW_DETAIL;
00264             break;
00265         case CTB_CONTENTVIEW:
00266             item_new = ITEM_VIEW_CONTENT;
00267             break;
00268         default:
00269             ;  // ignore: handled by assert at function entry
00270     }
00271 
00272     if ( strcmp(item_old, item_new) != 0 )
00273     {
00274         if ( item_old[0] != '\0' )
00275         {
00276             ipc_menu_set_item_state ( item_old, GROUP_VIEWS, STATE_NORMAL   );
00277         }
00278         ipc_menu_set_item_state ( item_new, GROUP_VIEWS, STATE_SELECTED );
00279 
00280         item_old = item_new;
00281     }
00282 }
00283 
00284 
00285 void menu_update_view_mode(ctb_viewmodes_t view_mode)
00286 {
00287     g_viewmode = view_mode;
00288     update_viewmode_text();
00289     ipc_menu_show_menu(filemodel_get_menu_content());
00290 }
00291 
00292 
00293 // select one of the sorting orders
00294 void menu_select_sort_order ( const ctb_sort_order_t sort_order )
00295 {
00296     LOGPRINTF("entry: sort_order [%d]", sort_order);
00297     g_assert(g_sort_item && g_sort_item->str);
00298 
00299     const char *item_new = NULL;
00300     switch (sort_order)
00301     {
00302         case CTB_SORT_BY_NAME:
00303             item_new = ITEM_SORT_NAME;
00304             break;
00305 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00306         case CTB_SORT_BY_TYPE:
00307             item_new = ITEM_SORT_TYPE;
00308             break;
00309         case CTB_SORT_BY_SIZE:
00310             item_new = ITEM_SORT_SIZE;
00311             break;
00312 #endif
00313         case CTB_SORT_BY_DATE_ADDED:
00314             item_new = ITEM_SORT_DATE_ADDED;
00315             break;
00316         case CTB_SORT_BY_DATE_READ:
00317             item_new =  ITEM_SORT_DATE_READ;
00318             break;
00319         case CTB_SORT_BY_AUTHOR:
00320             item_new = ITEM_SORT_AUTHOR;
00321             break;
00322         default:
00323             g_assert(0);
00324     }
00325 
00326     if ( strcmp(g_sort_item->str, item_new) != 0 )
00327     {
00328         if ( g_sort_item->str[0] )
00329         {
00330             ipc_menu_set_item_state ( g_sort_item->str, GROUP_SORT, STATE_NORMAL   );
00331         }
00332         ipc_menu_set_item_state ( item_new, GROUP_SORT, STATE_SELECTED );
00333 
00334         g_string_assign( g_sort_item, item_new );
00335     }
00336 }
00337 
00338 
00339 void menu_set_current_is_media(const gboolean is_media)
00340 {
00341     static gboolean prev_is_media = -1;
00342 
00343     if (prev_is_media != is_media)
00344     {
00345         prev_is_media = is_media;
00346 
00347 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00348         ipc_menu_set_item_state(ITEM_ACTION_SHORTCUT,
00349                                 GROUP_ACTIONS,
00350                                 is_media ? STATE_NORMAL : STATE_DISABLED);
00351         ipc_menu_set_item_state(ITEM_VIEW_DETAIL,
00352                                 GROUP_VIEWS,
00353                                 is_media ? STATE_NORMAL : STATE_DISABLED);
00354 #endif
00355         ipc_menu_show_menu(filemodel_get_menu_content());
00356     }
00357 }
00358 
00359 
00360 //----------------------------------------------------------------------------
00361 // Callbacks from popupmenu
00362 //----------------------------------------------------------------------------
00363 
00364 void menu_on_goto_location(const gchar *location)
00365 {
00366     START_TIMER();
00367     g_assert( location  &&  location[0] != '\0' );
00368 
00369     if ( strcmp(location, "desktop") == 0 )
00370     {
00371         fileview_show_desktop();
00372         menu_show();
00373     }
00374     else
00375     {
00376         ERRORPRINTF("unknown location [%s]", location);
00377     }
00378 }
00379 
00380 
00381 void menu_on_item_activated ( const gchar *item,
00382                               const gchar *group,
00383                               const gchar *menu,
00384                               const gchar *state )
00385 {
00386     START_TIMER();
00387     gboolean ok = TRUE;
00388 
00389     LOGPRINTF("entry: item [%s] group [%s]", item, group);
00390 
00391     if ( strcmp(group, GROUP_VIEWS) == 0 )
00392     {
00393         if ( strcmp(state, STATE_NORMAL) == 0 )
00394         {
00395             ctb_viewtypes_t view_type = 0;
00396             if (strcmp(item, ITEM_VIEW_ICON) == 0 )
00397             {
00398                 view_type = CTB_ICONVIEW;
00399             }
00400             else if (strcmp(item, ITEM_VIEW_DETAIL) == 0 )
00401             {
00402                 view_type = CTB_LISTVIEW;
00403             }
00404             else if (strcmp(item, ITEM_VIEW_CONTENT) == 0 )
00405             {
00406                 view_type = CTB_CONTENTVIEW;
00407             }
00408             else
00409             {
00410                 WARNPRINTF("unexpected menu item [%s] in group [%s]", item, group);
00411                 ok = FALSE;
00412             }
00413 
00414             if (ok) fileview_set_view_type( view_type, TRUE );
00415         }
00416         else
00417         {
00418             WARNPRINTF("ignore state [%s] item [%s][%s]", state, item, group);
00419         }
00420     }
00421     else if ( strcmp(group, GROUP_SORT) == 0 )
00422     {
00423         ctb_sort_order_t sort_order = 0;
00424         if ( strcmp(state, STATE_NORMAL) == 0 )
00425         {
00426             if (      strcmp(item, ITEM_SORT_NAME  ) == 0 )
00427             {
00428                 sort_order = CTB_SORT_BY_NAME;
00429             }
00430 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00431             else if ( strcmp(item, ITEM_SORT_TYPE  ) == 0 )
00432             {
00433                 sort_order = CTB_SORT_BY_TYPE;
00434             }
00435             else if ( strcmp(item, ITEM_SORT_SIZE  ) == 0 )
00436             {
00437                 sort_order = CTB_SORT_BY_SIZE;
00438             }
00439 #endif
00440             else if ( strcmp(item, ITEM_SORT_DATE_ADDED  ) == 0 )
00441             {
00442                 sort_order = CTB_SORT_BY_DATE_ADDED;
00443             }
00444             else if ( strcmp(item, ITEM_SORT_DATE_READ  ) == 0 )
00445             {
00446                 sort_order = CTB_SORT_BY_DATE_READ;
00447             }
00448             else if ( strcmp(item, ITEM_SORT_AUTHOR) == 0 )
00449             {
00450                 sort_order = CTB_SORT_BY_AUTHOR;
00451             }
00452             else
00453             {
00454                 WARNPRINTF("unexpected menu item [%s] in group [%s]", item, group);
00455                 ok = FALSE;
00456             }
00457 
00458             if (ok)
00459             {
00460                 fileview_set_sort_order( sort_order );
00461             }
00462         }
00463         else
00464         {
00465             WARNPRINTF("ignore state [%s] item [%s][%s]", state, item, group);
00466         }
00467     }
00468     else if ( strcmp(group, GROUP_ACTIONS) == 0 )
00469     {
00470         if ( strcmp(state, STATE_NORMAL) == 0 )
00471         {
00472             if ( strcmp(item, ITEM_ACTION_DELETE    ) == 0 )
00473             {
00474                 fileview_toggle_delete_mode();
00475             }
00476 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00477             else if ( strcmp(item, ITEM_ACTION_SHORTCUT  ) == 0 )
00478             {
00479                 fileview_create_shortcut();
00480             }
00481 #endif
00482             else
00483             {
00484                 WARNPRINTF("unexpected menu item [%s] in group [%s]", item, group);
00485                 ok = FALSE;
00486             }
00487         }
00488         else
00489         {
00490             WARNPRINTF("ignore state [%s] item [%s][%s]", state, item, group);
00491         }
00492     }
00493     else
00494     {
00495         WARNPRINTF("unexpected menu group [%s]", group);
00496     }
00497     ipc_menu_updates_finished();
00498 }
00499 
Generated by  doxygen 1.6.2-20100208