From db097feab6f7c0d2ed6ff334200668d1a01d0dbe Mon Sep 17 00:00:00 2001 From: skeezix Date: Tue, 24 Feb 2009 00:03:36 -0500 Subject: [PATCH] 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 --- apps/pndnotifyd.c | 83 +++++++++++++++++++++++++++----- include/pnd_discovery.h | 1 + lib/pnd_discovery.c | 2 +- testdata/pndsample/x86_echo.pnd | Bin 387551 -> 387551 bytes testdata/pndsample/x86_ls.pnd | Bin 489775 -> 489775 bytes 5 files changed, 73 insertions(+), 13 deletions(-) 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 ad943f659161980a8df94d450eb691e4c32e3241..54373736357703f454dd9dd46ca151251a443497 100644 GIT binary patch delta 94 zcmcbAS^WNG@eSt9qNb*X76t}yj0_Ac4UCM8jf_D8lYN@ySU`-;dCdlGy!w!hiI`q=;g>^~za delta 94 zcmcbAS^WNG@eSt9qJ|a*<^~3Dj0_Ac4UCM8jf_D8lYN@ySU`-;dCdlGy!w!hiI`q=;g&E6tm diff --git a/testdata/pndsample/x86_ls.pnd b/testdata/pndsample/x86_ls.pnd index d412a70155b13b583ccee9631f27cab838b5719b..994b6b0808b12e9e3b812ecfbadc1fb9374eec27 100644 GIT binary patch delta 98 zcmZ4gNp}4w*$w8+qNb*X76t}yj0_Ac4UCM8jf_D8lYN@ySU`-;dCdlGy!fS7grn;mS<2LN>OB_#j= delta 98 zcmZ4gNp}4w*$w8+qJ|a*<^~3Dj0_Ac4UCM8jf_D8lYN@ySU`-;dCdlGy!fS7grn;mS<2LMxSBwPRh -- 2.39.5