summaryrefslogtreecommitdiff
path: root/src/bookmarks/bookmarkstreemodel.cpp
blob: d64f1588b216d810d30c1b153fb6d1b3e3478e95 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ============================================================
 * rekonq
 * ============================================================
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================
 * Description: rekonq bookmarks model
 * ============================================================ */

#include "bookmarkstreemodel.hpp"
#include "bookmarkstreeformats.hpp"
#include <QBuffer>
#include <QDateTime>
#include <QMimeData>

BookmarksTreeModel::BookmarksTreeModel(QObject *parent) : QAbstractItemModel(parent)
{
  rootItem = new BookmarksTreeItem(BookmarksTreeItem::Root, {.title = tr("Title"), .href = tr("Address")}, nullptr);
}

BookmarksTreeModel::~BookmarksTreeModel() { delete rootItem; }

QVariant BookmarksTreeModel::headerData(int section, Qt::Orientation, int role) const
{
  if (role != Qt::DisplayRole) return {};
  return rootItem->data(static_cast<BookmarksTreeItem::Attributes>(section));
}

QVariant BookmarksTreeModel::data(const QModelIndex &index, int role) const
{
  if (!index.isValid()) return {};

  auto *it = item(index);

  // find item Alias points to
  if (it->type() == BookmarksTreeItem::Alias) {
    // find the item this is an alias of
    auto *child = rootItem->findChild(it->data(BookmarksTreeItem::Href).toString());
    if (child == nullptr) return {};
    it = child;
  }

  switch (role) {
  case Qt::DecorationRole:
    if (index.column() == 0) return it->icon();
    break;
  case Qt::ToolTipRole:
    return it->tooltip();
  case Qt::DisplayRole:
  case Qt::EditRole:
    if (index.column() == 0) return it->data(BookmarksTreeItem::Title);
    if (index.column() == 1) return it->data(BookmarksTreeItem::Href);
    break;
  case CompletionMatchingRole:
    if (it->type() == BookmarksTreeItem::Bookmark) return it->data(BookmarksTreeItem::Href);
    return it->data(BookmarksTreeItem::Title);
  default:
    break;
  }

  return {};
}

bool BookmarksTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
  if (!index.isValid()) return false;

  bool success = false;

  if (role == Qt::DisplayRole || role == Qt::EditRole) {
    auto *item = static_cast<BookmarksTreeItem *>(index.internalPointer());
    success = item->setData(static_cast<BookmarksTreeItem::Attributes>(index.column()), value);
  }

  if (success) {
    emit dataChanged(index, index, {role});
    m_isModified = true;
  }

  return success;
}

Qt::ItemFlags BookmarksTreeModel::flags(const QModelIndex &index) const
{
  switch (item(index)->type()) {
  case BookmarksTreeItem::Root:
    return QAbstractItemModel::flags(index) /*| Qt::ItemIsDragEnabled*/ | Qt::ItemIsDropEnabled;
  case BookmarksTreeItem::Folder:
    return QAbstractItemModel::flags(index) | (index.column() == 0 ? Qt::ItemIsEditable : Qt::NoItemFlags) |
           Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
  case BookmarksTreeItem::Bookmark:
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemNeverHasChildren;
  case BookmarksTreeItem::Separator:
    return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemNeverHasChildren;
  case BookmarksTreeItem::Alias:
    // TODO find aliased item and return its flags
    return QAbstractItemModel::flags(index);
  }

  __builtin_unreachable();
}

int BookmarksTreeModel::rowCount(const QModelIndex &index) const
{
  if (index.column() > 0) return 0;
  return static_cast<int>(item(index)->childCount());
}

QModelIndex BookmarksTreeModel::appendItem(BookmarksTreeItem::Types type, BookmarksTreeItem::Attributes_t data,
                                           const QModelIndex &parent)
{
  auto *parentItem = item(parent);
  const auto row = rowCount(parent);

  beginInsertRows(parent, row, row);
  auto *child = new BookmarksTreeItem(type, std::move(data), parentItem);
  child->setData(BookmarksTreeItem::Added, QDateTime::currentDateTime());
  bool success = parentItem->appendChild(child);
  endInsertRows();

  if (success) {
    m_isModified = true;
    return createIndex(row, 0, child);
  }
  else
    return {};
}

bool BookmarksTreeModel::removeRows(int position, int rows, const QModelIndex &parent)
{
  auto *parentItem = item(parent);

  beginRemoveRows(parent, position, position + rows - 1);
  bool success = parentItem->removeChildAt(position, rows);
  endRemoveRows();

  if (success) m_isModified = true;
  return success;
}

QModelIndex BookmarksTreeModel::index(int row, int column, const QModelIndex &parent) const
{
  if (!this->hasIndex(row, column, parent)) return {};

  BookmarksTreeItem *parentItem = item(parent);
  BookmarksTreeItem *childItem = parentItem->child(row);
  if (childItem) return createIndex(row, column, childItem);
  return {};
}

QModelIndex BookmarksTreeModel::parent(const QModelIndex &index) const
{
  if (!index.isValid()) return {};

  auto *childItem = static_cast<BookmarksTreeItem *>(index.internalPointer());
  auto *parentItem = childItem->parent();

  if (parentItem == rootItem) return {};

  return createIndex(parentItem->row(), 0, parentItem);
}

QModelIndex BookmarksTreeModel::parentFolder(const QModelIndex &index) const
{
  // invalid index is the root index -> return it back
  if (!index.isValid()) return {};

  if (item(index)->type() == BookmarksTreeItem::Bookmark) { return index.parent(); }

  return index;
}

BookmarksTreeItem *BookmarksTreeModel::item(const QModelIndex &index) const
{
  if (!index.isValid()) return rootItem;
  return static_cast<BookmarksTreeItem *>(index.internalPointer());
}

/*
 * Drag'n'Drop implementation
 * How drag and drop actually works: the view encodes the data of the original item (using
 * BookmarksTreeModel::mimeData), and then uses BookmarksTreeModel::dropMimeData to create the new item. If successful,
 * the old item is removed (through BookmarksTreeModel::removeRows).
 */

QMimeData *BookmarksTreeModel::mimeData(const QModelIndexList &indexes) const
{
  QByteArray data;
  QBuffer buffer(&data);
  buffer.open(QIODevice::WriteOnly | QIODevice::Text);

  QVector<const BookmarksTreeItem *> items;
  for (const QModelIndex &index : indexes) {
    if (index.isValid() && index.column() == 0) items.append(item(index));
  }
  const auto write_result = xbel::write(&buffer, items);
  Q_ASSERT(write_result == false);

  auto *mimeData = new QMimeData;
  mimeData->setData(mimeType, data);
  return mimeData;
}

bool BookmarksTreeModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column,
                                      const QModelIndex &parent)
{
  if (action == Qt::IgnoreAction) return true;
  if (action != Qt::MoveAction) return false; // we only implement MoveAction's
  if (!mimeData->hasFormat(mimeType) || column > 0) return false;

  auto data = mimeData->data(mimeType);
  QBuffer buffer(&data);
  buffer.open(QIODevice::ReadOnly | QIODevice::Text);

  auto *fake_root = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr);
  const auto read_result = xbel::read(&buffer, fake_root);
  Q_ASSERT(read_result.isEmpty());
  const auto childCount = static_cast<int>(fake_root->childCount());

  auto *parentItem = item(parent);

  beginInsertRows(parent, row, row + childCount - 1);
  for (int i = 0; i < childCount; ++i) parentItem->insertChild(row + i, fake_root->takeChild(i));
  endInsertRows();

  delete fake_root;

  m_isModified = true;
  return true;
}

QList<QString> BookmarksTreeModel::load(QIODevice *buffer) { return readFns[Formats::FormatXbel](buffer, rootItem); }

void BookmarksTreeModel::save(QIODevice *buffer)
{
  if (!buffer->isOpen() || !buffer->isWritable()) return;
  m_isModified = !writeFns[Formats::FormatXbel](buffer, {rootItem});
}