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

#include "bookmarkstreeitem.hpp"

BookmarksTreeItem::BookmarksTreeItem(Types type, Attributes_t &&args, BookmarksTreeItem *parent)
    : m_parent(parent), m_type(type), m_data(args)
{
  switch (m_type) {
  case Folder:
    m_icon = QIcon::fromTheme("folder");
    break;
  case Bookmark:
    m_icon = QIcon::fromTheme("bookmark");
    break;
  case Alias:
  case Root:
  case Separator:
    break;
  }
}

BookmarksTreeItem::~BookmarksTreeItem() { qDeleteAll(m_children); }

bool BookmarksTreeItem::appendChild(BookmarksTreeItem *childItem)
{
  // only root and folders can have children
  if (m_type == Folder || m_type == Root) {
    m_children.append(childItem);
    return true;
  }
  return false;
}

bool BookmarksTreeItem::insertChild(int position, BookmarksTreeItem *childItem)
{
  if (m_type == Folder || m_type == Root) {
    // position is invalid (-1) when dropping an item onto the folder, which leads to crash
    // make sure that position passed is >= 0 (insert item at first position)
    childItem->m_parent = this;
    m_children.insert(qMax(position, 0), childItem);
    return true;
  }

  return false;
}

bool BookmarksTreeItem::removeChildAt(int index, int count)
{
  if (index < 0 || index + count > m_children.size()) return false;

  // delete the item at index count times
  for (int i = 0; i < count; ++i) { delete m_children.takeAt(index); }
  return true;
}

bool BookmarksTreeItem::setData(Attributes column, const QVariant &data)
{
  switch (column) {
  case Title:
    m_data.title = data.toString();
    return true;
  case Href:
    m_data.href = data.toUrl();
    return true;
  case Added:
    m_data.added = data.toDateTime();
    return true;
  case Visited:
    m_data.visited = data.toDateTime();
    return true;
  case Modified:
    m_data.modified = data.toDateTime();
    return true;
  case Description:
    m_data.description = data.toString();
    return true;
  case Id:
    m_data.id = data.toString();
    return true;
  }

  return false;
}

void BookmarksTreeItem::setExpanded(bool expanded)
{
  if (m_type == BookmarksTreeItem::Folder) m_isExpanded = expanded;
}

QString BookmarksTreeItem::tooltip() const
{
  auto msg = QString("<p><b>%1</b></p>").arg(m_data.title);
  if (const auto href = m_data.href.toString(); !href.isEmpty())
    msg += QString("<p>%2</p>").arg(m_data.href.toString());
  return msg;
}

int BookmarksTreeItem::row() const
{
  if (m_parent) return static_cast<int>(m_parent->m_children.indexOf(const_cast<BookmarksTreeItem *>(this)));
  return 0;
}