1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2019-04-02
 * Description : plugin to export image as wallpaper - Windows version.
 *
 * SPDX-FileCopyrightText: 2019      by Igor Antropov <antropovi at yahoo dot com>
 * SPDX-FileCopyrightText: 2019-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "wallpaperplugin.h"

// Qt includes

#include <QMessageBox>
#include <QProcess>
#include <QDir>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "digikam_debug.h"

// Windows includes

#ifdef Q_OS_WIN
#   include <windows.h>
#   include <wininet.h>
#   include <shlobj.h>
#endif

namespace DigikamGenericWallpaperPlugin
{

bool s_checkErrorCode(HRESULT status, const QString& path, const QString& context)
{
    if (FAILED(status))
    {
        // Code to catch error string from error code with Windows API.

        LPWSTR bufPtr        = nullptr;
        DWORD werr           = GetLastError();
        FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                       FORMAT_MESSAGE_FROM_SYSTEM     |
                       FORMAT_MESSAGE_IGNORE_INSERTS,
                       nullptr,
                        werr,
                       0,
                       (LPWSTR)&bufPtr,
                       0,
                       nullptr);

        // cppcheck-suppress knownConditionTrueFalse
        QString errStr = (bufPtr) ? QString::fromUtf16((const ushort*)bufPtr).trimmed()
                                  : i18n("Unknown Error %1", werr);
        LocalFree(bufPtr);

        QMessageBox::warning(nullptr,
                             i18nc("@title:window",
                                   "Error While to Set Image as Wallpaper"),
                             i18n("Cannot change wallpaper image from current desktop with\n"
                                  "%1\n\nContext: %2\n\nError: %3",
                                  path,
                                  context,
                                  errStr));

        return false;
    }

    return true;
}

bool WallpaperPlugin::setWallpaper(const QString& path, int layout) const
{
    // NOTE: IDesktopWallpaper is only defined with Windows >= 8.
    //       To be compatible with Windows 7, we needs to use IActiveDesktop instead.

    CoInitializeEx(0, COINIT_APARTMENTTHREADED);

    IActiveDesktop* iADesktop = nullptr;
    HRESULT status            = CoCreateInstance(CLSID_ActiveDesktop,
                                                 nullptr,
                                                 CLSCTX_INPROC_SERVER,
                                                 IID_IActiveDesktop,
                                                 (void**)&iADesktop);

    if (!s_checkErrorCode(status, path, i18n("Cannot create desktop context.")))
    {
        CoUninitialize();

        return false;
    }

    WALLPAPEROPT wOption;
    ZeroMemory(&wOption, sizeof(WALLPAPEROPT));
    wOption.dwSize            = sizeof(WALLPAPEROPT);

    switch (layout)
    {
        case Mosaic:
        {
            wOption.dwStyle = WPSTYLE_TILE;
            break;
        }

        case Centered:
        {
            wOption.dwStyle = WPSTYLE_CENTER;
            break;
        }

        default:    // *Adjusted*
        {
            wOption.dwStyle = WPSTYLE_STRETCH;
            break;
        }
    }

    status = iADesktop->SetWallpaper((PCWSTR)QDir::toNativeSeparators(path).utf16(), 0);

    if (!s_checkErrorCode(status, path, i18n("Cannot set wall paper image path.")))
    {
        iADesktop->Release();
        CoUninitialize();

        return false;
    }

    status = iADesktop->SetWallpaperOptions(&wOption, 0);

    if (!s_checkErrorCode(status, path, i18n("Cannot set wallpaper properties.")))
    {
        iADesktop->Release();
        CoUninitialize();

        return false;
    }

    status = iADesktop->ApplyChanges(AD_APPLY_ALL);

    if (!s_checkErrorCode(status, path, i18n("Cannot apply changes to desktop wallpaper.")))
    {
        iADesktop->Release();
        CoUninitialize();

        return false;
    }

    iADesktop->Release();
    CoUninitialize();

    return true;
}

} // namespace DigikamGenericWallpaperPlugin