1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-05 04:25:19 +02:00

Create section directory just before writing files to it

Previously, section directories would be created when the section was
added.
This commit is contained in:
Maarten ter Huurne 2015-04-22 18:46:03 +02:00
parent 9ac3351aa5
commit 08ecd7d8d9
3 changed files with 71 additions and 60 deletions

View File

@ -1026,13 +1026,10 @@ void GMenu2X::deleteLink() {
void GMenu2X::addSection() { void GMenu2X::addSection() {
InputDialog id(*this, input, ts, tr["Insert a name for the new section"]); InputDialog id(*this, input, ts, tr["Insert a name for the new section"]);
if (id.exec()) { if (id.exec()) {
//only if a section with the same name does not exist // Look up section; create if it doesn't exist yet.
if (find(menu->getSections().begin(), menu->getSections().end(), id.getInput()) auto idx = menu->sectionNamed(id.getInput());
== menu->getSections().end()) { // Switch to the new section.
//section directory doesn't exists menu->setSectionIndex(idx);
if (menu->addSection(id.getInput()))
menu->setSectionIndex( menu->getSections().size()-1 ); //switch to the new section
}
} }
} }

View File

@ -28,6 +28,8 @@
#include <unistd.h> #include <unistd.h>
#include <ini.h> #include <ini.h>
#include <cassert> #include <cassert>
#include <cerrno>
#include <cstring>
#ifdef HAVE_LIBOPK #ifdef HAVE_LIBOPK
#include <opk.h> #include <opk.h>
@ -129,6 +131,23 @@ void Menu::readSections(std::string parentDir)
closedir(dirp); closedir(dirp);
} }
string Menu::createSectionDir(string const& sectionName)
{
string parentDir = GMenu2X::getHome() + "/sections";
if (mkdir(parentDir.c_str(), 0755) && errno != EEXIST) {
WARNING("Failed to create parent sections dir: %s\n", strerror(errno));
return "";
}
string childDir = parentDir + "/" + sectionName;
if (mkdir(parentDir.c_str(), 0755) && errno != EEXIST) {
WARNING("Failed to create child section dir: %s\n", strerror(errno));
return "";
}
return childDir;
}
void Menu::skinUpdated() { void Menu::skinUpdated() {
ConfIntHash &skinConfInt = gmenu2x.skinConfInt; ConfIntHash &skinConfInt = gmenu2x.skinConfInt;
@ -404,13 +423,14 @@ void Menu::addActionLink(uint section, const string &title, Action action, const
links[section].push_back(link); links[section].push_back(link);
} }
bool Menu::addLink(string path, string file, string section) { bool Menu::addLink(string path, string file, string sectionName) {
if (section.empty()) { if (sectionName.empty()) {
section = selSection(); sectionName = selSection();
} else if (find(sections.begin(),sections.end(),section)==sections.end()) { }
//section directory doesn't exists
if (!addSection(section)) string sectionDir = createSectionDir(sectionName);
return false; if (sectionDir.empty()) {
return false;
} }
//strip the extension from the filename //strip the extension from the filename
@ -422,25 +442,14 @@ bool Menu::addLink(string path, string file, string section) {
title = title.substr(0, pos); title = title.substr(0, pos);
} }
string linkpath = GMenu2X::getHome() + "/sections"; string linkpath = sectionDir + "/" + title;
if (!fileExists(linkpath)) int x = 2;
mkdir(linkpath.c_str(), 0755);
linkpath = GMenu2X::getHome() + "/sections/" + section;
if (!fileExists(linkpath))
mkdir(linkpath.c_str(), 0755);
linkpath += "/" + title;
int x=2;
while (fileExists(linkpath)) { while (fileExists(linkpath)) {
stringstream ss; stringstream ss;
linkpath = ""; ss << sectionDir << '/' << title << x;
ss << x;
ss >> linkpath; ss >> linkpath;
linkpath = GMenu2X::getHome()+"/sections/"+section+"/"+title+linkpath;
x++; x++;
} }
INFO("Adding link: '%s'\n", linkpath.c_str()); INFO("Adding link: '%s'\n", linkpath.c_str());
if (path[path.length()-1]!='/') path += "/"; if (path[path.length()-1]!='/') path += "/";
@ -490,15 +499,11 @@ bool Menu::addLink(string path, string file, string section) {
if (!manual.empty()) f << "manual=" << manual << endl; if (!manual.empty()) f << "manual=" << manual << endl;
f.close(); f.close();
sync(); sync();
int isection = find(sections.begin(),sections.end(),section) - sections.begin();
if (isection>=0 && isection<(int)sections.size()) {
INFO("Section: '%s(%i)'\n", sections[isection].c_str(), isection); auto idx = sectionNamed(sectionName);
auto link = new LinkApp(gmenu2x, linkpath, true);
LinkApp* link = new LinkApp(gmenu2x, linkpath, true); link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]);
link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); links[idx].push_back(link);
links[isection].push_back( link );
}
} else { } else {
ERROR("Error while opening the file '%s' for write.\n", linkpath.c_str()); ERROR("Error while opening the file '%s' for write.\n", linkpath.c_str());
@ -509,19 +514,15 @@ bool Menu::addLink(string path, string file, string section) {
return true; return true;
} }
bool Menu::addSection(const string &sectionName) { int Menu::sectionNamed(const char *sectionName)
string sectiondir = GMenu2X::getHome() + "/sections"; {
if (!fileExists(sectiondir)) auto it = find(sections.begin(), sections.end(), sectionName);
mkdir(sectiondir.c_str(), 0755); int idx = it - sections.begin();
if (it == sections.end()) {
sectiondir = sectiondir + "/" + sectionName; sections.emplace_back(sectionName);
if (mkdir(sectiondir.c_str(), 0755) == 0) { links.emplace_back();
sections.push_back(sectionName);
vector<Link*> ll;
links.push_back(ll);
return true;
} }
return false; return idx;
} }
void Menu::deleteSelectedLink() void Menu::deleteSelectedLink()
@ -669,10 +670,8 @@ void Menu::openPackage(std::string path, bool order)
} }
for (;;) { for (;;) {
unsigned int i;
bool has_metadata = false; bool has_metadata = false;
const char *name; const char *name;
LinkApp *link;
for (;;) { for (;;) {
string::size_type pos; string::size_type pos;
@ -704,16 +703,11 @@ void Menu::openPackage(std::string path, bool order)
// Note: OPK links can only be deleted by removing the OPK itself, // Note: OPK links can only be deleted by removing the OPK itself,
// but that is not something we want to do in the menu, // but that is not something we want to do in the menu,
// so consider this link undeletable. // so consider this link undeletable.
link = new LinkApp(gmenu2x, path, false, opk, name); auto link = new LinkApp(gmenu2x, path, false, opk, name);
link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]);
addSection(link->getCategory()); auto idx = sectionNamed(link->getCategory());
for (i = 0; i < sections.size(); i++) { links[idx].push_back(link);
if (sections[i] == link->getCategory()) {
links[i].push_back(link);
break;
}
}
} }
opk_close(opk); opk_close(opk);

