tahoma2d/toonz/sources/toonzlib/screensavermaker.cpp

95 lines
2.5 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "toonz/screensavermaker.h"
#include "texception.h"
2016-03-19 06:57:51 +13:00
#include "tsystem.h"
#include <memory>
#ifdef _MSC_VER
2016-03-19 06:57:51 +13:00
#pragma warning(disable : 4996)
#endif
2016-03-19 06:57:51 +13:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
#include <windows.h>
#include <shellapi.h>
2016-03-19 06:57:51 +13:00
//#include <iostream.h>
#include <sys/stat.h>
#include <stdio.h>
#include <QUrl>
#include <QDesktopServices>
2016-06-15 18:43:10 +12:00
void makeScreenSaver(TFilePath scrFn, TFilePath swfFn,
std::string screenSaverName) {
struct _stat results;
if (_wstat(swfFn.getWideString().c_str(), &results) != 0)
throw TException(L"Can't stat file " + swfFn.getWideString());
int swfSize = results.st_size;
std::unique_ptr<char> swf(new char[swfSize]);
2016-06-15 18:43:10 +12:00
FILE *chan = _wfopen(swfFn.getWideString().c_str(), L"rb");
if (!chan) throw TException(L"fopen failed on " + swfFn.getWideString());
fread(swf.get(), swfSize, 1, chan);
fclose(chan);
TFilePath svscrn = TSystem::getBinDir() + "screensaver.dat";
if (!TFileStatus(svscrn).doesExist()) {
throw TException(std::wstring(L"Screensaver template not found: ") +
svscrn.getWideString());
}
TSystem::copyFile(scrFn, svscrn);
HANDLE hUpdateRes =
BeginUpdateResourceW(scrFn.getWideString().c_str(), FALSE);
if (hUpdateRes == NULL)
throw TException(L"can't write " + scrFn.getWideString());
BOOL result = UpdateResource(hUpdateRes, "FLASHFILE", MAKEINTRESOURCE(101),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
swf.get(), swfSize);
if (result == FALSE)
throw TException(L"can't add resource to " + scrFn.getWideString());
/*
result = UpdateResource(
hUpdateRes,
RT_STRING,
MAKEINTRESOURCE(1),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
(void*)screenSaverName.c_str(),
screenSaverName.size());
if (result == FALSE)
throw TException(L"can't add name to "+scrFn.getWideString());
*/
if (!EndUpdateResource(hUpdateRes, FALSE))
throw TException(L"Couldn't write " + scrFn.getWideString());
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void previewScreenSaver(TFilePath scr) {
try {
TSystem::showDocument(scr);
} catch (...) {
}
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void installScreenSaver(TFilePath scr) {
std::wstring cmd = L"desk.cpl,InstallScreenSaver " + scr.getWideString();
int ret = (int)ShellExecuteW(0, L"open", L"rundll32.exe", cmd.c_str(), 0,
SW_SHOWNORMAL);
if (ret <= 32) throw;
2016-03-19 06:57:51 +13:00
}
#else
2016-06-15 18:43:10 +12:00
void makeScreenSaver(TFilePath scrFn, TFilePath swfFn,
std::string screenSaverName) {}
2016-03-19 06:57:51 +13:00
/*
void previewScreenSaver(TFilePath scr)
{
}
*/
2016-06-15 18:43:10 +12:00
void installScreenSaver(TFilePath scr) {}
2016-03-19 06:57:51 +13:00
#endif