summaryrefslogtreecommitdiff
path: root/src/bookmarks/bookmarkstreeitem.hpp
blob: 1f87ecad5382f0a136155f8465d5b498c21597a0 (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
118
119
120
121
122
123
124
125
126
/* ============================================================
 * rekonq
 * ============================================================
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================
 * Description: rekonq bookmarks model
 * ============================================================ */

#pragma once

#include <QDateTime>
#include <QIcon>
#include <QUrl>
#include <QVariant>
#include <QVector>
#include <utility>

class BookmarksTreeItem {
public:
  enum BookmarkAction {
    OPEN = 0,
    OPEN_IN_TAB,
    OPEN_IN_WINDOW,
    OPEN_FOLDER,
    BOOKMARK_PAGE,
    NEW_FOLDER,
    NEW_SEPARATOR,
    COPY,
    EDIT,
#ifdef HAVE_NEPOMUK
    FANCYBOOKMARK,
#endif
    DELETE,
    NUM_ACTIONS,
    SET_TOOLBAR_FOLDER,
    UNSET_TOOLBAR_FOLDER
  };

  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();

  [[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;
};