Date:2015-04-26 20:57:58 (5 years 3 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
900900    LinkApp *linkApp = menu->selLinkApp();
901901    if (!linkApp) return;
902902
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();
908904    string newSection = oldSection;
909905
910906    string linkTitle = linkApp->getTitle();
...... 
978974        linkApp->setSelectorDir(linkSelDir);
979975        linkApp->setSelectorBrowser(linkSelBrowser);
980976        linkApp->setClock(linkClock);
977        linkApp->save();
981978
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);
1002983        }
1003        linkApp->save();
1004984    }
1005985}
1006986
src/menu.cpp
522522    }
523523}
524524
525bool 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;
525bool 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    }
531552
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];
535563
536564    // Move link.
565    auto it = oldSectionLinks.begin() + iLink;
537566    auto link = it->release();
538    oldSectionLinks->erase(it);
539    newSectionLinks->emplace_back(link);
567    oldSectionLinks.erase(it);
568    newSectionLinks.emplace_back(link);
540569
541570    // Select the same link in the new section.
542571    setSectionIndex(newSectionIndex);
543    setLinkIndex(newSectionLinks->size() - 1);
572    setLinkIndex(newSectionLinks.size() - 1);
544573
545574    return true;
546575}
src/menu.h
142142    void deleteSelectedLink();
143143    void deleteSelectedSection();
144144
145    bool moveSelectedLink(std::string const& newSection);
146
145147    void skinUpdated();
146148    void orderLinks();
147149
...... 
150152    virtual void paint(Surface &s);
151153    virtual bool handleButtonPress(InputManager::Button button);
152154
153    bool linkChangeSection(uint linkIndex, uint oldSectionIndex, uint newSectionIndex);
154
155155    int selLinkIndex();
156156    Link *selLink();
157157    LinkApp *selLinkApp();

Archive Download the corresponding diff file



interactive