File: set-wallpaper-plugin.cpp

package info (click to toggle)
peony-extensions 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 792 kB
  • sloc: cpp: 4,086; sh: 15; makefile: 4
file content (85 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download | duplicates (2)
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
/*
 * Peony-Qt's Library
 *
 * Copyright (C) 2019, Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: Meihong <hemeihong@kylinos.cn>
 *
 */

#include "set-wallpaper-plugin.h"
#include <file-info.h>
#include <gio/gio.h>

#include <QAction>
#include <QFileInfo>
#include <QTranslator>
#include <QLocale>
#include <QApplication>
#include <QProcess>
#include <QUrl>
#include <QDebug>

#define BACKGROUND_SETTINGS "org.mate.background"
#define PICTRUE "picture-filename"

using namespace Peony;

SetWallPaperPlugin::SetWallPaperPlugin(QObject *parent) : QObject (parent)
{
    QTranslator *t = new QTranslator(this);
    QString path = ":/translations/peony-set-wallpaper-extension_"+QLocale::system().name();
    qDebug()<<"system().name and path:"<<QLocale::system().name() << path;
    qDebug()<<"\n\n\n translate:"<<t->load(path);
    QApplication::installTranslator(t);
}

QList<QAction*> SetWallPaperPlugin::menuActions(Types types, const QString &uri, const QStringList &selectionUris)
{
    QList<QAction*> actions;
    if (types == MenuPluginInterface::DirectoryView || types == MenuPluginInterface::DesktopWindow)
    {
        if (selectionUris.count() == 1 && is_picture_file(selectionUris.first())) {
            if (selectionUris.first().contains("trash:///"))
                return actions;
            QAction *set_action = new QAction(tr("Set as wallpaper"), nullptr);
            actions<<set_action;
            connect(set_action, &QAction::triggered, [=](){
                if (QGSettings::isSchemaInstalled(BACKGROUND_SETTINGS))
                {
                   m_bg_settings = new QGSettings(BACKGROUND_SETTINGS, QByteArray(), this);
                   QUrl url= selectionUris.first();
                   bool success = m_bg_settings->trySet("pictureFilename", url.path());
                   qDebug() << "set as wallpaper result:" <<success <<url.path();
                }
            });
        }
    }


    return actions;
}

bool SetWallPaperPlugin::is_picture_file(QString file_name)
{
    QFileInfo file(file_name);
    qDebug() << "file_name:" <<file.suffix();
    if (m_picture_type_list.contains(file.suffix()))
        return true;

    return false;
}