From f7817b19abac1ef1adde67c75bdf676fd5319893 Mon Sep 17 00:00:00 2001 From: Ayla Date: Mon, 11 Jul 2011 02:49:44 +0200 Subject: [PATCH] The links will now be loaded from both the system and the user-specific directories. --- src/menu.cpp | 119 +++++++++++++++++++++++++++++---------------------- src/menu.h | 8 +++- 2 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/menu.cpp b/src/menu.cpp index 248b168..c93ad61 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -39,35 +39,9 @@ Menu::Menu(GMenu2X *gmenu2x) { this->gmenu2x = gmenu2x; iFirstDispSection = 0; - DIR *dirp; - struct stat st; - struct dirent *dptr; - string filepath; + readSections(GMENU2X_SYSTEM_DIR "/sections"); + readSections(GMenu2X::getHome() + "/sections"); - dirp = opendir((GMenu2X::getHome()+"/sections").c_str()); - if (dirp == NULL) { - mkdir((GMenu2X::getHome()+"/sections").c_str(), 0770); - mkdir((GMenu2X::getHome()+"/sections/settings").c_str(), 0770); - mkdir((GMenu2X::getHome()+"/sections/applications").c_str(), 0770); - mkdir((GMenu2X::getHome()+"/sections/emulators").c_str(), 0770); - mkdir((GMenu2X::getHome()+"/sections/games").c_str(), 0770); - - dirp = opendir((GMenu2X::getHome()+"/sections").c_str()); - } - - while ((dptr = readdir(dirp))) { - if (dptr->d_name[0]=='.') continue; - filepath = GMenu2X::getHome()+(string)"/sections/"+dptr->d_name; - int statRet = stat(filepath.c_str(), &st); - if (!S_ISDIR(st.st_mode)) continue; - if (statRet != -1) { - sections.push_back((string)dptr->d_name); - linklist ll; - links.push_back(ll); - } - } - - closedir(dirp); sort(sections.begin(),sections.end(),case_less()); setSectionIndex(0); readLinks(); @@ -81,6 +55,34 @@ uint Menu::firstDispRow() { return iFirstDispRow; } +void Menu::readSections(std::string parentDir) +{ + DIR *dirp; + struct stat st; + struct dirent *dptr; + + dirp = opendir(parentDir.c_str()); + if (!dirp) return; + + while ((dptr = readdir(dirp))) { + int statret; + if (dptr->d_name[0]=='.') continue; + + string filepath = parentDir + "/" + dptr->d_name; + statret = stat(filepath.c_str(), &st); + if (!S_ISDIR(st.st_mode)) continue; + if (statret != -1) { + if (find(sections.begin(), sections.end(), (string)dptr->d_name) == sections.end()) { + sections.push_back((string)dptr->d_name); + linklist ll; + links.push_back(ll); + } + } + } + + closedir(dirp); +} + void Menu::loadIcons() { //reload section icons for (uint i=0; i(int)sections.size()) section = iSection; - return GMenu2X::getHome()+"/sections/"+sections[section]+"/"; -} - /*==================================== LINKS MANAGEMENT ====================================*/ @@ -207,7 +204,11 @@ bool Menu::addLink(string path, string file, string section) { title = title.substr(0, pos); } - string linkpath = GMenu2X::getHome()+"/sections/"+section+"/"+title; + string linkpath = GMenu2X::getHome()+"/sections/"+section; + if (!fileExists(linkpath)) + mkdir(linkpath.c_str(), 0755); + + linkpath += "/" + title; int x=2; while (fileExists(linkpath)) { stringstream ss; @@ -293,8 +294,12 @@ bool Menu::addLink(string path, string file, string section) { } bool Menu::addSection(const string §ionName) { - string sectiondir = GMenu2X::getHome()+"/sections/"+sectionName; - if (mkdir(sectiondir.c_str(),0777)==0) { + string sectiondir = GMenu2X::getHome() + "/sections"; + if (!fileExists(sectiondir)) + mkdir(sectiondir.c_str(), 0755); + + sectiondir = sectiondir + "/" + sectionName; + if (mkdir(sectiondir.c_str(), 0755) == 0) { sections.push_back(sectionName); linklist ll; links.push_back(ll); @@ -403,32 +408,44 @@ void Menu::setLinkIndex(int i) { iLink = i; } +void Menu::readLinksOfSection(std::string path, std::vector &linkfiles) +{ + DIR *dirp; + struct stat st; + struct dirent *dptr; + + if ((dirp = opendir(path.c_str())) == NULL) return; + + while ((dptr = readdir(dirp))) { + if (dptr->d_name[0] == '.') continue; + string filepath = path + "/" + dptr->d_name; + int statret = stat(filepath.c_str(), &st); + if (S_ISDIR(st.st_mode)) continue; + if (statret != -1) + linkfiles.push_back(filepath); + } + + closedir(dirp); +} + void Menu::readLinks() { vector linkfiles; iLink = 0; iFirstDispRow = 0; - - DIR *dirp; - struct stat st; - struct dirent *dptr; string filepath; for (uint i=0; isections.size() ? iSection : i); - while ((dptr = readdir(dirp))) { - if (dptr->d_name[0]=='.') continue; - filepath = sectionPath(i)+dptr->d_name; - int statRet = stat(filepath.c_str(), &st); - if (S_ISDIR(st.st_mode)) continue; - if (statRet != -1) { - linkfiles.push_back(filepath); - } - } + readLinksOfSection(GMENU2X_SYSTEM_DIR "/sections/" + + sections[correct], linkfiles); + + readLinksOfSection(GMenu2X::getHome() + "/sections/" + + sections[correct], linkfiles); sort(linkfiles.begin(), linkfiles.end(),case_less()); for (uint x=0; x &linkfiles); + public: Menu(GMenu2X *gmenu2x); ~Menu(); @@ -80,8 +86,6 @@ public: void linkDown(); void setLinkIndex(int i); - string sectionPath(int section = -1); - const vector &getSections() { return sections; } void renameSection(int index, const string &name); };