Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Edric Milaret | 5f316da | 2015-09-28 11:57:42 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015 by Savoir-faire Linux * |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "utils.h" |
| 20 | |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 21 | #ifdef Q_OS_WIN32 |
| 22 | #include <lmcons.h> |
| 23 | #include <shobjidl.h> |
| 24 | #include <shlguid.h> |
| 25 | #include <shlobj.h> |
| 26 | #include <shlwapi.h> |
| 27 | #endif |
| 28 | |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 29 | bool |
Edric Milaret | 1eca029 | 2015-06-29 12:03:36 -0400 | [diff] [blame] | 30 | Utils::CreateStartupLink() |
| 31 | { |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 32 | #ifdef Q_OS_WIN32 |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 33 | TCHAR szPath[MAX_PATH]; |
| 34 | GetModuleFileName(NULL, szPath, MAX_PATH); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 35 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 36 | std::wstring programPath(szPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 37 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 38 | TCHAR startupPath[MAX_PATH]; |
| 39 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
| 40 | |
| 41 | std::wstring linkPath(startupPath); |
| 42 | linkPath += TEXT("\\Ring.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 43 | |
| 44 | return Utils::CreateLink(programPath.c_str(), linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 45 | #else |
| 46 | return true; |
| 47 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool |
| 51 | Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) { |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 52 | #ifdef Q_OS_WIN32 |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 53 | HRESULT hres; |
| 54 | IShellLink* psl; |
| 55 | |
| 56 | hres = CoCreateInstance(CLSID_ShellLink, NULL, |
| 57 | CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); |
| 58 | if (SUCCEEDED(hres)) |
| 59 | { |
| 60 | IPersistFile* ppf; |
| 61 | psl->SetPath(lpszPathObj); |
Edric Milaret | 90bd5a8 | 2015-06-08 10:07:59 -0400 | [diff] [blame] | 62 | psl->SetArguments(TEXT("--minimized")); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 63 | |
| 64 | hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); |
| 65 | if (SUCCEEDED(hres)) |
| 66 | { |
| 67 | hres = ppf->Save(lpszPathLink, TRUE); |
| 68 | ppf->Release(); |
| 69 | } |
| 70 | psl->Release(); |
| 71 | } |
| 72 | return hres; |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 73 | #else |
| 74 | return true; |
| 75 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
| 79 | Utils::DeleteStartupLink() { |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 80 | #ifdef Q_OS_WIN32 |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 81 | TCHAR startupPath[MAX_PATH]; |
| 82 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 83 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 84 | std::wstring linkPath(startupPath); |
| 85 | linkPath += TEXT("\\Ring.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 86 | |
| 87 | DeleteFile(linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 88 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool |
| 92 | Utils::CheckStartupLink() { |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 93 | #ifdef Q_OS_WIN32 |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 94 | TCHAR startupPath[MAX_PATH]; |
| 95 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 96 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 97 | std::wstring linkPath(startupPath); |
| 98 | linkPath += TEXT("\\Ring.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 99 | return PathFileExists(linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 100 | #else |
| 101 | return true; |
| 102 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 105 | QString |
| 106 | Utils::GetRingtonePath() { |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 107 | #ifdef Q_OS_WIN32 |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 108 | TCHAR workingDirectory[MAX_PATH]; |
| 109 | GetCurrentDirectory(MAX_PATH, workingDirectory); |
| 110 | |
| 111 | QString ringtonePath = QString::fromWCharArray(workingDirectory); |
| 112 | ringtonePath += "\\ringtones\\konga.ul"; |
| 113 | |
| 114 | return ringtonePath; |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 115 | #else |
| 116 | return QString("/usr/local"); |
| 117 | #endif |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 120 | QString |
| 121 | Utils::GenGUID() { |
| 122 | #ifdef Q_OS_WIN32 |
| 123 | GUID gidReference; |
| 124 | wchar_t *str; |
| 125 | HRESULT hCreateGuid = CoCreateGuid(&gidReference); |
| 126 | if (hCreateGuid == S_OK) { |
| 127 | StringFromCLSID(gidReference, &str); |
| 128 | auto gStr = QString::fromWCharArray(str); |
| 129 | return gStr.remove("{").remove("}").toLower(); |
| 130 | } |
| 131 | else |
| 132 | return QString(""); |
| 133 | #else |
| 134 | return QString(""); |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | QString |
| 139 | Utils::GetISODate() { |
| 140 | #ifdef Q_OS_WIN32 |
| 141 | SYSTEMTIME lt; |
| 142 | GetSystemTime(<); |
| 143 | return QString("%1-%2-%3T%4:%5:%6Z").arg(lt.wYear).arg(lt.wMonth,2,10,QChar('0')).arg(lt.wDay,2,10,QChar('0')) |
| 144 | .arg(lt.wHour,2,10,QChar('0')).arg(lt.wMinute,2,10,QChar('0')).arg(lt.wSecond,2,10,QChar('0')); |
| 145 | #else |
| 146 | return QString(""); |
| 147 | #endif |
| 148 | } |
| 149 | |