1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-04 23:37:10 +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(); LinkApp *linkApp = menu->selLinkApp();
if (!linkApp) return; if (!linkApp) return;
vector<string> pathV; string oldSection = menu->selSection();
split(pathV,linkApp->getFile(),"/");
string oldSection = "";
if (pathV.size()>1)
oldSection = pathV[pathV.size()-2];
string newSection = oldSection; string newSection = oldSection;
string linkTitle = linkApp->getTitle(); string linkTitle = linkApp->getTitle();
@ -978,29 +974,13 @@ void GMenu2X::editLink() {
linkApp->setSelectorDir(linkSelDir); linkApp->setSelectorDir(linkSelDir);
linkApp->setSelectorBrowser(linkSelBrowser); linkApp->setSelectorBrowser(linkSelBrowser);
linkApp->setClock(linkClock); 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(); 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) { bool Menu::moveSelectedLink(string const& newSection)
// Fetch sections. {
auto oldSectionLinks = sectionLinks(oldSectionIndex); LinkApp *linkApp = selLinkApp();
if (!oldSectionLinks) return false; if (!linkApp) {
auto newSectionLinks = sectionLinks(newSectionIndex); return false;
if (!newSectionLinks) return false; }
// Find link in old section. // Note: Get new index first, since it might move the selected index.
if (linkIndex >= oldSectionLinks->size()) return false; auto const newSectionIndex = sectionNamed(newSection);
auto it = oldSectionLinks->begin() + linkIndex; 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. // Move link.
auto it = oldSectionLinks.begin() + iLink;
auto link = it->release(); auto link = it->release();
oldSectionLinks->erase(it); oldSectionLinks.erase(it);
newSectionLinks->emplace_back(link); newSectionLinks.emplace_back(link);
// Select the same link in the new section. // Select the same link in the new section.
setSectionIndex(newSectionIndex); setSectionIndex(newSectionIndex);
setLinkIndex(newSectionLinks->size() - 1); setLinkIndex(newSectionLinks.size() - 1);
return true; return true;
} }

View File

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