File indexing completed on 2022-11-29 20:24:09

0001 /* This file is part of the KDE project
0002    Copyright (C) 2000, 2010 David Faure <faure@kde.org>
0003    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License version 2 or at your option version 3 as published by
0008    the Free Software Foundation.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "globalbookmarkmanager.h"
0022 #include "kbookmarkmodel/commandhistory.h"
0023 #include "kbookmarkmodel/model.h"
0024 #include <KBookmarkManager>
0025 #include <QDateTime>
0026 #include <QLocale>
0027 
0028 GlobalBookmarkManager *GlobalBookmarkManager::s_mgr = nullptr;
0029 
0030 GlobalBookmarkManager::GlobalBookmarkManager()
0031     : QObject(nullptr)
0032     , m_mgr(nullptr)
0033     , m_model(nullptr)
0034 {
0035 }
0036 
0037 GlobalBookmarkManager::~GlobalBookmarkManager()
0038 {
0039 }
0040 
0041 KBookmarkGroup GlobalBookmarkManager::root()
0042 {
0043     return mgr()->root();
0044 }
0045 
0046 KBookmark GlobalBookmarkManager::bookmarkAt(const QString &a)
0047 {
0048     return self()->mgr()->findByAddress(a);
0049 }
0050 
0051 bool GlobalBookmarkManager::managerSave()
0052 {
0053     return mgr()->save();
0054 }
0055 
0056 void GlobalBookmarkManager::saveAs(const QString &fileName)
0057 {
0058     mgr()->saveAs(fileName);
0059 }
0060 
0061 void GlobalBookmarkManager::setUpdate(bool update)
0062 {
0063     mgr()->setUpdate(update);
0064 }
0065 
0066 QString GlobalBookmarkManager::path() const
0067 {
0068     return mgr()->path();
0069 }
0070 
0071 void GlobalBookmarkManager::createManager(const QString &filename, const QString &dbusObjectName, CommandHistory *commandHistory)
0072 {
0073     if (m_mgr) {
0074         // qCDebug(KEDITBOOKMARKS_LOG)<<"createManager called twice";
0075         delete m_mgr;
0076     }
0077 
0078     // qCDebug(KEDITBOOKMARKS_LOG)<<"DBus Object name: "<<dbusObjectName;
0079     m_mgr = KBookmarkManager::managerForFile(filename, dbusObjectName);
0080 
0081     commandHistory->setBookmarkManager(m_mgr);
0082 
0083     if (m_model) {
0084         m_model->setRoot(root());
0085     } else {
0086         m_model = new KBookmarkModel(root(), commandHistory, this);
0087     }
0088 }
0089 
0090 void GlobalBookmarkManager::notifyManagers(const KBookmarkGroup &grp)
0091 {
0092     m_model->notifyManagers(grp);
0093 }
0094 
0095 void GlobalBookmarkManager::notifyManagers()
0096 {
0097     notifyManagers(root());
0098 }
0099 
0100 void GlobalBookmarkManager::reloadConfig()
0101 {
0102     mgr()->emitConfigChanged();
0103 }
0104 
0105 QString GlobalBookmarkManager::makeTimeStr(const QString &in)
0106 {
0107     int secs;
0108     bool ok;
0109     secs = in.toInt(&ok);
0110     if (!ok)
0111         return QString();
0112     return makeTimeStr(secs);
0113 }
0114 
0115 QString GlobalBookmarkManager::makeTimeStr(int b)
0116 {
0117     QDateTime dt;
0118     dt.fromSecsSinceEpoch(b);
0119     QLocale l;
0120     return (dt.daysTo(QDateTime::currentDateTime()) > 31) ? l.toString(dt.date(), QLocale::LongFormat) : l.toString(dt, QLocale::LongFormat);
0121 }