mirror of
https://github.com/Almamu/linux-wallpaperengine.git
synced 2025-09-14 13:56:48 +08:00
added appData
This commit is contained in:
parent
754b3d3cd4
commit
5b9862ef6a
@ -6,6 +6,7 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
set(CMAKE_INSTALL_PREFIX "/opt/${PROJECT_NAME}" CACHE PATH "..." FORCE)
|
||||
endif()
|
||||
|
||||
|
||||
set_property(GLOBAL PROPERTY OS_FOLDERS ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
|
||||
@ -530,6 +531,8 @@ add_executable(
|
||||
${DEMOMODE_SOURCES}
|
||||
)
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
|
||||
|
||||
qt_add_resources(RESOURCES resources.qrc)
|
||||
target_sources(linux-wallpaperengine PRIVATE ${RESOURCES})
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <QtConcurrent/qtconcurrentrun.h>
|
||||
#include <X11/X.h>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <qapplication.h>
|
||||
@ -40,7 +42,7 @@
|
||||
|
||||
#define PICTURE_SIZE 128
|
||||
|
||||
UIWindow::UIWindow(QWidget* parent, QApplication* qapp, SingleInstanceManager* ig) {
|
||||
UIWindow::UIWindow(QWidget* parent, QApplication* qapp, SingleInstanceManager* ig, const std::string& appDataLocation) {
|
||||
this->qapp = qapp;
|
||||
this->screenSelector = new QComboBox(this);
|
||||
this->wallpaperEngine = new QProcess(this);
|
||||
@ -48,6 +50,25 @@ UIWindow::UIWindow(QWidget* parent, QApplication* qapp, SingleInstanceManager* i
|
||||
this->buttonLayout = new QGridLayout(this);
|
||||
|
||||
this->wallpaperSettingsWidget = nullptr;
|
||||
|
||||
this->appDataPath = appDataLocation;
|
||||
|
||||
if (!std::filesystem::exists(this->appDataPath + "selectedWallpapers.json")) {
|
||||
std::ofstream ofs(this->appDataPath + "selectedWallpapers.json");
|
||||
if (!ofs) {
|
||||
sLog.error("Failed to create file");
|
||||
return;
|
||||
}
|
||||
nlohmann::json j = nlohmann::json::object();
|
||||
ofs << j.dump(4);
|
||||
ofs.close();
|
||||
}
|
||||
std::ifstream file(this->appDataPath + "selectedWallpapers.json");
|
||||
if (!file) {
|
||||
sLog.error("Failed to open file");
|
||||
}
|
||||
this->selectedWallpapersJSON = nlohmann::json::parse(file);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
|
||||
@ -111,6 +132,7 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
|
||||
// screen select dropdown
|
||||
const QList<QScreen*> screens = QGuiApplication::screens();
|
||||
for (QScreen* screen : screens) {
|
||||
this->selectedWallpapers[screen->name().toStdString()] = "";
|
||||
this->screenSelector->addItem(screen->name());
|
||||
}
|
||||
this->screenSelector->setCurrentIndex(0);
|
||||
@ -130,6 +152,7 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
|
||||
|
||||
QObject::connect(this->screenSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int index) {
|
||||
updateSelectedButton();
|
||||
sLog.out(this->selectedWallpapers[this->screenSelector->currentText().toStdString()]);
|
||||
this->wallpaperSettingsWidget->update(this->selectedWallpapers[this->screenSelector->currentText().toStdString()]);
|
||||
});
|
||||
|
||||
@ -166,6 +189,7 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
|
||||
connect(this->wallpaperSettingsWidget, &WallpaperSettingsWidget::applySettings, this, [this](const std::string& flags) {
|
||||
this->extraFlags[this->screenSelector->currentText().toStdString()] = split(flags, ' ');
|
||||
startNewWallpaperEngine();
|
||||
// updateStoredSelectedWallpapers();
|
||||
});
|
||||
|
||||
splitLayout->addWidget(leftWidget, 2);
|
||||
@ -174,9 +198,6 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
|
||||
mainlayout->addWidget(splitWidget);
|
||||
this->setLayout(mainlayout);
|
||||
|
||||
// update Buttons
|
||||
updateSelectedButton();
|
||||
|
||||
// SYSTEM TRAY
|
||||
auto* trayIcon = new QSystemTrayIcon(QIcon(":/assets/wallpaper-icon.png"));
|
||||
|
||||
@ -198,12 +219,23 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
|
||||
}
|
||||
}
|
||||
});
|
||||
// apply stored Selected wallpapers
|
||||
for (const auto& n : this->selectedWallpapers) {
|
||||
nlohmann::json obj = this->selectedWallpapersJSON[n.first];
|
||||
if (!obj.is_object()) continue;
|
||||
|
||||
std::string wallpaper = obj.value("wallpaper", "");
|
||||
|
||||
if (wallpaper.empty()) continue;
|
||||
if (this->selectedWallpapers.find(n.first) == this->selectedWallpapers.end()) continue;
|
||||
this->selectedWallpapers[n.first] = wallpaper;
|
||||
}
|
||||
// updateSelectedButtons
|
||||
updateSelectedButton();
|
||||
startNewWallpaperEngine();
|
||||
}
|
||||
|
||||
void UIWindow::showEvent(QShowEvent* event) {
|
||||
QtConcurrent::run([this]() {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
void UIWindow::closeEvent(QCloseEvent* event) {
|
||||
@ -212,6 +244,15 @@ void UIWindow::closeEvent(QCloseEvent* event) {
|
||||
}
|
||||
|
||||
void UIWindow::startNewWallpaperEngine() {
|
||||
sLog.out("Start new WallpaperEngine..");
|
||||
|
||||
for (const auto& n : this->extraFlags) {
|
||||
std::string str;
|
||||
for (const auto& s : n.second) {
|
||||
str.append(s);
|
||||
}
|
||||
sLog.out(n.first + " | " + str);
|
||||
}
|
||||
if (wallpaperEngine->state() == QProcess::Running) {
|
||||
// Stop WallpaperProcess
|
||||
wallpaperEngine->terminate();
|
||||
@ -221,7 +262,6 @@ void UIWindow::startNewWallpaperEngine() {
|
||||
|
||||
}
|
||||
}
|
||||
// delete this->wallpaperEngine;
|
||||
// create args
|
||||
QStringList args;
|
||||
|
||||
@ -236,11 +276,20 @@ void UIWindow::startNewWallpaperEngine() {
|
||||
args.push_back(QString::fromStdString(wallpaper.second));
|
||||
}
|
||||
|
||||
std::string argsStr;
|
||||
for (const auto& s : args) {
|
||||
argsStr.append(s.toStdString() + " ");
|
||||
}
|
||||
|
||||
// start Wallpaper Process
|
||||
wallpaperEngine->start(QCoreApplication::applicationFilePath(), args);
|
||||
if (!wallpaperEngine->waitForStarted(3000)) {
|
||||
sLog.out(wallpaperEngine->error());
|
||||
}
|
||||
}
|
||||
|
||||
void UIWindow::updateSelectedButton() {
|
||||
std::string selected = this->selectedWallpapers[this->screenSelector->currentText().toStdString()];
|
||||
for (int i = 0; i < this->buttonLayout->rowCount(); i++) {
|
||||
for (int j = 0; j < this->buttonLayout->columnCount(); j++) {
|
||||
auto* item = this->buttonLayout->itemAtPosition(i, j);
|
||||
@ -254,7 +303,6 @@ void UIWindow::updateSelectedButton() {
|
||||
|
||||
button->setEnabled(true);
|
||||
|
||||
std::string selected = this->selectedWallpapers[this->screenSelector->currentText().toStdString()];
|
||||
QString currentStyle = button->styleSheet();
|
||||
QString newStyle = currentStyle;
|
||||
if (button->property("path").toString().toStdString() == selected) {
|
||||
@ -268,6 +316,20 @@ void UIWindow::updateSelectedButton() {
|
||||
}
|
||||
}
|
||||
|
||||
void UIWindow::updateStoredSelectedWallpapers() {
|
||||
std::ofstream file(this->appDataPath + "selectedWallpapers.json");
|
||||
if (!file) {
|
||||
sLog.error("Failed to create file!");
|
||||
return;
|
||||
}
|
||||
for (const auto& wallpaper : this->selectedWallpapers) {
|
||||
this->selectedWallpapersJSON[wallpaper.first]["wallpaper"] = wallpaper.second;
|
||||
this->selectedWallpapersJSON[wallpaper.first]["flags"] = "";
|
||||
}
|
||||
file << this->selectedWallpapersJSON.dump(4);
|
||||
file.close();
|
||||
}
|
||||
|
||||
std::vector<std::string> UIWindow::split(const std::string &str, char delimiter) {
|
||||
// Using str in a string stream
|
||||
std::stringstream ss(str);
|
||||
|
@ -33,13 +33,14 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <QVBoxLayout>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "Qt/WallpaperSettingsWidget.h"
|
||||
|
||||
class UIWindow : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
UIWindow(QWidget* parent, QApplication* qapp, SingleInstanceManager* instanceGuard);
|
||||
UIWindow(QWidget* parent, QApplication* qapp, SingleInstanceManager* instanceGuard, const std::string& appDataLocation);
|
||||
void setupUIWindow(std::vector<std::string> wallpaperPaths);
|
||||
|
||||
private:
|
||||
@ -55,8 +56,12 @@ class UIWindow : public QWidget {
|
||||
std::map<std::string, std::vector<std::string>> extraFlags;
|
||||
QProcess* wallpaperEngine;
|
||||
|
||||
std::string appDataPath;
|
||||
nlohmann::json selectedWallpapersJSON;
|
||||
|
||||
void startNewWallpaperEngine();
|
||||
void updateSelectedButton();
|
||||
void updateStoredSelectedWallpapers();
|
||||
static std::vector<std::string> split(const std::string &str, char r);
|
||||
|
||||
protected:
|
||||
|
24
src/main.cpp
24
src/main.cpp
@ -45,9 +45,6 @@ void initLogging ()
|
||||
int main (int argc, char* argv[]) {
|
||||
initLogging ();
|
||||
|
||||
std::cout << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() << "\n";
|
||||
|
||||
|
||||
if (argc <= 1) {
|
||||
QApplication qapp(argc, argv);
|
||||
globalApp = &qapp;
|
||||
@ -58,6 +55,25 @@ int main (int argc, char* argv[]) {
|
||||
sLog.out("App is already running!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string appDataLocation;
|
||||
|
||||
// TODO: Use desktop file as marker
|
||||
// Data directory
|
||||
if (QCoreApplication::applicationDirPath() == INSTALL_PREFIX) {
|
||||
// is installed properly
|
||||
appDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString() + "/";
|
||||
} else {
|
||||
// is not installed
|
||||
appDataLocation = QCoreApplication::applicationDirPath().toStdString() + "/appData/";
|
||||
}
|
||||
sLog.out("AppDataLocation: " + appDataLocation);
|
||||
if (!std::filesystem::exists(appDataLocation)) {
|
||||
if (!std::filesystem::create_directory(appDataLocation)) {
|
||||
sLog.error("Could't create appData directory");
|
||||
}
|
||||
}
|
||||
|
||||
std::string path = Steam::FileSystem::workshopDirectory(431960);
|
||||
sLog.out("Found workshopDirectory: " + path);
|
||||
|
||||
@ -75,7 +91,7 @@ int main (int argc, char* argv[]) {
|
||||
|
||||
sLog.out("Starting App..");
|
||||
|
||||
auto* uiWindow = new UIWindow(nullptr, &qapp, g_instanceManager);
|
||||
auto* uiWindow = new UIWindow(nullptr, &qapp, g_instanceManager, appDataLocation);
|
||||
|
||||
uiWindow->setupUIWindow(wallpaperPaths);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user