1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-12-25 07:29:55 +02:00

Put code for moving links to a different section into Menu class

This is another step in bringing the management of the links on the
file system into a single location (the Menu class).

Fixes were applied as well. For example a directory for the new section
is now created if it did not exist yet.
This commit is contained in:
Maarten ter Huurne 2015-04-26 20:57:58 +02:00
parent 73ceffa51d
commit 8e76dffc70
3 changed files with 50 additions and 41 deletions

View File

@ -900,11 +900,7 @@ void GMenu2X::editLink() {
LinkApp *linkApp = menu->selLinkApp();
if (!linkApp) return;
vector<string> pathV;
split(pathV,linkApp->getFile(),"/");
string oldSection = "";
if (pathV.size()>1)
oldSection = pathV[pathV.size()-2];
string oldSection = menu->selSection();
string newSection = oldSection;
string linkTitle = linkApp->getTitle();
@ -978,29 +974,13 @@ void GMenu2X::editLink() {
linkApp->setSelectorDir(linkSelDir);
linkApp->setSelectorBrowser(linkSelBrowser);
linkApp->setClock(linkClock);
INFO("New Section: '%s'\n", newSection.c_str());
//if section changed move file and update link->file
if (oldSection!=newSection) {
vector<string>::const_iterator newSectionIndex = find(menu->getSections().begin(),menu->getSections().end(),newSection);
if (newSectionIndex==menu->getSections().end()) return;
string newFileName = "sections/"+newSection+"/"+linkTitle;
uint x=2;
while (fileExists(newFileName)) {
string id = "";
stringstream ss; ss << x; ss >> id;
newFileName = "sections/"+newSection+"/"+linkTitle+id;
x++;
}
rename(linkApp->getFile().c_str(),newFileName.c_str());
linkApp->setFile(newFileName);
INFO("New section index: %zd.\n", newSectionIndex - menu->getSections().begin());
menu->linkChangeSection(menu->selLinkIndex(), menu->selSectionIndex(), newSectionIndex - menu->getSections().begin());
}
linkApp->save();
if (oldSection != newSection) {
INFO("Changed section: '%s' -> '%s'\n",
oldSection.c_str(), newSection.c_str());
menu->moveSelectedLink(newSection);
}
}
}

View File

@ -522,25 +522,54 @@ void Menu::deleteSelectedSection()
}
}
bool Menu::linkChangeSection(uint linkIndex, uint oldSectionIndex, uint newSectionIndex) {
// Fetch sections.
auto oldSectionLinks = sectionLinks(oldSectionIndex);
if (!oldSectionLinks) return false;
auto newSectionLinks = sectionLinks(newSectionIndex);
if (!newSectionLinks) return false;
bool Menu::moveSelectedLink(string const& newSection)
{
LinkApp *linkApp = selLinkApp();
if (!linkApp) {
return false;
}
// Find link in old section.
if (linkIndex >= oldSectionLinks->size()) return false;
auto it = oldSectionLinks->begin() + linkIndex;
// Note: Get new index first, since it might move the selected index.
auto const newSectionIndex = sectionNamed(newSection);
auto const oldSectionIndex = iSection;
string const& file = linkApp->getFile();
string linkTitle = file.substr(file.rfind('/') + 1);
string sectionDir = createSectionDir(newSection);
if (sectionDir.empty()) {
return false;
}
string newFileName = sectionDir + "/" + linkTitle;
unsigned int x = 2;
while (fileExists(newFileName)) {
string id = "";
stringstream ss; ss << x; ss >> id;
newFileName = sectionDir + "/" + linkTitle + id;
x++;
}
if (rename(file.c_str(), newFileName.c_str())) {
WARNING("Link file move from '%s' to '%s' failed: %s\n",
file.c_str(), newFileName.c_str(), strerror(errno));
return false;
}
linkApp->setFile(newFileName);
// Fetch sections.
auto& newSectionLinks = links[newSectionIndex];
auto& oldSectionLinks = links[oldSectionIndex];
// Move link.
auto it = oldSectionLinks.begin() + iLink;
auto link = it->release();
oldSectionLinks->erase(it);
newSectionLinks->emplace_back(link);
oldSectionLinks.erase(it);
newSectionLinks.emplace_back(link);
// Select the same link in the new section.
setSectionIndex(newSectionIndex);
setLinkIndex(newSectionLinks->size() - 1);
setLinkIndex(newSectionLinks.size() - 1);
return true;
}

View File

@ -142,6 +142,8 @@ public:
void deleteSelectedLink();
void deleteSelectedSection();
bool moveSelectedLink(std::string const& newSection);
void skinUpdated();
void orderLinks();
@ -150,8 +152,6 @@ public:
virtual void paint(Surface &s);
virtual bool handleButtonPress(InputManager::Button button);
bool linkChangeSection(uint linkIndex, uint oldSectionIndex, uint newSectionIndex);
int selLinkIndex();
Link *selLink();
LinkApp *selLinkApp();