wxString Path::GetDesktopDir() { char *desktopDir = xdg_user_dir_lookup_with_fallback("DESKTOP", NULL); if (desktopDir != NULL) { wxString desktop = wxString(desktopDir); std::cout << "Desktop/XDG: " << desktop << std::endl; free (desktopDir); return desktop; } free (desktopDir); wxString homeDir; if (wxGetEnv("HOME", &homeDir)) { wxString desktopDir = Path::Combine(homeDir, "Desktop"); if(!wxDirExists(desktopDir)) { std::cout << "Using home as a fallback: " << homeDir << std::endl; return homeDir; } else { std::cout << "Desktop/Guess: " << desktopDir << std::endl; return desktopDir; } } else { std::cout << "Desktop: not found" << std::endl; return wxEmptyString; } }
/** * xdg_user_dir_lookup: * @type: a string specifying the type of directory * @returns: a newly allocated absolute pathname * * Looks up a XDG user directory of the specified type. * Example of types are "DESKTOP" and "DOWNLOAD". * * The return value is always != NULL (unless out of memory), * and if a directory * for the type is not specified by the user the default * is the home directory. Except for DESKTOP which defaults * to ~/Desktop. * * The return value is newly allocated and must be freed with * free(). **/ static char * xdg_user_dir_lookup (const char *type) { char *dir, *home_dir, *user_dir; dir = xdg_user_dir_lookup_with_fallback (type, NULL); if (dir != NULL) return dir; home_dir = getenv ("HOME"); if (home_dir == NULL) return strdup ("/tmp"); /* Special case desktop for historical compatibility */ if (strcmp (type, "DESKTOP") == 0) { user_dir = (char*) malloc (strlen (home_dir) + strlen ("/Desktop") + 1); if (user_dir == NULL) return NULL; strcpy (user_dir, home_dir); strcat (user_dir, "/Desktop"); return user_dir; } return strdup (home_dir); }
int main(int argc, char **argv) { static bool show_series = false, show_all = false, use_cache = true; static int i_sid = 0; static char usage_str[] = "[SID[:PID]]"; static struct opt_table opts[] = { OPT_WITH_ARG("--items-list|-i", opt_set_intval, NULL, &i_sid, "List episodes in a series. Requires a SID as a parameter."), OPT_WITHOUT_ARG("--series-list|-s", opt_set_bool, &show_series, "List the series available. The first element is the SID."), OPT_WITHOUT_ARG("--all|-a", opt_set_bool, &show_all, "List all items in all non-empty series."), OPT_WITHOUT_ARG("--force|-f", opt_set_invbool, &use_cache, "Force bypass the cached metadata."), OPT_WITHOUT_ARG("--help|-h", opt_usage_and_exit, usage_str, "Show this message."), OPT_ENDTABLE }; opt_register_table(opts, NULL); if(!opt_parse(&argc, argv, opt_log_stderr)) { /* opt_parse will print an error to stderr. */ exit(1); } if (!show_all && !show_series && !i_sid && (argc == 1)) { opt_usage_and_exit(usage_str); } struct iv_series *index; struct iv_config *config; int return_val = 0; cache_dir = xdg_user_dir_lookup_with_fallback("CACHE", "/tmp"); if(NULL == (config = iviewiir_configure())) { fprintf(stderr, "Couldn't configure iviewiir, exiting\n"); return 1; } int index_len = iviewiir_index(config, &index); if(0 >= index_len) { fprintf(stderr, "No items in index, exiting\n"); return_val = 1; goto config_cleanup; } /* Check if they want everything listed */ if(show_all) { list_all(config, index, index_len); return_val = 0; goto index_cleanup; } /* Check if they wanted a series list. */ if(show_series) { int i; for(i=0; i<index_len; i++) { /* Heuristic to trim out empty series. */ if((int)9e6 < index[i].id) { continue; } printf("%d - %s\n", index[i].id, index[i].title); } return_val = 0; goto index_cleanup; } /* Check if they want an episode list. */ if(i_sid) { return_val = list_items(config, index, index_len, i_sid); goto index_cleanup; } /* If we've reached here and there are no arguments, print help message. */ if (argc == 1) { opt_usage_and_exit(usage_str); } /* Otherwise, if they supplied a SID or SID:PID tuple, download the PID */ int i = 1; while(i < argc) { if(NULL != strchr(argv[i], ':')) { // SID:PID const unsigned long sid = strtoul(strtok(argv[i], ":"), NULL, 10); const unsigned int pid = strtoul(strtok(NULL, ":"), NULL, 10); return_val += download_item(config, index, index_len, sid, pid); } else { // Check if it's a valid SID const unsigned int sid = strtoul(argv[i], NULL, 10); struct iv_episode *items; // Fetch episode lists for the SID debug("sid: %d\n", sid); int series_index; if(-1 == (series_index = iv_find_series(sid, index, index_len, NULL))) { printf("No such series"); return_val += 1; continue; } // Fetch items in series ssize_t items_len = iv_easy_series(config, &index[series_index], &items); if(1 > items_len) { printf("No items in series.\n"); return_val += 1; continue; } for(i=0; i<items_len; i++) { return_val += download_item(config, index, index_len, sid, items[i].id); } iv_destroy_series(items, items_len); } i++; } index_cleanup: iv_destroy_index(index, index_len); config_cleanup: iv_destroy_config(config); free(cache_dir); return return_val; }