2016-02-25 20:19:27 +00:00
|
|
|
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
|
|
|
|
// Copyright (C) 2010 Winch Gate Property Limited
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "stdpch.h"
|
|
|
|
#include "utils.h"
|
2016-10-09 13:08:37 +00:00
|
|
|
#include "configfile.h"
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
#include "nel/misc/path.h"
|
|
|
|
|
2016-09-22 15:24:41 +00:00
|
|
|
#ifdef DEBUG_NEW
|
|
|
|
#define new DEBUG_NEW
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 21:29:11 +00:00
|
|
|
QString qBytesToHumanReadable(qint64 bytes)
|
|
|
|
{
|
|
|
|
static std::vector<std::string> units;
|
|
|
|
|
|
|
|
if (units.empty())
|
|
|
|
{
|
|
|
|
units.push_back(QObject::tr("B").toUtf8().constData());
|
|
|
|
units.push_back(QObject::tr("KiB").toUtf8().constData());
|
|
|
|
units.push_back(QObject::tr("MiB").toUtf8().constData());
|
|
|
|
units.push_back(QObject::tr("GiB").toUtf8().constData());
|
|
|
|
units.push_back(QObject::tr("TiB").toUtf8().constData());
|
|
|
|
units.push_back(QObject::tr("PiB").toUtf8().constData());
|
|
|
|
}
|
|
|
|
|
2016-09-21 13:44:02 +00:00
|
|
|
return QString::fromUtf8(NLMISC::bytesToHumanReadableUnits(bytes, units).c_str());
|
2016-05-25 21:29:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 15:05:20 +00:00
|
|
|
bool isDirectoryEmpty(const QString &directory, bool recursize)
|
|
|
|
{
|
|
|
|
bool res = true;
|
|
|
|
|
|
|
|
if (!directory.isEmpty())
|
|
|
|
{
|
|
|
|
QDir dir(directory);
|
|
|
|
|
|
|
|
if (dir.exists())
|
|
|
|
{
|
2016-10-11 15:28:54 +00:00
|
|
|
// process all files and directories excepted parent and current ones
|
2016-09-29 15:05:20 +00:00
|
|
|
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot);
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); ++i)
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo = list.at(i);
|
|
|
|
|
|
|
|
if (fileInfo.isDir())
|
|
|
|
{
|
2016-10-11 15:28:54 +00:00
|
|
|
// don't consider empty directories as files, but process it recursively if required
|
2016-09-29 15:05:20 +00:00
|
|
|
if (recursize) if (!isDirectoryEmpty(fileInfo.absoluteFilePath(), true)) return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-11 15:28:54 +00:00
|
|
|
// we found a file, directory is not empty
|
2016-09-29 15:05:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-09 13:09:30 +00:00
|
|
|
bool isDirectoryWritable(const QString &directory)
|
|
|
|
{
|
|
|
|
// check if directory is writable by current user
|
|
|
|
QFile file(directory + "/writable_test_for_ryzom_installer.txt");
|
|
|
|
|
|
|
|
if (!file.open(QFile::WriteOnly)) return false;
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// remove it
|
|
|
|
file.remove();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:50:11 +00:00
|
|
|
qint64 getDirectorySize(const QString &directory, bool recursize)
|
2016-05-29 18:27:49 +00:00
|
|
|
{
|
|
|
|
qint64 size = 0;
|
|
|
|
|
2016-06-12 12:18:36 +00:00
|
|
|
if (!directory.isEmpty())
|
2016-05-29 18:27:49 +00:00
|
|
|
{
|
2016-06-12 12:18:36 +00:00
|
|
|
QDir dir(directory);
|
2016-05-29 18:27:49 +00:00
|
|
|
|
2016-06-12 12:18:36 +00:00
|
|
|
if (dir.exists())
|
2016-05-29 18:27:49 +00:00
|
|
|
{
|
2016-10-11 15:28:54 +00:00
|
|
|
// process all files and directories excepted parent and current ones
|
2016-06-12 12:18:36 +00:00
|
|
|
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot);
|
2016-05-29 18:27:49 +00:00
|
|
|
|
2016-06-12 12:18:36 +00:00
|
|
|
for (int i = 0; i < list.size(); ++i)
|
2016-05-29 18:27:49 +00:00
|
|
|
{
|
2016-06-12 12:18:36 +00:00
|
|
|
QFileInfo fileInfo = list.at(i);
|
|
|
|
|
|
|
|
if (fileInfo.isDir())
|
|
|
|
{
|
2016-08-14 10:50:11 +00:00
|
|
|
if (recursize) size += getDirectorySize(fileInfo.absoluteFilePath(), true);
|
2016-06-12 12:18:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size += fileInfo.size();
|
|
|
|
}
|
2016-05-29 18:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
QString qFromUtf8(const std::string &str)
|
|
|
|
{
|
|
|
|
return QString::fromUtf8(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string qToUtf8(const QString &str)
|
|
|
|
{
|
|
|
|
return str.toUtf8().constData();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString qFromUtf16(const ucstring &str)
|
|
|
|
{
|
|
|
|
return QString::fromUtf16(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
ucstring qToUtf16(const QString &str)
|
|
|
|
{
|
|
|
|
return ucstring::makeFromUtf8(qToUtf8(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString qFromWide(const wchar_t *str)
|
|
|
|
{
|
|
|
|
return QString::fromUtf16((ushort*)str);
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t* qToWide(const QString &str)
|
|
|
|
{
|
|
|
|
return (wchar_t*)str.utf16();
|
|
|
|
}
|
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
bool shortcutExists(const QString &shortcut)
|
|
|
|
{
|
|
|
|
return !shortcut.isEmpty() && NLMISC::CFile::isExists(qToUtf8(appendShortcutExtension(shortcut)));
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:48:06 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
bool createShortcut(const QString &shortcut, const QString &name, const QString &executable, const QString &arguments, const QString &icon, const QString &workingDir)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-09-21 13:20:56 +00:00
|
|
|
CCOMHelper comHelper;
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
IShellLinkW* psl;
|
|
|
|
|
|
|
|
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
|
|
|
|
// has already been called.
|
2016-06-18 20:40:02 +00:00
|
|
|
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID*)&psl);
|
2016-10-03 07:59:19 +00:00
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
2016-10-03 07:59:19 +00:00
|
|
|
IPersistFile* ppf = NULL;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// Set the path to the shortcut target and add the description.
|
2016-07-25 16:27:53 +00:00
|
|
|
psl->SetPath(qToWide(QDir::toNativeSeparators(executable)));
|
|
|
|
psl->SetIconLocation(qToWide(QDir::toNativeSeparators(icon)), 0);
|
|
|
|
psl->SetDescription(qToWide(name));
|
2016-06-18 20:40:02 +00:00
|
|
|
psl->SetArguments(qToWide(arguments));
|
|
|
|
psl->SetWorkingDirectory(qToWide(QDir::toNativeSeparators(workingDir)));
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-09-29 18:46:29 +00:00
|
|
|
// Query IShellLink for the IPersistFile interface, used for saving the shortcut in persistent storage.
|
2016-02-25 20:19:27 +00:00
|
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
2016-10-03 07:59:19 +00:00
|
|
|
QString path(shortcut + ".lnk");
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// Save the link by calling IPersistFile::Save.
|
2016-09-29 18:46:29 +00:00
|
|
|
hres = ppf->Save(qToWide(QDir::toNativeSeparators(path)), TRUE);
|
2016-10-04 17:27:29 +00:00
|
|
|
|
|
|
|
if (FAILED(hres))
|
|
|
|
{
|
|
|
|
qDebug() << "Unable to create shortcut" << path;
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
ppf->Release();
|
|
|
|
}
|
2016-10-03 07:59:19 +00:00
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
psl->Release();
|
|
|
|
}
|
2016-10-03 07:59:19 +00:00
|
|
|
|
2016-06-18 20:40:02 +00:00
|
|
|
return SUCCEEDED(hres);
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
bool resolveShortcut(const QWidget &window, const QString &shortcut, QString &path)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-09-21 13:20:56 +00:00
|
|
|
CCOMHelper comHelper;
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
IShellLinkW* psl;
|
|
|
|
WIN32_FIND_DATAW wfd;
|
|
|
|
|
|
|
|
path.clear(); // Assume failure
|
|
|
|
|
|
|
|
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
|
|
|
|
// has already been called.
|
|
|
|
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
IPersistFile* ppf;
|
|
|
|
|
|
|
|
// Get a pointer to the IPersistFile interface.
|
|
|
|
hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
// Add code here to check return value from MultiByteWideChar
|
|
|
|
// for success.
|
|
|
|
|
|
|
|
// Load the shortcut.
|
2016-10-03 07:59:19 +00:00
|
|
|
hres = ppf->Load(qToWide(QDir::toNativeSeparators(shortcut)), STGM_READ);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
// Resolve the link.
|
2016-06-18 20:40:02 +00:00
|
|
|
hres = psl->Resolve((HWND)window.winId(), 0);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
WCHAR szGotPath[MAX_PATH];
|
|
|
|
|
|
|
|
// Get the path to the link target.
|
|
|
|
hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATAW*)&wfd, SLGP_SHORTPATH);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
WCHAR szDescription[MAX_PATH];
|
|
|
|
|
|
|
|
// Get the description of the target.
|
|
|
|
hres = psl->GetDescription(szDescription, MAX_PATH);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
// Handle success
|
2016-06-18 20:40:02 +00:00
|
|
|
path = QDir::fromNativeSeparators(qFromWide(szGotPath));
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the pointer to the IPersistFile interface.
|
|
|
|
ppf->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the pointer to the IShellLink interface.
|
|
|
|
psl->Release();
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:40:02 +00:00
|
|
|
return SUCCEEDED(hres);
|
|
|
|
}
|
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
#elif defined(NL_OS_MAC)
|
2016-06-18 20:40:02 +00:00
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
bool createShortcut(const QString &shortcut, const QString &name, const QString &executable, const QString &arguments, const QString &icon, const QString &workingDir)
|
2016-06-18 20:40:02 +00:00
|
|
|
{
|
2016-10-09 13:08:37 +00:00
|
|
|
QString appPath(shortcut + ".app");
|
2016-06-19 18:50:23 +00:00
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
// directories
|
|
|
|
QString contentsPath(appPath + "/Contents");
|
|
|
|
QString binaryPath(contentsPath + "/MacOS");
|
|
|
|
QString dataPath(contentsPath + "/Resources");
|
2016-07-25 16:27:53 +00:00
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
// files
|
|
|
|
QString binaryFile(binaryPath + "/shortcut.sh");
|
|
|
|
QString iconFile(dataPath + "/ryzom.icns");
|
|
|
|
QString pkgInfoFile(contentsPath + "/PkgInfo");
|
|
|
|
QString plistFile(contentsPath + "/Info.plist");
|
2016-07-25 16:27:53 +00:00
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
// silently create directories
|
|
|
|
QDir().mkpath(binaryPath);
|
|
|
|
QDir().mkpath(dataPath);
|
|
|
|
|
|
|
|
if (!isDirectoryWritable(binaryPath) || !isDirectoryWritable(dataPath)) return false;
|
|
|
|
|
|
|
|
// write icon
|
|
|
|
if (!writeResource(":/icons/ryzom.icns", iconFile)) return false;
|
|
|
|
|
|
|
|
// write PkgInfo
|
|
|
|
if (!writeResource(":/templates/PkgInfo", pkgInfoFile)) return false;
|
|
|
|
|
|
|
|
// variables
|
|
|
|
QMap<QString, QString> strings;
|
|
|
|
|
|
|
|
// build command
|
2016-10-10 16:54:03 +00:00
|
|
|
QString command = QString("exec \"%1\"").arg(executable);
|
|
|
|
if (!arguments.isEmpty()) command += " " + arguments;
|
2016-07-25 16:27:53 +00:00
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
strings.clear();
|
|
|
|
strings["COMMAND"] = command;
|
|
|
|
|
|
|
|
// write shortcut.sh
|
|
|
|
if (!writeResourceWithTemplates(":/templates/shortcut.sh", binaryFile, strings)) return false;
|
|
|
|
|
|
|
|
// set executable flags to .sh
|
|
|
|
QFile::setPermissions(binaryFile, QFile::permissions(binaryFile) | QFile::ExeGroup | QFile::ExeUser | QFile::ExeOther);
|
|
|
|
|
|
|
|
CConfigFile *config = CConfigFile::getInstance();
|
|
|
|
|
2016-10-11 15:42:46 +00:00
|
|
|
// HTML escape values because they'll be in a XML file
|
2016-10-09 13:08:37 +00:00
|
|
|
strings.clear();
|
2016-10-11 15:42:46 +00:00
|
|
|
strings["NAME"] = name.toHtmlEscaped();
|
|
|
|
strings["COPYRIGHT"] = config->getProductPublisher().toHtmlEscaped();
|
2016-10-09 13:08:37 +00:00
|
|
|
strings["VERSION"] = QApplication::applicationVersion();
|
|
|
|
|
|
|
|
// write Info.plist
|
|
|
|
if (!writeResourceWithTemplates(":/templates/Info.plist", plistFile, strings)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool resolveShortcut(const QWidget &window, const QString &pathLink, QString &pathObj)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
bool createShortcut(const QString &shortcut, const QString &name, const QString &executable, const QString &arguments, const QString &icon, const QString &workingDir)
|
|
|
|
{
|
2016-07-25 16:27:53 +00:00
|
|
|
// build command
|
|
|
|
QString command = executable;
|
|
|
|
if (!arguments.isEmpty()) command += " " + arguments;
|
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
// variables
|
|
|
|
QMap<QString, QString> strings;
|
|
|
|
strings["NAME"] = name;
|
|
|
|
strings["COMMAND"] = command;
|
|
|
|
strings["ICON"] = icon;
|
2016-07-25 16:27:53 +00:00
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
// destination file
|
2016-10-03 07:59:19 +00:00
|
|
|
QString path(shortcut + ".desktop");
|
2016-09-29 18:46:29 +00:00
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
// replace strings
|
|
|
|
if (!writeResourceWithTemplates(":/templates/template.desktop", path, strings)) return false;
|
2016-07-25 16:27:53 +00:00
|
|
|
|
2016-09-28 21:32:24 +00:00
|
|
|
// set executable flags to .desktop
|
|
|
|
QFile::setPermissions(path, QFile::permissions(path) | QFile::ExeGroup | QFile::ExeUser | QFile::ExeOther);
|
|
|
|
|
2016-07-25 16:27:53 +00:00
|
|
|
return true;
|
2016-06-18 20:40:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
bool resolveShortcut(const QWidget &window, const QString &pathLink, QString &pathObj)
|
2016-06-18 20:40:02 +00:00
|
|
|
{
|
|
|
|
return false;
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
2016-05-14 16:48:06 +00:00
|
|
|
|
|
|
|
#endif
|
2016-09-21 13:20:56 +00:00
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
bool removeShortcut(const QString &shortcut)
|
|
|
|
{
|
|
|
|
// empty links are invalid
|
|
|
|
if (shortcut.isEmpty()) return false;
|
|
|
|
|
|
|
|
// append extension if missing
|
|
|
|
QString fullPath = appendShortcutExtension(shortcut);
|
|
|
|
|
|
|
|
// link doesn't exist
|
|
|
|
if (!NLMISC::CFile::isExists(qToUtf8(fullPath))) return false;
|
|
|
|
|
|
|
|
// remove it
|
2016-10-09 13:08:37 +00:00
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
// under OS X, it's a directory
|
|
|
|
return QDir(fullPath).removeRecursively();
|
|
|
|
#else
|
|
|
|
// a file under other platforms
|
2016-10-03 07:59:19 +00:00
|
|
|
return QFile::remove(fullPath);
|
2016-10-09 13:08:37 +00:00
|
|
|
#endif
|
2016-10-03 07:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString appendShortcutExtension(const QString &shortcut)
|
2016-10-01 11:05:11 +00:00
|
|
|
{
|
|
|
|
QString extension;
|
|
|
|
|
2016-10-01 12:10:43 +00:00
|
|
|
#if defined(Q_OS_WIN32)
|
2016-10-01 11:05:11 +00:00
|
|
|
extension = ".lnk";
|
2016-10-01 12:10:43 +00:00
|
|
|
#elif defined(Q_OS_MAC)
|
2016-10-09 13:08:37 +00:00
|
|
|
extension = ".app";
|
2016-10-01 11:05:11 +00:00
|
|
|
#else
|
|
|
|
extension = ".desktop";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// already the good extension
|
2016-10-03 07:59:19 +00:00
|
|
|
if (shortcut.indexOf(extension) > -1) return shortcut;
|
2016-10-01 11:05:11 +00:00
|
|
|
|
2016-10-03 07:59:19 +00:00
|
|
|
return shortcut + extension;
|
2016-10-01 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 15:24:41 +00:00
|
|
|
QString getVersionFromExecutable(const QString &path)
|
|
|
|
{
|
|
|
|
// launch executable with --version argument
|
|
|
|
QProcess process;
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.start(path, QStringList() << "--version", QIODevice::ReadWrite);
|
|
|
|
|
|
|
|
if (!process.waitForStarted()) return "";
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
// read all output
|
2016-09-22 16:09:43 +00:00
|
|
|
while (process.waitForReadyRead(1000)) data.append(process.readAll());
|
2016-09-22 15:24:41 +00:00
|
|
|
|
2016-09-22 16:09:43 +00:00
|
|
|
if (!data.isEmpty())
|
|
|
|
{
|
|
|
|
QString versionString = QString::fromUtf8(data);
|
2016-09-22 15:24:41 +00:00
|
|
|
|
2016-09-22 16:09:43 +00:00
|
|
|
// parse version from output (client)
|
|
|
|
QRegExp reg("([A-Za-z0-1_.]+) ((DEV|FV) ([0-9.]+))");
|
|
|
|
if (reg.indexIn(versionString) > -1) return reg.cap(2);
|
2016-09-22 15:24:41 +00:00
|
|
|
|
2016-09-22 16:09:43 +00:00
|
|
|
// parse version from output (other tools)
|
|
|
|
reg.setPattern("([A-Za-z_ ]+) ([0-9.]+)");
|
|
|
|
if (reg.indexIn(versionString) > -1) return reg.cap(2);
|
|
|
|
}
|
2016-09-22 15:24:41 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// try to parse version of executable in resources
|
|
|
|
DWORD verHandle = NULL;
|
|
|
|
uint size = 0;
|
|
|
|
VS_FIXEDFILEINFO *verInfo = NULL;
|
|
|
|
|
|
|
|
// get size to be allocated
|
|
|
|
uint32_t verSize = GetFileVersionInfoSizeW(qToWide(path), &verHandle);
|
|
|
|
if (!verSize) return "";
|
|
|
|
|
|
|
|
// allocate buffer
|
|
|
|
QScopedArrayPointer<char> verData(new char[verSize]);
|
|
|
|
|
|
|
|
// get pointer on version structure
|
|
|
|
if (!GetFileVersionInfoW(qToWide(path), verHandle, verSize, &verData[0])) return "";
|
|
|
|
|
|
|
|
// get pointer on version
|
|
|
|
if (!VerQueryValueA(&verData[0], "\\", (void**)&verInfo, &size)) return "";
|
|
|
|
|
|
|
|
// check if version is right
|
|
|
|
if (size && verInfo && verInfo->dwSignature == 0xfeef04bd)
|
|
|
|
{
|
|
|
|
// Doesn't matter if you are on 32 bit or 64 bit,
|
|
|
|
// DWORD is always 32 bits, so first two revision numbers
|
|
|
|
// come from dwFileVersionMS, last two come from dwFileVersionLS
|
|
|
|
return QString("%1.%2.%3.%4")
|
|
|
|
.arg((verInfo->dwFileVersionMS >> 16) & 0xffff)
|
|
|
|
.arg((verInfo->dwFileVersionMS >> 0) & 0xffff)
|
|
|
|
.arg((verInfo->dwFileVersionLS >> 16) & 0xffff)
|
|
|
|
.arg((verInfo->dwFileVersionLS >> 0) & 0xffff);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-10-09 13:08:37 +00:00
|
|
|
bool writeResource(const QString &resource, const QString &path)
|
|
|
|
{
|
|
|
|
// all resources start with :/
|
|
|
|
if (!resource.startsWith(":/")) return false;
|
|
|
|
|
|
|
|
// open resource
|
|
|
|
QFile file(resource);
|
|
|
|
|
|
|
|
// unable to open it
|
|
|
|
if (!file.open(QFile::ReadOnly)) return false;
|
|
|
|
|
|
|
|
QByteArray data(file.readAll());
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// write file
|
|
|
|
file.setFileName(path);
|
|
|
|
|
|
|
|
// unable to write it
|
|
|
|
if (!file.open(QFile::WriteOnly)) return false;
|
|
|
|
|
|
|
|
// problem writting
|
|
|
|
if (file.write(data) != data.length()) return false;
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool writeResourceWithTemplates(const QString &resource, const QString &path, const QMap<QString, QString> &strings)
|
|
|
|
{
|
|
|
|
// all resources start with :/
|
|
|
|
if (!resource.startsWith(":/")) return false;
|
|
|
|
|
|
|
|
// open resource
|
|
|
|
QFile file(resource);
|
|
|
|
|
|
|
|
// unable to open it
|
|
|
|
if (!file.open(QFile::ReadOnly)) return false;
|
|
|
|
|
|
|
|
// data are UTF-8 text
|
|
|
|
QString data = QString::fromUtf8(file.readAll());
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// write file
|
|
|
|
file.setFileName(path);
|
|
|
|
|
|
|
|
// unable to write it
|
|
|
|
if (!file.open(QFile::WriteOnly)) return false;
|
|
|
|
|
|
|
|
// replace strings
|
|
|
|
QMap<QString, QString>::ConstIterator it = strings.begin(), iend = strings.end();
|
|
|
|
|
|
|
|
while (it != iend)
|
|
|
|
{
|
|
|
|
// replace variables with their value
|
|
|
|
data.replace("$" + it.key(), it.value());
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write
|
|
|
|
file.write(data.toUtf8());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-21 13:20:56 +00:00
|
|
|
CCOMHelper::CCOMHelper()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// to fix the bug with QFileDialog::getExistingDirectory hanging under Windows
|
|
|
|
m_mustUninit = SUCCEEDED(CoInitialize(NULL));
|
|
|
|
#else
|
|
|
|
m_mustUninit = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CCOMHelper::~CCOMHelper()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// only call CoUninitialize if CoInitialize succeeded
|
|
|
|
if (m_mustUninit) CoUninitialize();
|
|
|
|
#endif
|
|
|
|
}
|