File indexing completed on 2025-01-19 03:58:03

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-11-21
0007  * Description : Central object for managing bookmarks
0008  *
0009  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2009-2010 by Michael G. Hansen <mike at mghansen dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "gpsbookmarkowner.h"
0017 
0018 // Qt includes
0019 
0020 #include <QStandardItemModel>
0021 #include <QStandardPaths>
0022 #include <QPointer>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "gpsbookmarkmodelhelper.h"
0031 #include "gpsundocommand.h"
0032 #include "gpsitemmodel.h"
0033 #include "bookmarknode.h"
0034 #include "bookmarksmenu.h"
0035 #include "bookmarksdlg.h"
0036 
0037 namespace Digikam
0038 {
0039 
0040 class Q_DECL_HIDDEN GPSBookmarkOwner::Private
0041 {
0042 public:
0043 
0044     explicit Private()
0045       : parent             (nullptr),
0046         bookmarkManager    (nullptr),
0047         bookmarkMenu       (nullptr),
0048         addBookmarkEnabled (true),
0049         bookmarkModelHelper(nullptr)
0050     {
0051     }
0052 
0053     QWidget*                  parent;
0054     BookmarksManager*         bookmarkManager;
0055     BookmarksMenu*            bookmarkMenu;
0056     QPointer<BookmarksDialog> bookmarksDialog;
0057     bool                      addBookmarkEnabled;
0058     GPSBookmarkModelHelper*   bookmarkModelHelper;
0059     GeoCoordinates            lastCoordinates;
0060     QString                   lastTitle;
0061 };
0062 
0063 GPSBookmarkOwner::GPSBookmarkOwner(GPSItemModel* const gpsItemModel, QWidget* const parent)
0064     : QObject(parent),
0065       d(new Private())
0066 {
0067     d->parent = parent;
0068 
0069     const QString bookmarksFileName = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
0070                                                                        QLatin1String("/geobookmarks.xml");
0071     d->bookmarkManager              = new BookmarksManager(bookmarksFileName, this);
0072     d->bookmarkManager->load();
0073     d->bookmarkMenu                 = new BookmarksMenu(d->bookmarkManager, d->parent);
0074     d->bookmarkModelHelper          = new GPSBookmarkModelHelper(d->bookmarkManager,
0075                                                                  gpsItemModel, this);
0076 
0077     createBookmarksMenu();
0078 }
0079 
0080 GPSBookmarkOwner::~GPSBookmarkOwner()
0081 {
0082     delete d;
0083 }
0084 
0085 QMenu* GPSBookmarkOwner::getMenu() const
0086 {
0087     return dynamic_cast<QMenu*>(d->bookmarkMenu);
0088 }
0089 
0090 QString GPSBookmarkOwner::currentTitle() const
0091 {
0092     if (d->lastTitle.isEmpty())
0093     {
0094         return currentUrl();
0095     }
0096 
0097     return d->lastTitle;
0098 }
0099 
0100 QString GPSBookmarkOwner::currentUrl() const
0101 {
0102     // TODO : check if this QUrl from QString conversion is fine.
0103 
0104     return d->lastCoordinates.geoUrl();
0105 }
0106 
0107 void GPSBookmarkOwner::slotOpenBookmark(const QUrl& url)
0108 {
0109     const QString gps                         = url.url().toLower();
0110     bool  okay                                = false;
0111     const GeoCoordinates coordinate = GeoCoordinates::fromGeoUrl(gps, &okay);
0112 
0113     if (okay)
0114     {
0115         GPSDataContainer position;
0116         position.setCoordinates(coordinate);
0117         Q_EMIT positionSelected(position);
0118     }
0119 }
0120 
0121 void GPSBookmarkOwner::slotShowBookmarksDialog()
0122 {
0123     if (d->bookmarksDialog &&
0124         (d->bookmarksDialog->isMinimized() || !d->bookmarksDialog->isHidden()))
0125     {
0126         d->bookmarksDialog->showNormal();       // krazy:exclude=qmethods
0127         d->bookmarksDialog->activateWindow();
0128         d->bookmarksDialog->raise();
0129     }
0130     else
0131     {
0132         d->bookmarksDialog = new BookmarksDialog(d->parent, d->bookmarkManager);
0133         d->bookmarksDialog->show();
0134     }
0135 }
0136 
0137 void GPSBookmarkOwner::slotAddBookmark()
0138 {
0139     AddBookmarkDialog* const dlg = new AddBookmarkDialog(currentUrl(), currentTitle(),
0140                                                          d->parent, d->bookmarkManager);
0141     dlg->exec();
0142 }
0143 
0144 void GPSBookmarkOwner::changeAddBookmark(const bool state)
0145 {
0146     d->addBookmarkEnabled = state;
0147 
0148     createBookmarksMenu();
0149 }
0150 
0151 void GPSBookmarkOwner::createBookmarksMenu()
0152 {
0153     d->bookmarkMenu->clear();
0154 
0155     QList<QAction*> bookmarksActions;
0156 
0157     QAction* const showAllBookmarksAction = new QAction(i18n("Edit Bookmarks"), d->parent);
0158     bookmarksActions.append(showAllBookmarksAction);
0159 
0160     connect(showAllBookmarksAction, SIGNAL(triggered()),
0161             this, SLOT(slotShowBookmarksDialog()));
0162 
0163     QAction* const addBookmark = new QAction(i18n("Add Bookmark..."), d->parent);
0164     bookmarksActions.append(addBookmark);
0165 
0166     connect(addBookmark, SIGNAL(triggered()),
0167             this, SLOT(slotAddBookmark()));
0168 
0169     d->bookmarkMenu->setInitialActions(bookmarksActions);
0170 
0171     connect(d->bookmarkMenu, SIGNAL(openUrl(QUrl)),
0172             this, SLOT(slotOpenBookmark(QUrl)));
0173 }
0174 
0175 BookmarksManager* GPSBookmarkOwner::bookmarkManager() const
0176 {
0177     return d->bookmarkManager;
0178 }
0179 
0180 GPSBookmarkModelHelper* GPSBookmarkOwner::bookmarkModelHelper() const
0181 {
0182     return d->bookmarkModelHelper;
0183 }
0184 
0185 void GPSBookmarkOwner::setPositionAndTitle(const GeoCoordinates& coordinates,
0186                                            const QString& title)
0187 {
0188     d->lastCoordinates = coordinates;
0189     d->lastTitle       = title;
0190 }
0191 
0192 } // namespace Digikam
0193 
0194 #include "moc_gpsbookmarkowner.cpp"