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