12 #include "pnd_logger.h"
14 #include "pnd_container.h"
15 #include "pnd_discovery.h"
16 #include "../lib/pnd_pathiter.h"
21 #include "freedesktop_cats.h"
23 // all categories known -- visible, hidden, subcats, whatever.
24 pnd_box_handle m_categories = NULL;
25 unsigned int m_categorycount = 0;
27 // all categories being published right now -- a subset copied from m_categories
28 mm_category_t *g_categories [ MAX_CATS ];
29 unsigned char g_categorycount;
32 mm_catmap_t g_catmaps [ MAX_CATS ];
33 unsigned char g_catmapcount = 0;
35 extern pnd_conf_handle g_conf;
37 void category_init ( void ) {
38 m_categories = pnd_box_new ( "Schrodinger's cat" );
42 unsigned char category_push ( char *catname, char *parentcatname, pnd_disco_t *app, pnd_conf_handle ovrh, char *fspath, unsigned char visiblep ) {
45 // check category list; if found, append app to the end of it.
46 // if not found, add it to the category list and plop the app in there.
47 // app's are just app-refs, which contain links to the disco-t list -- thus removal is only in one place, and
48 // an app can be in multiple categories if we like..
51 // find or create category
54 if ( ( c = pnd_box_find_by_key ( m_categories, catname ) ) ) {
55 // category was found..
57 // category wasn't found..
58 //pnd_log ( PND_LOG_DEFAULT, "New category '%s'\n", catname );
59 c = pnd_box_allocinsert ( m_categories, catname, sizeof(mm_category_t) );
60 c -> catname = strdup ( catname );
61 if ( parentcatname ) {
62 c -> parent_catname = strdup ( parentcatname );
66 c -> catflags = CFNORMAL;
68 c -> catflags = CFHIDDEN;
71 // if user prefers subcats-as-folders, lets reflag this sucker
72 if ( parentcatname && pnd_conf_get_as_int_d ( g_conf, "tabs.subcat_as_folders", 1 ) ) {
73 //printf ( "subcat: %s parent: %s\n", catname, parentcatname ? parentcatname : "none" );
74 c -> catflags = CFSUBCAT;
80 c -> fspath = strdup ( fspath );
87 return ( 1 ); // create cat, but skip app
90 // alloc and populate appref
92 mm_appref_t *ar = malloc ( sizeof(mm_appref_t) );
97 bzero ( ar, sizeof(mm_appref_t) );
102 // plug it into category
103 // and sort it on insert!
105 ar -> next = c -> refs;
107 #else // with sorting
108 // if no refs at all, or new guy has no title, just stick it in at head
109 if ( c -> refs && ar -> ref -> title_en ) {
110 mm_appref_t *iter = c -> refs;
111 mm_appref_t *last = NULL;
115 if ( iter -> ref -> title_en ) {
116 if ( cat_sort_score ( c, ar, iter ) < 0 ) {
117 // new guy is smaller than the current guy!
121 // since new guy must have a name by here, we're bigger than any guy who does not have a name
130 // smaller than the current guy, so stitch in
135 ar -> next = c -> refs;
139 // we're the biggest, just append to last
144 ar -> next = c -> refs;
153 int cat_sort_score ( mm_category_t *cat, mm_appref_t *s1, mm_appref_t *s2 ) {
155 // are we in a directory browser, or looking at pnd-files?
156 if ( cat -> fspath ) {
157 // directory browser mode
160 return ( 0 ); // equal
162 } else if ( s1 -> ref -> object_type == pnd_object_type_directory &&
163 s2 -> ref -> object_type == pnd_object_type_directory )
165 // both are directories, be nice
166 return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
167 } else if ( s1 -> ref -> object_type == pnd_object_type_directory &&
168 s2 -> ref -> object_type != pnd_object_type_directory )
170 return ( -1 ); // dir on the left is earlier than file on the right
171 } else if ( s1 -> ref -> object_type != pnd_object_type_directory &&
172 s2 -> ref -> object_type == pnd_object_type_directory )
174 return ( 1 ); // dir on the right is earlier than file on the left
177 return ( strcmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
185 // if this is comparing subcat folder to subcat folder, or pnd to pnd, or pnd to subcat folder?
186 unsigned char s1sub = 0;
187 unsigned char s2sub = 0;
188 if ( s1 -> ref -> object_type == pnd_object_type_directory ) {
191 if ( s2 -> ref -> object_type == pnd_object_type_directory ) {
195 if ( ( s1sub ) && ( s2sub ) ) {
196 return ( strcasecmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
197 } else if ( ( s1sub ) && ( ! s2sub ) ) {
199 } else if ( ( ! s1sub ) && ( s2sub ) ) {
201 } else if ( ( ! s1sub ) && ( ! s2sub ) ) {
202 return ( strcasecmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
205 return ( strcasecmp ( s1 -> ref -> title_en, s2 -> ref -> title_en ) );
208 void category_dump ( void ) {
210 // WHY AREN'T I SORTING ON INSERT?
213 mm_category_t *iter = pnd_box_get_head ( m_categories );
214 unsigned int counter = 0;
218 pnd_log ( PND_LOG_DEFAULT, "Category %u: '%s' * %u\n", counter, iter -> catname, iter -> refcount );
219 mm_appref_t *ar = iter -> refs;
222 pnd_log ( PND_LOG_DEFAULT, " Appref %s\n", IFNULL(ar -> ref -> title_en,"No Name") );
226 iter = pnd_box_get_next ( iter );
233 void category_freeall ( void ) {
234 mm_category_t *c, *cnext;
235 mm_appref_t *iter, *next;
237 c = pnd_box_get_head ( m_categories );
240 cnext = pnd_box_get_next ( c );
253 if ( c -> catname ) {
254 free ( c -> catname );
259 free ( c -> fspath );
263 pnd_box_delete_node ( m_categories, c );
272 unsigned char category_map_setup ( void ) {
274 char *searchpath = pnd_box_get_head ( g_conf );
276 if ( ! searchpath ) {
280 // look through conf for useful keys
281 while ( searchpath ) {
282 char *k = pnd_box_get_key ( searchpath );
284 // does this key look like a category mapping key?
285 if ( strncasecmp ( k, "categories.@", 12 ) == 0 ) {
288 // iterate across 'words' in v, assigning catmaps to them
291 //pnd_log ( pndn_debug, "target(%s) from(%s)\n", k, buffer );
293 category_push ( k, NULL /* parent cat */, NULL, 0, NULL /* fspath */, 1 );
294 g_catmaps [ g_catmapcount ].target = pnd_box_find_by_key ( m_categories, k );
295 g_catmaps [ g_catmapcount ].from = strdup ( buffer );
301 } // if key looks like catmap
303 searchpath = pnd_box_get_next ( searchpath );
304 } // while each conf key
309 mm_category_t *category_map_query ( char *cat ) {
312 for ( i = 0; i < g_catmapcount; i++ ) {
313 if ( strcasecmp ( g_catmaps [ i ].from, cat ) == 0 ) {
314 return ( g_catmaps [ i ].target );
321 unsigned char category_meta_push ( char *catname, char *parentcatname, pnd_disco_t *app, pnd_conf_handle ovrh, unsigned char visiblep ) {
324 char catnamebuffer [ 512 ] = "";
327 //fprintf ( stderr, "meta push: '%s'\n", catname );
330 return ( 1 ); // fine, just nada
333 // we don't screw with "All" category that mmenu.c generates on the fly
334 if ( strncmp ( catname, "All ", 4 ) == 0 ) {
335 goto category_done_audit;
338 // category cleansing; lets..
339 // - ensure we only let good freedesktop categories through
340 // - we fix case.. no more UtIliTy (a good cat, studlycaps)
341 // - no more good cats but swapped ancestry; Utility as child of something?
342 // - if bogus, we just ship it off to BAD_CAT
344 unsigned char cat_is_clean = 1;
345 freedesktop_cat_t *fdcat = NULL, *fdpcat = NULL;
346 fdcat = freedesktop_category_query ( catname );
347 if ( parentcatname ) {
348 fdpcat = freedesktop_category_query ( parentcatname );
351 // ensure requested cat is good
353 // requested cat is bad, send it to Other
355 printf ( "PXML Fail %s: Cat request %s (parent %s) -> bad cat\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
357 // do the Other substitution right away, so remaining code has something to look at in fdcat
358 fdcat = freedesktop_category_query ( BADCATNAME );
359 catname = fdcat -> cat;
361 parentcatname = NULL;
364 // use canonicle entry, so our Case is now correct!
365 catname = fdcat -> cat;
368 // ensure parent is good, if specified
369 if ( parentcatname ) {
371 // requested cat is bad, send it to Other
373 printf ( "PXML Fail %s: Cat request %s (parent %s) -> parent bad cat\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
374 // fix immediately so code doesn't explode
375 parentcatname = NULL;
377 // use canonicle entry, so our Case is now correct!
378 parentcatname = fdpcat -> cat;
382 // ensure ancestry is good
383 // - if cat request is for child, ensure its a child
384 // - if parent specified, ensure its a parent
385 // - if child specified, ensure its parent is the right parent(?!)
387 if ( parentcatname ) {
388 // implies catname request is for child, with parent parentcatname
390 if ( fdcat -> parent_cat == NULL ) {
391 // but wait, catname is actually a parent cat...
393 printf ( "PXML Fail %s: Cat request %s (parent %s) -> cat wants to be child, but FD says its a parent\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
395 if ( fdpcat -> parent_cat ) {
396 // but wait, parent cat is actually a subcat!
398 printf ( "PXML Fail %s: Cat request %s (parent %s) -> parent cat, FD says its a child\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
402 // implies request is for a parent cat - itself has no parent
404 if ( fdcat -> parent_cat ) {
405 // but wait, cat actually has a parent!
407 printf ( "PXML Fail %s: Cat request %s (parent %s) -> cat wants to be parent, FD says its a child\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
412 // ensure that if this is a child cat, its parent is the right parent
413 if ( parentcatname ) {
414 if ( ( ! fdcat -> parent_cat ) ||
417 // child cat points to a different parent than requested parent!
419 printf ( "PXML Fail %s: Cat request %s (parent %s) -> cat wants to be child of a cat which FD says is the wrong parent (1)\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
420 } else if ( strcasecmp ( fdcat -> parent_cat, fdpcat -> cat ) != 0 ) {
421 // child cat points to a different parent than requested parent!
423 printf ( "PXML Fail %s: Cat request %s (parent %s) -> cat wants to be child of a cat which FD says is the wrong parent (2)\n", app -> title_en ? app -> title_en : "no name?", catname, parentcatname ? parentcatname : "n/a" );
427 // did testing fail? if so, bump to Other!
429 if ( ! cat_is_clean ) {
430 // set Other visibility
431 visiblep = cat_is_visible ( g_conf, BADCATNAME );
433 fdcat = freedesktop_category_query ( BADCATNAME );
434 catname = fdcat -> cat;
435 // nullify parent cat request (if any)
437 parentcatname = NULL;
439 //printf ( "PXML Category Pass: Cat request %s (parent %s)\n", catname, parentcatname ? parentcatname : "n/a" );
442 // push bad categories into Other (if we're not targeting All right now)
444 if ( 1 /*pnd_conf_get_as_int_d ( g_conf, "categories.good_cats_only", 1 )*/ ) {
447 if ( strncmp ( catname, "All ", 4 ) != 0 ) {
449 // if this is a parent cat..
450 if ( catname && ! parentcatname ) {
452 // if bad, shove it to Other
453 if ( ! freedesktop_check_cat ( catname ) ) {
454 parentcatname = NULL;
455 catname = BADCATNAME;
456 visiblep = cat_is_visible ( g_conf, catname );
459 } else if ( catname && parentcatname ) {
462 // if parent is bad, then we probably already pushed it over, so don't do it again.
463 // if its parent is okay, but subcat is bad, push it to other. (ie: lets avoid duplication in Other)
464 if ( ! freedesktop_check_cat ( parentcatname ) ) {
468 } else if ( ! freedesktop_check_cat ( catname ) ) {
469 parentcatname = NULL;
470 catname = BADCATNAME;
471 visiblep = cat_is_visible ( g_conf, catname );
474 } // parent or child cat?
483 // if invisible, and a parent category name is known, prepend it for ease of use
485 if ( ! visiblep && parentcatname ) {
486 snprintf ( catnamebuffer, 500, "%s.%s", parentcatname, catname );
487 catname = catnamebuffer;
491 // do we honour cat mapping at all?
492 if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
494 // is this guy mapped?
495 cat = category_map_query ( catname );
498 category_push ( cat -> catname, parentcatname /* parent cat */, app, ovrh, NULL /* fspath */, visiblep );
502 // not mapped.. but default?
503 if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_default_on", 0 ) ) {
504 char *def = pnd_conf_get_as_char ( g_conf, "categories.map_default_cat" );
506 category_push ( def, parentcatname /* parent cat */, app, ovrh, NULL /* fspath */, visiblep );
511 } // cat map is desired?
513 // is app already in the target cat? (ie: its being pushed twice due to cat mapping or Other'ing or something..)
515 if ( category_contains_app ( catname, app -> unique_id ) ) {
516 printf ( "App Fail: app (%s %s) is already in cat %s\n", app -> title_en ? app -> title_en : "no name?", app -> unique_id, catname );
517 return ( 1 ); // success, already there!
521 // not default, just do it
522 category_push ( catname, parentcatname /* parent cat */, app, ovrh, NULL /* fspath */, visiblep );
524 // if subcats as folders, then lets just make up a dummy app that pretends to be a folder,
525 // and stuff it into the parent cat
526 if ( parentcatname && pnd_conf_get_as_int_d ( g_conf, "tabs.subcat_as_folders", 1 ) ) {
528 // it is implicit that since we're talking parentcat, its already been created in a previous call
529 // therefore, we need to..
530 // i) find the parent cat
531 // ii) ensure it already has a faux-disco container
532 // iii) ensure that disco container doesn't already contain a disco-entry for this subcat
533 // iv) create the dummy app folder by pushing the disco into the apprefs as normal
534 // v) create a dummy '..' for going back up, in the child
536 mm_category_t *pcat = pnd_box_find_by_key ( m_categories, parentcatname );
538 if ( ! pcat -> disco ) {
539 pcat -> disco = pnd_box_new ( pcat -> catname );
542 // if this subcat is already in the faux-disco list, then its probably already
543 // handled so we needn't concern ourselves anymore. If not, then we can
544 // create it and push it into the parent as a new 'app'
545 pnd_disco_t *disco = pnd_box_find_by_key ( pcat -> disco, catname );
549 disco = pnd_box_allocinsert ( pcat -> disco, catname, sizeof(pnd_disco_t) );
552 // create the subcat faux-disco entry, and register into parent cat
554 sprintf ( uid, "%p", catname );
556 disco -> unique_id = strdup ( uid );
557 if ( strchr ( catname, '.' ) ) {
558 disco -> title_en = strdup ( strchr ( catname, '.' ) + 1 );
560 disco -> title_en = strdup ( catname );
562 disco -> object_flags = PND_DISCO_GENERATED;
563 disco -> object_type = pnd_object_type_directory; // suggest to Grid that its a dir
564 disco -> object_path = strdup ( catname );
566 category_push ( parentcatname, NULL /* parent cat */, disco, 0 /*ovrh*/, NULL /* fspath */, 1 /* visible */ );
568 // create .. faux-disco entry into child cat
569 disco = pnd_box_allocinsert ( pcat -> disco, catname, sizeof(pnd_disco_t) );
571 sprintf ( uid, "%p", uid );
573 disco -> unique_id = strdup ( uid );
574 disco -> title_en = strdup ( ".." );
575 disco -> object_flags = PND_DISCO_GENERATED;
576 disco -> object_type = pnd_object_type_directory; // suggest to Grid that its a dir
578 category_push ( catname, parentcatname /* parent cat */, disco, 0 /*ovrh*/, NULL /* fspath */, 1 /* visible */ );
580 } // making faux disco entries
582 } // disco already exist?
584 } // subcat as folder?
589 //fprintf ( stderr, "cat meta-push : vis[%30s,%d b] : tally; vis %d invis %d\n", catname, visiblep, g_categorycount, _categories_inviscount );
594 unsigned char category_fs_restock ( mm_category_t *cat ) {
596 if ( ! cat -> fspath ) {
597 return ( 1 ); // not a filesystem browser tab
600 // clear any existing baggage
604 mm_appref_t *iter = cat -> refs, *next;
613 if ( cat -> disco ) {
614 pnd_disco_t *p = pnd_box_get_head ( cat -> disco );
617 n = pnd_box_get_next ( p );
618 pnd_disco_destroy ( p );
621 pnd_box_delete ( cat -> disco );
624 // rescan the filesystem
627 //pnd_log ( pndn_debug, "Restocking cat %s with path %s\n", cat -> catname, cat -> fspath );
630 if ( ( d = opendir ( cat -> fspath ) ) ) {
631 struct dirent *de = readdir ( d );
636 cat -> disco = pnd_box_new ( cat -> catname );
641 char fullpath [ PATH_MAX ];
642 sprintf ( fullpath, "%s/%s", cat -> fspath, de -> d_name );
643 int statret = stat ( fullpath, &buffy );
645 // if file is executable somehow or another
647 buffy.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)
650 // determine unique-id
651 sprintf ( uid, "%d", (int) de -> d_ino );
654 switch ( de -> d_type ) {
657 if ( strcmp ( de -> d_name, "." ) == 0 ) {
658 // ignore ".", but ".." is fine
659 } else if ( strcmp ( de -> d_name, ".." ) == 0 && strcmp ( cat -> fspath, "/" ) == 0 ) {
660 // ignore ".." only if we're at the true root
662 disco = pnd_box_allocinsert ( cat -> disco, uid, sizeof(pnd_disco_t) );
663 disco -> object_type = pnd_object_type_directory; // suggest to Grid that its a dir
668 disco = pnd_box_allocinsert ( cat -> disco, uid, sizeof(pnd_disco_t) );
669 disco -> object_type = pnd_object_type_unknown; // suggest to Grid that its a file
674 // found a directory or executable?
676 // register with current category
677 disco -> unique_id = strdup ( uid );
678 disco -> title_en = strdup ( de -> d_name );
679 disco -> object_flags = PND_DISCO_GENERATED;
680 disco -> object_path = strdup ( cat -> fspath );
681 disco -> object_filename = strdup ( de -> d_name );
682 category_push ( cat -> catname, NULL /* parent cat */, disco, 0 /* no ovr */, NULL /* fspath already set */, 1 /* visible */ );
683 // if a override icon exists, cache it up
684 cache_icon ( disco, pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 ),
685 pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 ) );
700 static int catname_cmp ( const void *p1, const void *p2 ) {
701 //mm_category_t *c1 = (mm_category_t*) p1;
702 //mm_category_t *c2 = (mm_category_t*) p2;
703 mm_category_t *c1 = *( (mm_category_t**) p1 );
704 mm_category_t *c2 = *( (mm_category_t**) p2 );
706 if ( ( isalnum ( c1 -> catname [ 0 ] ) ) && ( ! isalnum ( c1 -> catname [ 1 ] ) ) ) {
708 } else if ( ( ! isalnum ( c1 -> catname [ 0 ] ) ) && ( isalnum ( c1 -> catname [ 1 ] ) ) ) {
710 } else if ( ( ! isalnum ( c1 -> catname [ 0 ] ) ) && ( ! isalnum ( c1 -> catname [ 1 ] ) ) ) {
714 int i = strcasecmp ( c1 -> catname, c2 -> catname );
715 //printf ( "cat name compare %p %s to %p %s = %d\n", p1, c1 -> catname, p2, c2 -> catname, i );
720 void category_sort ( void ) {
721 // we probably don't want to sort tab categories, since the user may have specified an ordering
722 // But we can sort invisi-cats, to make them easier to find, and ordered by parent category
725 qsort ( _categories_invis, _categories_inviscount, sizeof(mm_category_t), catname_cmp );
729 qsort ( g_categories, g_categorycount, sizeof(mm_category_t*), catname_cmp );
735 void category_publish ( unsigned int filter_mask, char *param ) {
736 unsigned char interested;
738 // clear published categories
739 memset ( g_categories, '\0', sizeof(mm_category_t*) * MAX_CATS );
742 // figure out the start
743 mm_category_t *iter = pnd_box_get_head ( m_categories );
745 // for each category we know...
750 // is this category desired?
751 if ( filter_mask == CFALL ) {
753 } else if ( filter_mask == CFBYNAME ) {
754 if ( strcasecmp ( iter -> catname, param ) == 0 ) {
757 } else if ( iter -> catflags == filter_mask ) {
762 // set us up the bomb; notice that we're just duplicating the pointers, not making
763 // any new data here; none of this should ever be free'd!
764 g_categories [ g_categorycount ] = iter;
769 iter = pnd_box_get_next ( iter );
775 for ( i = 0; i < g_categorycount; i++ ) {
776 printf ( "Unsorted cat %d %p: %s\n", i, &(g_categories [ i ]), g_categories [ i ] -> catname );
780 // sort published categories
786 unsigned int category_count ( unsigned int filter_mask ) {
787 mm_category_t *iter = pnd_box_get_head ( m_categories );
788 unsigned int count = 0;
790 // for each category we know...
793 // is this category desired?
794 if ( iter -> catflags == filter_mask ) {
799 iter = pnd_box_get_next ( iter );
805 int category_index ( char *catname ) {
808 for ( i = 0; i < g_categorycount; i++ ) {
810 if ( strcasecmp ( g_categories [ i ] -> catname, catname ) == 0 ) {
819 unsigned char category_contains_app ( char *catname, char *unique_id ) {
821 mm_category_t *c = pnd_box_find_by_key ( m_categories, catname );
824 return ( 0 ); // wtf?
828 return ( 0 ); // no apps at all
831 mm_appref_t *iter = c -> refs;
835 if ( strcmp ( iter -> ref -> unique_id, unique_id ) == 0 ) {