From 528bca1008fa268fe200ac0dbad9ff06f9ea3c08 Mon Sep 17 00:00:00 2001 From: kh1 Date: Thu, 25 Oct 2012 12:52:01 +0200 Subject: Rewrite to use Qt functions to create the shortcut. Change-Id: Ic6dbc9bf228689c183385fba5ae600555442ba11 Reviewed-by: Tim Jenssen --- src/libs/installer/createshortcutoperation.cpp | 96 +++++++++++--------------- 1 file changed, 42 insertions(+), 54 deletions(-) (limited to 'src/libs/installer/createshortcutoperation.cpp') diff --git a/src/libs/installer/createshortcutoperation.cpp b/src/libs/installer/createshortcutoperation.cpp index a88b63fb6..769c99360 100644 --- a/src/libs/installer/createshortcutoperation.cpp +++ b/src/libs/installer/createshortcutoperation.cpp @@ -31,78 +31,66 @@ **************************************************************************/ #include "createshortcutoperation.h" -#include "errors.h" #include "fileutils.h" -#include "kdupdaterapplication.h" -#include "kdupdaterpackagesinfo.h" +#include +#include -#include -#include -#include - -#include #include +using namespace QInstaller; + #ifdef Q_OS_WIN -# include -# include -#endif +#include +#include -using namespace QInstaller; +struct DeCoInitializer +{ + DeCoInitializer() + : neededCoInit(CoInitialize(NULL) == S_OK) + { + } + ~DeCoInitializer() + { + if (neededCoInit) + CoUninitialize(); + } + bool neededCoInit; +}; +#endif -static bool createLink(QString fileName, QString linkName, QString workingDir, QString arguments = QString()) +static bool createLink(const QString &fileName, const QString &linkName, QString workingDir, + QString arguments = QString()) { + bool success = QFile::link(fileName, linkName); #ifdef Q_OS_WIN - bool ret = false; - fileName = QDir::toNativeSeparators(fileName); - linkName = QDir::toNativeSeparators(linkName); + if (!success) + return success; + if (workingDir.isEmpty()) workingDir = QFileInfo(fileName).absolutePath(); workingDir = QDir::toNativeSeparators(workingDir); - //### assume that they add .lnk + // CoInitialize cleanup object + DeCoInitializer _; - IShellLink *psl; - bool neededCoInit = false; + IShellLink *psl = NULL; + if (FAILED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl))) + return success; - HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl); + psl->SetPath((wchar_t *)QDir::toNativeSeparators(fileName).utf16()); + psl->SetWorkingDirectory((wchar_t *)workingDir.utf16()); + if (!arguments.isNull()) + psl->SetArguments((wchar_t*)arguments.utf16()); - if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized - neededCoInit = true; - CoInitialize(NULL); - hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl); + IPersistFile *ppf = NULL; + if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (void **)&ppf))) { + ppf->Save((wchar_t*)QDir::toNativeSeparators(linkName).utf16(), TRUE); + ppf->Release(); } - - if (SUCCEEDED(hres)) { - hres = psl->SetPath((wchar_t *)fileName.utf16()); - if (SUCCEEDED(hres) && !arguments.isNull()) - hres = psl->SetArguments((wchar_t*)arguments.utf16()); - if (SUCCEEDED(hres)) { - hres = psl->SetWorkingDirectory((wchar_t *)workingDir.utf16()); - if (SUCCEEDED(hres)) { - IPersistFile *ppf; - hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf); - if (SUCCEEDED(hres)) { - hres = ppf->Save((wchar_t*)linkName.utf16(), TRUE); - if (SUCCEEDED(hres)) - ret = true; - ppf->Release(); - } - } - } - psl->Release(); - } - - if (neededCoInit) - CoUninitialize(); - - return ret; -#else - Q_UNUSED(workingDir) - Q_UNUSED(arguments) - return QFile::link(fileName, linkName); + psl->Release(); #endif + return success; } /* @@ -198,7 +186,7 @@ bool CreateShortcutOperation::undoOperation() QStringList pathParts = QString(linkPath).remove(QDir::homePath()).split(QLatin1String("/")); for (int i = pathParts.count(); i > 0; --i) { QString possibleToDeleteDir = QDir::homePath() + QStringList(pathParts.mid(0, i)).join(QLatin1String("/")); - removeSystemGeneratedFiles(possibleToDeleteDir); + QInstaller::removeSystemGeneratedFiles(possibleToDeleteDir); if (!possibleToDeleteDir.isEmpty() && QDir().rmdir(possibleToDeleteDir)) qDebug() << "Deleted directory:" << possibleToDeleteDir; else -- cgit v1.2.3