| 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 "wallpapersetter.h" |
| 22 |
#include "application.h" |
| 23 |
#include "unionimage.h" |
| 24 |
#include "baseutils.h" |
| 25 |
#include <QTimer> |
| 26 |
#include <QFile> |
| 27 |
#include <QFileInfo> |
| 28 |
#include <QProcess> |
| 29 |
#include <QImage> |
| 30 |
#include <QDebug> |
| 31 |
#include <QDBusInterface> |
| 32 |
#include <QtDBus> |
| 33 |
#include <QGuiApplication> |
| 34 |
#include <QScreen> |
| 35 |
|
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
namespace { |
| 39 |
const QString WALLPAPER_PATH = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + "deepin" + QDir::separator() + "deepin-album" + QDir::separator() + "wallpapers" + QDir::separator(); |
| 40 |
} |
| 41 |
|
| 42 |
WallpaperSetter *WallpaperSetter::m_setter = nullptr; |
| 43 |
WallpaperSetter *WallpaperSetter::instance() |
| 44 |
{ |
| 45 |
if (! m_setter) { |
| 46 |
m_setter = new WallpaperSetter(); |
| 47 |
} |
| 48 |
|
| 49 |
return m_setter; |
| 50 |
} |
| 51 |
|
| 52 |
WallpaperSetter::WallpaperSetter(QObject *parent) : QObject(parent) |
| 53 |
{ |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
bool WallpaperSetter::setBackground(const QString &pictureFilePath) |
| 58 |
{ |
| 59 |
QImage tImg; |
| 60 |
QString errMsg; |
| 61 |
QFileInfo info(pictureFilePath); |
| 62 |
QString tempWallPaperpath; |
| 63 |
tempWallPaperpath = WALLPAPER_PATH + info.fileName(); |
| 64 |
QFileInfo tempInfo(tempWallPaperpath); |
| 65 |
if (!UnionImage_NameSpace::loadStaticImageFromFile(pictureFilePath, tImg, errMsg)) { |
| 66 |
// qDebug() << "load wallpaper path error!"; |
| 67 |
return false; |
| 68 |
} |
| 69 |
//临时文件目录不存在,先创建临时文件目录 |
| 70 |
QDir tempImgDir(WALLPAPER_PATH); |
| 71 |
if (!tempImgDir.exists() && !tempImgDir.mkdir(tempImgDir.path())) { |
| 72 |
// qDebug() << "save temp wallpaper path error!"; |
| 73 |
return false; |
| 74 |
} |
| 75 |
if (!tImg.save(tempWallPaperpath, "JPG", 100)) { |
| 76 |
// qDebug() << "save temp wallpaper path error!"; |
| 77 |
return false; |
| 78 |
} |
| 79 |
if (!tempInfo.exists()) { |
| 80 |
// qDebug() << "save temp wallpaper path error!"; |
| 81 |
return false; |
| 82 |
} |
| 83 |
QDBusMessage msgIntrospect = QDBusMessage::createMethodCall("com.deepin.daemon.Appearance", "/com/deepin/daemon/Appearance", "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 84 |
QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(msgIntrospect); |
| 85 |
call.waitForFinished(); |
| 86 |
|
| 87 |
QDBusReply<QString> reply = call.reply(); |
| 88 |
QString value = reply.value(); |
| 89 |
if (value.contains("SetMonitorBackground")) { |
| 90 |
QDBusMessage msg = QDBusMessage::createMethodCall("com.deepin.daemon.Appearance", "/com/deepin/daemon/Appearance", "com.deepin.daemon.Appearance", "SetMonitorBackground"); |
| 91 |
QString mm; |
| 92 |
if (Application::isWaylandPlatform()) { |
| 93 |
QDBusInterface interface("com.deepin.daemon.Display", "/com/deepin/daemon/Display", "com.deepin.daemon.Display"); |
| 94 |
mm = qvariant_cast< QString >(interface.property("Primary")); |
| 95 |
} else { |
| 96 |
mm = qApp->primaryScreen()->name(); |
| 97 |
} |
| 98 |
msg.setArguments({mm, tempWallPaperpath}); |
| 99 |
QDBusConnection::sessionBus().asyncCall(msg); |
| 100 |
qDebug() << "FileUtils::setBackground call Appearance SetMonitorBackground"; |
| 101 |
} else { |
| 102 |
QDBusMessage msg = QDBusMessage::createMethodCall("com.deepin.daemon.Appearance", "/com/deepin/daemon/Appearance", "com.deepin.daemon.Appearance", "Set"); |
| 103 |
msg.setArguments({"Background", tempWallPaperpath}); |
| 104 |
QDBusConnection::sessionBus().asyncCall(msg); |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |