Debian Codegraph @

src/libdbm/util/devicemonitor.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: GPL-3.0-only
4
5 #include "devicemonitor.h"
6
7 #include "utils.h"
8
9 #include <QDebug>
10 #include <QThread>
11 #include <QDataStream>
12 #include <QJsonDocument>
13 #include <QJsonObject>
14 #include <QJsonArray>
15 #include <QString>
16
17 //#pragma execution_character_set("utf-8")
18
19 DeviceMonitor::DeviceMonitor(QObject *parent) : QObject(parent)
20 {
21 qRegisterMetaType<QList<DeviceInfo>>();
22
23 m_timer = new QTimer(this);
24 m_timer->setInterval(2000);
25 connect(m_timer, &QTimer::timeout, this, [ = ] {
26 QList<DeviceInfo> list = Utils::ListUsbDrives();
27 qDebug() << "list length:" << list.length();
28 for (int i = 0; i < list.size(); i++)
29 {
30 qDebug() << "list i:" << i << " path:" << list.at(i).path << " label:" << list.at(i).label;
31 }
32
33 QList<DeviceInfo> intersectList = this->getIntersectDevice(list);
34 qDebug() << "intersectlist count:" << intersectList.size();
35 QList<DeviceInfo> addList = this->getNorDevice(list, intersectList);
36 QList<DeviceInfo> delList = this->getNorDevice(m_deviceList, intersectList);
37
38 if ((!addList.isEmpty()) || !delList.isEmpty()) {
39 emit this->removablePartitionsChanged(addList, delList);
40 qDebug() << "addlist count = " << addList.count();
41 qDebug() << "reducelist count =" << delList.count();
42 }
43
44 this->m_deviceList = list;
45 });
46
47 connect(this, &DeviceMonitor::pauseMonitor, m_timer, &QTimer::stop);
48 connect(this, &DeviceMonitor::startMonitor, this, [ = ]() {
49 this->m_deviceList.clear();
50 m_timer->start();
51 });
52 }
53
54 QList<DeviceInfo> DeviceMonitor::getIntersectDevice(const QList<DeviceInfo>& list)
55 {
56 QList<DeviceInfo> intersectList;
57
58 foreach (DeviceInfo info, list) {
59 foreach (DeviceInfo tempInfo, m_deviceList) {
60 if ((tempInfo == info)&&(tempInfo.total == info.total)&&(tempInfo.used == info.used)) {
61 intersectList.push_back(info);
62 }
63 }
64 }
65
66 return intersectList;
67 }
68
69 QList<DeviceInfo> DeviceMonitor::getNorDevice(const QList<DeviceInfo>& calcuList, const QList<DeviceInfo>& refList)
70 {
71 QList<DeviceInfo> XorList;
72
73 foreach (DeviceInfo info, calcuList) {
74 bool bInsert = true;
75
76 foreach (DeviceInfo tempInfo, refList) {
77 if (info.path == tempInfo.path) {
78 bInsert = false;
79 break;
80 }
81 }
82
83 if (bInsert) {
84 XorList.push_back(info);
85 }
86 }
87
88 return XorList;
89 }
90
91 const QList<DeviceInfo> DeviceMonitor::deviceList() const
92 {
93 return Utils::ListUsbDrives();
94 }
95
96 QString deviceListToJson(QList<DeviceInfo> deviceList)
97 {
98 QJsonArray array;
99 for (auto device : deviceList) {
100 QJsonObject obj;
101 obj.insert("path", device.path);
102 obj.insert("label", device.label);
103 obj.insert("used", static_cast<int>(device.used));
104 obj.insert("total", static_cast<int>(device.total));
105 obj.insert("fstype", device.fstype);
106 obj.insert("needformat", device.needFormat);
107 obj.insert("strDev", device.strDev);
108 obj.insert("isDisk", device.isDisk);
109 qDebug() << device.needFormat;
110 array.push_back(obj);
111 }
112
113 // QJsonObject obj;
114 // obj.insert("path", "Utest");
115 // obj.insert("label", "U盘test");
116 // obj.insert("used", 10000);
117 // obj.insert("total", 100000);
118 // obj.insert("needformat", false);
119 // array.push_back(obj);
120 QJsonDocument doc;
121 doc.setArray(array);
122 return QString::fromUtf8(doc.toJson());
123 }
124
125 QList<DeviceInfo> deviceListFromJson(QString json)
126 {
127 QList<DeviceInfo> list;
128 QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
129 for (auto jsonObj : doc.array()) {
130 DeviceInfo device;
131 auto obj = jsonObj.toObject();
132 device.path = obj.value("path").toString();
133 device.label = obj.value("label").toString();
134 device.used = static_cast<quint32>(obj.value("used").toInt());
135 device.total = static_cast<quint32>(obj.value("total").toInt());
136 device.fstype = obj.value("fstype").toString();
137 device.needFormat = obj.value("needformat").toBool();
138 device.isDisk = obj.value("isDisk").toBool();
139 device.strDev = obj.value("strDev").toString();
140 list.push_back(device);
141 }
142 return list;
143 }
144
145 QDataStream &operator<<(QDataStream &out, const DeviceInfo &msg)
146 {
147 out << msg.path << msg.used << msg.total << msg.label;
148 return out;
149 }
150
151 QDataStream &operator>>(QDataStream &in, DeviceInfo &msg)
152 {
153 QString path;
154 quint32 used;
155 quint32 total;
156 QString label;
157 in >> path >> used >> total >> label;
158 msg = DeviceInfo(path, used, total, label);
159 return in;
160 }