00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kkernel_win.h"
00023
00024 #include <config.h>
00025 #include <QtCore/QBool>
00026 #include <QtCore/QTextCodec>
00027
00028 #ifdef Q_OS_WIN
00029
00030 #include "kglobal.h"
00031 #include <klocale.h>
00032
00033 #include <QtCore/QDir>
00034 #include <QtCore/QString>
00035
00036 #include <windows.h>
00037 #include <shellapi.h>
00038 #include <process.h>
00039
00040 #if defined(__MINGW32__)
00041 # define WIN32_CAST_CHAR (const WCHAR*)
00042 #else
00043 # define WIN32_CAST_CHAR (LPCWSTR)
00044 #endif
00045
00046 static void kMessageOutput(QtMsgType type, const char *msg);
00047
00048 static class kMessageOutputInstaller {
00049 public:
00050 kMessageOutputInstaller() {
00051 qInstallMsgHandler(kMessageOutput);
00052 }
00053 } kMessageOutputInstallerInstance;
00054
00055 static HINSTANCE kdecoreDllInstance = NULL;
00056 static wchar_t kde4prefixUtf16[MAX_PATH + 2];
00057 static QString *kde4Prefix = NULL;
00058
00059 void initKde4prefixUtf16()
00060 {
00061
00062 GetModuleFileNameW(kdecoreDllInstance, kde4prefixUtf16, MAX_PATH + 1);
00063 int bs1 = 0, bs2 = 0;
00064
00065
00066 int pos;
00067 for (pos = 0; pos < MAX_PATH + 1 && kde4prefixUtf16[pos] != 0; ++pos) {
00068 if (kde4prefixUtf16[pos] == '\\') {
00069 bs1 = bs2;
00070 bs2 = pos;
00071 kde4prefixUtf16[pos] = '/';
00072 }
00073 }
00074 Q_ASSERT(bs1);
00075 Q_ASSERT(pos < MAX_PATH + 1);
00076 kde4prefixUtf16[bs1] = '/';
00077 kde4prefixUtf16[bs1+1] = 0;
00078 }
00079
00080
00081
00082 QString getKde4Prefix()
00083 {
00084
00085 return kde4Prefix ? *kde4Prefix : QString::fromUtf16((ushort*) kde4prefixUtf16);
00086 }
00087
00092 extern "C"
00093 BOOL WINAPI DllMain ( HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpReserved)
00094 {
00095 switch ( fdwReason ) {
00096 case DLL_PROCESS_ATTACH:
00097 kdecoreDllInstance = hinstDLL;
00098 initKde4prefixUtf16();
00099 kde4Prefix = new QString( QString::fromUtf16((ushort*) kde4prefixUtf16) );
00100 break;
00101 case DLL_PROCESS_DETACH:
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 if( lpReserved == NULL )
00114 delete kde4Prefix;
00115 kde4Prefix = 0;
00116 break;
00117 default:
00118 break;
00119 }
00120 return true;
00121 }
00122
00133 QString getWin32RegistryValue ( HKEY key, const QString& subKey, const QString& item, bool *ok = 0 )
00134 {
00135 #define FAILURE \
00136 { if (ok) \
00137 *ok = false; \
00138 return QString(); }
00139
00140 if ( subKey.isEmpty() )
00141 FAILURE;
00142 HKEY hKey;
00143 TCHAR *lszValue;
00144 DWORD dwType=REG_SZ;
00145 DWORD dwSize;
00146
00147 if ( ERROR_SUCCESS!=RegOpenKeyExW ( key, WIN32_CAST_CHAR subKey.utf16(), 0, KEY_READ, &hKey ) )
00148 FAILURE;
00149
00150 if ( ERROR_SUCCESS!=RegQueryValueExW ( hKey, WIN32_CAST_CHAR item.utf16(), NULL, NULL, NULL, &dwSize ) )
00151 FAILURE;
00152
00153 lszValue = new TCHAR[dwSize];
00154
00155 if ( ERROR_SUCCESS!=RegQueryValueExW ( hKey, WIN32_CAST_CHAR item.utf16(), NULL, &dwType, ( LPBYTE ) lszValue, &dwSize ) ) {
00156 delete [] lszValue;
00157 FAILURE;
00158 }
00159 RegCloseKey ( hKey );
00160
00161 QString res = QString::fromUtf16 ( ( const ushort* ) lszValue );
00162 delete [] lszValue;
00163 return res;
00164 }
00165
00166
00167 bool showWin32FilePropertyDialog ( const QString& fileName )
00168 {
00169 QString path_ = QDir::convertSeparators ( QFileInfo ( fileName ).absoluteFilePath() );
00170
00171 SHELLEXECUTEINFOW execInfo;
00172 memset ( &execInfo,0,sizeof ( execInfo ) );
00173 execInfo.cbSize = sizeof ( execInfo );
00174 execInfo.fMask = SEE_MASK_INVOKEIDLIST | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
00175 const QString verb ( QLatin1String ( "properties" ) );
00176 execInfo.lpVerb = WIN32_CAST_CHAR verb.utf16();
00177 execInfo.lpFile = WIN32_CAST_CHAR path_.utf16();
00178 return ShellExecuteExW ( &execInfo );
00179 }
00180
00181
00182
00183 QByteArray getWin32LocaleName()
00184 {
00185 bool ok;
00186 QString localeNumber = getWin32RegistryValue ( HKEY_CURRENT_USER,
00187 QLatin1String("Control Panel\\International"),
00188 "Locale", &ok );
00189 if ( !ok )
00190 return QByteArray();
00191 QString localeName = getWin32RegistryValue ( HKEY_LOCAL_MACHINE,
00192 QLatin1String("SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout\\DosKeybCodes"),
00193 localeNumber, &ok );
00194 if ( !ok )
00195 return QByteArray();
00196 return localeName.toLatin1();
00197 }
00198
00202 QString getWin32ShellFoldersPath ( const QString& folder )
00203 {
00204 return getWin32RegistryValue ( HKEY_CURRENT_USER,
00205 QLatin1String("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"),
00206 folder );
00207 }
00208
00209 void kMessageOutput(QtMsgType type, const char *msg)
00210 {
00211 #if 1
00212 int BUFSIZE=4096;
00213 char *buf = new char[BUFSIZE];
00214 switch (type) {
00215 case QtDebugMsg:
00216 strlcpy(buf,"Debug:",BUFSIZE);
00217 strlcat(buf,msg,BUFSIZE);
00218 break;
00219 case QtWarningMsg:
00220 strlcpy(buf,"Warning:",BUFSIZE);
00221 strlcat(buf,msg,BUFSIZE);
00222 break;
00223 case QtCriticalMsg:
00224 strlcpy(buf,"Critical:",BUFSIZE);
00225 strlcat(buf,msg,BUFSIZE);
00226 break;
00227 case QtFatalMsg:
00228 strlcpy(buf,"Fatal:",BUFSIZE);
00229 strlcat(buf,msg,BUFSIZE);
00230
00231 break;
00232 }
00233 strlcat(buf,"\n",BUFSIZE);
00234 OutputDebugStringA(buf);
00235 delete[] buf;
00236 #else
00237 switch (type) {
00238 case QtDebugMsg:
00239 fprintf(stderr, "Debug: %s\n", msg);
00240 break;
00241 case QtWarningMsg:
00242 fprintf(stderr, "Warning: %s\n", msg);
00243 break;
00244 case QtCriticalMsg:
00245 fprintf(stderr, "Critical: %s\n", msg);
00246 break;
00247 case QtFatalMsg:
00248 fprintf(stderr, "Fatal: %s\n", msg);
00249
00250 }
00251 #endif
00252 }
00253
00254 #endif // Q_OS_WIN