Changeset View
Changeset View
Standalone View
Standalone View
krusader/BookMan/krbookmarkhandler.cpp
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | |||||
43 | #include <QMouseEvent> | 43 | #include <QMouseEvent> | ||
44 | #include <QCursor> | 44 | #include <QCursor> | ||
45 | 45 | | |||
46 | #include <KConfigCore/KSharedConfig> | 46 | #include <KConfigCore/KSharedConfig> | ||
47 | #include <KI18n/KLocalizedString> | 47 | #include <KI18n/KLocalizedString> | ||
48 | #include <KWidgetsAddons/KMessageBox> | 48 | #include <KWidgetsAddons/KMessageBox> | ||
49 | #include <KXmlGui/KActionCollection> | 49 | #include <KXmlGui/KActionCollection> | ||
50 | #include <KBookmarks/KBookmarkManager> | 50 | #include <KBookmarks/KBookmarkManager> | ||
51 | #include <utility> | ||||
51 | 52 | | |||
52 | #define SPECIAL_BOOKMARKS true | 53 | #define SPECIAL_BOOKMARKS true | ||
53 | 54 | | |||
54 | // ------------------------ for internal use | 55 | // ------------------------ for internal use | ||
55 | #define BOOKMARKS_FILE "krusader/krbookmarks.xml" | 56 | #define BOOKMARKS_FILE "krusader/krbookmarks.xml" | ||
56 | #define CONNECT_BM(X) { disconnect(X, SIGNAL(activated(QUrl)), 0, 0); connect(X, SIGNAL(activated(QUrl)), this, SLOT(slotActivated(QUrl))); } | 57 | #define CONNECT_BM(X) { disconnect(X, SIGNAL(activated(QUrl)), 0, 0); connect(X, SIGNAL(activated(QUrl)), this, SLOT(slotActivated(QUrl))); } | ||
57 | 58 | | |||
58 | KrBookmarkHandler::KrBookmarkHandler(KrMainWindow *mainWindow) : | 59 | KrBookmarkHandler::KrBookmarkHandler(KrMainWindow *mainWindow) : | ||
59 | QObject(mainWindow->widget()), | 60 | QObject(mainWindow->widget()), | ||
60 | _mainWindow(mainWindow), | 61 | _mainWindow(mainWindow), | ||
61 | _middleClick(false), | 62 | _middleClick(false), | ||
62 | _mainBookmarkPopup(0), | 63 | _mainBookmarkPopup(nullptr), | ||
63 | _specialBookmarks(), | | |||
64 | _quickSearchAction(nullptr), | 64 | _quickSearchAction(nullptr), | ||
65 | _quickSearchBar(nullptr), | 65 | _quickSearchBar(nullptr), | ||
66 | _quickSearchMenu(nullptr) | 66 | _quickSearchMenu(nullptr) | ||
67 | { | 67 | { | ||
68 | // create our own action collection and make the shortcuts apply only to parent | 68 | // create our own action collection and make the shortcuts apply only to parent | ||
69 | _privateCollection = new KActionCollection(this); | 69 | _privateCollection = new KActionCollection(this); | ||
70 | _collection = _mainWindow->actions(); | 70 | _collection = _mainWindow->actions(); | ||
71 | 71 | | |||
Show All 26 Lines | |||||
98 | KrBookmarkHandler::~KrBookmarkHandler() | 98 | KrBookmarkHandler::~KrBookmarkHandler() | ||
99 | { | 99 | { | ||
100 | delete manager; | 100 | delete manager; | ||
101 | delete _privateCollection; | 101 | delete _privateCollection; | ||
102 | } | 102 | } | ||
103 | 103 | | |||
104 | void KrBookmarkHandler::bookmarkCurrent(QUrl url) | 104 | void KrBookmarkHandler::bookmarkCurrent(QUrl url) | ||
105 | { | 105 | { | ||
106 | QPointer<KrAddBookmarkDlg> dlg = new KrAddBookmarkDlg(_mainWindow->widget(), url); | 106 | QPointer<KrAddBookmarkDlg> dlg = new KrAddBookmarkDlg(_mainWindow->widget(), std::move(url)); | ||
107 | if (dlg->exec() == QDialog::Accepted) { | 107 | if (dlg->exec() == QDialog::Accepted) { | ||
108 | KrBookmark *bm = new KrBookmark(dlg->name(), dlg->url(), _collection); | 108 | KrBookmark *bm = new KrBookmark(dlg->name(), dlg->url(), _collection); | ||
109 | addBookmark(bm, dlg->folder()); | 109 | addBookmark(bm, dlg->folder()); | ||
110 | } | 110 | } | ||
111 | delete dlg; | 111 | delete dlg; | ||
112 | } | 112 | } | ||
113 | 113 | | |||
114 | void KrBookmarkHandler::addBookmark(KrBookmark *bm, KrBookmark *folder) | 114 | void KrBookmarkHandler::addBookmark(KrBookmark *bm, KrBookmark *folder) | ||
115 | { | 115 | { | ||
116 | if (folder == 0) | 116 | if (folder == nullptr) | ||
117 | folder = _root; | 117 | folder = _root; | ||
118 | 118 | | |||
119 | // add to the list (bottom) | 119 | // add to the list (bottom) | ||
120 | folder->children().append(bm); | 120 | folder->children().append(bm); | ||
121 | 121 | | |||
122 | exportToFile(); | 122 | exportToFile(); | ||
123 | } | 123 | } | ||
124 | 124 | | |||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Line(s) | 223 | if (file.open(QIODevice::WriteOnly)) { | |||
225 | stream.setCodec("UTF-8"); | 225 | stream.setCodec("UTF-8"); | ||
226 | stream << doc.toString(); | 226 | stream << doc.toString(); | ||
227 | file.close(); | 227 | file.close(); | ||
228 | } else { | 228 | } else { | ||
229 | KMessageBox::error(_mainWindow->widget(), i18n("Unable to write to %1", filename), i18n("Error")); | 229 | KMessageBox::error(_mainWindow->widget(), i18n("Unable to write to %1", filename), i18n("Error")); | ||
230 | } | 230 | } | ||
231 | } | 231 | } | ||
232 | 232 | | |||
233 | bool KrBookmarkHandler::importFromFileBookmark(QDomElement &e, KrBookmark *parent, QString path, QString *errorMsg) | 233 | bool KrBookmarkHandler::importFromFileBookmark(QDomElement &e, KrBookmark *parent, const QString& path, QString *errorMsg) | ||
234 | { | 234 | { | ||
235 | QString url, name, iconName; | 235 | QString url, name, iconName; | ||
236 | // verify tag | 236 | // verify tag | ||
237 | if (e.tagName() != "bookmark") { | 237 | if (e.tagName() != "bookmark") { | ||
238 | *errorMsg = i18n("%1 instead of %2", e.tagName(), QLatin1String("bookmark")); | 238 | *errorMsg = i18n("%1 instead of %2", e.tagName(), QLatin1String("bookmark")); | ||
239 | return false; | 239 | return false; | ||
240 | } | 240 | } | ||
241 | // verify href | 241 | // verify href | ||
Show All 19 Lines | 260 | } else { | |||
261 | bm->setURL(QUrl(url)); | 261 | bm->setURL(QUrl(url)); | ||
262 | bm->setIconName(iconName); | 262 | bm->setIconName(iconName); | ||
263 | } | 263 | } | ||
264 | parent->children().append(bm); | 264 | parent->children().append(bm); | ||
265 | 265 | | |||
266 | return true; | 266 | return true; | ||
267 | } | 267 | } | ||
268 | 268 | | |||
269 | bool KrBookmarkHandler::importFromFileFolder(QDomNode &first, KrBookmark *parent, QString path, QString *errorMsg) | 269 | bool KrBookmarkHandler::importFromFileFolder(QDomNode &first, KrBookmark *parent, const QString& path, QString *errorMsg) | ||
270 | { | 270 | { | ||
271 | QString name; | 271 | QString name; | ||
272 | QDomNode n = first; | 272 | QDomNode n = first; | ||
273 | while (!n.isNull()) { | 273 | while (!n.isNull()) { | ||
274 | QDomElement e = n.toElement(); | 274 | QDomElement e = n.toElement(); | ||
275 | if (e.tagName() == "bookmark") { | 275 | if (e.tagName() == "bookmark") { | ||
276 | if (!importFromFileBookmark(e, parent, path, errorMsg)) | 276 | if (!importFromFileBookmark(e, parent, path, errorMsg)) | ||
277 | return false; | 277 | return false; | ||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Line(s) | 382 | { | |||
387 | 387 | | |||
388 | // run the loop twice, in order to put the folders on top. stupid but easy :-) | 388 | // run the loop twice, in order to put the folders on top. stupid but easy :-) | ||
389 | // note: this code drops the separators put there by the user | 389 | // note: this code drops the separators put there by the user | ||
390 | QListIterator<KrBookmark *> it(parent->children()); | 390 | QListIterator<KrBookmark *> it(parent->children()); | ||
391 | while (it.hasNext()) { | 391 | while (it.hasNext()) { | ||
392 | KrBookmark *bm = it.next(); | 392 | KrBookmark *bm = it.next(); | ||
393 | 393 | | |||
394 | if (!bm->isFolder()) continue; | 394 | if (!bm->isFolder()) continue; | ||
395 | QMenu *newMenu = new QMenu(menu); | 395 | auto *newMenu = new QMenu(menu); | ||
396 | newMenu->setIcon(Icon(bm->iconName())); | 396 | newMenu->setIcon(Icon(bm->iconName())); | ||
397 | newMenu->setTitle(bm->text()); | 397 | newMenu->setTitle(bm->text()); | ||
398 | QAction *menuAction = menu->addMenu(newMenu); | 398 | QAction *menuAction = menu->addMenu(newMenu); | ||
399 | QVariant v; | 399 | QVariant v; | ||
400 | v.setValue<KrBookmark *>(bm); | 400 | v.setValue<KrBookmark *>(bm); | ||
401 | menuAction->setData(v); | 401 | menuAction->setData(v); | ||
402 | 402 | | |||
403 | buildMenu(bm, newMenu, depth + 1); | 403 | buildMenu(bm, newMenu, depth + 1); | ||
Show All 18 Lines | 418 | if (depth == 0) { | |||
422 | bool hasLan = group.readEntry("BM Lan", true); | 422 | bool hasLan = group.readEntry("BM Lan", true); | ||
423 | bool hasVirtualFS = group.readEntry("BM Virtual FS", true); | 423 | bool hasVirtualFS = group.readEntry("BM Virtual FS", true); | ||
424 | bool hasJumpback = group.readEntry("BM Jumpback", true); | 424 | bool hasJumpback = group.readEntry("BM Jumpback", true); | ||
425 | 425 | | |||
426 | if (hasPopularURLs) { | 426 | if (hasPopularURLs) { | ||
427 | menu->addSeparator(); | 427 | menu->addSeparator(); | ||
428 | 428 | | |||
429 | // add the popular links submenu | 429 | // add the popular links submenu | ||
430 | QMenu *newMenu = new QMenu(menu); | 430 | auto *newMenu = new QMenu(menu); | ||
431 | newMenu->setTitle(i18n("Popular URLs")); | 431 | newMenu->setTitle(i18n("Popular URLs")); | ||
432 | newMenu->setIcon(Icon("folder-bookmark")); | 432 | newMenu->setIcon(Icon("folder-bookmark")); | ||
433 | QAction *bmfAct = menu->addMenu(newMenu); | 433 | QAction *bmfAct = menu->addMenu(newMenu); | ||
434 | _specialBookmarks.append(bmfAct); | 434 | _specialBookmarks.append(bmfAct); | ||
435 | // add the top 15 urls | 435 | // add the top 15 urls | ||
436 | #define MAX 15 | 436 | #define MAX 15 | ||
437 | QList<QUrl> list = _mainWindow->popularUrls()->getMostPopularUrls(MAX); | 437 | QList<QUrl> list = _mainWindow->popularUrls()->getMostPopularUrls(MAX); | ||
438 | QList<QUrl>::Iterator it; | 438 | QList<QUrl>::Iterator it; | ||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Line(s) | |||||
514 | menu->addSeparator(); | 514 | menu->addSeparator(); | ||
515 | menu->addAction(KrActions::actAddBookmark); | 515 | menu->addAction(KrActions::actAddBookmark); | ||
516 | _specialBookmarks.append(KrActions::actAddBookmark); | 516 | _specialBookmarks.append(KrActions::actAddBookmark); | ||
517 | QAction *bmAct = menu->addAction(Icon("bookmarks"), | 517 | QAction *bmAct = menu->addAction(Icon("bookmarks"), | ||
518 | i18n("Manage Bookmarks"), manager, SLOT(slotEditBookmarks())); | 518 | i18n("Manage Bookmarks"), manager, SLOT(slotEditBookmarks())); | ||
519 | _specialBookmarks.append(bmAct); | 519 | _specialBookmarks.append(bmAct); | ||
520 | 520 | | |||
521 | // make sure the menu is connected to us | 521 | // make sure the menu is connected to us | ||
522 | disconnect(menu, SIGNAL(triggered(QAction*)), 0, 0); | 522 | disconnect(menu, SIGNAL(triggered(QAction*)), nullptr, nullptr); | ||
523 | } | 523 | } | ||
524 | 524 | | |||
525 | menu->installEventFilter(this); | 525 | menu->installEventFilter(this); | ||
526 | } | 526 | } | ||
527 | 527 | | |||
528 | void KrBookmarkHandler::clearBookmarks(KrBookmark *root, bool removeBookmarks) | 528 | void KrBookmarkHandler::clearBookmarks(KrBookmark *root, bool removeBookmarks) | ||
529 | { | 529 | { | ||
530 | for (auto it = root->children().begin(); it != root->children().end(); it = root->children().erase(it)) { | 530 | for (auto it = root->children().begin(); it != root->children().end(); it = root->children().erase(it)) { | ||
Show All 16 Lines | |||||
547 | void KrBookmarkHandler::bookmarksChanged(const QString&, const QString&) | 547 | void KrBookmarkHandler::bookmarksChanged(const QString&, const QString&) | ||
548 | { | 548 | { | ||
549 | importFromFile(); | 549 | importFromFile(); | ||
550 | } | 550 | } | ||
551 | 551 | | |||
552 | bool KrBookmarkHandler::eventFilter(QObject *obj, QEvent *ev) | 552 | bool KrBookmarkHandler::eventFilter(QObject *obj, QEvent *ev) | ||
553 | { | 553 | { | ||
554 | auto eventType = ev->type(); | 554 | auto eventType = ev->type(); | ||
555 | QMenu *menu = dynamic_cast<QMenu *>(obj); | 555 | auto *menu = dynamic_cast<QMenu *>(obj); | ||
556 | 556 | | |||
557 | if (eventType == QEvent::Show && menu) { | 557 | if (eventType == QEvent::Show && menu) { | ||
558 | _setQuickSearchText(""); | 558 | _setQuickSearchText(""); | ||
559 | _quickSearchMenu = menu; | 559 | _quickSearchMenu = menu; | ||
560 | qDebug() << "Bookmark search: menu" << menu << "is shown"; | 560 | qDebug() << "Bookmark search: menu" << menu << "is shown"; | ||
561 | 561 | | |||
562 | return QObject::eventFilter(obj, ev); | 562 | return QObject::eventFilter(obj, ev); | ||
563 | } | 563 | } | ||
Show All 19 Lines | 565 | if (eventType == QEvent::Close && menu && _quickSearchMenu) { | |||
583 | } | 583 | } | ||
584 | 584 | | |||
585 | return QObject::eventFilter(obj, ev); | 585 | return QObject::eventFilter(obj, ev); | ||
586 | } | 586 | } | ||
587 | 587 | | |||
588 | // Having it occur on keypress is consistent with other shortcuts, | 588 | // Having it occur on keypress is consistent with other shortcuts, | ||
589 | // such as Ctrl+W and accelerator keys | 589 | // such as Ctrl+W and accelerator keys | ||
590 | if (eventType == QEvent::KeyPress && menu) { | 590 | if (eventType == QEvent::KeyPress && menu) { | ||
591 | QKeyEvent *kev = static_cast<QKeyEvent *>(ev); | 591 | auto *kev = dynamic_cast<QKeyEvent *>(ev); | ||
592 | QList<QAction *> acts = menu->actions(); | 592 | QList<QAction *> acts = menu->actions(); | ||
593 | bool quickSearchStarted = false; | 593 | bool quickSearchStarted = false; | ||
594 | bool searchInSpecialItems = KConfigGroup(krConfig, "Look&Feel").readEntry("Search in special items", false); | 594 | bool searchInSpecialItems = KConfigGroup(krConfig, "Look&Feel").readEntry("Search in special items", false); | ||
595 | 595 | | |||
596 | if (kev->key() == Qt::Key_Left && kev->modifiers() == Qt::NoModifier) { | 596 | if (kev->key() == Qt::Key_Left && kev->modifiers() == Qt::NoModifier) { | ||
597 | menu->close(); | 597 | menu->close(); | ||
598 | return true; | 598 | return true; | ||
599 | } | 599 | } | ||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | 684 | } else if (nMatches > 1) { | |||
691 | } | 691 | } | ||
692 | } else { | 692 | } else { | ||
693 | menu->setActiveAction(nullptr); | 693 | menu->setActiveAction(nullptr); | ||
694 | } | 694 | } | ||
695 | return true; | 695 | return true; | ||
696 | } | 696 | } | ||
697 | 697 | | |||
698 | if (eventType == QEvent::MouseButtonRelease) { | 698 | if (eventType == QEvent::MouseButtonRelease) { | ||
699 | switch (static_cast<QMouseEvent *>(ev)->button()) { | 699 | switch (dynamic_cast<QMouseEvent *>(ev)->button()) { | ||
700 | case Qt::RightButton: | 700 | case Qt::RightButton: | ||
701 | _middleClick = false; | 701 | _middleClick = false; | ||
702 | if (obj->inherits("QMenu")) { | 702 | if (obj->inherits("QMenu")) { | ||
703 | QMenu *menu = static_cast<QMenu *>(obj); | 703 | auto *menu = dynamic_cast<QMenu *>(obj); | ||
704 | QAction *act = menu->actionAt(static_cast<QMouseEvent *>(ev)->pos()); | 704 | QAction *act = menu->actionAt(dynamic_cast<QMouseEvent *>(ev)->pos()); | ||
705 | 705 | | |||
706 | if (obj == _mainBookmarkPopup && _specialBookmarks.contains(act)) { | 706 | if (obj == _mainBookmarkPopup && _specialBookmarks.contains(act)) { | ||
707 | rightClickOnSpecialBookmark(); | 707 | rightClickOnSpecialBookmark(); | ||
708 | return true; | 708 | return true; | ||
709 | } | 709 | } | ||
710 | 710 | | |||
711 | KrBookmark *bm = dynamic_cast<KrBookmark *>(act); | 711 | auto *bm = dynamic_cast<KrBookmark *>(act); | ||
712 | if (bm != 0) { | 712 | if (bm != nullptr) { | ||
713 | rightClicked(menu, bm); | 713 | rightClicked(menu, bm); | ||
714 | return true; | 714 | return true; | ||
715 | } else if (act && act->data().canConvert<KrBookmark *>()) { | 715 | } else if (act && act->data().canConvert<KrBookmark *>()) { | ||
716 | KrBookmark *bm = act->data().value<KrBookmark *>(); | 716 | auto *bm = act->data().value<KrBookmark *>(); | ||
717 | rightClicked(menu, bm); | 717 | rightClicked(menu, bm); | ||
718 | } | 718 | } | ||
719 | } | 719 | } | ||
720 | break; | 720 | break; | ||
721 | case Qt::LeftButton: | 721 | case Qt::LeftButton: | ||
722 | _middleClick = false; | 722 | _middleClick = false; | ||
723 | break; | 723 | break; | ||
724 | case Qt::MidButton: | 724 | case Qt::MidButton: | ||
▲ Show 20 Lines • Show All 157 Lines • Show Last 20 Lines |