Debian Codegraph @

src/album/fileinotify.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "fileinotify.h"
22 #include "application.h"
23 #include "imageengineapi.h"
24 #include "signalmanager.h"
25 #include "utils/unionimage.h"
26
27 #include <sys/inotify.h>
28 #include <dirent.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <stdio.h>
33
34 #include <QDebug>
35 #include <QFile>
36 #include <QFileInfo>
37 #include <QDir>
38
39 enum {MASK = IN_MODIFY | IN_CREATE | IN_DELETE};
40
41 FileInotify::FileInotify(QObject *parent): QThread(parent)
42 {
43 m_allPic.clear();
44 m_handleId = inotify_init();
45 if (m_handleId == -1) {
46 qDebug() << "init inotify fialed ";
47 }
48 m_Supported = UnionImage_NameSpace::unionImageSupportFormat();
49
50 m_timer = new QTimer();
51 connect(m_timer, &QTimer::timeout, this, &FileInotify::onNeedSendPictures);
52 m_timer->start(1500);
53 }
54
55 FileInotify::~FileInotify()
56 {
57 clear();
58 }
59
60 bool FileInotify::isVaild()
61 {
62 return (m_handleId != -1);
63 }
64
65 void FileInotify::addWather(const QString &paths)
66 {
67 if (m_handleId < 0)
68 return;
69
70 QMutexLocker loker(&m_mutex);
71 if (!isVaild())
72 return;
73 QFileInfo info(paths);
74 if (!info.exists() || !info.isDir()) {
75 return;
76 }
77 m_currentDir = paths + "/";
78
79 std::string str = m_currentDir.toStdString();
80 const char *path = str.data();
81
82 m_wd = inotify_add_watch(m_handleId, path, IN_MODIFY);
83 if (m_wd == -1) {
84 qDebug() << " inotify_add_watch failed";
85 return;
86 }
87 watchedDirId.insert(paths, m_wd);
88
89 if (!m_running) {
90 m_running = true;
91 start();
92 }
93 pathLoadOnce();
94 }
95
96 //void FileInotify::removeWatcher(const QString &path) //暂未使用
97 //{
98 // QMutexLocker loker(&m_mutex);
99 // if (!isVaild())
100 // return;
101 // auto it = watchedDirId.find(path);
102 // if (it != watchedDirId.end()) {
103 // inotify_rm_watch(m_handleId, it.value());
104 // watchedDirId.erase(it);
105 // }
106 //}
107
108 void FileInotify::clear()
109 {
110 m_allPic.clear();
111 m_Supported.clear();
112 m_newFile.clear();
113 m_Supported.clear();
114 QMutexLocker loker(&m_mutex);
115 for (auto it : watchedDirId) {
116 inotify_rm_watch(m_handleId, it);
117 }
118 watchedDirId.clear();
119 if (m_timer->isActive()) {
120 m_timer->stop();
121 delete m_timer;
122 m_timer = nullptr;
123 }
124 }
125
126 void FileInotify::getAllPicture(bool isFirst)
127 {
128 QDir dir(m_currentDir);
129 if (!dir.exists()) {
130 return;
131 }
132 dir.setFilter(QDir::Files); //设置类型过滤器,只为文件格式
133 unsigned int dir_count = dir.count();
134 if (dir_count == 0) {
135 return;
136 }
137 QFileInfoList list = dir.entryInfoList();
138
139 //筛选出新增图片文件
140 for (auto fileinfo : list) {
141 if (m_Supported.contains(fileinfo.suffix().toUpper())) {
142 QString filename = fileinfo.fileName();
143 filename = m_currentDir + filename;
144 if (!m_allPic.contains(filename)) {
145 m_allPic << filename;
146 m_newFile << filename;
147 }
148 }
149 }
150 //初次导入,判重
151 if (isFirst) {
152 QStringList allpaths = DBManager::instance()->getAllPaths();
153 for (int i = 0; i < m_allPic.size(); i++) {
154 if (allpaths.contains(m_allPic.at(i))) {
155 m_newFile.removeOne(m_allPic.at(i));
156 }
157 }
158 }
159 }
160
161 //void FileInotify::fileNumChange()
162 //{
163 // QDir dir(m_currentDir);
164 // if (!dir.exists()) {
165 // return;
166 // }
167 // dir.setFilter(QDir::Files | QDir::NoSymLinks); //设置类型过滤器,只为文件格式
168 // dir.setNameFilters(m_Supported);
169 // unsigned int dir_count = dir.count();
170 // if (dir_count <= 0) {
171 // return;
172 // }
173 // QFileInfoList list = dir.entryInfoList();
174 // for (auto fileinfo : list) {
175 // if (m_Supported.contains(fileinfo.suffix().toUpper())) {
176 // QString filename = fileinfo.fileName();
177 // filename += m_currentDir;
178 // if (!m_allPic.contains(filename)) {
179 // qDebug() << "filename add: " << filename;
180 // m_allPic << filename;
181 // }
182 // }
183 // }
184 //}
185
186 void FileInotify::pathLoadOnce()
187 {
188 getAllPicture(true);
189 }
190
191 void FileInotify::onNeedSendPictures()
192 {
193 //发送导入
194 if (m_newFile.size() > 0) {
195 emit dApp->signalM->sigMonitorChanged(m_newFile);
196 m_newFile.clear();
197 }
198 }
199
200 void FileInotify::run()
201 {
202 int len;
203 char buf[1024];
204 inotify_event *event;
205 len = static_cast<int>(read(m_handleId, buf, sizeof(buf) - 1));
206 while (len > 0) {
207 int nread = 0;
208 while (len > 0) {
209 event = (inotify_event *)&buf[nread];
210 nread = nread + static_cast<int>(sizeof(inotify_event) + event->len);
211 len = len - static_cast<int>(sizeof(inotify_event) + event->len);
212 qDebug() << " need to get new file" << event->mask;
213 getAllPicture(false);
214 }
215 len = static_cast<int>(read(m_handleId, buf, sizeof(buf) - 1));
216 }
217 }