minimenu now has a (lame) 'reveal this normally hidden' category option in the select...
[pandora-libraries.git] / minimenu / mmcat.c
1
2 #include <string.h>
3 #include <strings.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <dirent.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9
10 #include "pnd_conf.h"
11 #include "pnd_logger.h"
12 #include "pnd_pxml.h"
13 #include "pnd_container.h"
14 #include "pnd_discovery.h"
15 #include "../lib/pnd_pathiter.h"
16
17 #include "mmenu.h"
18 #include "mmcache.h"
19 #include "mmcat.h"
20
21 mm_category_t _categories [ MAX_CATS ];
22 mm_category_t *g_categories = _categories;
23 unsigned char g_categorycount = 0;
24
25 mm_category_t _categories_invis [ MAX_CATS ];
26 unsigned char _categories_inviscount = 0;
27
28 mm_catmap_t g_catmaps [ MAX_CATS ];
29 unsigned char g_catmapcount = 0;
30
31 extern pnd_conf_handle g_conf;
32
33 unsigned char category_push ( char *catname, pnd_disco_t *app, pnd_conf_handle ovrh, char *fspath ) {
34   mm_category_t *c;
35
36   // check category list; if found, append app to the end of it.
37   // if not found, add it to the category list and plop the app in there.
38   // app's are just app-refs, which contain links to the disco-t list -- thus removal is only in one place, and
39   // an app can be in multiple categories if we like..
40   //
41
42   // find or create category
43   //
44
45   if ( ( c = category_query ( catname ) ) ) {
46     // category was found..
47   } else {
48     // category wasn't found..
49     pnd_log ( PND_LOG_DEFAULT, "New category '%s'\n", catname );
50     g_categories [ g_categorycount ].catname = strdup ( catname );
51     g_categories [ g_categorycount ].refs = NULL;
52     c = &(g_categories [ g_categorycount ]);
53
54     if ( fspath ) {
55       g_categories [ g_categorycount ].fspath = strdup ( fspath );;
56     }
57
58     g_categorycount++;
59   }
60
61   if ( ! app ) {
62     return ( 1 ); // create cat, but skip app
63   }
64
65   // alloc and populate appref
66   //
67   mm_appref_t *ar = malloc ( sizeof(mm_appref_t) );
68   if ( ! ar ) {
69     return ( 0 );
70   }
71
72   bzero ( ar, sizeof(mm_appref_t) );
73
74   ar -> ref = app;
75   ar -> ovrh = ovrh;
76
77   // plug it into category
78   //   and sort it on insert!
79 #if 0 // no sorting
80   ar -> next = c -> refs;
81   c -> refs = ar;
82 #else // with sorting
83   // if no refs at all, or new guy has no title, just stick it in at head
84   if ( c -> refs && ar -> ref -> title_en ) {
85     mm_appref_t *iter = c -> refs;
86     mm_appref_t *last = NULL;
87
88     while ( iter ) {
89
90       if ( iter -> ref -> title_en ) {
91         if ( cat_sort_score ( ar, iter ) < 0 ) {
92           // new guy is smaller than the current guy!
93           break;
94         }
95       } else {
96         // since new guy must have a name by here, we're bigger than any guy who does not have a name
97         // --> continue
98       }
99
100       last = iter;
101       iter = iter -> next;
102     }
103
104     if ( iter ) {
105       // smaller than the current guy, so stitch in
106       if ( last ) {
107         ar -> next = iter;
108         last -> next = ar;
109       } else {
110         ar -> next = c -> refs;
111         c -> refs = ar;
112       }
113     } else {
114       // we're the biggest, just append to last
115       last -> next = ar;
116     }
117
118   } else {
119     ar -> next = c -> refs;
120     c -> refs = ar;
121   }
122 #endif
123   c -> refcount++;
124
125   return ( 1 );
126 }
127
128 mm_category_t *category_query ( char *catname ) {
129   unsigned char i;
130
131   for ( i = 0; i < g_categorycount; i++ ) {
132
133     if ( strcasecmp ( g_categories [ i ].catname, catname ) == 0 ) {
134       return ( &(g_categories [ i ]) );
135     }
136
137   }
138
139   return ( NULL );
140 }
141
142 int cat_sort_score ( mm_appref_t *s1, mm_appref_t *s2 ) {
143
144   extern unsigned char ui_category;
145
146   // are we in a directory browser, or looking at pnd-files?
147   if ( g_categories [ ui_category ].fspath ) {
148
149     if ( s1 == s2 ) {
150       return ( 0 ); // equal
151
152     } else if ( s1 -> ref -> object_type == pnd_object_type_directory &&
153                 s2 -> ref -> object_type == pnd_object_type_directory )
154     {
155       // both are directories, be nice
156       return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
157     } else if ( s1 -> ref -> object_type == pnd_object_type_directory &&
158                 s2 -> ref -> object_type != pnd_object_type_directory )
159     {
160       return ( -1 ); // dir on the left is earlier than file on the right
161     } else if ( s1 -> ref -> object_type != pnd_object_type_directory &&
162                 s2 -> ref -> object_type == pnd_object_type_directory )
163     {
164       return ( 1 ); // dir on the right is earlier than file on the left
165     } else {
166       // file on file
167       return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
168     }
169
170   }
171
172   return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
173 }
174
175 void category_dump ( void ) {
176   unsigned int i;
177
178   // WHY AREN'T I SORTING ON INSERT?
179
180   // dump
181   for ( i = 0; i < g_categorycount; i++ ) {
182     pnd_log ( PND_LOG_DEFAULT, "Category %u: '%s' * %u\n", i, g_categories [ i ].catname, g_categories [ i ].refcount );
183     mm_appref_t *ar = g_categories [ i ].refs;
184
185     while ( ar ) {
186       pnd_log ( PND_LOG_DEFAULT, "  Appref %s\n", IFNULL(ar -> ref -> title_en,"No Name") );
187       ar = ar -> next;
188     }
189
190   } // for
191
192   return;
193 }
194
195 void category_freeall ( void ) {
196   unsigned int i;
197   mm_appref_t *iter, *next;
198
199   for ( i = 0; i < g_categorycount; i++ ) {
200
201     iter = g_categories [ i ].refs;
202
203     while ( iter ) {
204       next = iter -> next;
205       free ( iter );
206       iter = next;
207     }
208
209     g_categories [ i ].refs = NULL;
210
211   } // for
212
213   g_categorycount = 0;
214
215   return;
216 }
217
218 unsigned char category_map_setup ( void ) {
219
220   char *searchpath = pnd_box_get_head ( g_conf );
221
222   if ( ! searchpath ) {
223     return ( 0 );
224   }
225
226   // look through conf for useful keys
227   while ( searchpath ) {
228     char *k = pnd_box_get_key ( searchpath );
229
230     // does this key look like a category mapping key?
231     if ( strncasecmp ( k, "categories.@", 12 ) == 0 ) {
232       k += 12;
233
234       // iterate across 'words' in v, assigning catmaps to them
235       SEARCHCHUNK_PRE
236       {
237         //pnd_log ( pndn_debug, "target(%s) from(%s)\n", k, buffer );
238
239         category_push ( k, NULL, 0, NULL /* fspath */ );
240         g_catmaps [ g_catmapcount ].target = category_query ( k );
241         g_catmaps [ g_catmapcount ].from = strdup ( buffer );
242         g_catmapcount++;
243
244       }
245       SEARCHCHUNK_POST
246
247     } // if key looks like catmap
248
249     searchpath = pnd_box_get_next ( searchpath );
250   } // while each conf key
251
252   return ( 1 );
253 }
254
255 mm_category_t *category_map_query ( char *cat ) {
256   unsigned char i;
257
258   for ( i = 0; i < g_catmapcount; i++ ) {
259     if ( strcasecmp ( g_catmaps [ i ].from, cat ) == 0 ) {
260       return ( g_catmaps [ i ].target );
261     }
262   }
263
264   return ( NULL );
265 }
266
267 unsigned char category_meta_push ( char *catname, pnd_disco_t *app, pnd_conf_handle ovrh, unsigned char visiblep ) {
268   mm_category_t *cat;
269   unsigned char catcount = g_categorycount;
270
271   if ( ! catname ) {
272     return ( 1 ); // fine, just nada
273   }
274
275   if ( ! visiblep ) {
276     //return ( 1 ); // fine, suppress it
277
278     // serious evidence this was a rushed program
279     g_categories = _categories_invis;
280     g_categorycount = _categories_inviscount;
281
282   }
283
284   // do we honour cat mapping at all?
285   if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
286
287     // is this guy mapped?
288     cat = category_map_query ( catname );
289
290     if ( cat ) {
291       category_push ( cat -> catname, app, ovrh, NULL /* fspath */ );
292       goto visibility_hack_cleanup;
293     }
294
295     // not mapped.. but default?
296     if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_default_on", 0 ) ) {
297       char *def = pnd_conf_get_as_char ( g_conf, "categories.map_default_cat" );
298       if ( def ) {
299         category_push ( def, app, ovrh, NULL /* fspath */ );
300         goto visibility_hack_cleanup;
301       }
302     }
303
304   } // cat map is desired?
305
306   // not default, just do it
307   category_push ( catname, app, ovrh, NULL /* fspath */ );
308   // hack :(
309  visibility_hack_cleanup:
310   if ( ! visiblep ) {
311     _categories_inviscount = g_categorycount;
312     g_categories = _categories;
313     g_categorycount = catcount;
314   }
315   return ( 1 );
316 }
317
318 unsigned char category_fs_restock ( mm_category_t *cat ) {
319
320   if ( ! cat -> fspath ) {
321     return ( 1 ); // not a filesystem browser tab
322   }
323
324   // clear any existing baggage
325   //
326
327   // apprefs
328   mm_appref_t *iter = cat -> refs, *next;
329   while ( iter ) {
330     next = iter -> next;
331     free ( iter );
332     iter = next;
333   }
334   cat -> refs = NULL;
335
336   // discos
337   if ( cat -> disco ) {
338     pnd_disco_t *p = pnd_box_get_head ( cat -> disco );
339     pnd_disco_t *n;
340     while ( p ) {
341       n = pnd_box_get_next ( p );
342       pnd_disco_destroy ( p );
343       p = n;
344     }
345     pnd_box_delete ( cat -> disco );
346   }
347
348   // rescan the filesystem
349   //
350
351   //pnd_log ( pndn_debug, "Restocking cat %s with path %s\n", cat -> catname, cat -> fspath );
352   DIR *d;
353
354   if ( ( d = opendir ( cat -> fspath ) ) ) {
355     struct dirent *de = readdir ( d );
356
357     pnd_disco_t *disco;
358     char uid [ 100 ];
359
360     cat -> disco = pnd_box_new ( cat -> catname );
361
362     while ( de ) {
363
364       struct stat buffy;
365       char fullpath [ PATH_MAX ];
366       sprintf ( fullpath, "%s/%s", cat -> fspath, de -> d_name );
367       int statret = stat ( fullpath, &buffy );
368
369       // if file is executable somehow or another
370       if ( statret == 0 &&
371            buffy.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)
372          )
373       {
374         // determine unique-id
375         sprintf ( uid, "%d", (int) de -> d_ino );
376         disco = NULL;
377
378         switch ( de -> d_type ) {
379
380         case DT_DIR:
381           if ( strcmp ( de -> d_name, "." ) == 0 ) {
382             // ignore ".", but ".." is fine
383           } else {
384             disco = pnd_box_allocinsert ( cat -> disco, uid, sizeof(pnd_disco_t) );
385             disco -> object_type = pnd_object_type_directory; // suggest to Grid that its a dir
386           }
387           break;
388         case DT_UNKNOWN:
389         case DT_REG:
390           disco = pnd_box_allocinsert ( cat -> disco, uid, sizeof(pnd_disco_t) );
391           disco -> object_type = pnd_object_type_unknown; // suggest to Grid that its a file
392           break;
393
394         } // switch
395
396         // found a directory or executable?
397         if ( disco ) {
398           // register with this category
399           disco -> unique_id = strdup ( uid );
400           disco -> title_en = strdup ( de -> d_name );
401           disco -> object_flags = PND_DISCO_GENERATED;
402           disco -> object_path = strdup ( cat -> fspath );
403           disco -> object_filename = strdup ( de -> d_name );
404           category_push ( cat -> catname, disco, 0, NULL /* fspath already set */ );
405           // if a override icon exists, cache it up
406           cache_icon ( disco, pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 ),
407                        pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 ) );
408         }
409
410       } // stat
411
412       // next
413       de = readdir ( d );
414     }
415
416     closedir ( d );
417   }
418
419   return ( 1 );
420 }