Root/
Source at commit 8e76dffc70b34fe36afd875363c6f4dd6919b72e created 8 years 11 months ago. By Maarten ter Huurne, Put code for moving links to a different section into Menu class | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
3 | * massimiliano.torromeo@gmail.com * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include <sstream> |
22 | #include <sys/stat.h> |
23 | #include <sys/types.h> |
24 | #include <dirent.h> |
25 | #include <algorithm> |
26 | #include <math.h> |
27 | #include <fstream> |
28 | #include <unistd.h> |
29 | #include <cassert> |
30 | #include <cerrno> |
31 | #include <cstring> |
32 | |
33 | #ifdef HAVE_LIBOPK |
34 | #include <opk.h> |
35 | #endif |
36 | |
37 | #include "gmenu2x.h" |
38 | #include "linkapp.h" |
39 | #include "menu.h" |
40 | #include "monitor.h" |
41 | #include "filelister.h" |
42 | #include "utilities.h" |
43 | #include "debug.h" |
44 | |
45 | using namespace std; |
46 | |
47 | |
48 | Menu::Animation::Animation() |
49 | : curr(0) |
50 | { |
51 | } |
52 | |
53 | void Menu::Animation::adjust(int delta) |
54 | { |
55 | curr += delta; |
56 | } |
57 | |
58 | void Menu::Animation::step() |
59 | { |
60 | if (curr == 0) { |
61 | ERROR("Computing step past animation end\n"); |
62 | } else if (curr < 0) { |
63 | const int v = ((1 << 16) - curr) / 32; |
64 | curr = std::min(0, curr + v); |
65 | } else { |
66 | const int v = ((1 << 16) + curr) / 32; |
67 | curr = std::max(0, curr - v); |
68 | } |
69 | } |
70 | |
71 | Menu::Menu(GMenu2X& gmenu2x) |
72 | : gmenu2x(gmenu2x) |
73 | , btnContextMenu(gmenu2x, "skin:imgs/menu.png", "", |
74 | std::bind(&GMenu2X::showContextMenu, &gmenu2x)) |
75 | { |
76 | readSections(GMENU2X_SYSTEM_DIR "/sections"); |
77 | readSections(GMenu2X::getHome() + "/sections"); |
78 | |
79 | setSectionIndex(0); |
80 | readLinks(); |
81 | |
82 | #ifdef HAVE_LIBOPK |
83 | { |
84 | DIR *dirp = opendir(CARD_ROOT); |
85 | if (dirp) { |
86 | struct dirent *dptr; |
87 | while ((dptr = readdir(dirp))) { |
88 | if (dptr->d_type != DT_DIR) |
89 | continue; |
90 | |
91 | if (!strcmp(dptr->d_name, ".") || !strcmp(dptr->d_name, "..")) |
92 | continue; |
93 | |
94 | openPackagesFromDir((string) CARD_ROOT + "/" + |
95 | dptr->d_name + "/apps"); |
96 | } |
97 | closedir(dirp); |
98 | } |
99 | } |
100 | #endif |
101 | |
102 | btnContextMenu.setPosition(gmenu2x.resX - 38, gmenu2x.bottomBarIconY); |
103 | } |
104 | |
105 | Menu::~Menu() |
106 | { |
107 | } |
108 | |
109 | void Menu::readSections(std::string const& parentDir) |
110 | { |
111 | DIR *dirp = opendir(parentDir.c_str()); |
112 | if (!dirp) return; |
113 | |
114 | struct dirent *dptr; |
115 | while ((dptr = readdir(dirp))) { |
116 | if (dptr->d_name[0] != '.' && dptr->d_type == DT_DIR) { |
117 | // Create section if it doesn't exist yet. |
118 | sectionNamed(dptr->d_name); |
119 | } |
120 | } |
121 | |
122 | closedir(dirp); |
123 | } |
124 | |
125 | string Menu::createSectionDir(string const& sectionName) |
126 | { |
127 | string parentDir = GMenu2X::getHome() + "/sections"; |
128 | if (mkdir(parentDir.c_str(), 0755) && errno != EEXIST) { |
129 | WARNING("Failed to create parent sections dir: %s\n", strerror(errno)); |
130 | return ""; |
131 | } |
132 | |
133 | string childDir = parentDir + "/" + sectionName; |
134 | if (mkdir(childDir.c_str(), 0755) && errno != EEXIST) { |
135 | WARNING("Failed to create child section dir: %s\n", strerror(errno)); |
136 | return ""; |
137 | } |
138 | |
139 | return childDir; |
140 | } |
141 | |
142 | void Menu::skinUpdated() { |
143 | ConfIntHash &skinConfInt = gmenu2x.skinConfInt; |
144 | |
145 | //recalculate some coordinates based on the new element sizes |
146 | linkColumns = (gmenu2x.resX - 10) / skinConfInt["linkWidth"]; |
147 | linkRows = (gmenu2x.resY - 35 - skinConfInt["topBarHeight"]) / skinConfInt["linkHeight"]; |
148 | |
149 | //reload section icons |
150 | decltype(links)::size_type i = 0; |
151 | for (auto& sectionName : sections) { |
152 | gmenu2x.sc["skin:sections/" + sectionName + ".png"]; |
153 | |
154 | for (auto& link : links[i]) { |
155 | link->loadIcon(); |
156 | } |
157 | |
158 | i++; |
159 | } |
160 | } |
161 | |
162 | void Menu::calcSectionRange(int &leftSection, int &rightSection) { |
163 | ConfIntHash &skinConfInt = gmenu2x.skinConfInt; |
164 | const int linkWidth = skinConfInt["linkWidth"]; |
165 | const int screenWidth = gmenu2x.resX; |
166 | const int numSections = sections.size(); |
167 | rightSection = min( |
168 | max(1, (screenWidth - 20 - linkWidth) / (2 * linkWidth)), |
169 | numSections / 2); |
170 | leftSection = max( |
171 | -rightSection, |
172 | rightSection - numSections + 1); |
173 | } |
174 | |
175 | bool Menu::runAnimations() { |
176 | if (sectionAnimation.isRunning()) { |
177 | sectionAnimation.step(); |
178 | } |
179 | return sectionAnimation.isRunning(); |
180 | } |
181 | |
182 | void Menu::paint(Surface &s) { |
183 | const uint width = s.width(), height = s.height(); |
184 | Font &font = *gmenu2x.font; |
185 | SurfaceCollection &sc = gmenu2x.sc; |
186 | |
187 | ConfIntHash &skinConfInt = gmenu2x.skinConfInt; |
188 | const int topBarHeight = skinConfInt["topBarHeight"]; |
189 | const int bottomBarHeight = skinConfInt["bottomBarHeight"]; |
190 | const int linkWidth = skinConfInt["linkWidth"]; |
191 | const int linkHeight = skinConfInt["linkHeight"]; |
192 | RGBAColor &selectionBgColor = gmenu2x.skinConfColors[COLOR_SELECTION_BG]; |
193 | |
194 | // Apply section header animation. |
195 | int leftSection, rightSection; |
196 | calcSectionRange(leftSection, rightSection); |
197 | int sectionFP = sectionAnimation.currentValue(); |
198 | int sectionDelta = (sectionFP * linkWidth + (1 << 15)) >> 16; |
199 | int centerSection = iSection - sectionDelta / linkWidth; |
200 | sectionDelta %= linkWidth; |
201 | if (sectionDelta < 0) { |
202 | rightSection++; |
203 | } else if (sectionDelta > 0) { |
204 | leftSection--; |
205 | } |
206 | |
207 | // Paint section headers. |
208 | s.box(width / 2 - linkWidth / 2, 0, linkWidth, topBarHeight, selectionBgColor); |
209 | const uint sectionLinkPadding = (topBarHeight - 32 - font.getLineSpacing()) / 3; |
210 | const uint numSections = sections.size(); |
211 | for (int i = leftSection; i <= rightSection; i++) { |
212 | uint j = (centerSection + numSections + i) % numSections; |
213 | string sectionIcon = "skin:sections/" + sections[j] + ".png"; |
214 | Surface *icon = sc.exists(sectionIcon) |
215 | ? sc[sectionIcon] |
216 | : sc.skinRes("icons/section.png"); |
217 | int x = width / 2 + i * linkWidth + sectionDelta; |
218 | if (i == leftSection) { |
219 | int t = sectionDelta > 0 ? linkWidth - sectionDelta : -sectionDelta; |
220 | x -= (((t * t) / linkWidth) * t) / linkWidth; |
221 | } else if (i == rightSection) { |
222 | int t = sectionDelta < 0 ? sectionDelta + linkWidth : sectionDelta; |
223 | x += (((t * t) / linkWidth) * t) / linkWidth; |
224 | } |
225 | icon->blit(s, x - 16, sectionLinkPadding, 32, 32); |
226 | font.write(s, sections[j], x, topBarHeight - sectionLinkPadding, |
227 | Font::HAlignCenter, Font::VAlignBottom); |
228 | } |
229 | sc.skinRes("imgs/section-l.png")->blit(s, 0, 0); |
230 | sc.skinRes("imgs/section-r.png")->blit(s, width - 10, 0); |
231 | |
232 | auto& sectionLinks = links[iSection]; |
233 | auto numLinks = sectionLinks.size(); |
234 | gmenu2x.drawScrollBar( |
235 | linkRows, (numLinks + linkColumns - 1) / linkColumns, iFirstDispRow); |
236 | |
237 | //Links |
238 | const uint linksPerPage = linkColumns * linkRows; |
239 | const int linkSpacingX = (width - 10 - linkColumns * linkWidth) / linkColumns; |
240 | const int linkMarginX = ( |
241 | width - linkWidth * linkColumns - linkSpacingX * (linkColumns - 1) |
242 | ) / 2; |
243 | const int linkSpacingY = (height - 35 - topBarHeight - linkRows * linkHeight) / linkRows; |
244 | for (uint i = iFirstDispRow * linkColumns; i < iFirstDispRow * linkColumns + linksPerPage && i < numLinks; i++) { |
245 | const int ir = i - iFirstDispRow * linkColumns; |
246 | const int x = linkMarginX + (ir % linkColumns) * (linkWidth + linkSpacingX); |
247 | const int y = ir / linkColumns * (linkHeight + linkSpacingY) + topBarHeight + 2; |
248 | sectionLinks.at(i)->setPosition(x, y); |
249 | |
250 | if (i == (uint)iLink) { |
251 | sectionLinks.at(i)->paintHover(); |
252 | } |
253 | |
254 | sectionLinks.at(i)->paint(); |
255 | } |
256 | |
257 | if (selLink()) { |
258 | font.write(s, selLink()->getDescription(), |
259 | width / 2, height - bottomBarHeight + 2, |
260 | Font::HAlignCenter, Font::VAlignBottom); |
261 | } |
262 | |
263 | LinkApp *linkApp = selLinkApp(); |
264 | if (linkApp) { |
265 | #ifdef ENABLE_CPUFREQ |
266 | font.write(s, linkApp->clockStr(gmenu2x.confInt["maxClock"]), |
267 | gmenu2x.cpuX, gmenu2x.bottomBarTextY, |
268 | Font::HAlignLeft, Font::VAlignMiddle); |
269 | #endif |
270 | //Manual indicator |
271 | if (!linkApp->getManual().empty()) |
272 | sc.skinRes("imgs/manual.png")->blit( |
273 | s, gmenu2x.manualX, gmenu2x.bottomBarIconY); |
274 | } |
275 | } |
276 | |
277 | bool Menu::handleButtonPress(InputManager::Button button) { |
278 | switch (button) { |
279 | case InputManager::ACCEPT: |
280 | if (selLink() != NULL) selLink()->run(); |
281 | return true; |
282 | case InputManager::UP: |
283 | linkUp(); |
284 | return true; |
285 | case InputManager::DOWN: |
286 | linkDown(); |
287 | return true; |
288 | case InputManager::LEFT: |
289 | linkLeft(); |
290 | return true; |
291 | case InputManager::RIGHT: |
292 | linkRight(); |
293 | return true; |
294 | case InputManager::ALTLEFT: |
295 | decSectionIndex(); |
296 | return true; |
297 | case InputManager::ALTRIGHT: |
298 | incSectionIndex(); |
299 | return true; |
300 | case InputManager::MENU: |
301 | gmenu2x.showContextMenu(); |
302 | return true; |
303 | default: |
304 | return false; |
305 | } |
306 | } |
307 | |
308 | /*==================================== |
309 | SECTION MANAGEMENT |
310 | ====================================*/ |
311 | |
312 | vector<unique_ptr<Link>> *Menu::sectionLinks(int i) |
313 | { |
314 | if (i<0 || i>=(int)links.size()) { |
315 | i = selSectionIndex(); |
316 | } |
317 | |
318 | if (i<0 || i>=(int)links.size()) { |
319 | return nullptr; |
320 | } |
321 | |
322 | return &links[i]; |
323 | } |
324 | |
325 | void Menu::decSectionIndex() { |
326 | sectionAnimation.adjust(-1 << 16); |
327 | setSectionIndex(iSection - 1); |
328 | } |
329 | |
330 | void Menu::incSectionIndex() { |
331 | sectionAnimation.adjust(1 << 16); |
332 | setSectionIndex(iSection + 1); |
333 | } |
334 | |
335 | int Menu::selSectionIndex() { |
336 | return iSection; |
337 | } |
338 | |
339 | const string &Menu::selSection() { |
340 | return sections[iSection]; |
341 | } |
342 | |
343 | void Menu::setSectionIndex(int i) { |
344 | if (i<0) |
345 | i=sections.size()-1; |
346 | else if (i>=(int)sections.size()) |
347 | i=0; |
348 | iSection = i; |
349 | |
350 | iLink = 0; |
351 | iFirstDispRow = 0; |
352 | } |
353 | |
354 | /*==================================== |
355 | LINKS MANAGEMENT |
356 | ====================================*/ |
357 | void Menu::addActionLink(uint section, string const& title, Action action, |
358 | string const& description, string const& icon) |
359 | { |
360 | assert(section < sections.size()); |
361 | |
362 | Link *link = new Link(gmenu2x, action); |
363 | link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); |
364 | link->setTitle(title); |
365 | link->setDescription(description); |
366 | if (gmenu2x.sc.exists(icon) |
367 | || (icon.substr(0,5)=="skin:" |
368 | && !gmenu2x.sc.getSkinFilePath(icon.substr(5,icon.length())).empty()) |
369 | || fileExists(icon)) { |
370 | link->setIcon(icon); |
371 | } |
372 | |
373 | links[section].emplace_back(link); |
374 | } |
375 | |
376 | bool Menu::addLink(string const& path, string const& file) |
377 | { |
378 | string const& sectionName = selSection(); |
379 | |
380 | string sectionDir = createSectionDir(sectionName); |
381 | if (sectionDir.empty()) { |
382 | return false; |
383 | } |
384 | |
385 | //strip the extension from the filename |
386 | string title = file; |
387 | string::size_type pos = title.rfind("."); |
388 | if (pos!=string::npos && pos>0) { |
389 | string ext = title.substr(pos, title.length()); |
390 | transform(ext.begin(), ext.end(), ext.begin(), ::tolower); |
391 | title = title.substr(0, pos); |
392 | } |
393 | |
394 | string linkpath = sectionDir + "/" + title; |
395 | int x = 2; |
396 | while (fileExists(linkpath)) { |
397 | stringstream ss; |
398 | ss << sectionDir << '/' << title << x; |
399 | ss >> linkpath; |
400 | x++; |
401 | } |
402 | INFO("Adding link: '%s'\n", linkpath.c_str()); |
403 | |
404 | string dirPath = path; |
405 | if (dirPath.empty() || dirPath.back() != '/') dirPath += '/'; |
406 | |
407 | //search for a manual |
408 | pos = file.rfind("."); |
409 | string exename = dirPath + file.substr(0, pos); |
410 | string manual = ""; |
411 | if (fileExists(exename+".man.png")) { |
412 | manual = exename+".man.png"; |
413 | } else if (fileExists(exename+".man.txt")) { |
414 | manual = exename+".man.txt"; |
415 | } else { |
416 | //scan directory for a file like *readme* |
417 | FileLister fl; |
418 | fl.setShowDirectories(false); |
419 | fl.setFilter(".txt"); |
420 | fl.browse(dirPath); |
421 | bool found = false; |
422 | for (uint x=0; x<fl.size() && !found; x++) { |
423 | string lcfilename = fl[x]; |
424 | |
425 | if (lcfilename.find("readme") != string::npos) { |
426 | found = true; |
427 | manual = dirPath + fl.getFiles()[x]; |
428 | } |
429 | } |
430 | } |
431 | |
432 | INFO("Manual: '%s'\n", manual.c_str()); |
433 | |
434 | string shorttitle=title, description="", exec=dirPath+file, icon=""; |
435 | if (fileExists(exename+".png")) icon = exename+".png"; |
436 | |
437 | //Reduce title lenght to fit the link width |
438 | if (gmenu2x.font->getTextWidth(shorttitle)>gmenu2x.skinConfInt["linkWidth"]) { |
439 | while (gmenu2x.font->getTextWidth(shorttitle+"..")>gmenu2x.skinConfInt["linkWidth"]) |
440 | shorttitle = shorttitle.substr(0,shorttitle.length()-1); |
441 | shorttitle += ".."; |
442 | } |
443 | |
444 | ofstream f(linkpath.c_str()); |
445 | if (f.is_open()) { |
446 | f << "title=" << shorttitle << endl; |
447 | f << "exec=" << exec << endl; |
448 | if (!description.empty()) f << "description=" << description << endl; |
449 | if (!icon.empty()) f << "icon=" << icon << endl; |
450 | if (!manual.empty()) f << "manual=" << manual << endl; |
451 | f.close(); |
452 | sync(); |
453 | |
454 | auto idx = sectionNamed(sectionName); |
455 | auto link = new LinkApp(gmenu2x, linkpath, true); |
456 | link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); |
457 | links[idx].emplace_back(link); |
458 | } else { |
459 | |
460 | ERROR("Error while opening the file '%s' for write.\n", linkpath.c_str()); |
461 | |
462 | return false; |
463 | } |
464 | |
465 | return true; |
466 | } |
467 | |
468 | int Menu::sectionNamed(const char *sectionName) |
469 | { |
470 | auto it = lower_bound(sections.begin(), sections.end(), sectionName); |
471 | int idx = it - sections.begin(); |
472 | if (it == sections.end() || *it != sectionName) { |
473 | sections.emplace(it, sectionName); |
474 | links.emplace(links.begin() + idx); |
475 | // Make sure the selected section doesn't change. |
476 | if (idx <= iSection) { |
477 | iSection++; |
478 | } |
479 | } |
480 | return idx; |
481 | } |
482 | |
483 | void Menu::deleteSelectedLink() |
484 | { |
485 | string iconpath = selLink()->getIconPath(); |
486 | |
487 | INFO("Deleting link '%s'\n", selLink()->getTitle().c_str()); |
488 | |
489 | if (selLinkApp()!=NULL) |
490 | unlink(selLinkApp()->getFile().c_str()); |
491 | sectionLinks()->erase( sectionLinks()->begin() + selLinkIndex() ); |
492 | setLinkIndex(selLinkIndex()); |
493 | |
494 | bool icon_used = false; |
495 | for (auto& section : links) { |
496 | for (auto& link : section) { |
497 | if (iconpath == link->getIconPath()) { |
498 | icon_used = true; |
499 | } |
500 | } |
501 | } |
502 | if (!icon_used) { |
503 | gmenu2x.sc.del(iconpath); |
504 | } |
505 | } |
506 | |
507 | void Menu::deleteSelectedSection() |
508 | { |
509 | string const& sectionName = selSection(); |
510 | INFO("Deleting section '%s'\n", sectionName.c_str()); |
511 | |
512 | gmenu2x.sc.del("sections/" + sectionName + ".png"); |
513 | auto idx = selSectionIndex(); |
514 | links.erase(links.begin() + idx); |
515 | sections.erase(sections.begin() + idx); |
516 | setSectionIndex(0); //reload sections |
517 | |
518 | string path = GMenu2X::getHome() + "/sections/" + sectionName; |
519 | if (rmdir(path.c_str()) && errno != ENOENT) { |
520 | WARNING("Removal of section dir \"%s\" failed: %s\n", |
521 | path.c_str(), strerror(errno)); |
522 | } |
523 | } |
524 | |
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 | } |
552 | |
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]; |
563 | |
564 | // Move link. |
565 | auto it = oldSectionLinks.begin() + iLink; |
566 | auto link = it->release(); |
567 | oldSectionLinks.erase(it); |
568 | newSectionLinks.emplace_back(link); |
569 | |
570 | // Select the same link in the new section. |
571 | setSectionIndex(newSectionIndex); |
572 | setLinkIndex(newSectionLinks.size() - 1); |
573 | |
574 | return true; |
575 | } |
576 | |
577 | void Menu::linkLeft() { |
578 | if (iLink % linkColumns == 0) |
579 | setLinkIndex(sectionLinks()->size() > iLink + linkColumns - 1 |
580 | ? iLink + linkColumns - 1 : sectionLinks()->size() - 1); |
581 | else |
582 | setLinkIndex(iLink - 1); |
583 | } |
584 | |
585 | void Menu::linkRight() { |
586 | if (iLink % linkColumns == linkColumns - 1 |
587 | || iLink == (int)sectionLinks()->size() - 1) |
588 | setLinkIndex(iLink - iLink % linkColumns); |
589 | else |
590 | setLinkIndex(iLink + 1); |
591 | } |
592 | |
593 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
594 | |
595 | void Menu::linkUp() { |
596 | int l = iLink - linkColumns; |
597 | if (l < 0) { |
598 | const auto numLinks = sectionLinks()->size(); |
599 | unsigned int rows = DIV_ROUND_UP(numLinks, linkColumns); |
600 | l = (rows * linkColumns) + l; |
601 | if (l >= static_cast<int>(numLinks)) |
602 | l -= linkColumns; |
603 | } |
604 | setLinkIndex(l); |
605 | } |
606 | |
607 | void Menu::linkDown() { |
608 | int l = iLink + linkColumns; |
609 | const auto numLinks = sectionLinks()->size(); |
610 | if (l >= static_cast<int>(numLinks)) { |
611 | unsigned int rows = DIV_ROUND_UP(numLinks, linkColumns); |
612 | unsigned int curCol = DIV_ROUND_UP(iLink + 1, linkColumns); |
613 | if (rows > curCol) |
614 | l = numLinks - 1; |
615 | else |
616 | l %= linkColumns; |
617 | } |
618 | setLinkIndex(l); |
619 | } |
620 | |
621 | int Menu::selLinkIndex() { |
622 | return iLink; |
623 | } |
624 | |
625 | Link *Menu::selLink() { |
626 | auto curSectionLinks = sectionLinks(); |
627 | if (curSectionLinks->empty()) { |
628 | return nullptr; |
629 | } else { |
630 | return curSectionLinks->at(iLink).get(); |
631 | } |
632 | } |
633 | |
634 | LinkApp *Menu::selLinkApp() { |
635 | return dynamic_cast<LinkApp*>(selLink()); |
636 | } |
637 | |
638 | void Menu::setLinkIndex(int i) { |
639 | auto links = sectionLinks(); |
640 | if (!links) { |
641 | // No sections. |
642 | return; |
643 | } |
644 | const int numLinks = static_cast<int>(sectionLinks()->size()); |
645 | if (i < 0) |
646 | i = numLinks - 1; |
647 | else if (i >= numLinks) |
648 | i = 0; |
649 | iLink = i; |
650 | |
651 | int row = i / linkColumns; |
652 | if (row >= (int)(iFirstDispRow + linkRows - 1)) |
653 | iFirstDispRow = min(row + 1, (int)DIV_ROUND_UP(numLinks, linkColumns) - 1) |
654 | - linkRows + 1; |
655 | else if (row <= (int)iFirstDispRow) |
656 | iFirstDispRow = max(row - 1, 0); |
657 | } |
658 | |
659 | #ifdef HAVE_LIBOPK |
660 | void Menu::openPackagesFromDir(std::string const& path) |
661 | { |
662 | DEBUG("Opening packages from directory: %s\n", path.c_str()); |
663 | if (readPackages(path)) { |
664 | #ifdef ENABLE_INOTIFY |
665 | monitors.emplace_back(new Monitor(path.c_str())); |
666 | #endif |
667 | } |
668 | } |
669 | |
670 | void Menu::openPackage(std::string const& path, bool order) |
671 | { |
672 | /* First try to remove existing links of the same OPK |
673 | * (needed for instance when an OPK is modified) */ |
674 | removePackageLink(path); |
675 | |
676 | struct OPK *opk = opk_open(path.c_str()); |
677 | if (!opk) { |
678 | ERROR("Unable to open OPK %s\n", path.c_str()); |
679 | return; |
680 | } |
681 | |
682 | for (;;) { |
683 | bool has_metadata = false; |
684 | const char *name; |
685 | |
686 | for (;;) { |
687 | string::size_type pos; |
688 | int ret = opk_open_metadata(opk, &name); |
689 | if (ret < 0) { |
690 | ERROR("Error while loading meta-data\n"); |
691 | break; |
692 | } else if (!ret) |
693 | break; |
694 | |
695 | /* Strip .desktop */ |
696 | string metadata(name); |
697 | pos = metadata.rfind('.'); |
698 | metadata = metadata.substr(0, pos); |
699 | |
700 | /* Keep only the platform name */ |
701 | pos = metadata.rfind('.'); |
702 | metadata = metadata.substr(pos + 1); |
703 | |
704 | if (metadata == PLATFORM || metadata == "all") { |
705 | has_metadata = true; |
706 | break; |
707 | } |
708 | } |
709 | |
710 | if (!has_metadata) |
711 | break; |
712 | |
713 | // Note: OPK links can only be deleted by removing the OPK itself, |
714 | // but that is not something we want to do in the menu, |
715 | // so consider this link undeletable. |
716 | auto link = new LinkApp(gmenu2x, path, false, opk, name); |
717 | link->setSize(gmenu2x.skinConfInt["linkWidth"], gmenu2x.skinConfInt["linkHeight"]); |
718 | |
719 | auto idx = sectionNamed(link->getCategory()); |
720 | links[idx].emplace_back(link); |
721 | } |
722 | |
723 | opk_close(opk); |
724 | |
725 | if (order) |
726 | orderLinks(); |
727 | } |
728 | |
729 | bool Menu::readPackages(std::string const& parentDir) |
730 | { |
731 | DIR *dirp = opendir(parentDir.c_str()); |
732 | if (!dirp) { |
733 | return false; |
734 | } |
735 | |
736 | while (struct dirent *dptr = readdir(dirp)) { |
737 | if (dptr->d_type != DT_REG) |
738 | continue; |
739 | |
740 | char *c = strrchr(dptr->d_name, '.'); |
741 | if (!c) /* File without extension */ |
742 | continue; |
743 | |
744 | if (strcasecmp(c + 1, "opk")) |
745 | continue; |
746 | |
747 | if (dptr->d_name[0] == '.') { |
748 | // Ignore hidden files. |
749 | // Mac OS X places these on SD cards, probably to store metadata. |
750 | continue; |
751 | } |
752 | |
753 | openPackage(parentDir + '/' + dptr->d_name, false); |
754 | } |
755 | |
756 | closedir(dirp); |
757 | orderLinks(); |
758 | |
759 | return true; |
760 | } |
761 | |
762 | #ifdef ENABLE_INOTIFY |
763 | /* Remove all links that correspond to the given path. |
764 | * If "path" is a directory, it will remove all links that |
765 | * correspond to an OPK present in the directory. */ |
766 | void Menu::removePackageLink(std::string const& path) |
767 | { |
768 | for (auto section = links.begin(); section != links.end(); ++section) { |
769 | for (auto link = section->begin(); link != section->end(); ++link) { |
770 | LinkApp *app = dynamic_cast<LinkApp *>(link->get()); |
771 | if (!app || !app->isOpk() || app->getOpkFile().empty()) |
772 | continue; |
773 | |
774 | if (app->getOpkFile().compare(0, path.size(), path) == 0) { |
775 | DEBUG("Removing link corresponding to package %s\n", |
776 | app->getOpkFile().c_str()); |
777 | section->erase(link); |
778 | if (section - links.begin() == iSection |
779 | && iLink == (int) section->size()) { |
780 | setLinkIndex(iLink - 1); |
781 | } |
782 | --link; |
783 | } |
784 | } |
785 | } |
786 | |
787 | /* Remove registered monitors */ |
788 | for (auto it = monitors.begin(); it < monitors.end(); ++it) { |
789 | if ((*it)->getPath().compare(0, path.size(), path) == 0) { |
790 | monitors.erase(it); |
791 | } |
792 | } |
793 | } |
794 | #endif |
795 | #endif |
796 | |
797 | static bool compare_links(unique_ptr<Link> const& a, unique_ptr<Link> const& b) |
798 | { |
799 | LinkApp *app1 = dynamic_cast<LinkApp *>(a.get()); |
800 | LinkApp *app2 = dynamic_cast<LinkApp *>(b.get()); |
801 | bool app1_is_opk = app1 && app1->isOpk(), |
802 | app2_is_opk = app2 && app2->isOpk(); |
803 | |
804 | if (app1_is_opk && !app2_is_opk) { |
805 | return false; |
806 | } |
807 | if (app2_is_opk && !app1_is_opk) { |
808 | return true; |
809 | } |
810 | return a->getTitle().compare(b->getTitle()) < 0; |
811 | } |
812 | |
813 | void Menu::orderLinks() |
814 | { |
815 | for (auto& section : links) { |
816 | sort(section.begin(), section.end(), compare_links); |
817 | } |
818 | } |
819 | |
820 | void Menu::readLinks() |
821 | { |
822 | iLink = 0; |
823 | iFirstDispRow = 0; |
824 | |
825 | for (uint i=0; i<links.size(); i++) { |
826 | links[i].clear(); |
827 | |
828 | int correct = (i>sections.size() ? iSection : i); |
829 | string const& section = sections[correct]; |
830 | |
831 | readLinksOfSection( |
832 | links[i], GMENU2X_SYSTEM_DIR "/sections/" + section, false); |
833 | readLinksOfSection( |
834 | links[i], GMenu2X::getHome() + "/sections/" + section, true); |
835 | } |
836 | |
837 | orderLinks(); |
838 | } |
839 | |
840 | void Menu::readLinksOfSection( |
841 | vector<unique_ptr<Link>>& links, string const& path, bool deletable) |
842 | { |
843 | DIR *dirp = opendir(path.c_str()); |
844 | if (!dirp) return; |
845 | |
846 | while (struct dirent *dptr = readdir(dirp)) { |
847 | if (dptr->d_type != DT_REG) continue; |
848 | string linkfile = path + '/' + dptr->d_name; |
849 | |
850 | LinkApp *link = new LinkApp(gmenu2x, linkfile, deletable); |
851 | if (link->targetExists()) { |
852 | link->setSize( |
853 | gmenu2x.skinConfInt["linkWidth"], |
854 | gmenu2x.skinConfInt["linkHeight"]); |
855 | links.emplace_back(link); |
856 | } else { |
857 | delete link; |
858 | } |
859 | } |
860 | |
861 | closedir(dirp); |
862 | } |
863 |
Branches:
install_locations
master
opkrun
packages