File indexing completed on 2024-04-21 04:58:39

0001 /*
0002     SPDX-FileCopyrightText: 2008 Urs Wolfer <uwolfer@kde.org>
0003     SPDX-FileCopyrightText: 2009 Tony Murray <murraytony@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef REMOTEDESKTOPSMODEL_H
0009 #define REMOTEDESKTOPSMODEL_H
0010 
0011 #include "bookmarkmanager.h"
0012 
0013 #include <QAbstractTableModel>
0014 #include <QDateTime>
0015 
0016 #ifdef BUILD_ZEROCONF
0017 namespace KDNSSD
0018 {
0019 class ServiceBrowser;
0020 }
0021 #endif
0022 
0023 struct RemoteDesktop {
0024 public:
0025     enum Source { None = 0x0, Bookmarks = 0x1, History = 0x2, Zeroconf = 0x4 };
0026     Q_DECLARE_FLAGS(Sources, Source)
0027     QString title;
0028     QString url;
0029     QDateTime lastConnected;
0030     QDateTime created;
0031     int visits;
0032     RemoteDesktop::Source source;
0033     bool favorite;
0034     bool operator<(const RemoteDesktop &rd) const
0035     {
0036         if (lastConnected == rd.lastConnected)
0037             return url < rd.url;
0038         return rd.lastConnected < lastConnected; // seems backward but gets the desired result
0039     }
0040     bool operator==(const RemoteDesktop &rd) const
0041     {
0042         return url == rd.url;
0043     }
0044 };
0045 
0046 class RemoteDesktopsModel : public QAbstractTableModel
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051     explicit RemoteDesktopsModel(QObject *parent, KBookmarkManager *manager);
0052     ~RemoteDesktopsModel() override;
0053 
0054     enum DisplayItems { Favorite, Title, LastConnected, VisitCount, Created, Source };
0055     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0056     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0057     Qt::ItemFlags flags(const QModelIndex &index) const override;
0058     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0059     QVariant data(const QModelIndex &index, int role) const override;
0060     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0061 
0062 private:
0063     QList<RemoteDesktop> remoteDesktops;
0064     QString getLastConnectedString(QDateTime lastConnected, bool fuzzy = false) const;
0065     void removeAllItemsFromSources(RemoteDesktop::Sources sources);
0066     void buildModelFromBookmarkGroup(const KBookmarkGroup &group);
0067     KBookmarkManager *m_manager;
0068 
0069 #ifdef BUILD_ZEROCONF
0070     KDNSSD::ServiceBrowser *zeroconfBrowser;
0071     QHash<QString, QString> m_protocols;
0072 #endif
0073 
0074 private Q_SLOTS:
0075     void bookmarksChanged();
0076 #ifdef BUILD_ZEROCONF
0077     void servicesChanged();
0078 #endif
0079 };
0080 
0081 Q_DECLARE_OPERATORS_FOR_FLAGS(RemoteDesktop::Sources)
0082 
0083 #endif