Date: | 2015-04-26 20:57:58 (8 years 7 months ago) |
---|---|
Author: | Maarten ter Huurne |
Commit: | 8e76dffc70b34fe36afd875363c6f4dd6919b72e |
Message: | 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. |
Files: |
src/gmenu2x.cpp (2 diffs) src/menu.cpp (1 diff) src/menu.h (2 diffs) |
Change Details
src/gmenu2x.cpp | ||
---|---|---|
900 | 900 | LinkApp *linkApp = menu->selLinkApp(); |
901 | 901 | if (!linkApp) return; |
902 | 902 | |
903 | vector<string> pathV; | |
904 | split(pathV,linkApp->getFile(),"/"); | |
905 | string oldSection = ""; | |
906 | if (pathV.size()>1) | |
907 | oldSection = pathV[pathV.size()-2]; | |
903 | string oldSection = menu->selSection(); | |
908 | 904 | string newSection = oldSection; |
909 | 905 | |
910 | 906 | string linkTitle = linkApp->getTitle(); |
... | ... | |
978 | 974 | linkApp->setSelectorDir(linkSelDir); |
979 | 975 | linkApp->setSelectorBrowser(linkSelBrowser); |
980 | 976 | linkApp->setClock(linkClock); |
977 | linkApp->save(); | |
981 | 978 | |
982 | INFO("New Section: '%s'\n", newSection.c_str()); | |
983 | ||
984 | //if section changed move file and update link->file | |
985 | if (oldSection!=newSection) { | |
986 | vector<string>::const_iterator newSectionIndex = find(menu->getSections().begin(),menu->getSections().end(),newSection); | |
987 | if (newSectionIndex==menu->getSections().end()) return; | |
988 | string newFileName = "sections/"+newSection+"/"+linkTitle; | |
989 | uint x=2; | |
990 | while (fileExists(newFileName)) { | |
991 | string id = ""; | |
992 | stringstream ss; ss << x; ss >> id; | |
993 | newFileName = "sections/"+newSection+"/"+linkTitle+id; | |
994 | x++; | |
995 | } | |
996 | rename(linkApp->getFile().c_str(),newFileName.c_str()); | |
997 | linkApp->setFile(newFileName); | |
998 | ||
999 | INFO("New section index: %zd.\n", newSectionIndex - menu->getSections().begin()); | |
1000 | ||
1001 | menu->linkChangeSection(menu->selLinkIndex(), menu->selSectionIndex(), newSectionIndex - menu->getSections().begin()); | |
979 | if (oldSection != newSection) { | |
980 | INFO("Changed section: '%s' -> '%s'\n", | |
981 | oldSection.c_str(), newSection.c_str()); | |
982 | menu->moveSelectedLink(newSection); | |
1002 | 983 | } |
1003 | linkApp->save(); | |
1004 | 984 | } |
1005 | 985 | } |
1006 | 986 |
src/menu.cpp | ||
---|---|---|
522 | 522 | } |
523 | 523 | } |
524 | 524 | |
525 | bool Menu::linkChangeSection(uint linkIndex, uint oldSectionIndex, uint newSectionIndex) { | |
526 | // Fetch sections. | |
527 | auto oldSectionLinks = sectionLinks(oldSectionIndex); | |
528 | if (!oldSectionLinks) return false; | |
529 | auto newSectionLinks = sectionLinks(newSectionIndex); | |
530 | if (!newSectionLinks) return false; | |
525 | bool Menu::moveSelectedLink(string const& newSection) | |
526 | { | |
527 | LinkApp *linkApp = selLinkApp(); | |
528 | if (!linkApp) { | |
529 | return false; | |
530 | } | |
531 | ||
532 | // Note: Get new index first, since it might move the selected index. | |
533 | auto const newSectionIndex = sectionNamed(newSection); | |
534 | auto const oldSectionIndex = iSection; | |
535 | ||
536 | string const& file = linkApp->getFile(); | |
537 | string linkTitle = file.substr(file.rfind('/') + 1); | |
538 | ||
539 | string sectionDir = createSectionDir(newSection); | |
540 | if (sectionDir.empty()) { | |
541 | return false; | |
542 | } | |
543 | ||
544 | string newFileName = sectionDir + "/" + linkTitle; | |
545 | unsigned int x = 2; | |
546 | while (fileExists(newFileName)) { | |
547 | string id = ""; | |
548 | stringstream ss; ss << x; ss >> id; | |
549 | newFileName = sectionDir + "/" + linkTitle + id; | |
550 | x++; | |
551 | } | |
531 | 552 | |
532 | // Find link in old section. | |
533 | if (linkIndex >= oldSectionLinks->size()) return false; | |
534 | auto it = oldSectionLinks->begin() + linkIndex; | |
553 | if (rename(file.c_str(), newFileName.c_str())) { | |
554 | WARNING("Link file move from '%s' to '%s' failed: %s\n", | |
555 | file.c_str(), newFileName.c_str(), strerror(errno)); | |
556 | return false; | |
557 | } | |
558 | linkApp->setFile(newFileName); | |
559 | ||
560 | // Fetch sections. | |
561 | auto& newSectionLinks = links[newSectionIndex]; | |
562 | auto& oldSectionLinks = links[oldSectionIndex]; | |
535 | 563 | |
536 | 564 | // Move link. |
565 | auto it = oldSectionLinks.begin() + iLink; | |
537 | 566 | auto link = it->release(); |
538 | oldSectionLinks->erase(it); | |
539 | newSectionLinks->emplace_back(link); | |
567 | oldSectionLinks.erase(it); | |
568 | newSectionLinks.emplace_back(link); | |
540 | 569 | |
541 | 570 | // Select the same link in the new section. |
542 | 571 | setSectionIndex(newSectionIndex); |
543 | setLinkIndex(newSectionLinks->size() - 1); | |
572 | setLinkIndex(newSectionLinks.size() - 1); | |
544 | 573 | |
545 | 574 | return true; |
546 | 575 | } |
src/menu.h | ||
---|---|---|
142 | 142 | void deleteSelectedLink(); |
143 | 143 | void deleteSelectedSection(); |
144 | 144 | |
145 | bool moveSelectedLink(std::string const& newSection); | |
146 | ||
145 | 147 | void skinUpdated(); |
146 | 148 | void orderLinks(); |
147 | 149 | |
... | ... | |
150 | 152 | virtual void paint(Surface &s); |
151 | 153 | virtual bool handleButtonPress(InputManager::Button button); |
152 | 154 | |
153 | bool linkChangeSection(uint linkIndex, uint oldSectionIndex, uint newSectionIndex); | |
154 | ||
155 | 155 | int selLinkIndex(); |
156 | 156 | Link *selLink(); |
157 | 157 | LinkApp *selLinkApp(); |
Branches:
install_locations
master
opkrun
packages