| 1 |
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "drecentmanager.h" |
| 6 |
#include <QMimeDatabase> |
| 7 |
#include <QDomDocument> |
| 8 |
#include <QTextStream> |
| 9 |
#include <QDateTime> |
| 10 |
#include <QFileInfo> |
| 11 |
#include <QFile> |
| 12 |
#include <QDir> |
| 13 |
#include <QUrl> |
| 14 |
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
| 15 |
#include <QStringConverter> |
| 16 |
#endif |
| 17 |
|
| 18 |
DCORE_BEGIN_NAMESPACE |
| 19 |
|
| 20 |
#define RECENT_PATH QDir::homePath() + "/.local/share/recently-used.xbel" |
| 21 |
|
| 22 |
/*! |
| 23 |
\class Dtk::Core::DRecentManager |
| 24 |
\inmodule dtkcore |
| 25 |
|
| 26 |
\brief DRecentManager 是用来管理最近文件列表的类,提供了添加与删除文件项. |
| 27 |
|
| 28 |
遵循 freedesktop 标准,在本地 share 目录存放,文件名为: recently-used.xbel,所以每个用户都有不同的列表。 |
| 29 |
该类的存在就是为 deepin 应用提供一个工具类,方便让打开的文件添加到最近文件列表中。 |
| 30 |
|
| 31 |
\sa Dtk::Core::DRecentData |
| 32 |
*/ |
| 33 |
|
| 34 |
/*! |
| 35 |
\class Dtk::Core::DRecentData |
| 36 |
\inmodule dtkcore |
| 37 |
|
| 38 |
\brief 文件信息结构体. |
| 39 |
|
| 40 |
\table |
| 41 |
\row |
| 42 |
\li appName |
| 43 |
\li 应用名称 |
| 44 |
\row |
| 45 |
\li appExec |
| 46 |
\li 应用命令行名称 |
| 47 |
\row |
| 48 |
\li mimeType |
| 49 |
\li 文件 mimetype 名称,一般不需要填写,DRecentManager 内部自动获取 |
| 50 |
\endtable |
| 51 |
\sa Dtk::Core::DRecentManager |
| 52 |
*/ |
| 53 |
|
| 54 |
/*! |
| 55 |
\brief DRecentManager::addItem 在最近列表中添加一个项. |
| 56 |
\a uri 文件路径 |
| 57 |
\a data 数据信息 |
| 58 |
\return 如果返回 true 则成功添加,false 为添加失败 |
| 59 |
*/ |
| 60 |
|
| 61 |
bool DRecentManager::addItem(const QString &uri, DRecentData &data) |
| 62 |
{ |
| 63 |
if (!QFileInfo(uri).exists() || uri.isEmpty()) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
QFile file(RECENT_PATH); |
| 68 |
file.open(QIODevice::ReadWrite | QIODevice::Text); |
| 69 |
|
| 70 |
QString dateTime = QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate); |
| 71 |
QDomDocument doc; |
| 72 |
|
| 73 |
if (!doc.setContent(&file)) { |
| 74 |
doc.clear(); |
| 75 |
doc.appendChild(doc.createProcessingInstruction("xml","version=\'1.0\' encoding=\'utf-8\'")); |
| 76 |
QDomElement xbelEle = doc.createElement("xbel"); |
| 77 |
xbelEle.setAttribute("xmlns:mime", "http://www.freedesktop.org/standards/shared-mime-info"); |
| 78 |
xbelEle.setAttribute("version", "1.0"); |
| 79 |
xbelEle.setAttribute("xmlns:bookmark", "http://www.freedesktop.org/standards/desktop-bookmarks"); |
| 80 |
doc.appendChild(xbelEle); |
| 81 |
} |
| 82 |
file.close(); |
| 83 |
|
| 84 |
// need to add file:// protocol. |
| 85 |
QUrl url = QUrl::fromLocalFile(uri); |
| 86 |
|
| 87 |
// get the MimeType name of the file. |
| 88 |
if (data.mimeType.isEmpty()) { |
| 89 |
data.mimeType = QMimeDatabase().mimeTypeForFile(uri).name(); |
| 90 |
} |
| 91 |
|
| 92 |
QDomElement rootEle = doc.documentElement(); |
| 93 |
QDomNodeList nodeList = rootEle.elementsByTagName("bookmark"); |
| 94 |
QDomElement bookmarkEle; |
| 95 |
bool isFound = false; |
| 96 |
|
| 97 |
// find bookmark element exists. |
| 98 |
for (int i = 0; i < nodeList.size(); ++i) { |
| 99 |
const QString fileUrl = nodeList.at(i).toElement().attribute("href"); |
| 100 |
|
| 101 |
if (fileUrl == url.toEncoded(QUrl::FullyDecoded)) { |
| 102 |
bookmarkEle = nodeList.at(i).toElement(); |
| 103 |
isFound = true; |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// update element content. |
| 109 |
if (isFound) { |
| 110 |
QDomNodeList appList = bookmarkEle.elementsByTagName("bookmark:application"); |
| 111 |
QDomElement appEle; |
| 112 |
bool appExists = false; |
| 113 |
|
| 114 |
for (int i = 0; i < appList.size(); ++i) { |
| 115 |
appEle = appList.at(i).toElement(); |
| 116 |
|
| 117 |
if (appEle.attribute("name") == data.appName && |
| 118 |
appEle.attribute("exec") == data.appExec) { |
| 119 |
appExists = true; |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (appExists) { |
| 125 |
int count = appEle.attribute("count").toInt() + 1; |
| 126 |
bookmarkEle.setAttribute("modified", dateTime); |
| 127 |
bookmarkEle.setAttribute("visited", dateTime); |
| 128 |
appEle.setAttribute("modified", dateTime); |
| 129 |
appEle.setAttribute("count", QString::number(count)); |
| 130 |
} else { |
| 131 |
QDomNode appsNode = bookmarkEle.elementsByTagName("bookmark:applications").at(0); |
| 132 |
|
| 133 |
appEle = doc.createElement("bookmark:application"); |
| 134 |
appEle.setAttribute("name", data.appName); |
| 135 |
appEle.setAttribute("exec", data.appExec); |
| 136 |
appEle.setAttribute("modified", dateTime); |
| 137 |
appEle.setAttribute("count", "1"); |
| 138 |
appsNode.toElement().appendChild(appEle); |
| 139 |
} |
| 140 |
} |
| 141 |
// add new elements if they don't exist. |
| 142 |
else { |
| 143 |
QDomElement bookmarkEle, infoEle, metadataEle, mimeEle, appsEle, appChildEle; |
| 144 |
QString hrefStr = url.toEncoded(QUrl::FullyEncoded); |
| 145 |
|
| 146 |
bookmarkEle = doc.createElement("bookmark"); |
| 147 |
bookmarkEle.setAttribute("href", hrefStr); |
| 148 |
bookmarkEle.setAttribute("added", dateTime); |
| 149 |
bookmarkEle.setAttribute("modified", dateTime); |
| 150 |
bookmarkEle.setAttribute("visited", dateTime); |
| 151 |
|
| 152 |
infoEle = doc.createElement("info"); |
| 153 |
bookmarkEle.appendChild(infoEle); |
| 154 |
|
| 155 |
metadataEle = doc.createElement("metadata"); |
| 156 |
metadataEle.setAttribute("owner", "http://freedesktop.org"); |
| 157 |
infoEle.appendChild(metadataEle); |
| 158 |
|
| 159 |
mimeEle = doc.createElement("mime:mime-type"); |
| 160 |
mimeEle.setAttribute("type", data.mimeType); |
| 161 |
metadataEle.appendChild(mimeEle); |
| 162 |
|
| 163 |
appsEle = doc.createElement("bookmark:applications"); |
| 164 |
appChildEle = doc.createElement("bookmark:application"); |
| 165 |
appChildEle.setAttribute("name", data.appName); |
| 166 |
appChildEle.setAttribute("exec", data.appExec); |
| 167 |
appChildEle.setAttribute("modified", dateTime); |
| 168 |
appChildEle.setAttribute("count", "1"); |
| 169 |
|
| 170 |
appsEle.appendChild(appChildEle); |
| 171 |
metadataEle.appendChild(appsEle); |
| 172 |
|
| 173 |
QDomNode result = rootEle.appendChild(bookmarkEle); |
| 174 |
if (result.isNull()) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// write to file. |
| 180 |
if (!file.open(QIODevice::WriteOnly)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
QTextStream out(&file); |
| 185 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 186 |
out.setEncoding(QStringConverter::Utf8); |
| 187 |
#else |
| 188 |
out.setCodec("UTF-8"); |
| 189 |
#endif |
| 190 |
out << doc.toString(); |
| 191 |
out.flush(); |
| 192 |
file.close(); |
| 193 |
|
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
/*! |
| 198 |
\brief DRecentManager::removeItem 在最近列表中移除单个文件路径 |
| 199 |
\a target 需要移除的文件路径 |
| 200 |
*/ |
| 201 |
|
| 202 |
void DRecentManager::removeItem(const QString &target) |
| 203 |
{ |
| 204 |
removeItems(QStringList() << target); |
| 205 |
} |
| 206 |
|
| 207 |
/*! |
| 208 |
\brief DRecentManager::removeItem 在最近列表中移除多个文件路径 |
| 209 |
\a list 需要移除的文件路径列表 |
| 210 |
*/ |
| 211 |
|
| 212 |
void DRecentManager::removeItems(const QStringList &list) |
| 213 |
{ |
| 214 |
QFile file(RECENT_PATH); |
| 215 |
|
| 216 |
if (!file.open(QIODevice::ReadOnly)) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
QDomDocument doc; |
| 221 |
if (!doc.setContent(&file)) { |
| 222 |
file.close(); |
| 223 |
return; |
| 224 |
} |
| 225 |
file.close(); |
| 226 |
|
| 227 |
QDomElement rootEle = doc.documentElement(); |
| 228 |
QDomNodeList nodeList = rootEle.elementsByTagName("bookmark"); |
| 229 |
|
| 230 |
for (int i = 0; i < nodeList.count(); ) { |
| 231 |
const QString fileUrl = nodeList.at(i).toElement().attribute("href"); |
| 232 |
|
| 233 |
if (list.contains(QUrl::fromPercentEncoding(fileUrl.toLatin1())) || |
| 234 |
list.contains(QUrl(fileUrl).toEncoded(QUrl::FullyDecoded))) { |
| 235 |
rootEle.removeChild(nodeList.at(i)); |
| 236 |
} else { |
| 237 |
++i; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (!file.open(QIODevice::WriteOnly)) { |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
QTextStream out(&file); |
| 246 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 247 |
out.setEncoding(QStringConverter::Utf8); |
| 248 |
#else |
| 249 |
out.setCodec("UTF-8"); |
| 250 |
#endif |
| 251 |
out << doc.toString(); |
| 252 |
out.flush(); |
| 253 |
file.close(); |
| 254 |
|
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
DCORE_END_NAMESPACE |