1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "displayrecentview.h"
6#include "common/common.h"
7#include "displayitemdelegate.h"
8
9#include <QDir>
10#include <QJsonDocument>
11#include <QJsonObject>
12#include <QJsonArray>
13#include <QStandardItem>
14#include <QStandardItemModel>
15
16#define INIT_DATA "{\n \"Projects\":[],\n \"Documents\":[]\n}\n"
17
18QJsonDocument DisplayRecentView::readRecent()
19{
20 QJsonDocument recentDoc;
21 QFile file(cachePath());
22 if (!file.exists()) {
23 if (file.open(QFile::Truncate| QFile::WriteOnly)) {
24 file.write(INIT_DATA);
25 file.close();
26 }
27 } else {
28 if (file.open(QFile::ReadOnly)) {
29 recentDoc = QJsonDocument::fromJson(file.readAll());
30 file.close();
31 }
32
33 if (!recentDoc.object().keys().contains("Projects")
34 || !recentDoc.object().keys().contains("Documents")) {
35 file.remove();
36 file.close();
37 file.open(QFile::Truncate| QFile::WriteOnly);
38 file.write(INIT_DATA);
39 file.close();
40 }
41 }
42
43 if (file.open(QFile::ReadOnly)) {
44 recentDoc = QJsonDocument::fromJson(file.readAll());
45 file.close();
46 }
47 return recentDoc;
48}
49
50QList<QStandardItem *> DisplayRecentView::itemsFromFile()
51{
52 QJsonDocument doc = readRecent();
53 QJsonObject obj = doc.object();
54 QJsonArray array = obj.value(title()).toArray();
55 QList<QStandardItem*> result;
56 for (auto one : array) {
57 QString filePath(one.toString());
58 auto rowItem = new QStandardItem (icon(filePath), filePath);
59 rowItem->setToolTip(filePath);
60 if (!cache.contains(filePath)) {
61 cache << filePath;
62 }
63 result << rowItem;
64 }
65 return result;
66}
67
68DisplayRecentView::DisplayRecentView(QWidget *parent)
69 : QListView (parent)
70 , cache({})
71 , model(new QStandardItemModel(this))
72{
73 QListView::viewport()->setAutoFillBackground(false);
74 QListView::setAttribute(Qt::WA_TranslucentBackground, true);
75 QListView::setBackgroundRole(QPalette::Base);
76 QListView::setModel(model);
77 QListView::setEditTriggers(QListView::NoEditTriggers);
78 QListView::setItemDelegate(new DisplayItemDelegate(this));
79 QListView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
80 QListView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
81 QListView::setSelectionMode(QAbstractItemView::NoSelection);
82}
83
84QString DisplayRecentView::cachePath()
85{
86 return CustomPaths::user(CustomPaths::Configures)
87 + QDir::separator() + "recent.support";
88}
89
90void DisplayRecentView::add(const QString &data)
91{
92 model->clear(); //删除数据
93 if (cache.contains(data)) {
94 cache.removeOne(data);
95 }
96 cache.insert(0, data); //置顶
97 saveToFile(cache); //保存序列
98 load(); //重新加载文件
99}
100
101QIcon DisplayRecentView::icon(const QString &data)
102{
103 QFileInfo info(data);
104 if (info.isFile()) {
105 return iconProvider.icon(QFileIconProvider::File);
106 }
107 if (info.isDir()) {
108 return iconProvider.icon(QFileIconProvider::Folder);
109 }
110 return QIcon();
111}
112
113void DisplayRecentView::load()
114{
115 model->appendColumn(itemsFromFile());
116}
117
118void DisplayRecentView::saveToFile(const QStringList &cache)
119{
120 QJsonDocument doc = readRecent();
121 QJsonObject docObj = doc.object();
122 QJsonArray array;
123 for (auto item : cache) {
124 array << item;
125 }
126 docObj[title()] = array;
127 doc.setObject(docObj);
128
129 QFile file(cachePath());
130 if (file.open(QFile::WriteOnly)) {
131 file.write(doc.toJson());
132 file.close();
133 }
134}
135