diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 45ea6b6..e171f8d 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -900,11 +900,7 @@ void GMenu2X::editLink() { LinkApp *linkApp = menu->selLinkApp(); if (!linkApp) return; - vector 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::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); + } } } diff --git a/src/menu.cpp b/src/menu.cpp index 79c0f24..467d5b8 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -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; } diff --git a/src/menu.h b/src/menu.h index a8ab8a4..76511d6 100644 --- a/src/menu.h +++ b/src/menu.h @@ -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();