Fix even more leaks
[pandora-libraries.git] / minimenu / mmui.c
index 3a0c175..25a9f3b 100644 (file)
@@ -240,12 +240,15 @@ mm_imgcache_t g_imagecache [ IMG_TRUEMAX ] = {
   { IMG_HOURGLASS,            "graphics.IMG_HOURGLASS", },
   { IMG_FOLDER,               "graphics.IMG_FOLDER", },
   { IMG_EXECBIN,              "graphics.IMG_EXECBIN", },
+  { IMG_SUBCATFOLDER,         "graphics.IMG_SUBCATFOLDER", "graphics.IMG_FOLDER", },
+  { IMG_DOTDOTFOLDER,         "graphics.IMG_DOTDOTFOLDER", "graphics.IMG_FOLDER", },
   { IMG_MAX,                  NULL },
 };
 
 unsigned char ui_imagecache ( char *basepath ) {
   unsigned int i;
   char fullpath [ PATH_MAX ];
+  unsigned char try;
 
   // loaded
 
@@ -256,23 +259,39 @@ unsigned char ui_imagecache ( char *basepath ) {
       exit ( -1 );
     }
 
-    char *filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].confname );
+    for ( try = 0; try < 2; try++ ) {
 
-    if ( ! filename ) {
-      pnd_log ( pndn_error, "ERROR: Missing filename in conf for key: %s\n", g_imagecache [ i ].confname );
-      return ( 0 );
-    }
+      char *filename;
 
-    if ( filename [ 0 ] == '/' ) {
-      strncpy ( fullpath, filename, PATH_MAX );
-    } else {
-      sprintf ( fullpath, "%s/%s", basepath, filename );
-    }
+      if ( try == 0 ) {
+       filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].confname );
+      } else {
+       if ( g_imagecache [ i ].alt_confname ) {
+         filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].alt_confname );
+       } else {
+         return ( 0 );
+       }
+      }
 
-    if ( ! ( g_imagecache [ i ].i = IMG_Load ( fullpath ) ) ) {
-      pnd_log ( pndn_error, "ERROR: Couldn't load static cache image: %s\n", fullpath );
-      return ( 0 );
-    }
+      if ( ! filename ) {
+       pnd_log ( pndn_error, "ERROR: (Try %u) Missing filename in conf for key: %s\n", try + 1, g_imagecache [ i ].confname );
+       if ( try == 0 ) { continue; } else { return ( 0 ); }
+      }
+
+      if ( filename [ 0 ] == '/' ) {
+       strncpy ( fullpath, filename, PATH_MAX );
+      } else {
+       sprintf ( fullpath, "%s/%s", basepath, filename );
+      }
+
+      if ( ( g_imagecache [ i ].i = IMG_Load ( fullpath ) ) ) {
+       break; // no retry needed
+      } else {
+       pnd_log ( pndn_error, "ERROR: (Try %u) Couldn't load static cache image: %s\n", try + 1, fullpath );
+       if ( try == 0 ) { continue; } else { return ( 0 ); }
+      }
+
+    } // try twice
 
   } // for
 
@@ -704,7 +723,17 @@ void ui_render ( void ) {
              // filesystem (file or directory icon)
              if ( appiter -> ref -> object_flags & PND_DISCO_GENERATED ) {
                if ( appiter -> ref -> object_type == pnd_object_type_directory ) {
-                 iconsurface = g_imagecache [ IMG_FOLDER ].i;
+
+                 // is this a subcat, a .., or a filesystem folder?
+                 //iconsurface = g_imagecache [ IMG_FOLDER ].i;
+                 if ( g_categories [ ui_category ] -> fspath ) {
+                   iconsurface = g_imagecache [ IMG_FOLDER ].i;
+                 } else if ( strcmp ( appiter -> ref -> title_en, ".." ) == 0 ) {
+                   iconsurface = g_imagecache [ IMG_DOTDOTFOLDER ].i;
+                 } else {
+                   iconsurface = g_imagecache [ IMG_SUBCATFOLDER ].i;
+                 }
+
                } else {
                  iconsurface = g_imagecache [ IMG_EXECBIN ].i;
                }
@@ -3405,7 +3434,7 @@ void ui_menu_context ( mm_appref_t *a ) {
          context_alive = 0; // nolonger visible, so lets just get out
 
        }
-    
+
        break;
 
       case context_app_recategorize:
@@ -3779,7 +3808,11 @@ unsigned char ui_menu_get_text_line ( char *title, char *footer, char *initialva
          r_buffer [ 0 ] = '\0'; // truncate!
 
        } else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_END ) { // return, or "B"
-         return ( 1 );
+         // on Enter/Return or B, if the buffer has 1 or more chars, we return it as valid.. otherwise, invalid.
+         if ( strlen ( r_buffer ) > 0 ) {
+           return ( 1 );
+         }
+         return ( 0 );
 
        } else if ( event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT ) {
          shifted = 1;
@@ -3829,7 +3862,7 @@ unsigned char ui_menu_get_text_line ( char *title, char *footer, char *initialva
     } // while waiting for input
 
   } // while
-  
+
   return ( 0 );
 }
 
