1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-11-21
 * Description : Central object for managing bookmarks
 *
 * SPDX-FileCopyrightText: 2010-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2009-2010 by Michael G. Hansen <mike at mghansen dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "gpsbookmarkowner.h"

// Qt includes

#include <QStandardItemModel>
#include <QStandardPaths>
#include <QPointer>
#include <QApplication>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "gpsbookmarkmodelhelper.h"
#include "gpsundocommand.h"
#include "gpsitemmodel.h"
#include "bookmarknode.h"
#include "bookmarksmenu.h"
#include "bookmarksdlg.h"

namespace Digikam
{

class Q_DECL_HIDDEN GPSBookmarkOwner::Private
{
public:

    Private() = default;

    QWidget*                  parent                = nullptr;
    BookmarksManager*         bookmarkManager       = nullptr;
    BookmarksMenu*            bookmarkMenu          = nullptr;
    QPointer<BookmarksDialog> bookmarksDialog;
    bool                      addBookmarkEnabled    = true;
    GPSBookmarkModelHelper*   bookmarkModelHelper   = nullptr;
    GeoCoordinates            lastCoordinates;
    QString                   lastTitle;
};

GPSBookmarkOwner::GPSBookmarkOwner(GPSItemModel* const gpsItemModel, QWidget* const parent)
    : QObject(parent),
      d      (new Private)
{
    d->parent = parent;

    const QString bookmarksFileName = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
                                                                       QLatin1String("/geobookmarks.xml");
    d->bookmarkManager              = new BookmarksManager(bookmarksFileName, this);
    d->bookmarkManager->load();
    d->bookmarkMenu                 = new BookmarksMenu(d->bookmarkManager, d->parent);
    d->bookmarkModelHelper          = new GPSBookmarkModelHelper(d->bookmarkManager,
                                                                 gpsItemModel, this);

    createBookmarksMenu();
}

GPSBookmarkOwner::~GPSBookmarkOwner()
{
    delete d;
}

QMenu* GPSBookmarkOwner::getMenu() const
{
    return dynamic_cast<QMenu*>(d->bookmarkMenu);
}

QString GPSBookmarkOwner::currentTitle() const
{
    if (d->lastTitle.isEmpty())
    {
        return currentUrl();
    }

    return d->lastTitle;
}

QString GPSBookmarkOwner::currentUrl() const
{
    // TODO : check if this QUrl from QString conversion is fine.

    return d->lastCoordinates.geoUrl();
}

void GPSBookmarkOwner::slotOpenBookmark(const QUrl& url)
{
    const QString gps                         = url.url().toLower();
    bool  okay                                = false;
    const GeoCoordinates coordinate = GeoCoordinates::fromGeoUrl(gps, &okay);

    if (okay)
    {
        GPSDataContainer position;
        position.setCoordinates(coordinate);

        Q_EMIT positionSelected(position);
    }
}

void GPSBookmarkOwner::slotShowBookmarksDialog()
{

#ifdef Q_OS_DARWIN

    // See bug #486341

    QPointer<BookmarksDialog> dlg = new BookmarksDialog(d->parent, d->bookmarkManager);
    dlg->exec();

#else

    if (
        d->bookmarksDialog &&
        (d->bookmarksDialog->isMinimized() || !d->bookmarksDialog->isHidden())
       )
    {
        d->bookmarksDialog->showNormal();       // krazy:exclude=qmethods
        d->bookmarksDialog->activateWindow();
        d->bookmarksDialog->raise();
    }
    else
    {
        d->bookmarksDialog = new BookmarksDialog(qApp->activeWindow(), d->bookmarkManager);
        d->bookmarksDialog->show();
        d->bookmarksDialog->raise();
    }

#endif

}

void GPSBookmarkOwner::slotAddBookmark()
{
    QPointer<AddBookmarkDialog> dlg = new AddBookmarkDialog(currentUrl(), currentTitle(),
                                                            d->parent, d->bookmarkManager);
    dlg->exec();
}

void GPSBookmarkOwner::changeAddBookmark(const bool state)
{
    d->addBookmarkEnabled = state;

    createBookmarksMenu();
}

void GPSBookmarkOwner::createBookmarksMenu()
{
    d->bookmarkMenu->clear();

    QList<QAction*> bookmarksActions;

    QAction* const showAllBookmarksAction = new QAction(i18n("Edit Bookmarks"), d->parent);<--- Variable 'showAllBookmarksAction' can be declared as pointer to const
    bookmarksActions.append(showAllBookmarksAction);

    connect(showAllBookmarksAction, SIGNAL(triggered()),
            this, SLOT(slotShowBookmarksDialog()));

    QAction* const addBookmark = new QAction(i18n("Add Bookmark..."), d->parent);<--- Variable 'addBookmark' can be declared as pointer to const
    bookmarksActions.append(addBookmark);

    connect(addBookmark, SIGNAL(triggered()),
            this, SLOT(slotAddBookmark()));

    d->bookmarkMenu->setInitialActions(bookmarksActions);

    connect(d->bookmarkMenu, SIGNAL(openUrl(QUrl)),
            this, SLOT(slotOpenBookmark(QUrl)));
}

BookmarksManager* GPSBookmarkOwner::bookmarkManager() const
{
    return d->bookmarkManager;
}

GPSBookmarkModelHelper* GPSBookmarkOwner::bookmarkModelHelper() const
{
    return d->bookmarkModelHelper;
}

void GPSBookmarkOwner::setPositionAndTitle(const GeoCoordinates& coordinates,
                                           const QString& title)
{
    d->lastCoordinates = coordinates;
    d->lastTitle       = title;
}

} // namespace Digikam

#include "moc_gpsbookmarkowner.cpp"