From a9c9e10abcc62fa40468da4ab8c6bc2c7aa43392 Mon Sep 17 00:00:00 2001 From: skeezix Date: Wed, 2 Nov 2011 17:13:30 -0400 Subject: [PATCH] Added basic libpnd function to scan a .desktop and create a disco-t from it --- include/pnd_desktop.h | 1 + lib/pnd_desktop.c | 127 ++++++++++++++++++++++++++++++++++++++++++ test/discotest.c | 19 ++++++- 3 files changed, 145 insertions(+), 2 deletions(-) diff --git a/include/pnd_desktop.h b/include/pnd_desktop.h index d9c52e9..94bc2eb 100644 --- a/include/pnd_desktop.h +++ b/include/pnd_desktop.h @@ -14,6 +14,7 @@ extern "C" { #define PND_DOTDESKTOP_HEADER "[Desktop Entry]" #define PND_DOTDESKTOP_SOURCE "X-Pandora-Source=libpnd" unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ); +pnd_disco_t *pnd_parse_dotdesktop ( char *ddpath ); // emit_dotinfo() will spit out a .desktop 'info entry', similar to the way emit_dotdesktop does its thing // - rather than slide this into emit_dotdesktop(), we wish to allow apps to do this or not by calling this diff --git a/lib/pnd_desktop.c b/lib/pnd_desktop.c index 84e420a..2d20809 100644 --- a/lib/pnd_desktop.c +++ b/lib/pnd_desktop.c @@ -3,6 +3,7 @@ #include #include /* for unlink */ #include /* for free */ +#include /* for stat */ #include "pnd_apps.h" #include "pnd_container.h" @@ -724,3 +725,129 @@ unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen return ( target ); } + +// parse_dotdesktop() can be used to read a libpnd generated .desktop and return a limited +// but useful disco-t structure back; possibly useful for scanning .desktops rather than +// scanning pnd-files? +pnd_disco_t *pnd_parse_dotdesktop ( char *ddpath ) { + + // will verify the .desktop has the libpnd-marking on it (X-Pandora-Source): PND_DOTDESKTOP_SOURCE + + // attempt to extract.. + // - unique-id (from filename or field) + // - subapp number (from filename) + // - exec required info + // - icon path + // - name (title-en) + // - comment (desc-en) + // - option_no_x11 + // - object path + // - appdata name (or unique-id if not present) + // - start dir + // - args + // - clockspeed + // - categories + + // determine file length + struct stat statbuf; + + if ( stat ( ddpath, &statbuf) < 0 ) { + return ( NULL ); // couldn't open + } + + // buffers.. + char dd [ 1024 ]; + unsigned char libpnd_origin = 0; + + // disco + pnd_disco_t *p = malloc ( sizeof(pnd_disco_t) ); + if ( ! p ) { + return ( NULL ); + } + bzero ( p, sizeof(pnd_disco_t) ); + + // inhale file + FILE *f = fopen ( ddpath, "r" ); + + if ( ! f ) { + return ( NULL ); // not up or shut up! + } + + while ( fgets ( dd, 1024, f ) ) { + char *nl = strchr ( dd, '\n' ); + if ( nl ) { + *nl = '\0'; + } + + // grep + // + + if ( strncmp ( dd, "Name=", 5 ) == 0 ) { + p -> title_en = strdup ( dd + 5 ); + } else if ( strncmp ( dd, "Icon=", 5 ) == 0 ) { + p -> icon = strdup ( dd + 5 ); + } else if ( strcmp ( dd, PND_DOTDESKTOP_SOURCE ) == 0 ) { + libpnd_origin = 1; + } else if ( strncmp ( dd, "X-Pandora-UID=", 14 ) == 0 ) { + p -> unique_id = strdup ( dd + 14 ); + } else if ( strncmp ( dd, "Comment=", 8 ) == 0 ) { + p -> desc_en = strdup ( dd + 8 ); + } else if ( strncmp ( dd, "Exec=", 5 ) == 0 ) { + + char *e = strstr ( dd, " -e " ); + if ( e ) { + e += 5; + + char *space = strchr ( e, ' ' ); + p -> exec = strndup ( e, space - e - 1 ); + } + + char *b = strstr ( dd, " -b " ); + if ( b ) { + b += 5; + char *space = strchr ( b, '\0' ); + p -> appdata_dirname = strndup ( b, space - b - 1 ); + } + + } else if ( strncmp ( dd, "Categories=", 11 ) == 0 ) { + // HACK; only honours first category + char *semi = strchr ( dd, ';' ); + if ( semi ) { + p -> main_category = strndup ( dd + 11, semi - dd + 11 ); + } else { + p -> main_category = strdup ( dd + 11 ); + } + semi = strchr ( p -> main_category, ';' ); + if ( semi ) { + *semi = '\0'; + } + } + + // + // /grep + + } // while + + fclose ( f ); + + // filter + if ( ! libpnd_origin ) { + pnd_disco_destroy ( p ); + free ( p ); + return ( NULL ); + } + + // additional + p -> object_type = pnd_object_type_pnd; + char *slash = strrchr ( ddpath, '/' ); + if ( slash ) { + p -> object_path = strndup ( ddpath, slash - ddpath ); + p -> object_filename = strdup ( slash + 1 ); + } else { + p -> object_path = "./"; + p -> object_filename = strdup ( ddpath ); + } + + // return disco-t + return ( p ); +} diff --git a/test/discotest.c b/test/discotest.c index 580d3d4..8745706 100644 --- a/test/discotest.c +++ b/test/discotest.c @@ -20,6 +20,7 @@ int main ( int argc, char *argv[] ) { unsigned char do_exec = 0; unsigned char do_icon = 0; unsigned char do_dotdesktop = 0; + char dotdesktoppath [ 1024 ] = ""; for ( i = 1; i < argc; i++ ) { @@ -32,11 +33,15 @@ int main ( int argc, char *argv[] ) { } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) { printf ( "Will attempt to extract dotdesktop.\n" ); do_dotdesktop = 1; + } else if ( strstr ( argv [ i ], ".desktop" ) ) { + strncpy ( dotdesktoppath, argv [ i ], 1023 ); + printf ( "Will scan '%s' instead of performing discovery\n", dotdesktoppath ); } else { - printf ( "%s [-e] [-i] [-d]\n", argv [ 0 ] ); + printf ( "%s [-e] [-i] [-d] [dotdesktop-file]\n", argv [ 0 ] ); printf ( "-e\tOptional. Attempt to exec a random app.\n" ); printf ( "-i\tOptional. Attempt to dump icon files from the end of pnd's to ./testdata/dotdesktop.\n" ); printf ( "-d\tOptional. Attempt to dump dotdesktop files from the end of pnd's to ./testdata/dotdesktop.\n" ); + printf ( "dotdesktop-file\tIf path specified and ends with .desktop, try to scan that .desktop instead of pnd-discovery\n" ); exit ( 0 ); } @@ -109,7 +114,17 @@ int main ( int argc, char *argv[] ) { */ pnd_box_handle applist; - applist = pnd_disco_search ( appspath, overridespath ); + if ( dotdesktoppath [ 0 ] ) { + pnd_disco_t *p = pnd_parse_dotdesktop ( dotdesktoppath ); + pnd_box_handle disco_box = pnd_box_new ( "discovery" ); + if ( p ) { + pnd_disco_t *ai = pnd_box_allocinsert ( disco_box, dotdesktoppath, sizeof(pnd_disco_t) ); + memmove ( ai, p, sizeof(pnd_disco_t) ); + } + applist = disco_box; + } else { + applist = pnd_disco_search ( appspath, overridespath ); + } // list the found apps (if any) -- 2.39.5