pndevmapperd: support multiple charge devices
[pandora-libraries.git] / minimenu / mmapps.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #define __USE_GNU /* for strcasestr */
5 #include <string.h>
6 #include <sys/types.h> /* for stat(2) */
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <dirent.h>
10
11 #include "../lib/pnd_pathiter.h"
12 #include "pnd_logger.h"
13 #include "pnd_pxml.h"
14 #include "pnd_container.h"
15 #include "pnd_conf.h"
16 #include "pnd_discovery.h"
17 #include "pnd_desktop.h"
18
19 #include "mmapps.h"
20 #include "mmui.h"
21
22 mm_app_t *apps_fullscan ( char *searchpath ) {
23   mm_app_t *apphead = NULL;
24   mm_app_t *n = NULL;
25
26   SEARCHPATH_PRE
27   {
28     pnd_log ( pndn_debug, "Scanning path '%s'\n", buffer );
29
30     DIR *d = opendir ( buffer );
31     struct dirent *de;
32     char *c;
33     char fullpath [ PATH_MAX ];
34
35     if ( d ) {
36
37       while ( ( de = readdir ( d ) ) ) {
38         pnd_log ( pndn_debug, "  Found file: '%s'\n", de -> d_name );
39
40         // candidate?
41         if ( ( c = strrchr ( de -> d_name, '.' ) ) &&
42              ( strcasecmp ( c, ".desktop" ) == 0 ) )
43         {
44           pnd_log ( pndn_debug, "    ..filename suggests a .desktop\n" );
45
46           sprintf ( fullpath, "%s/%s", buffer, de -> d_name );
47
48           n = apps_fetch_from_dotdesktop ( fullpath );
49
50           if ( n ) {
51             // got an app, prepend to the applist
52             pnd_log ( pndn_rem, "Found application '%s': '%s'\n", n -> dispname, n -> exec );
53             if ( ! apphead ) {
54               apphead = n;
55               apphead -> next = NULL;
56             } else {
57               n -> next = apphead;
58               apphead = n;
59             }
60           } else {
61             pnd_log ( pndn_debug, "  No application found.\n" );
62           } // if got an app back
63
64         } else {
65           pnd_log ( pndn_debug, "    ..filename suggests ignore\n" );
66         }
67
68       } // while
69
70       closedir ( d );
71     } else {
72       pnd_log ( pndn_warning, "WARN: Couldn't open directory '%s', skipping\n" );
73     }
74
75
76   }
77   SEARCHPATH_POST
78
79   return ( apphead );
80 }
81
82 mm_app_t *apps_fetch_from_dotdesktop ( char *path ) {
83
84   FILE *f = fopen ( path, "r" );
85   char buffer [ 1024 ];
86   mm_app_t *p = (mm_app_t *) malloc ( sizeof(mm_app_t) );
87   char *c;
88
89   if ( ! f ) {
90     if ( p ) {
91       free ( p );
92     }
93     return ( NULL );
94   }
95
96   if ( ! p ) {
97     fclose ( f );
98     return ( NULL );
99   }
100
101   bzero ( p, sizeof(mm_app_t) );
102
103   unsigned char apptype = 0;
104   unsigned char pndcreated = 0;
105
106   while ( fgets ( buffer, 1000, f ) ) {
107     char *equals;
108
109     // chop
110     if ( ( c = strchr ( buffer, '\n' ) ) ) {
111       *c = '\0'; // truncate trailing newline
112     }
113
114     //pnd_log ( pndn_debug, ".desktop line: '%s'\n", buffer );
115
116     if ( strcmp ( buffer, PND_DOTDESKTOP_SOURCE ) == 0 ) {
117       pndcreated = 1;
118     }
119
120     if ( ( equals = strchr ( buffer, '=' ) ) ) {
121       *equals = '\0';
122     }
123
124     if ( strcasecmp ( buffer, "type" ) == 0 &&
125          strcasecmp ( equals + 1, "application" ) == 0 )
126     {
127       apptype = 1;
128     }
129
130     if ( strcasecmp ( buffer, "name" ) == 0 ) {
131       p -> dispname = strdup ( equals + 1 );
132     }
133
134     if ( strcasecmp ( buffer, "exec" ) == 0 ) {
135       p -> exec = strdup ( equals + 1 );
136     }
137
138     if ( strcasecmp ( buffer, "icon" ) == 0 ) {
139       p -> iconpath = strdup ( equals + 1 );
140     }
141
142   } // while
143
144   fclose ( f );
145
146   if ( ! apptype ) {
147     pnd_log ( pndn_debug, ".desktop is not an application; ignoring.\n" );
148     free ( p );
149     return ( NULL ); // not an application
150   }
151
152   if ( ! pndcreated ) {
153     pnd_log ( pndn_debug, ".desktop is not from libpnd; ignoring.\n" );
154     free ( p );
155     return ( NULL ); // not created by libpnd
156   }
157
158   return ( p );
159 }