1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2025-04-21 12:27:27 +03:00

Only allow empty sections to be deleted

Since not all links can be deleted, an action that deletes a section
and all the links it contains cannot be reliably implemented.
Therefore, the action will only be offered if all links have already
been deleted (or were never added in the first place).
This commit is contained in:
Maarten ter Huurne
2015-04-25 21:59:51 +02:00
parent aeb9c12f5d
commit c2759f1263
22 changed files with 13 additions and 59 deletions

View File

@@ -71,9 +71,11 @@ ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu)
options.push_back(std::make_shared<MenuOption>(
tr["Add section"],
std::bind(&GMenu2X::addSection, &gmenu2x)));
options.push_back(std::make_shared<MenuOption>(
tr["Delete section"],
std::bind(&GMenu2X::deleteSection, &gmenu2x)));
if (menu.sectionLinks()->empty()) {
options.push_back(std::make_shared<MenuOption>(
tr["Delete section"],
std::bind(&GMenu2X::deleteSection, &gmenu2x)));
}
// Compute bounding box.
int w = 0;

View File

@@ -1024,15 +1024,14 @@ void GMenu2X::addSection() {
}
}
void GMenu2X::deleteSection() {
MessageBox mb(*this,tr["You will lose all the links in this section."]+"\n"+tr["Are you sure?"]);
mb.setButton(InputManager::ACCEPT, tr["Yes"]);
mb.setButton(InputManager::CANCEL, tr["No"]);
if (mb.exec() == InputManager::ACCEPT) {
if (rmtree(getHome() + "/sections/" + menu->selSection()))
menu->deleteSelectedSection();
void GMenu2X::deleteSection()
{
string path = getHome() + "/sections/" + menu->selSection();
if (rmdir(path.c_str()) && errno != ENOENT) {
WARNING("Removal of section dir \"%s\" failed: %s\n",
path.c_str(), strerror(errno));
}
menu->deleteSelectedSection();
}
#ifdef ENABLE_CPUFREQ

View File

@@ -71,8 +71,6 @@ private:
*/
void calcSectionRange(int &leftSection, int &rightSection);
std::vector<std::unique_ptr<Link>> *sectionLinks(int i = -1);
void readLinks();
void freeLinks();
@@ -160,6 +158,7 @@ public:
void setLinkIndex(int i);
const std::vector<std::string> &getSections() { return sections; }
std::vector<std::unique_ptr<Link>> *sectionLinks(int i = -1);
};
#endif // MENU_H

View File

@@ -160,34 +160,6 @@ bool fileExists(const string &file) {
return access(file.c_str(), F_OK) == 0;
}
bool rmtree(string path) {
DIR *dirp;
struct stat st;
struct dirent *dptr;
string filepath;
DEBUG("RMTREE: '%s'\n", path.c_str());
if ((dirp = opendir(path.c_str())) == NULL) return false;
if (path[path.length()-1]!='/') path += "/";
while ((dptr = readdir(dirp))) {
filepath = dptr->d_name;
if (filepath=="." || filepath=="..") continue;
filepath = path+filepath;
int statRet = stat(filepath.c_str(), &st);
if (statRet == -1) continue;
if (S_ISDIR(st.st_mode)) {
if (!rmtree(filepath)) return false;
} else {
if (unlink(filepath.c_str())!=0) return false;
}
}
closedir(dirp);
return rmdir(path.c_str())==0;
}
int constrain(int x, int imin, int imax) {
return min(imax, max(imin, x));
}

View File

@@ -79,7 +79,6 @@ inline std::string trimExtension(std::string const& filename) {
}
bool fileExists(const std::string &file);
bool rmtree(std::string path);
int constrain(int x, int imin, int imax);