@@ -3908,17 +3941,13 @@ void ui_manage_categories ( void ) {
     switch ( sel ) {
 
     case 0: // list custom
-      if ( mmcustom_count ) {
-       ui_pick_custom_category ( 0 );
-      } else {
-       ui_menu_oneby ( "Warning", "B/Enter to accept", "There are none registered." );
-      }
+      ui_pick_custom_category ( 0 );
       break;
 
     case 1: // list custom sub
       if ( mmcustom_count ) {
 
-       char *maincat = ui_pick_custom_category ( 0 );
+       char *maincat = ui_pick_custom_category ( 2 );
 
        if ( maincat ) {
          unsigned int subcount = mmcustom_count_subcats ( maincat );
@@ -3990,7 +4019,7 @@ void ui_manage_categories ( void ) {
       break;
 
     case 3: // register custom sub
-      if ( mmcustom_count ) {
+      if ( 1 /*mmcustom_count -- we allow FD cats now, so this isn't applicable error */ ) {
 
        char *maincat = ui_pick_custom_category ( 1 /* include FD */ );
 
@@ -4061,7 +4090,7 @@ void ui_manage_categories ( void ) {
 
     case 5: // unreg custom sub
       if ( mmcustom_count ) {
-       char *maincat = ui_pick_custom_category ( 0 );
+       char *maincat = ui_pick_custom_category ( 2 );
 
        if ( maincat ) {
          unsigned int subcount = mmcustom_count_subcats ( maincat );
@@ -4134,42 +4163,62 @@ void ui_manage_categories ( void ) {
   return;
 }
 
-char *ui_pick_custom_category ( unsigned char include_fd ) {
+// mode 0 == custom main only; 1 == custom main + FD main; 2 == custom main + FD mains-with-custom-subs
+char *ui_pick_custom_category ( unsigned char mode ) {
   char **list;
   int i;
   unsigned int counter = 0;
 
-  if ( include_fd ) {
+  // alloc space for list, depending on scope
+  if ( mode > 0 ) {
     list = malloc ( (mmcustom_count+freedesktop_count()) * sizeof(char*) );
   } else {
     list = malloc ( mmcustom_count * sizeof(char*) );
   }
 
-  // add custom
+  // add custom mains
   for ( i = 0; i < mmcustom_count; i++ ) {
     if ( mmcustom_complete [ i ].parent_cat == NULL ) {
       list [ counter++ ] = mmcustom_complete [ i ].cat;
     }
   }
 
-  // add FD
-  if ( include_fd ) {
+  // add FD if needed
+  if ( mode > 0 ) {
     i = 3;
+
     while ( 1 ) {
 
       if ( ! freedesktop_complete [ i ].cat ) {
        break;
       }
 
+      // if FD main cat
       if ( freedesktop_complete [ i ].parent_cat == NULL ) {
-       list [ counter++ ] = freedesktop_complete [ i ].cat;
-      }
+
+       // mode 1 == include them all
+       // mode 2 == include them if they have a custom subcat
+       if ( ( mode == 1 ) ||
+            ( mmcustom_subcount ( freedesktop_complete [ i ].cat ) ) )
+       {
+         list [ counter++ ] = freedesktop_complete [ i ].cat;
+       }
+
+      } // if parent cat
 
       i++;
     } // while
+
   } // if
 
-  int sel = ui_modal_single_menu ( list, counter, "Custom Main Categories", "Any button to exit." );
+  // we actually showing anything?
+  if ( ! counter ) {
+    ui_menu_oneby ( "Warning", "B/Enter to accept", "There are none registered." );
+    return ( NULL );
+  }
+
+  // do it
+  int sel = ui_modal_single_menu ( list, counter, "Custom Categories", "Any button to exit." );
 
   if ( sel < 0 ) {
     free ( list );