View File

@ -92,6 +92,13 @@ private:
void readLinksOfSection(std::vector<Link*>& links, void readLinksOfSection(std::vector<Link*>& links,
std::string const& path, bool deletable); std::string const& path, bool deletable);
/**
* Attempts to creates a section directory if it does not exist yet.
* @return The full path of the section directory, or the empty string
* if the directory could not be created.
*/
std::string createSectionDir(std::string const& sectionName);
void decSectionIndex(); void decSectionIndex();
void incSectionIndex(); void incSectionIndex();
void linkLeft(); void linkLeft();
@ -120,8 +127,21 @@ public:
void addActionLink(uint section, const std::string &title, void addActionLink(uint section, const std::string &title,
Action action, const std::string &description="", Action action, const std::string &description="",
const std::string &icon=""); const std::string &icon="");
bool addLink(std::string path, std::string file, std::string section=""); bool addLink(std::string path, std::string file, std::string sectionName="");
bool addSection(const std::string &sectionName);
/**
* Looks up a section by name, adding it if it doesn't exist yet.
* @return The index of the section.
*/
int sectionNamed(const char *sectionName);
/**
* Looks up a section by name, adding it if it doesn't exist yet.
* @return The index of the section.
*/
int sectionNamed(const std::string &sectionName) {
return sectionNamed(sectionName.c_str());
}
void deleteSelectedLink(); void deleteSelectedLink();
void deleteSelectedSection(); void deleteSelectedSection();