Qt 4.8
Macros | Functions
qdesktopservices_win.cpp File Reference
#include <qsettings.h>
#include <qdir.h>
#include <private/qsystemlibrary_p.h>
#include <qurl.h>
#include <qstringlist.h>
#include <qprocess.h>
#include <qtemporaryfile.h>
#include <qcoreapplication.h>
#include <qt_windows.h>
#include <shlobj.h>
#include <qguifunctions_wince.h>

Go to the source code of this file.

Macros

#define CSIDL_MYMUSIC   13
 
#define CSIDL_MYVIDEO   14
 

Functions

static QString expandEnvStrings (const QString &command)
 
static bool launchWebBrowser (const QUrl &url)
 
static bool openDocument (const QUrl &file)
 

Macro Definition Documentation

◆ CSIDL_MYMUSIC

#define CSIDL_MYMUSIC   13

Definition at line 60 of file qdesktopservices_win.cpp.

Referenced by launchWebBrowser().

◆ CSIDL_MYVIDEO

#define CSIDL_MYVIDEO   14

Definition at line 61 of file qdesktopservices_win.cpp.

Referenced by launchWebBrowser().

Function Documentation

◆ expandEnvStrings()

static QString expandEnvStrings ( const QString command)
static

Definition at line 79 of file qdesktopservices_win.cpp.

Referenced by launchWebBrowser().

80 {
81 #if defined(Q_OS_WINCE)
82  return command;
83 #else
84  wchar_t buffer[MAX_PATH];
85  if (ExpandEnvironmentStrings((wchar_t*)command.utf16(), buffer, MAX_PATH))
86  return QString::fromWCharArray(buffer);
87  else
88  return command;
89 #endif
90 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ launchWebBrowser()

static bool launchWebBrowser ( const QUrl url)
static

Definition at line 92 of file qdesktopservices_win.cpp.

93 {
94  if (url.scheme() == QLatin1String("mailto")) {
95  //Retrieve the commandline for the default mail client
96  //the default key used below is the command line for the mailto: shell command
97  DWORD bufferSize = sizeof(wchar_t) * MAX_PATH;
98  long returnValue = -1;
99  QString command;
100 
101  HKEY handle;
102  LONG res;
103  wchar_t keyValue[MAX_PATH] = {0};
104  QString keyName(QLatin1String("mailto"));
105 
106  //Check if user has set preference, otherwise use default.
107  res = RegOpenKeyEx(HKEY_CURRENT_USER,
108  L"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\mailto\\UserChoice",
109  0, KEY_READ, &handle);
110  if (res == ERROR_SUCCESS) {
111  returnValue = RegQueryValueEx(handle, L"Progid", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
112  if (!returnValue)
113  keyName = QString::fromUtf16((const ushort*)keyValue);
114  RegCloseKey(handle);
115  }
116  keyName += QLatin1String("\\Shell\\Open\\Command");
117  res = RegOpenKeyExW(HKEY_CLASSES_ROOT, (const wchar_t*)keyName.utf16(), 0, KEY_READ, &handle);
118  if (res != ERROR_SUCCESS)
119  return false;
120 
121  bufferSize = sizeof(wchar_t) * MAX_PATH;
122  returnValue = RegQueryValueEx(handle, L"", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
123  if (!returnValue)
124  command = QString::fromRawData((QChar*)keyValue, bufferSize);
125  RegCloseKey(handle);
126 
127  if (returnValue)
128  return false;
129 
130  command = expandEnvStrings(command);
131  command = command.trimmed();
132  //Make sure the path for the process is in quotes
133  int index = -1 ;
134  if (command[0]!= QLatin1Char('\"')) {
135  index = command.indexOf(QLatin1String(".exe "), 0, Qt::CaseInsensitive);
136  command.insert(index+4, QLatin1Char('\"'));
137  command.insert(0, QLatin1Char('\"'));
138  }
139  //pass the url as the parameter
140  index = command.lastIndexOf(QLatin1String("%1"));
141  if (index != -1){
142  command.replace(index, 2, url.toString());
143  }
144  //start the process
145  PROCESS_INFORMATION pi;
146  ZeroMemory(&pi, sizeof(pi));
147  STARTUPINFO si;
148  ZeroMemory(&si, sizeof(si));
149  si.cb = sizeof(si);
150 
151  returnValue = CreateProcess(NULL, (wchar_t*)command.utf16(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
152 
153  if (!returnValue)
154  return false;
155 
156  CloseHandle(pi.hProcess);
157  CloseHandle(pi.hThread);
158  return true;
159  }
160 
161  if (!url.isValid())
162  return false;
163 
164  if (url.scheme().isEmpty())
165  return openDocument(url);
166 
167  quintptr returnValue = (quintptr)ShellExecute(0, 0, (wchar_t *)QString::fromUtf8(url.toEncoded().constData()).utf16(),
168  0, 0, SW_SHOWNORMAL);
169  return (returnValue > 32);
170 }
#define ShellExecute(a, b, c, d, e, f)
static QString keyName(const QString &rKey)
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
static bool openDocument(const QUrl &file)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static QString fromRawData(const QChar *, int size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
Definition: qstring.cpp:7673
QString trimmed() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end.
Definition: qstring.cpp:4506
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
unsigned short ushort
Definition: qglobal.h:995
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
quint16 index
static QString expandEnvStrings(const QString &command)
QString & insert(int i, QChar c)
Definition: qstring.cpp:1671
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static QString fromUtf16(const ushort *, int size=-1)
Returns a QString initialized with the first size characters of the Unicode string unicode (ISO-10646...
Definition: qstring.cpp:4329
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ openDocument()

static bool openDocument ( const QUrl file)
static

Definition at line 68 of file qdesktopservices_win.cpp.

Referenced by launchWebBrowser().

69 {
70  if (!file.isValid())
71  return false;
72  QString filePath = file.toLocalFile();
73  if (filePath.isEmpty())
74  filePath = file.toString();
75  quintptr returnValue = (quintptr)ShellExecute(0, 0, (wchar_t*)filePath.utf16(), 0, 0, SW_SHOWNORMAL);
76  return (returnValue > 32); //ShellExecute returns a value greater than 32 if successful
77 }
#define ShellExecute(a, b, c, d, e, f)
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition: qurl.cpp:6412
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290