From: skeezix Date: Tue, 24 Feb 2009 05:03:36 +0000 (-0500) Subject: Couple optimizations -- X-Git-Tag: Release-2010-05/1~204 X-Git-Url: http://git.openpandora.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db097feab6f7c0d2ed6ff334200668d1a01d0dbe;p=pandora-libraries.git Couple optimizations -- - when about to remove non-existant apps .desktop file, check if it has _Source=libpnd line in it. If not, leave it alone.. could be a bundled app, or user created or something - when about to extract icons, if the file already exists, don't bother extracting it again --- diff --git a/apps/pndnotifyd.c b/apps/pndnotifyd.c index 665c4f6..45b204f 100644 --- a/apps/pndnotifyd.c +++ b/apps/pndnotifyd.c @@ -12,6 +12,7 @@ #include // for stdio #include // for exit() #include // for exit() +#define __USE_GNU /* for strcasestr */ #include #include // for time() #include // for isdigit() @@ -251,15 +252,42 @@ int main ( int argc, char *argv[] ) { printf ( "Found app: %s\n", pnd_box_get_key ( d ) ); } - // attempt to create icon files; if successful, alter the disco struct to contain new - // path, otherwise leave it alone (since it could be a generic icon reference..) - if ( pnd_emit_icon ( dotdesktoppath, d ) ) { - // success; fix up icon path to new one.. - free ( d -> icon ); - char buffer [ FILENAME_MAX ]; - sprintf ( buffer, "%s/%s.png", dotdesktoppath, d -> unique_id ); - d -> icon = strdup ( buffer ); - } + // check if icon already exists (from a previous extraction say); if so, we needn't + // do it again + char existingpath [ FILENAME_MAX ]; + sprintf ( existingpath, "%s/%s.png", dotdesktoppath, d -> unique_id ); + + struct stat dirs; + if ( stat ( existingpath, &dirs ) == 0 ) { + // icon seems to exist, so just crib the location into the .desktop + + if ( ! g_daemon_mode ) { + printf ( " Found icon already existed, so reusing it! %s\n", existingpath ); + } + + if ( d -> icon ) { + free ( d -> icon ); + } + d -> icon = strdup ( existingpath ); + + } else { + // icon seems unreadable or does not exist; lets try to create it.. + + if ( ! g_daemon_mode ) { + printf ( " Icon not already present, so trying to write it! %s\n", existingpath ); + } + + // attempt to create icon files; if successful, alter the disco struct to contain new + // path, otherwise leave it alone (since it could be a generic icon reference..) + if ( pnd_emit_icon ( dotdesktoppath, d ) ) { + // success; fix up icon path to new one.. + if ( d -> icon ) { + free ( d -> icon ); + } + d -> icon = strdup ( existingpath ); + } + + } // icon already exists? // create the .desktop file if ( pnd_emit_dotdesktop ( dotdesktoppath, pndrun, d ) ) { @@ -312,14 +340,45 @@ int main ( int argc, char *argv[] ) { continue; } + // figure out full path + sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name ); + // file was previously created by libpnd; check Source= line - // TBD + // logic: default to 'yes' (in case we can't open the file for some reason) + // if we can open the file, default to no and look for the source flag we added; if + // that matches then we know its libpnd created, otherwise assume not. + unsigned char source_libpnd = 1; + { + char line [ 256 ]; + FILE *grep = fopen ( buffer, "r" ); + if ( grep ) { + source_libpnd = 0; + while ( fgets ( line, 255, grep ) ) { + if ( strcasestr ( line, PND_DOTDESKTOP_SOURCE ) ) { + source_libpnd = 2; + } + } // while + fclose ( grep ); + } + } + if ( source_libpnd ) { + if ( ! g_daemon_mode ) { + printf ( "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd ); + } + } else { + if ( ! g_daemon_mode ) { + printf ( "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer ); + } + continue; // skip deleting it + } // file is 'new'? - sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name ); if ( stat ( buffer, &dirs ) == 0 ) { if ( dirs.st_mtime >= createtime ) { - continue; + if ( ! g_daemon_mode ) { + printf ( "File '%s' seems 'new', so leave it alone.\n", buffer ); + } + continue; // skip deleting it } } diff --git a/include/pnd_discovery.h b/include/pnd_discovery.h index 4019e73..19982fe 100644 --- a/include/pnd_discovery.h +++ b/include/pnd_discovery.h @@ -61,6 +61,7 @@ void pnd_disco_destroy ( pnd_disco_t *p ); // a function name that simply could // emit_dotdesktop() will determine a filename and create a FILENAME.desktop file in the targetpath // TODO: Copy the icon into this directory as well, if its source is a .pnd or info is in the dico struct +#define PND_DOTDESKTOP_SOURCE "_Source=libpnd" unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ); // emit_icon() will attempt to copy the icon from a PXML directory, or from a pnd file if appended, diff --git a/lib/pnd_discovery.c b/lib/pnd_discovery.c index 6ca6205..cb09703 100644 --- a/lib/pnd_discovery.c +++ b/lib/pnd_discovery.c @@ -308,7 +308,7 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t fprintf ( f, "%s", buffer ); } - fprintf ( f, "_Source=libpnd\n" ); // should we need to know 'who' created the file during trimming + fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming fclose ( f ); diff --git a/testdata/pndsample/x86_echo.pnd b/testdata/pndsample/x86_echo.pnd index ad943f6..5437373 100644 Binary files a/testdata/pndsample/x86_echo.pnd and b/testdata/pndsample/x86_echo.pnd differ diff --git a/testdata/pndsample/x86_ls.pnd b/testdata/pndsample/x86_ls.pnd index d412a70..994b6b0 100644 Binary files a/testdata/pndsample/x86_ls.pnd and b/testdata/pndsample/x86_ls.pnd differ