| 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 "commandline.h" |
| 22 |
#include "application.h" |
| 23 |
#include "controller/signalmanager.h" |
| 24 |
#include "controller/wallpapersetter.h" |
| 25 |
//#include "controller/divdbuscontroller.h" |
| 26 |
#include "controller/configsetter.h" |
| 27 |
#include "frame/mainwidget.h" |
| 28 |
#include "utils/imageutils.h" |
| 29 |
#include "utils/baseutils.h" |
| 30 |
#include "utils/unionimage.h" |
| 31 |
|
| 32 |
#include "dthememanager.h" |
| 33 |
#include "ac-desktop-define.h" |
| 34 |
|
| 35 |
#include <QHBoxLayout> |
| 36 |
#include <QCommandLineOption> |
| 37 |
#include <QDBusConnection> |
| 38 |
#include <QDesktopWidget> |
| 39 |
#include <QDebug> |
| 40 |
#include <QFileInfo> |
| 41 |
#include <QStandardPaths> |
| 42 |
#include <QMimeDatabase> |
| 43 |
|
| 44 |
using namespace Dtk::Widget; |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
const QString DBUS_PATH = "/com/deepin/ImageViewer"; |
| 49 |
const QString DBUS_NAME = "com.deepin.ImageViewer"; |
| 50 |
const QString THEME_GROUP = "APP"; |
| 51 |
const QString THEME_TEXT = "AppTheme"; |
| 52 |
} |
| 53 |
|
| 54 |
struct CMOption { |
| 55 |
QString shortOption; |
| 56 |
QString longOption; |
| 57 |
QString description; |
| 58 |
QString valueName; |
| 59 |
}; |
| 60 |
|
| 61 |
static CMOption options[] = { |
| 62 |
{"o", "open", "Open the specified <image-file>.", "image-file"}, |
| 63 |
{"a", "album", "Enter the album <album-name>.", "album-name"}, |
| 64 |
// {"s", "search", "Go to search view and search image by <word>.", "word"}, |
| 65 |
// {"e", "edit", "Go to edit view and begin editing <image-file>.", "image-file"}, |
| 66 |
{"w", "wallpaper", "Set <image-file> as wallpaper.", "image-file"}, |
| 67 |
{"new-window", "new-window", "Display a window.", ""}, |
| 68 |
{"", "", "", ""} |
| 69 |
}; |
| 70 |
|
| 71 |
CommandLine *CommandLine::m_commandLine = nullptr; |
| 72 |
CommandLine *CommandLine::instance() |
| 73 |
{ |
| 74 |
if (! m_commandLine) { |
| 75 |
m_commandLine = new CommandLine(); |
| 76 |
} |
| 77 |
|
| 78 |
return m_commandLine; |
| 79 |
} |
| 80 |
|
| 81 |
CommandLine::CommandLine() |
| 82 |
: m_pwidget(nullptr) |
| 83 |
{ |
| 84 |
m_cmdParser.addHelpOption(); |
| 85 |
// m_cmdParser.addVersionOption(); |
| 86 |
// m_cmdParser.addPositionalArgument("value", QCoreApplication::translate( |
| 87 |
// "main", "Value that use for options."), "[value]"); |
| 88 |
for (const CMOption *i = options; ! i->shortOption.isEmpty(); ++i) { |
| 89 |
addOption(i); |
| 90 |
} |
| 91 |
|
| 92 |
m_pwidget = new QWidget(this); |
| 93 |
m_pwidget->setAttribute(Qt::WA_TransparentForMouseEvents); |
| 94 |
|
| 95 |
m_layout = new QHBoxLayout(this); |
| 96 |
m_layout->setContentsMargins(0, 0, 0, 0); |
| 97 |
m_mainWidget = new MainWidget(false, this); |
| 98 |
m_mainWidget->setObjectName("MainWidget"); |
| 99 |
m_layout->addWidget(m_mainWidget); |
| 100 |
setLayout(m_layout); |
| 101 |
} |
| 102 |
|
| 103 |
CommandLine::~CommandLine() |
| 104 |
{ |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
void CommandLine::addOption(const CMOption *option) |
| 109 |
{ |
| 110 |
QStringList ol; |
| 111 |
ol << option->shortOption; |
| 112 |
ol << option->longOption; |
| 113 |
QCommandLineOption cm(ol, option->description, option->valueName); |
| 114 |
|
| 115 |
m_cmdParser.addOption(cm); |
| 116 |
} |
| 117 |
|
| 118 |
/*! |
| 119 |
* \brief CommandLine::showHelp |
| 120 |
* QCommandLineParser::showHelp(int exitCode = 0) Will displays the help |
| 121 |
* information, and exits application automatically. However, |
| 122 |
* DApplication::loadDXcbPlugin() need exit by calling quick_exit(a.exec()). |
| 123 |
* So we should show the help message only by calling this function. |
| 124 |
*/ |
| 125 |
void CommandLine::showHelp() |
| 126 |
{ |
| 127 |
fputs(qPrintable(m_cmdParser.helpText()), stdout); |
| 128 |
} |
| 129 |
|
| 130 |
//设置管理线程的对象 |
| 131 |
void CommandLine::setThreads(ImageEngineImportObject *obj) |
| 132 |
{ |
| 133 |
m_obj = obj; |
| 134 |
} |
| 135 |
|
| 136 |
void CommandLine::viewImage(const QString &path, const QStringList &paths) |
| 137 |
{ |
| 138 |
emit dApp->signalM->enableMainMenu(false); |
| 139 |
if (paths.count() == 1) { |
| 140 |
using namespace UnionImage_NameSpace; |
| 141 |
QImage tImg; |
| 142 |
bool cache_exist = false; |
| 143 |
QFileInfo file(albumGlobal::CACHE_PATH + path); |
| 144 |
QString errMsg; |
| 145 |
QString dimension; |
| 146 |
QString tpath; |
| 147 |
bool breloadCache = false; |
| 148 |
QFileInfo srcfi(path); |
| 149 |
if (file.exists()) { |
| 150 |
QDateTime cachetime = file.metadataChangeTime(); //缓存修改时间 |
| 151 |
QDateTime srctime = srcfi.metadataChangeTime(); //源数据修改时间 |
| 152 |
if (srctime.toTime_t() > cachetime.toTime_t()) { //源文件近期修改过,重新生成缓存文件 |
| 153 |
cache_exist = false; |
| 154 |
breloadCache = true; |
| 155 |
tpath = path; |
| 156 |
if (!loadStaticImageFromFile(tpath, tImg, errMsg)) { |
| 157 |
qDebug() << errMsg; |
| 158 |
} |
| 159 |
dimension = QString::number(tImg.width()) + "x" + QString::number(tImg.height()); |
| 160 |
} else { |
| 161 |
cache_exist = true; |
| 162 |
tpath = albumGlobal::CACHE_PATH + path; |
| 163 |
if (!loadStaticImageFromFile(tpath, tImg, errMsg, "PNG")) { |
| 164 |
qDebug() << errMsg; |
| 165 |
} |
| 166 |
} |
| 167 |
} else { |
| 168 |
if (!loadStaticImageFromFile(path, tImg, errMsg)) { |
| 169 |
qDebug() << errMsg; |
| 170 |
} |
| 171 |
dimension = QString::number(tImg.width()) + "x" + QString::number(tImg.height()); |
| 172 |
} |
| 173 |
if (0 != tImg.height() && 0 != tImg.width() && (tImg.height() / tImg.width()) < 10 && (tImg.width() / tImg.height()) < 10) { |
| 174 |
if (tImg.height() != 200 && tImg.width() != 200) { |
| 175 |
if (tImg.height() >= tImg.width()) { |
| 176 |
cache_exist = true; |
| 177 |
tImg = tImg.scaledToWidth(200, Qt::FastTransformation); |
| 178 |
} else if (tImg.height() <= tImg.width()) { |
| 179 |
cache_exist = true; |
| 180 |
tImg = tImg.scaledToHeight(200, Qt::FastTransformation); |
| 181 |
} |
| 182 |
} |
| 183 |
if (!cache_exist) { |
| 184 |
if ((static_cast<float>(tImg.height()) / (static_cast<float>(tImg.width()))) > 3) { |
| 185 |
tImg = tImg.scaledToWidth(200, Qt::FastTransformation); |
| 186 |
} else { |
| 187 |
tImg = tImg.scaledToHeight(200, Qt::FastTransformation); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
QPixmap pixmap = QPixmap::fromImage(tImg); |
| 192 |
DBImgInfo dbi = getDBInfo(path, utils::base::isVideo(path)); |
| 193 |
if (breloadCache) { //更新缓存文件 |
| 194 |
QString spath = albumGlobal::CACHE_PATH + path; |
| 195 |
utils::base::mkMutiDir(spath.mid(0, spath.lastIndexOf('/'))); |
| 196 |
pixmap.save(spath, "PNG"); |
| 197 |
} |
| 198 |
ImageEngineApi::instance()->addImageData(path, dbi); |
| 199 |
SignalManager::ViewInfo info; |
| 200 |
info.album = ""; |
| 201 |
info.lastPanel = nullptr; |
| 202 |
info.path = path; |
| 203 |
info.paths = paths; |
| 204 |
info.dBImgInfos << dbi; |
| 205 |
// 未启动相册,从外部打开图片时,延迟发送查看图片 |
| 206 |
if (dApp->getMainWindow() == nullptr) { |
| 207 |
QTimer::singleShot(100, this, [ = ] { |
| 208 |
emit dApp->signalM->viewImage(info); |
| 209 |
emit dApp->signalM->showImageView(0); |
| 210 |
DBManager::instance()->insertImgInfos(DBImgInfoList() << dbi); |
| 211 |
}); |
| 212 |
} else { |
| 213 |
emit dApp->signalM->viewImage(info); |
| 214 |
emit dApp->signalM->showImageView(0); |
| 215 |
DBManager::instance()->insertImgInfos(DBImgInfoList() << dbi); |
| 216 |
} |
| 217 |
} else { |
| 218 |
QTimer::singleShot(300, this, [ = ] { |
| 219 |
if (paths.count() > 0) |
| 220 |
{ |
| 221 |
SignalManager::ViewInfo info; |
| 222 |
info.album = ""; |
| 223 |
info.lastPanel = nullptr; |
| 224 |
info.path = path; |
| 225 |
info.paths = paths; |
| 226 |
emit dApp->signalM->viewImage(info); |
| 227 |
emit dApp->signalM->showImageView(0); |
| 228 |
ImageEngineApi::instance()->loadImagesFromNewAPP(paths, m_obj); |
| 229 |
} |
| 230 |
}); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
bool CommandLine::processOption(QStringList &paslist) |
| 235 |
{ |
| 236 |
if (! m_cmdParser.parse(dApp->getDAppNew()->arguments())) { |
| 237 |
showHelp(); |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
QString defaulttheme = dApp->setter->value(THEME_GROUP, |
| 242 |
THEME_TEXT).toString(); |
| 243 |
|
| 244 |
if (DGuiApplicationHelper::LightType == DGuiApplicationHelper::instance()->themeType()) { |
| 245 |
dApp->viewerTheme->setCurrentTheme(ViewerThemeManager::Light); |
| 246 |
} else { |
| 247 |
dApp->viewerTheme->setCurrentTheme(ViewerThemeManager::Dark); |
| 248 |
} |
| 249 |
|
| 250 |
QStringList arguments = m_cmdParser.positionalArguments(); |
| 251 |
QString filepath = ""; |
| 252 |
bool bneedexit = true; |
| 253 |
for (const QString &path : arguments) { |
| 254 |
filepath = utils::base::UrlInfo(path).toLocalFile(); |
| 255 |
QFileInfo info(filepath); |
| 256 |
QMimeDatabase db; |
| 257 |
QMimeType mt = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchContent); |
| 258 |
QMimeType mt1 = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchExtension); |
| 259 |
|
| 260 |
QString str = info.suffix().toLower(); |
| 261 |
if (utils::image::supportedImageFormats().contains(str, Qt::CaseInsensitive) |
| 262 |
|| utils::base::isVideo(filepath)) { |
| 263 |
bneedexit = false; |
| 264 |
paslist << info.filePath(); |
| 265 |
ImageEngineApi::instance()->insertImage(info.filePath(), "", true); |
| 266 |
} else if (str.isEmpty()) { |
| 267 |
bneedexit = false; |
| 268 |
paslist << info.filePath(); |
| 269 |
ImageEngineApi::instance()->insertImage(info.filePath(), ""); |
| 270 |
} |
| 271 |
} |
| 272 |
if ("" != filepath && bneedexit) { |
| 273 |
exit(0); |
| 274 |
} |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
void CommandLine::resizeEvent(QResizeEvent *e) |
| 279 |
{ |
| 280 |
QWidget::resizeEvent(e); |
| 281 |
m_pwidget->setFixedHeight(this->height() - 23); |
| 282 |
m_pwidget->setFixedWidth(this->width()); |
| 283 |
m_pwidget->move(0, 0); |
| 284 |
} |