summaryrefslogtreecommitdiff
path: root/src/bookmarks/bookmarkstreeitem.hpp
blob: caed1ecb5256f4e159b15bb39432380c6ffc6a7b (plain)
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
/* ============================================================
 * rekonq
 * ============================================================
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================
 * Description: rekonq bookmark tree item
 * ============================================================ */

#pragma once

#include "rekonq.hpp"
#include <QAction>
#include <QDateTime>
#include <QIcon>
#include <QUrl>
#include <QVariant>
#include <QVector>
#include <utility>

class BookmarksTreeItem {
public:
  enum Types { Root, Folder, Bookmark, Separator, Alias };
  constexpr static int TypesCount = 5;

  struct Attributes_t {
    QString title{};
    QUrl href{};
    QDateTime added{};
    QDateTime visited{};
    QDateTime modified{};
    QString description{};
    QString id{};
  };
  enum Attributes { Title = 0, Href = 1, Added = 2, Visited = 3, Modified = 4, Description = 5, Id = 6 };
  constexpr static int AttributeCount = 7;

  explicit BookmarksTreeItem(Types type, Attributes_t &&args, BookmarksTreeItem *parent);
  ~BookmarksTreeItem();

  template <typename T> [[nodiscard]] QAction *action(T *sender, rekonq::OpenType type) const
  {
    auto *action = new QAction(m_data.title, sender);
    QObject::connect(action, &QAction::triggered, sender,
                     [this, sender, type]() { emit sender->loadUrl(m_data.href, type); });
    return action;
  }

  [[nodiscard]] BookmarksTreeItem *parent() const { return m_parent; }

  /**
   * Add an item to the end of the children list, if the item can have children
   * @return true if the item was added, false otherwise
   */
  bool appendChild(BookmarksTreeItem *childItem);
  bool insertChild(int position, BookmarksTreeItem *childItem);
  bool removeChildAt(int index, int count = 1);
  [[nodiscard]] BookmarksTreeItem *takeChild(int index) { return m_children.takeAt(index); }

  [[nodiscard]] BookmarksTreeItem *child(int index) const { return m_children.value(index); }
  [[nodiscard]] auto childCount() const { return m_children.count(); }

  [[nodiscard]] BookmarksTreeItem *findChild(const QString &id)
  {
    if (id.isEmpty()) return nullptr;
    if (m_data.id == id) return this;

    for (auto *child : qAsConst(m_children))
      if (child->m_data.id == id) return child;
    for (auto *child : qAsConst(m_children))
      if (auto *grandchild = child->findChild(id)) return grandchild;

    return nullptr;
  }

  [[nodiscard]] QVariant data(Attributes column) const
  {
    switch (column) {
    case Title:
      return m_data.title;
    case Href:
      return m_data.href;
    case Added:
      return m_data.added;
    case Visited:
      return m_data.visited;
    case Modified:
      return m_data.modified;
    case Description:
      return m_data.description;
    case Id:
      return m_data.id;
    }
    return {};
  }
  bool setData(Attributes column, const QVariant &data);

  [[nodiscard]] auto icon() const { return m_icon; }
  [[nodiscard]] bool isExpanded() const { return m_isExpanded; }
  void setExpanded(bool expanded);

  [[nodiscard]] QString tooltip() const;
  [[nodiscard]] Types type() const { return m_type; }
  [[nodiscard]] int row() const;

  QStringList metadata;

private:
  BookmarksTreeItem *m_parent;
  QVector<BookmarksTreeItem *> m_children;

  const Types m_type;
  QIcon m_icon;
  bool m_isExpanded = false;

  Attributes_t m_data;
};