Debian Codegraph @

src/album/application.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 "application.h"
22 #include "config.h"
23
24 #include "controller/configsetter.h"
25 #include "controller/globaleventfilter.h"
26 #include "controller/signalmanager.h"
27 #include "controller/viewerthememanager.h"
28 #include "controller/wallpapersetter.h"
29 #include "utils/unionimage.h"
30 #include "utils/baseutils.h"
31 #include "utils/imageutils.h"
32 #include "thumbnail/thumbnaillistview.h"
33 #include "imageengine/imageengineapi.h"
34 #include "mainwindow.h"
35
36 #include <QDebug>
37 #include <QTranslator>
38 #include <QIcon>
39 #include <QImageReader>
40 #include <sys/time.h>
41 #include <DApplicationSettings>
42 #include <QFile>
43 #include <DLog>
44 #include <dgiofile.h>
45 #include <dgiofileinfo.h>
46 #include <DAboutDialog>
47
48 // namespace
49
50 #define IMAGE_HEIGHT_DEFAULT 100
51 #define IMAGE_LOAD_DEFAULT 100
52
53 ImageLoader::ImageLoader(/*Application *parent, QStringList pathlist, QStringList pathlisttrash)
54 : m_parent(parent), m_pathlist(pathlist), m_pathlisttrash(pathlisttrash*/)
55 {
56
57 }
58
59
60 void ImageLoader::ImportImageLoader(const DBImgInfoList &dbInfos, QString albumname)
61 {
62 QStringList pathlist;
63
64 for (auto info : dbInfos) {
65 pathlist << info.filePath;
66 }
67 //导入相册数据库AlbumTable3
68 DBManager::instance()->insertIntoAlbumNoSignal(albumname, pathlist);
69 //导入图片数据库ImageTable3
70 DBManager::instance()->insertImgInfos(dbInfos);
71 if (pathlist.size() > 0) {
72 emit dApp->signalM->updateStatusBarImportLabel(pathlist, 1, albumname);
73 } else {
74 emit dApp->signalM->ImportFailed();
75 }
76 }
77
78
79 void ImageLoader::updateImageLoader(const QStringList &pathlist, const QList<QImage> &images)
80 {
81 for (int i = 0; i != pathlist.size(); ++i) {
82 QImage tImg;
83 if (images.isEmpty() || images.size() < i || images.at(i).isNull()) {
84 QString errMsg;
85 if (!UnionImage_NameSpace::loadStaticImageFromFile(pathlist.at(i), tImg, errMsg)) {
86 qDebug() << errMsg;
87 continue;
88 }
89 } else {
90 tImg = images.at(i);
91 }
92
93 //修改:将QPixmap的变形操作放在QImage里完成
94 if (0 != tImg.height() && 0 != tImg.width() && (tImg.height() / tImg.width()) < 10 && (tImg.width() / tImg.height()) < 10) {
95 if (tImg.height() != 140 && tImg.width() != 140) {
96 if (tImg.height() >= tImg.width()) {
97 tImg = tImg.scaledToWidth(140, Qt::FastTransformation);
98 } else if (tImg.height() <= tImg.width()) {
99 tImg = tImg.scaledToHeight(140, Qt::FastTransformation);
100 }
101 }
102 }
103 QString spath = albumGlobal::CACHE_PATH + pathlist.at(i);
104 utils::base::mkMutiDir(spath.mid(0, spath.lastIndexOf('/')));
105 tImg.save(spath, "PNG");
106
107 QPixmap pixmap = QPixmap::fromImage(tImg);
108 //不需要在此处更新图片
109 //ImageEngineApi::instance()->updateImageDataPixmap(pathlist.at(i), pixmap);
110 emit ImageEngineApi::instance()->sigOneImgReady(pathlist.at(i), pixmap);
111 }
112
113 emit dApp->signalM->sigUpdateImageLoader(pathlist);
114 }
115
116 DApplication *Application::dAppNew = nullptr;
117 Application *Application::dApp1 = nullptr;
118 Application::Application()
119 {
120 //设置单例
121 setupsinglecase();
122
123 installEventFilter(new GlobalEventFilter(this));
124 initChildren();
125 m_imageloader = new ImageLoader();
126 m_mainwindow = nullptr;
127 }
128
129 Application::~Application()
130 {
131 // if (m_imageloader) {
132 // delete m_imageloader;
133 // m_imageloader = nullptr;
134 // }
135 }
136
137 DApplication *Application::getDAppNew()
138 {
139 return dAppNew;
140 }
141
142 Application *Application::getApp()
143 {
144 if (dApp1 == nullptr)
145 dApp1 = new Application;
146 return dApp1;
147 }
148
149 void Application::setApp(DApplication *app)
150 {
151 dAppNew = app;
152 dAppNew->setApplicationDisplayName(tr("Album"));
153 dAppNew->setProductIcon(QIcon::fromTheme("deepin-album"));
154 dAppNew->setApplicationVersion(DApplication::buildVersion(VERSION));
155 dAppNew->setApplicationDescription(DApplication::translate("Main", "Album is a fashion manager for viewing and organizing photos and videos."));
156 }
157
158 bool Application::isWaylandPlatform()
159 {
160 auto e = QProcessEnvironment::systemEnvironment();
161 QString XDG_SESSION_TYPE = e.value(QStringLiteral("XDG_SESSION_TYPE"));
162 QString WAYLAND_DISPLAY = e.value(QStringLiteral("WAYLAND_DISPLAY"));
163
164 //判断wayland
165 if (XDG_SESSION_TYPE != QLatin1String("wayland") && !WAYLAND_DISPLAY.contains(QLatin1String("wayland"), Qt::CaseInsensitive)) {
166 return false;
167 }
168 return true;
169 }
170
171 void Application::initChildren()
172 {
173 viewerTheme = ViewerThemeManager::instance();
174 setter = ConfigSetter::instance();
175 signalM = SignalManager::instance();
176 wpSetter = WallpaperSetter::instance();
177 }
178
179 void Application::setupsinglecase()
180 {
181 QSharedMemory mem("deepin-album");
182 // 尝试将进程附加到共享内存段
183 if (mem.attach()) {
184 // 将共享内存与主进程分离, 如果此进程是附加到共享存储器段的最后一个进程,则系统释放共享存储器段,即销毁内容
185 mem.detach();
186 }
187 sharedMemory.setKey("deepin-album");
188
189
190 if (sharedMemory.attach())
191 _isRunning = true;
192 else {
193 _isRunning = false;
194 // attach data to shared memory.
195 QByteArray byteArray("0"); // default value to note that no message is available.
196 if (!sharedMemory.create(byteArray.size())) {
197 qDebug("无法创建实例化");
198 return;
199 }
200 sharedMemory.lock();
201 char *to = static_cast<char *>(sharedMemory.data());
202 const char *from = byteArray.data();
203 memcpy(to, from, static_cast<size_t>(qMin(sharedMemory.size(), byteArray.size())));
204 sharedMemory.unlock();
205
206 // start checking for messages of other instances.
207 QTimer *timer = new QTimer(this);
208 connect(timer, SIGNAL(timeout()), this, SLOT(checkForMessage()));
209 timer->start(1000);
210 }
211 }
212
213 bool Application::isRunning()
214 {
215 return _isRunning;
216 }
217
218 bool Application::sendMessage(const QString &message)
219 {
220 //既然封了函数,就把变量改成函数
221 if (!isRunning())
222 return false;
223
224 QByteArray byteArray("1");
225 byteArray.append(message.toUtf8());
226 byteArray.append("0"); // < should be as char here, not a string!
227 sharedMemory.lock();
228 char *to = static_cast<char *>(sharedMemory.data());
229 const char *from = byteArray.data();
230 memcpy(to, from, static_cast<size_t>(qMin(sharedMemory.size(), byteArray.size())));
231 sharedMemory.unlock();
232 return true;
233 }
234
235 void Application::setMainWindow(MainWindow *window)
236 {
237 if (nullptr != window) {
238 m_mainwindow = window;
239 }
240 }
241
242 MainWindow *Application::getMainWindow()
243 {
244 return m_mainwindow;
245 }
246
247 void Application::checkForMessage()
248 {
249 sharedMemory.lock();
250 QByteArray byteArray = QByteArray(static_cast<const char *>(sharedMemory.constData()), sharedMemory.size());
251 sharedMemory.unlock();
252 if (byteArray.left(1) == "0")
253 return;
254 byteArray.remove(0, 1);
255 QString message = QString::fromUtf8(byteArray.constData());
256 emit messageAvailable(message);
257
258 // remove message from shared memory.
259 byteArray = "0";
260 sharedMemory.lock();
261 char *to = static_cast<char *>(sharedMemory.data());
262 const char *from = byteArray.data();
263 memcpy(to, from, static_cast<size_t>(qMin(sharedMemory.size(), byteArray.size())));
264 sharedMemory.unlock();
265 }