11 #include "pnd_logger.h"
13 #include "pnd_container.h"
14 #include "pnd_discovery.h"
15 #include "../lib/pnd_pathiter.h"
21 mm_category_t _categories [ MAX_CATS ];
22 mm_category_t *g_categories = _categories;
23 unsigned char g_categorycount = 0;
25 mm_category_t _categories_invis [ MAX_CATS ];
26 unsigned char _categories_inviscount = 0;
28 mm_catmap_t g_catmaps [ MAX_CATS ];
29 unsigned char g_catmapcount = 0;
31 extern pnd_conf_handle g_conf;
33 unsigned char category_push ( char *catname, pnd_disco_t *app, pnd_conf_handle ovrh, char *fspath ) {
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..
42 // find or create category
45 if ( ( c = category_query ( catname ) ) ) {
46 // category was found..
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 ]);
55 g_categories [ g_categorycount ].fspath = strdup ( fspath );;
62 return ( 1 ); // create cat, but skip app
65 // alloc and populate appref
67 mm_appref_t *ar = malloc ( sizeof(mm_appref_t) );
72 bzero ( ar, sizeof(mm_appref_t) );
77 // plug it into category
78 // and sort it on insert!
80 ar -> next = c -> refs;
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;
90 if ( iter -> ref -> title_en ) {
91 if ( cat_sort_score ( ar, iter ) < 0 ) {
92 // new guy is smaller than the current guy!
96 // since new guy must have a name by here, we're bigger than any guy who does not have a name
105 // smaller than the current guy, so stitch in
110 ar -> next = c -> refs;
114 // we're the biggest, just append to last
119 ar -> next = c -> refs;
128 mm_category_t *category_query ( char *catname ) {
131 for ( i = 0; i < g_categorycount; i++ ) {
133 if ( strcasecmp ( g_categories [ i ].catname, catname ) == 0 ) {
134 return ( &(g_categories [ i ]) );
142 int cat_sort_score ( mm_appref_t *s1, mm_appref_t *s2 ) {
144 extern unsigned char ui_category;
146 // are we in a directory browser, or looking at pnd-files?
147 if ( g_categories [ ui_category ].fspath ) {
150 return ( 0 ); // equal
152 } else if ( s1 -> ref -> object_type == pnd_object_type_directory &&
153 s2 -> ref -> object_type == pnd_object_type_directory )
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 )
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 )
164 return ( 1 ); // dir on the right is earlier than file on the left
167 return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
172 return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
175 void category_dump ( void ) {
178 // WHY AREN'T I SORTING ON INSERT?
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;
186 pnd_log ( PND_LOG_DEFAULT, " Appref %s\n", IFNULL(ar -> ref -> title_en,"No Name") );
195 void category_freeall ( void ) {
197 mm_appref_t *iter, *next;
199 for ( i = 0; i < g_categorycount; i++ ) {
201 iter = g_categories [ i ].refs;
209 g_categories [ i ].refs = NULL;
218 unsigned char category_map_setup ( void ) {
220 char *searchpath = pnd_box_get_head ( g_conf );
222 if ( ! searchpath ) {
226 // look through conf for useful keys
227 while ( searchpath ) {
228 char *k = pnd_box_get_key ( searchpath );
230 // does this key look like a category mapping key?
231 if ( strncasecmp ( k, "categories.@", 12 ) == 0 ) {
234 // iterate across 'words' in v, assigning catmaps to them
237 //pnd_log ( pndn_debug, "target(%s) from(%s)\n", k, buffer );
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 );
247 } // if key looks like catmap
249 searchpath = pnd_box_get_next ( searchpath );
250 } // while each conf key
255 mm_category_t *category_map_query ( char *cat ) {
258 for ( i = 0; i < g_catmapcount; i++ ) {
259 if ( strcasecmp ( g_catmaps [ i ].from, cat ) == 0 ) {
260 return ( g_catmaps [ i ].target );
267 unsigned char category_meta_push ( char *catname, pnd_disco_t *app, pnd_conf_handle ovrh, unsigned char visiblep ) {
269 unsigned char catcount = g_categorycount;
272 return ( 1 ); // fine, just nada
276 //return ( 1 ); // fine, suppress it
278 // serious evidence this was a rushed program
279 g_categories = _categories_invis;
280 g_categorycount = _categories_inviscount;
284 // do we honour cat mapping at all?
285 if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
287 // is this guy mapped?
288 cat = category_map_query ( catname );
291 category_push ( cat -> catname, app, ovrh, NULL /* fspath */ );
292 goto visibility_hack_cleanup;
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" );
299 category_push ( def, app, ovrh, NULL /* fspath */ );
300 goto visibility_hack_cleanup;
304 } // cat map is desired?
306 // not default, just do it
307 category_push ( catname, app, ovrh, NULL /* fspath */ );
309 visibility_hack_cleanup:
311 _categories_inviscount = g_categorycount;
312 g_categories = _categories;
313 g_categorycount = catcount;
318 unsigned char category_fs_restock ( mm_category_t *cat ) {
320 if ( ! cat -> fspath ) {
321 return ( 1 ); // not a filesystem browser tab
324 // clear any existing baggage
328 mm_appref_t *iter = cat -> refs, *next;
337 if ( cat -> disco ) {
338 pnd_disco_t *p = pnd_box_get_head ( cat -> disco );
341 n = pnd_box_get_next ( p );
342 pnd_disco_destroy ( p );
345 pnd_box_delete ( cat -> disco );
348 // rescan the filesystem
351 //pnd_log ( pndn_debug, "Restocking cat %s with path %s\n", cat -> catname, cat -> fspath );
354 if ( ( d = opendir ( cat -> fspath ) ) ) {
355 struct dirent *de = readdir ( d );
360 cat -> disco = pnd_box_new ( cat -> catname );
365 char fullpath [ PATH_MAX ];
366 sprintf ( fullpath, "%s/%s", cat -> fspath, de -> d_name );
367 int statret = stat ( fullpath, &buffy );
369 // if file is executable somehow or another
371 buffy.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)
374 // determine unique-id
375 sprintf ( uid, "%d", (int) de -> d_ino );
378 switch ( de -> d_type ) {
381 if ( strcmp ( de -> d_name, "." ) == 0 ) {
382 // ignore ".", but ".." is fine
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
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
396 // found a directory or executable?
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 ) );