1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-26 13:08:26 +02:00

The links will now be loaded from both the system and the user-specific directories.

This commit is contained in:
Ayla 2011-07-11 02:49:44 +02:00
parent 439d25c292
commit f7817b19ab
2 changed files with 73 additions and 54 deletions

View File

@ -39,35 +39,9 @@ Menu::Menu(GMenu2X *gmenu2x) {
this->gmenu2x = gmenu2x; this->gmenu2x = gmenu2x;
iFirstDispSection = 0; iFirstDispSection = 0;
DIR *dirp; readSections(GMENU2X_SYSTEM_DIR "/sections");
struct stat st; readSections(GMenu2X::getHome() + "/sections");
struct dirent *dptr;
string filepath;
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()); sort(sections.begin(),sections.end(),case_less());
setSectionIndex(0); setSectionIndex(0);
readLinks(); readLinks();
@ -81,6 +55,34 @@ uint Menu::firstDispRow() {
return iFirstDispRow; 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() { void Menu::loadIcons() {
//reload section icons //reload section icons
for (uint i=0; i<sections.size(); i++) { for (uint i=0; i<sections.size(); i++) {
@ -163,11 +165,6 @@ void Menu::setSectionIndex(int i) {
iFirstDispRow = 0; iFirstDispRow = 0;
} }
string Menu::sectionPath(int section) {
if (section<0 || section>(int)sections.size()) section = iSection;
return GMenu2X::getHome()+"/sections/"+sections[section]+"/";
}
/*==================================== /*====================================
LINKS MANAGEMENT LINKS MANAGEMENT
====================================*/ ====================================*/
@ -207,7 +204,11 @@ bool Menu::addLink(string path, string file, string section) {
title = title.substr(0, pos); 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; int x=2;
while (fileExists(linkpath)) { while (fileExists(linkpath)) {
stringstream ss; stringstream ss;
@ -293,8 +294,12 @@ bool Menu::addLink(string path, string file, string section) {
} }
bool Menu::addSection(const string &sectionName) { bool Menu::addSection(const string &sectionName) {
string sectiondir = GMenu2X::getHome()+"/sections/"+sectionName; string sectiondir = GMenu2X::getHome() + "/sections";
if (mkdir(sectiondir.c_str(),0777)==0) { if (!fileExists(sectiondir))
mkdir(sectiondir.c_str(), 0755);
sectiondir = sectiondir + "/" + sectionName;
if (mkdir(sectiondir.c_str(), 0755) == 0) {
sections.push_back(sectionName); sections.push_back(sectionName);
linklist ll; linklist ll;
links.push_back(ll); links.push_back(ll);
@ -403,32 +408,44 @@ void Menu::setLinkIndex(int i) {
iLink = i; iLink = i;
} }
void Menu::readLinksOfSection(std::string path, std::vector<std::string> &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() { void Menu::readLinks() {
vector<string> linkfiles; vector<string> linkfiles;
iLink = 0; iLink = 0;
iFirstDispRow = 0; iFirstDispRow = 0;
DIR *dirp;
struct stat st;
struct dirent *dptr;
string filepath; string filepath;
for (uint i=0; i<links.size(); i++) { for (uint i=0; i<links.size(); i++) {
links[i].clear(); links[i].clear();
linkfiles.clear(); linkfiles.clear();
if ((dirp = opendir(sectionPath(i).c_str())) == NULL) continue; int correct = (i>sections.size() ? iSection : i);
while ((dptr = readdir(dirp))) { readLinksOfSection(GMENU2X_SYSTEM_DIR "/sections/"
if (dptr->d_name[0]=='.') continue; + sections[correct], linkfiles);
filepath = sectionPath(i)+dptr->d_name;
int statRet = stat(filepath.c_str(), &st); readLinksOfSection(GMenu2X::getHome() + "/sections/"
if (S_ISDIR(st.st_mode)) continue; + sections[correct], linkfiles);
if (statRet != -1) {
linkfiles.push_back(filepath);
}
}
sort(linkfiles.begin(), linkfiles.end(),case_less()); sort(linkfiles.begin(), linkfiles.end(),case_less());
for (uint x=0; x<linkfiles.size(); x++) { for (uint x=0; x<linkfiles.size(); x++) {
@ -439,8 +456,6 @@ void Menu::readLinks() {
else else
free(link); free(link);
} }
closedir(dirp);
} }
} }

View File

@ -48,6 +48,12 @@ private:
void readLinks(); void readLinks();
void freeLinks(); void freeLinks();
// Load all the sections of the given "sections" directory.
void readSections(std::string parentDir);
// Load all the links on the given section directory.
void readLinksOfSection(std::string path, std::vector<std::string> &linkfiles);
public: public:
Menu(GMenu2X *gmenu2x); Menu(GMenu2X *gmenu2x);
~Menu(); ~Menu();
@ -80,8 +86,6 @@ public:
void linkDown(); void linkDown();
void setLinkIndex(int i); void setLinkIndex(int i);
string sectionPath(int section = -1);
const vector<string> &getSections() { return sections; } const vector<string> &getSections() { return sections; }
void renameSection(int index, const string &name); void renameSection(int index, const string &name);
}; };