Json::Value Firnplayer::ScanDirectory(const std::string &Root) { // Initializations DIR *dir; struct dirent *ent; std::list<std::string> DirectoryQueue; Json::Value AllJson; // Push the root to the queue and start the scan loop DirectoryQueue.push_back(Root); while(DirectoryQueue.size()){ // Grap the front of the queue and erase it from the queue std::string ThisDirectory = *DirectoryQueue.begin(); DirectoryQueue.erase(DirectoryQueue.begin()); if((dir = opendir(ThisDirectory.c_str())) != NULL) { // Try to open the directory while ((ent = readdir(dir)) != NULL) { // First, see if it is a directory. If so, we add id to the queue for later scans. if(ent->d_type == 4 && *ent->d_name != '.') DirectoryQueue.push_back(cleanpath(ThisDirectory + "/" + ent->d_name)); else{ // If it is not a queue, we look closer at the file. // First, we want to see if it has the .mp3 ending. std::string FileName = cleanpath(ThisDirectory + "/" + ent->d_name); std::string LastFour(" "); std::transform(FileName.begin()+(FileName.size()-4), FileName.end(), LastFour.begin(), ::toupper); if(LastFour == ".MP3") { // Ok, it calls itself an mp3. Scan it! Json::Value &TrackJson = (AllJson[FileName] = Json::Value()); TagLib::MPEG::File file(FileName.c_str()); // If it has an ID3v2Tag, we use that. if(file.hasID3v2Tag()) { TagLib::ID3v2::Tag *tag = file.ID3v2Tag(true); for(TagLib::List<TagLib::ID3v2::Frame *>::ConstIterator theitr = tag->frameList().begin(); theitr != tag->frameList().end(); theitr++) { std::string framevalue = (*theitr)->toString().to8Bit(); std::string Trigger((*theitr)->frameID().data()); Trigger = std::string(Trigger.begin(), Trigger.begin()+4); TrackJson[Trigger] = framevalue; } } // Now save the file path, the bitrate and the track length. TrackJson["FILE"] = FileName; TagLib::MPEG::Properties *Properties = file.audioProperties(); TrackJson["LENG"] = Properties->length(); TrackJson["BITR"] = Properties->bitrate(); } } } } } return AllJson; }
// Constructor Firnplayer::Player::Player() { ConfigDir = cleanpath(HomePath() + "/.firnplayer"); ConfigFile = cleanpath(ConfigDir + "/Configuration.json"); TrackInfoFile = cleanpath(ConfigDir + "/TrackInfo.json"); ThePlayer = this; // Check if Config path exists and make if not. if(!FileExists(ConfigDir)) { std::printf("Config folder %s does not exist, creating.\n", ConfigDir.c_str()); boost::filesystem::create_directory(ConfigDir); if(!FileExists(ConfigDir)) { FatalErrorString = StringPrintf("Could not create directory %s, aborting\n", ConfigDir.c_str()); } } // Load and sanitize config file. if(FileExists(ConfigFile)) { std::printf("Reading Config\n"); LoadJson(ConfigFile, Options); } SanitizeConfig(); ActiveViewPort = ACTIVE_ARTISTS; // Set the frame colours ArtistView.SetFrameColPair(COLPAIR_FRAME); TrackView.SetFrameColPair(COLPAIR_FRAME); QueueView.SetFrameColPair(COLPAIR_FRAME); PlaylistView.SetFrameColPair(COLPAIR_FRAME); // Load track info if it exists. std::string TrackDBPath = cleanpath(ConfigDir + "/TrackInfo.Json"); if(FileExists(TrackDBPath)) { std::printf("Loading Track Info\n"); if(!LoadJson(TrackDBPath, TrackList)) // No queue lock. No other threads should be running. { std::printf("Stored track info file corrupt, starting empty.\n"); } else { Threads["Sorter"] = std::thread(Sortfunction, std::ref(Artists), std::ref(ArtistsQueue), std::ref(TrackList), std::ref(TrackListQueue)); } } Running = 1; }
void FileLocator::addPath(const char* newPathBegin,const char* newPathEnd) { /* Prefix relative paths with the current working directory: */ if(newPathEnd-newPathBegin>1&&newPathBegin[0]=='/') pathList.push_back(cleanpath(newPathBegin,newPathEnd)); else if(newPathEnd-newPathBegin>0) { char cwd[PATH_MAX]; if(getcwd(cwd,PATH_MAX)!=0) { std::string absPath=cwd; absPath.append(1,'/'); absPath.append(newPathBegin,newPathEnd); pathList.push_back(cleanpath(absPath.c_str())); } else pathList.push_back(cleanpath(newPathBegin,newPathEnd)); } }
int re_rotate_wallpaper(char *wallpaper) { char *path2wallpaper, *wall_base, *wall_path; DIR *dirp; struct dirent *dp; int ret; int found = FALSE; /* Suppose someone is doing something nasty with `lastwallpaper' */ cleanpath(wallpaper); path2wallpaper = malloc(MAXPATHLEN); if (path2wallpaper == NULL) err(1, "out of memory"); wall_path = malloc(MAXPATHLEN); if (wall_path == NULL) err(1, "out of memory"); wall_base = malloc(MAXPATHLEN); if (wall_base == NULL) err(1, "out of memory"); /* dirname ruins wallpaper, so run basename first. */ ret = snprintf(wall_base, MAXPATHLEN, "%s", basename(wallpaper)); if (ret < 0 || ret >= MAXPATHLEN) errx(1, "arguments out of bounds for wallpaperpath"); ret = snprintf(wall_path, MAXPATHLEN, "%s", dirname(wallpaper)); if (ret < 0 || ret >= MAXPATHLEN) errx(1, "arguments out of bounds for wallpaperpath"); #ifdef DEBUG printf("path: %s ;name: %s\n", wall_path, wall_base); #endif /* Open the directory */ if ((dirp = opendir(wall_path)) == NULL) err(1, "%s: ", wall_path); /* First lets find the current wallpaper in this dir. */ while ((dp = readdir(dirp)) != NULL) { /* step over . and .. */ if (dp->d_name[0] == '.') { if (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' && dp->d_name[2] == '\0')) continue; } if (strcmp (wall_base, dp->d_name) == 0) { /* We found our wallpaper, lets step to the next one. * If it happened to be the last member of list, cycle * and read the first entry. */ found = TRUE; if ((dp = readdir(dirp)) == NULL) { rewinddir(dirp); dp = readdir(dirp); } } /* Now that we found our wallpaper the _next_ one with a valid * extension will be set. */ if (found == TRUE && checkextension(dp->d_name) == 0) { ret = snprintf(path2wallpaper, MAXPATHLEN, "%s/%s", wall_path, dp->d_name); if (ret < 0 || ret >= MAXPATHLEN) errx(1, "arguments out of bounds" " for wallpaperpath"); set_wallpaper('o', path2wallpaper); break; } #ifdef DEBUG printf("looking for:%s ;current: %s\n", wall_base, dp->d_name); #endif } closedir(dirp); free(path2wallpaper); free(wall_path); free(wall_base); if (found == TRUE) return(TRUE); else errx(1, "no wallpaper was found"); }
void FileLocator::addPathFromApplication(const char* executablePath) { /* Separate the executable name from the executable path: */ const char* slashPtr=findLastOf(executablePath,'/'); std::string appName=slashPtr!=0?std::string(slashPtr+1):std::string(executablePath); /* Add the standard resource search path for private installed applications: */ if(getenv("HOME")!=0) { std::string path=getenv("HOME"); path.append("/."); path.append(appName); addPath(path); } /* Add the standard resource search paths for system-wide installed applications: */ addPath("/usr/share/"+appName); addPath("/usr/local/share/"+appName); /* Construct the fully-qualified executable name: */ std::string fullExePath; if(executablePath[0]=='/') fullExePath=executablePath; else { /* Try to find the full path to the executable: */ #ifdef __LINUX__ /* Get the full executable path through the /proc interface: */ char pse[PATH_MAX]; int pseLength=readlink("/proc/self/exe",pse,PATH_MAX); if(pseLength>=0) fullExePath=std::string(pse,pse+pseLength); else { #else /* Search for the executable just as the shell did: */ if(slashPtr==0&&getenv("PATH")!=0) { /* Search for the executable in the PATH list: */ const char* pathBegin=getenv("PATH"); while(*pathBegin!='\0') { /* Find the end of the next path: */ const char* pathEnd; for(pathEnd=pathBegin;*pathEnd!='\0'&&*pathEnd!=':';++pathEnd) ; /* Test the path if it is non-empty: */ if(pathEnd!=pathBegin) { /* Construct the full path name: */ std::string testName; if(*pathBegin!='/') { /* Start the path name with the current directory: */ char cwd[PATH_MAX]; if(getcwd(cwd,PATH_MAX)) { testName.append(cwd); testName.append(1,'/'); } } testName.append(pathBegin,pathEnd); testName.append(1,'/'); testName.append(appName); /* Test if the file exists and is an executable: */ struct stat testStat; if(stat(testName.c_str(),&testStat)==0) { /* Save the matching full path and stop searching: */ fullExePath=testName; break; } } /* Go to the next path: */ if(*pathEnd!='\0') ++pathEnd; pathBegin=pathEnd; } } else { /* Use the provided path starting at the current directory: */ char cwd[PATH_MAX]; if(getcwd(cwd,PATH_MAX)!=0) { fullExePath=cwd; fullExePath.append(1,'/'); } fullExePath.append(executablePath); } #endif #ifdef __LINUX__ } #endif } /* Find the last slash in the cleaned fully-qualified executable path name: */ std::string cleanFullExePath=cleanpath(fullExePath.c_str()); executablePath=cleanFullExePath.c_str(); slashPtr=findLastOf(executablePath,'/'); #ifdef __LINUX__ /* Check if the executable is part of a Linux application bundle: */ if(slashPtr!=0) { if(slashPtr-executablePath>=4&&strncasecmp(slashPtr-4,"/exe",4)==0) { /* Add the bundle's base directory to the search path list: */ addPath(executablePath,slashPtr-4); } else if(slashPtr-executablePath>=7&&strncasecmp(slashPtr-7,"/exe/64",7)==0) { /* Add the bundle's base directory to the search path list: */ addPath(executablePath,slashPtr-7); } } #endif #ifdef __DARWIN__ /* Check if the executable is part of a Mac OS X application bundle: */ if(slashPtr!=0&&slashPtr-executablePath>=19&&strncasecmp(slashPtr-19,".app/Contents/MacOS",19)==0) { /* Add the bundle's resource directory to the search path list: */ addPath(std::string(executablePath,slashPtr-5)+"Resources"); } #endif }