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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
* Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
*
* Author: zyc <zyc@uniontech.com>
*
* Maintainer: zyc <zyc@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logapplicationhelper.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QLocale>
std::atomic<LogApplicationHelper *> LogApplicationHelper::m_instance;
std::mutex LogApplicationHelper::m_mutex;
/**
* @brief LogApplicationHelper::LogApplicationHelper 构造函数,获取日志文件路径和应用名称
* @param parent 父对象
*/
LogApplicationHelper::LogApplicationHelper(QObject *parent)
: QObject(parent)
, m_DbusLauncher(new DBbusLauncher(DBbusLauncher::staticInterfaceName(), "/com/deepin/dde/daemon/Launcher", QDBusConnection::sessionBus(), this))
{
registerLauncherItemInfoMetaType();
registerLauncherItemInfoListMetaType();
init();
}
/**
* @brief LogApplicationHelper::init 初始化数据函数
*/
void LogApplicationHelper::init()
{
m_desktop_files.clear();
m_log_files.clear();
m_en_log_map.clear();
m_en_trans_map.clear();
m_trans_log_map.clear();
// get current system language shortname
m_current_system_language = QLocale::system().name();
// get desktop & log files
createDesktopFiles();
createLogFiles();
}
/**
* @brief LogApplicationHelper::createDesktopFiles 通过所有符合条件的destop文件获得包名和对应的应用文本
*/
void LogApplicationHelper::createDesktopFiles()
{
//在该目录下遍历所有desktop文件
QString path = "/usr/share/applications";
QDir dir(path);
if (!dir.exists())
return;
QStringList fileInfoList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
QStringList tempDesktopFiles;
QStringList regStr {"deepin-", "dde-", "org.deepin", "com.deepin", "uos-"};
for (QString desktop : fileInfoList) {
//需要符合以deepin或者dde开头的应用
for (auto &it : regStr) {
if (desktop.contains(it)) {
tempDesktopFiles.append(desktop);
break;
}
}
}
qDebug() << " tempDesktopFiles.count()" << tempDesktopFiles.count();
for (QString var : tempDesktopFiles) {
QString filePath = path + "/" + var;
QFile fi(filePath);
if (!fi.open(QIODevice::ReadOnly))
continue;
bool isDeepin = false;
bool isGeneric = false;
bool isName = false;
bool canDisplay = true;
while (!fi.atEnd()) {
QString lineStr = fi.readLine();
lineStr.replace("\n", "");
if (lineStr.startsWith("GenericName", Qt::CaseInsensitive) && !isGeneric) {
isGeneric = true;
}
if (lineStr.startsWith("Name", Qt::CaseInsensitive) && !isName) {
isName = true;
}
if (lineStr.startsWith("NoDisplay")) {
QStringList noDisplayList = lineStr.split("=", QString::SkipEmptyParts);
if (noDisplayList.value(1, "") == "true") {
canDisplay = false;
}
}
if (lineStr.startsWith("Hidden")) {
QStringList hiddenList = lineStr.split("=", QString::SkipEmptyParts);
if (hiddenList.value(1, "") == "true") {
canDisplay = false;
}
}
QString currentDesktop(qgetenv("XDG_CURRENT_DESKTOP"));
if (lineStr.startsWith("OnlyShowIn")) {
bool isHide = true;
QString onlyShowValue = lineStr.split("=", QString::SkipEmptyParts).value(1, "");
if (onlyShowValue.contains(currentDesktop)) {
isHide = false;
continue;
}
if (isHide) {
canDisplay = false;
}
}
if (lineStr.startsWith("NotShowIn")) {
bool isHide = false;
QStringList notShowInList = lineStr.split("=", QString::SkipEmptyParts).value(1, "").split(";", QString::SkipEmptyParts);
foreach (QString item, notShowInList) {
if (item == currentDesktop) {
isHide = true;
break;
}
}
if (isHide) {
canDisplay = false;
}
}
if (!lineStr.contains("X-Deepin-Vendor", Qt::CaseInsensitive)) {
continue;
}
QStringList _x_vendor_list = lineStr.split("=", QString::SkipEmptyParts);
if (_x_vendor_list.count() != 2) {
continue;
}
QString rval = _x_vendor_list[1];
if (0 == rval.compare("deepin", Qt::CaseInsensitive)) {
isDeepin = true;
break;
}
}
fi.close();
//转换插入应用包名和应用显示文本到数据结构
if (canDisplay) {
m_desktop_files.append(var);
parseField(filePath, var.split(QDir::separator()).last(), isDeepin, isGeneric, isName);
}
}
qDebug() << " m_desktop_files.count()" << m_desktop_files.count();
}
/**
* @brief LogApplicationHelper::createLogFiles 根据找到的符合要求的desktop文件的应用去初始化应用日志文件路径
*/
void LogApplicationHelper::createLogFiles()
{
QString homePath = QDir::homePath();
if (homePath.isEmpty()) {
return;
}
QString path = homePath + "/.cache/deepin/";
QDir appDir(path);
if (!appDir.exists()) {
return;
}
m_log_files = appDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
qDebug() << " m_log_files.size()" << appDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
for (auto i = 0; i < m_desktop_files.count(); ++i) {
QString desktopName = m_desktop_files[i].split(QDir::separator()).last();
QString _name = desktopName.mid(0, desktopName.lastIndexOf("."));
for (auto j = 0; j < m_log_files.count(); ++j) {
//desktop文件名和日志文件名比较,相同则符合条件
if (_name == m_log_files[j]) {
QString logPath = path + m_log_files[j];
m_en_log_map.insert(_name, logPath);
break;
}
}
}
}
/**
* @brief LogApplicationHelper::parseField 从desktop文件中提取正确的显示文本
* @param path desktop文件路径
* @param name 包名
* @param isDeepin 是否是deepin应用
* @param isGeneric 是否有GenericName字段
* @param isName 是否有Name字段
*/
void LogApplicationHelper::parseField(QString path, QString name, bool isDeepin, bool isGeneric,
bool isName)
{
Q_UNUSED(isName)
QFile fi(path);
if (!fi.open(QIODevice::ReadOnly)) {
return;
}
m_en_trans_map.insert(name.mid(0, name.lastIndexOf(".")), name.mid(0, name.lastIndexOf("."))); // desktop name
if (name.contains("shutdown")) {
}
while (!fi.atEnd()) {
QString lineStr = fi.readLine();
lineStr.replace("\n", "");
if (isDeepin) {
// GenericName所对应的当前语言的值=》GenericName无任何语言标记的值=》Name对应语言的值
// =》Name无任何语言标记的值 =》desktop的名称
if (isGeneric) {
if (!lineStr.startsWith("GenericName"))
continue;
} else {
if (!lineStr.startsWith("Name"))
continue;
}
} else {
// Name对应语言的值 =》Name无任何语言标记的值 =》desktop的名称
if (!lineStr.startsWith("Name"))
continue;
}
QStringList gNameList = lineStr.split("=", QString::SkipEmptyParts);
if (gNameList.count() != 2)
continue;
QString leftStr = gNameList[0];
QString genericName = gNameList[1];
if (leftStr.split("_").count() == 2) {
if (leftStr.contains(m_current_system_language)) {
m_en_trans_map.insert(name.mid(0, name.lastIndexOf(".")), genericName);
break;
}
} else if (leftStr.contains(m_current_system_language.split("_")[0])) {
m_en_trans_map.insert(name.mid(0, name.lastIndexOf(".")), genericName);
break;
} else if (0 == leftStr.compare("GenericName", Qt::CaseInsensitive) || 0 == leftStr.compare("Name", Qt::CaseInsensitive)) {
m_en_trans_map.insert(name.mid(0, name.lastIndexOf(".")), genericName); // GenericName=xxxx
}
}
}
/**
* @brief LogApplicationHelper::getLogFile 通过日志目录获取日志文件路经
* @param path 通过日志文件目录
* @return 日志文件路经
*/
QString LogApplicationHelper::getLogFile(QString path)
{
QString ret;
QDir subdir(path);
if (!subdir.exists())
return ret;
QStringList logFiles = subdir.entryList(QDir::NoDotAndDotDot | QDir::Files);
for (int j = 0; j < logFiles.count(); j++) {
QString fileName = logFiles.at(j);
if (!fileName.endsWith(".log"))
continue;
ret = QString("%1/%2").arg(path).arg(fileName);
break;
}
return ret;
}
/**
* @brief LogApplicationHelper::getMap 刷新并返回所有显示文本对应的应用日志路径
* @return
*/
QMap<QString, QString> LogApplicationHelper::getMap()
{
init();
QMap<QString, QString>::const_iterator iter = m_en_log_map.constBegin();
while (iter != m_en_log_map.constEnd()) {
QString displayName = m_en_trans_map.value(iter.key());
QString logPath = getLogFile(iter.value());
m_trans_log_map.insert(displayName, logPath);
++iter;
}
return m_trans_log_map;
}
/**
* @brief LogApplicationHelper::transName 从应用包名转换为应用显示文本
* @param str 应用包名
* @return 显示文本
*/
QString LogApplicationHelper::transName(QString str)
{
return m_en_trans_map.value(str);
}
|