• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kkernel_win.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00004    Copyright (C) 2007 Christian Ehrlicher <ch.ehrlicher@gmx.de>
00005    Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License version 2 as published by the Free Software Foundation.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
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     //the path is C:\some\path\kde4\bin\kdecore.dll
00062     GetModuleFileNameW(kdecoreDllInstance, kde4prefixUtf16, MAX_PATH + 1);
00063     int bs1 = 0, bs2 = 0;
00064 
00065     //we convert \ to / and remove \bin\kdecore.dll from the string
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 // can't use QCoreApplication::applicationDirPath() because sometimes we
00081 // don't have an instantiated QCoreApplication
00082 QString getKde4Prefix()
00083 {
00084   // we can get called after DLL_PROCESS_DETACH!
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         /* msdn:
00103            When handling DLL_PROCESS_DETACH, a DLL should free resources such
00104            as heap memory only if the DLL is being unloaded dynamically (the
00105            lpReserved parameter is NULL). If the process is terminating (the
00106            lpvReserved parameter is non-NULL), all threads in the process except
00107            the current thread either have exited already or have been explicitly
00108            terminated by a call to the ExitProcess function, which might leave
00109            some process resources such as heaps in an inconsistent state. In this
00110            case, it is not safe for the DLL to clean up the resources. Instead,
00111            the DLL should allow the operating system to reclaim the memory.
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 // note: QLocale().name().left(2).toLatin1() returns the same
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             //abort();
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         //abort();
00250     }
00251 #endif
00252 }
00253 
00254 #endif  // Q_OS_WIN

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal