Preliminary stab at app scan locking; pndnotifyd creates logfile /tmp/whatever and...
[pandora-libraries.git] / minimenu / mmenu.c
index 3dd91e4..919b509 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/wait.h>
 #include <dirent.h>
 #include <signal.h> // for sigaction
+#include <time.h>
 
 #include "pnd_logger.h"
 #include "pnd_pxml.h"
@@ -51,6 +52,7 @@
 #include "pnd_notify.h"
 #include "pnd_dbusnotify.h"
 #include "pnd_apps.h"
+#include "pnd_desktop.h"
 
 #include "mmenu.h"
 #include "mmwrapcmd.h"
@@ -60,6 +62,8 @@
 #include "mmui.h"
 #include "mmconf.h"
 
+#define PNDLOCKNAME "pndnotifyd-disco.lock" /* from pndnotifyd */
+
 pnd_box_handle g_active_apps = NULL;
 unsigned int g_active_appcount = 0;
 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
@@ -181,7 +185,14 @@ int main ( int argc, char *argv[] ) {
   }
 
   // override mmenu conf via user preference conf
-  conf_merge_into ( conf_determine_location ( g_conf ), g_conf );
+  // first check /tmp location in case we're storing this-session prefs; if not
+  // found, go back to normal NAND copy in homedir
+  struct stat statbuf;
+  if ( stat ( CONF_PREF_TEMPPATH, &statbuf ) == 0 ) {
+    conf_merge_into ( CONF_PREF_TEMPPATH, g_conf );
+  } else {
+    conf_merge_into ( conf_determine_location ( g_conf ), g_conf );
+  }
   conf_setup_missing ( g_conf );
 
   // desktop conf for app finding preferences
@@ -448,7 +459,7 @@ static unsigned int is_dir_empty ( char *fullpath ) {
     } else {
       // something else came in, so dir must not be empty
       closedir ( d );
-      return ( 0 ); 
+      return ( 0 );
     }
 
     de = readdir ( d );
@@ -495,87 +506,191 @@ void applications_scan ( void ) {
   g_active_apps = 0;
   pnd_box_handle merge_apps = 0;
 
-  // desktop apps?
-  if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.desktop_apps", 1 ) ) {
-    pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
-             pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ) );
-    g_active_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ), NULL );
-  }
+  // boy I wish I built a plugin system here
+  //
 
-  // menu apps?
-  if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.menu_apps", 1 ) ) {
-    pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
-             pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ) );
-    merge_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ), NULL );
-  }
+  // perform application discovery for pnd-files?
+  //
+  if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.disco_pnds", 1 ) ) {
+
+    // desktop apps?
+    if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.desktop_apps", 1 ) ) {
+      pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
+               pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ) );
+      g_active_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ), NULL );
+    }
 
-  // merge lists
-  if ( merge_apps ) {
-    if ( g_active_apps ) {
-      // the key from pnd_disco_search() is the _path_, so easy to look for duplicates
-      // this is pretty inefficient, being linked lists; perhaps should switch to hash tables when
-      // we expect thousands of apps.. or at least an index or something.
-      void *a = pnd_box_get_head ( merge_apps );
-      void *nexta = NULL;
-      while ( a ) {
-       nexta = pnd_box_get_next ( a );
-
-       // if the key for the node is also found in active apps, toss out the merging one
-       if ( pnd_box_find_by_key ( g_active_apps, pnd_box_get_key ( a ) ) ) {
-         //fprintf ( stderr, "Merging app id '%s' is duplicate; discarding it.\n", pnd_box_get_key ( a ) );
-         pnd_box_delete_node ( merge_apps, a );
+    // menu apps?
+    if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.menu_apps", 1 ) ) {
+      pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
+               pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ) );
+      merge_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ), NULL );
+    }
+
+    // merge lists
+    if ( merge_apps ) {
+      if ( g_active_apps ) {
+       // the key from pnd_disco_search() is the _path_, so easy to look for duplicates
+       // this is pretty inefficient, being linked lists; perhaps should switch to hash tables when
+       // we expect thousands of apps.. or at least an index or something.
+       void *a = pnd_box_get_head ( merge_apps );
+       void *nexta = NULL;
+       while ( a ) {
+         nexta = pnd_box_get_next ( a );
+
+         // if the key for the node is also found in active apps, toss out the merging one
+         if ( pnd_box_find_by_key ( g_active_apps, pnd_box_get_key ( a ) ) ) {
+           //fprintf ( stderr, "Merging app id '%s' is duplicate; discarding it.\n", pnd_box_get_key ( a ) );
+           pnd_box_delete_node ( merge_apps, a );
+         }
+
+         a = nexta;
        }
 
-       a = nexta;
+       // got menu apps, and got desktop apps, merge
+       pnd_box_append ( g_active_apps, merge_apps );
+      } else {
+       // got menu apps, had no desktop apps, so just assign
+       g_active_apps = merge_apps;
       }
-
-      // got menu apps, and got desktop apps, merge
-      pnd_box_append ( g_active_apps, merge_apps );
-    } else {
-      // got menu apps, had no desktop apps, so just assign
-      g_active_apps = merge_apps;
     }
-  }
 
-  // aux apps?
-  char *aux_apps = NULL;
-  merge_apps = 0;
-  aux_apps = pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" );
-  if ( aux_apps && aux_apps [ 0 ] ) {
-    pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n", aux_apps );
-    merge_apps = pnd_disco_search ( aux_apps, NULL );
-  }
+    // aux apps?
+    char *aux_apps = NULL;
+    merge_apps = 0;
+    aux_apps = pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" );
+    if ( aux_apps && aux_apps [ 0 ] ) {
+      pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n", aux_apps );
+      merge_apps = pnd_disco_search ( aux_apps, NULL );
+    }
 
-  // merge aux apps
-  if ( merge_apps ) {
-    if ( g_active_apps ) {
-
-      // LAME: snipped from above; should just catenate the 3 sets of searchpaths into a
-      // master searchpath, possibly removing duplicate paths _then_, and keep all this much
-      // more efficient
-
-      // the key from pnd_disco_search() is the _path_, so easy to look for duplicates
-      // this is pretty inefficient, being linked lists; perhaps should switch to hash tables when
-      // we expect thousands of apps.. or at least an index or something.
-      void *a = pnd_box_get_head ( merge_apps );
-      void *nexta = NULL;
-      while ( a ) {
-       nexta = pnd_box_get_next ( a );
-
-       // if the key for the node is also found in active apps, toss out the merging one
-       if ( pnd_box_find_by_key ( g_active_apps, pnd_box_get_key ( a ) ) ) {
-         fprintf ( stderr, "Merging app id '%s' is duplicate; discarding it.\n", pnd_box_get_key ( a ) );
-         pnd_box_delete_node ( merge_apps, a );
+    // merge aux apps
+    if ( merge_apps ) {
+      if ( g_active_apps ) {
+
+       // LAME: snipped from above; should just catenate the 3 sets of searchpaths into a
+       // master searchpath, possibly removing duplicate paths _then_, and keep all this much
+       // more efficient
+
+       // the key from pnd_disco_search() is the _path_, so easy to look for duplicates
+       // this is pretty inefficient, being linked lists; perhaps should switch to hash tables when
+       // we expect thousands of apps.. or at least an index or something.
+       void *a = pnd_box_get_head ( merge_apps );
+       void *nexta = NULL;
+       while ( a ) {
+         nexta = pnd_box_get_next ( a );
+
+         // if the key for the node is also found in active apps, toss out the merging one
+         if ( pnd_box_find_by_key ( g_active_apps, pnd_box_get_key ( a ) ) ) {
+           fprintf ( stderr, "Merging app id '%s' is duplicate; discarding it.\n", pnd_box_get_key ( a ) );
+           pnd_box_delete_node ( merge_apps, a );
+         }
+
+         a = nexta;
        }
 
-       a = nexta;
+       pnd_box_append ( g_active_apps, merge_apps );
+      } else {
+       g_active_apps = merge_apps;
       }
+    }
+
+  } // app discovery on pnd-files?
+
+  // perform app discovery on .desktop files?
+  //
+  if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.disco_dotdesktop", 0 ) ) {
+
+    // deal with locks
+    pnd_log ( pndn_debug, "Checking pndnotifyd disco lock %s\n", PNDLOCKNAME );
+
+    //   first, check if lock is 'old' -- maybe locker crashed, or if its old enough.. its just good, right?
+    time_t age = pnd_is_locked ( PNDLOCKNAME );
+
+    if ( age == 0 ) {
+      pnd_log ( pndn_debug, "Lock is all clear %s so no need to wait\n", PNDLOCKNAME );
+
+    } else if ( time ( NULL ) - age > pnd_conf_get_as_int_d ( g_conf, "minimenu.disco_lock_maxage_s", 120 ) ) {
+      // lock seems old, so who cares
+      pnd_log ( pndn_debug, "Lock is OLD so ignoring it: %s\n", PNDLOCKNAME );
 
-      pnd_box_append ( g_active_apps, merge_apps );
     } else {
-      g_active_apps = merge_apps;
+      // not too old, so lets wait.. we could be booting up!
+
+      int rv = pnd_wait_for_unlock ( PNDLOCKNAME,
+                                    pnd_conf_get_as_int_d ( g_conf, "minimenu.disco_lock_max", 20 ),
+                                    pnd_conf_get_as_int_d ( g_conf, "minimenu.disco_lock_usec_delta", 500000 ) );
+      if ( rv ) {
+       pnd_log ( pndn_debug, "Waited for lock and is now clear %s\n", PNDLOCKNAME );
+      } else {
+       pnd_log ( pndn_debug, "Waited for lock and timed out %s .. proceeding anyway.\n", PNDLOCKNAME );
+      }
+
+    } // locking
+
+    // where to scan..
+    char *chunks[10] = {
+      pnd_conf_get_as_char ( g_desktopconf, "desktop.dotdesktoppath" ),
+      pnd_conf_get_as_char ( g_desktopconf, "menu.dotdesktoppath" ),
+      pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" ),
+      //"/usr/share/applications",
+      NULL
+    };
+    char ddpath [ 1024 ];
+    unsigned int flags = PND_DOTDESKTOP_LIBPND_ONLY;
+
+    if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.disco_dotdesktop_all", 0 ) ) {
+      flags = 0; // get all
+    }
+
+    // app box?
+    if ( ! g_active_apps ) {
+      g_active_apps = pnd_box_new ( "discovery-dotdesktop" );
     }
-  }
+
+    // for each searchpath..
+    unsigned char i;
+    for ( i = 0; i < 5; i++ ) {
+
+      if ( ! chunks [ i ] ) {
+       break;
+      }
+
+      DIR *d = opendir ( chunks [ i ] );
+
+      if ( d ) {
+       struct dirent *de = readdir ( d );
+
+       // for each filename found
+       while ( de ) {
+
+         if ( strcmp ( de -> d_name, "." ) == 0 ) {
+           // irrelevent
+         } else if ( strcmp ( de -> d_name, ".." ) == 0 ) {
+           // irrelevent
+         } else {
+           snprintf ( ddpath, 1024, "%s/%s", chunks [ i ], de -> d_name );
+           pnd_disco_t *p = pnd_parse_dotdesktop ( ddpath, flags );
+           if ( p ) {
+             // store
+             pnd_disco_t *ai = pnd_box_allocinsert ( g_active_apps, ddpath, sizeof(pnd_disco_t) );
+             memmove ( ai, p, sizeof(pnd_disco_t) );
+             // free
+             free ( p );
+           }
+         }
+
+         // next!
+         de = readdir ( d );
+       }
+
+       closedir ( d );
+
+      } // for each dir
+
+    } // for each searchpath
+
+  } // app discovery in .desktops
 
   // do it
   g_active_appcount = pnd_box_get_size ( g_active_apps );
@@ -590,12 +705,20 @@ void applications_scan ( void ) {
   pnd_log ( pndn_debug, "Found pnd applications, and caching icons:\n" );
   pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
   unsigned int itercount = 0;
+  unsigned loadlater = pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 );
   while ( iter ) {
     //pnd_log ( pndn_debug, "  App: '%s'\n", IFNULL(iter->title_en,"No Name") );
 
+    // dump
+#if 0
+    printf ( "App %s\t%s\t Cat %s:%s:%s %s:%s:%s\n", iter -> title_en, iter -> unique_id,
+            iter -> main_category, iter -> main_category1, iter -> main_category2,
+            iter -> alt_category, iter -> alt_category1, iter -> alt_category2 );
+#endif
+
     // update cachescreen
-    // ... every 5 filenames, just to avoid slowing it too much
-    if ( itercount % 5 == 0 ) {
+    // ... every 10 filenames, just to avoid slowing it too much
+    if ( loadlater == 0 && itercount % 10 == 0 ) {
       ui_cachescreen ( 0 /* clear screen */, IFNULL(iter->title_en,"No Name") );
     }
 
@@ -617,15 +740,44 @@ void applications_scan ( void ) {
       }
 #endif
 
+      if ( ovrh ) {
+       // lets also check to see if this ovr is specifying category overrides; if so, we can trust those
+       // more than categories specified by pnd-packager.
+       char ovrkey [ 41 ];
+
+       snprintf ( ovrkey, 40, "Application-%u.maincategory", iter -> subapp_number );
+       if ( pnd_conf_get_as_char ( ovrh, ovrkey ) ) {
+         iter -> object_flags |= PND_DISCO_CUSTOM1;
+         //printf ( "App '%s' has main cat ovr %s\n", iter -> title_en, pnd_conf_get_as_char ( ovrh, ovrkey ) );
+       }
+
+       snprintf ( ovrkey, 40, "Application-%u.maincategorysub1", iter -> subapp_number );
+       if ( pnd_conf_get_as_char ( ovrh, ovrkey ) ) {
+         iter -> object_flags |= PND_DISCO_CUSTOM2;
+         //printf ( "App '%s' has sub cat ovr %s\n", iter -> title_en, pnd_conf_get_as_char ( ovrh, ovrkey ) );
+       }
+
+      } // got ovr loaded/
+
     } // ovr
 
     // cache the icon, unless deferred
     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 0 ) {
-      if ( iter -> pnd_icon_pos &&
-          ! cache_icon ( iter, maxwidth, maxheight ) )
+
+      // if app was from a pnd and has an icon-pos (we've already found where it is in the binary),
+      // OR its a .desktop and we've got a path
+      // THEN go try to cache/load the icon
+      if ( ( iter -> pnd_icon_pos ) ||
+          ( iter -> icon && iter -> object_flags & PND_DISCO_LIBPND_DD )
+        )
       {
-       pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
+
+       if ( ! cache_icon ( iter, maxwidth, maxheight ) ) {
+         pnd_log ( pndn_warning, "  WARNING: Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
+       }
+
       }
+
     }
 
     // cache the preview --> SHOULD DEFER
@@ -644,7 +796,7 @@ void applications_scan ( void ) {
         ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) == pnd_pxml_x11_required && g_x11_present == 1 )
        )
     {
+
       if ( iter -> title_en == NULL || iter -> title_en [ 0 ] == '\0' ) {
        // null title; just skip it.
       } else {
@@ -658,12 +810,19 @@ void applications_scan ( void ) {
        // is this app suppressed? if not, show it in whatever categories the user is allowing
        if ( iter -> unique_id && app_is_visible ( g_conf, iter -> unique_id ) ) {
 
+#if 0
+         pnd_log ( pndn_rem, "App %s [%s] cat %s %s %s alt %s %s %s\n",
+                   iter -> unique_id, IFNULL(iter->title_en,"n/a"),
+                   IFNULL(iter->main_category,"n/a"), IFNULL(iter->main_category1,"n/a"), IFNULL(iter->main_category2,"n/a"),
+                   IFNULL(iter->alt_category,"n/a"), IFNULL(iter->alt_category1,"n/a"), IFNULL(iter->alt_category2,"n/a") );
+#endif
+
          // main categories
          category_meta_push ( iter -> main_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category ), 1);
          category_meta_push ( iter -> main_category1, iter -> main_category, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category1 ), 0 );
          category_meta_push ( iter -> main_category2, iter -> main_category, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category2 ), 0 );
          // alt categories
-         category_meta_push ( iter -> alt_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category ), 1 );
+         category_meta_push ( iter -> alt_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category ), 2 );
          category_meta_push ( iter -> alt_category1, iter -> alt_category, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category1 ), 0 );
          category_meta_push ( iter -> alt_category2, iter -> alt_category, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category2 ), 0 );
 
@@ -713,6 +872,9 @@ void applications_scan ( void ) {
   // let deferred icon cache go now
   ui_post_scan();
 
+  // log completion
+  pnd_log ( pndn_debug, "Applications scan done.\n" );
+
   return;
 }
 
@@ -810,7 +972,7 @@ void emit_and_run ( char *buffer ) {
   } else {
     // child, do it
     execl ( "/bin/sh", "/bin/sh", "-c", buffer + strlen(MM_RUN) + 1, (char*) NULL );
-  } 
+  }
 
   return;
 }