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

KDEUI

kapplication_win.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (C) 2004-2008 Jarosław Staniek <staniek@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <QtGui/QApplication>
00021 #include <kstandarddirs.h>
00022 #include <klocale.h>
00023 
00024 #include <QTranslator>
00025 #include <QLocale>
00026 #include <QLibraryInfo>
00027 #include <QLibrary>
00028 
00038 void KApplication_init_windows()
00039 {
00040     //QString qt_transl_file = ::locate( "locale", KGlobal::locale()->language()
00041     //  + "/LC_MESSAGES/qt_" + KGlobal::locale()->language() + ".qm" );
00042 
00043     QString qt_transl_file = QString("qt_") + QLocale::system().name();
00044     qt_transl_file.truncate(5);
00045     QTranslator *qt_transl = new QTranslator();
00046     if (qt_transl->load( qt_transl_file, 
00047         QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
00048         qApp->installTranslator( qt_transl );
00049     else
00050         delete qt_transl;
00051 }
00052 
00053 // <copy of kdepim/libkdepim/utils.cpp, TODO: move to a shared helper library>
00054 
00055 #include <windows.h>
00056 #include <winperf.h>
00057 #include <psapi.h>
00058 #include <signal.h>
00059 #include <unistd.h>
00060 
00061 #include <QtCore/QList>
00062 #include <QtCore/QtDebug>
00063 
00064 static PPERF_OBJECT_TYPE FirstObject( PPERF_DATA_BLOCK PerfData )
00065 {
00066   return (PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength);
00067 }
00068 
00069 static PPERF_INSTANCE_DEFINITION FirstInstance( PPERF_OBJECT_TYPE PerfObj )
00070 {
00071   return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength);
00072 }
00073 
00074 static PPERF_OBJECT_TYPE NextObject( PPERF_OBJECT_TYPE PerfObj )
00075 {
00076   return (PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength);
00077 }
00078 
00079 static PPERF_COUNTER_DEFINITION FirstCounter( PPERF_OBJECT_TYPE PerfObj )
00080 {
00081   return (PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj + PerfObj->HeaderLength);
00082 }
00083 
00084 static PPERF_INSTANCE_DEFINITION NextInstance( PPERF_INSTANCE_DEFINITION PerfInst )
00085 {
00086   PPERF_COUNTER_BLOCK PerfCntrBlk 
00087     = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength);
00088   return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk + PerfCntrBlk->ByteLength);
00089 }
00090 
00091 static PPERF_COUNTER_DEFINITION NextCounter( PPERF_COUNTER_DEFINITION PerfCntr )
00092 {
00093   return (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength);
00094 }
00095 
00096 static PPERF_COUNTER_BLOCK CounterBlock(PPERF_INSTANCE_DEFINITION PerfInst)
00097 {
00098   return (PPERF_COUNTER_BLOCK) ((LPBYTE) PerfInst + PerfInst->ByteLength);
00099 }
00100 
00101 #define GETPID_TOTAL 64 * 1024
00102 #define GETPID_BYTEINCREMENT 1024
00103 #define GETPID_PROCESS_OBJECT_INDEX 230
00104 #define GETPID_PROC_ID_COUNTER 784
00105 
00106 QString fromWChar(const wchar_t *string, int size = -1)
00107 {
00108   return (sizeof(wchar_t) == sizeof(QChar)) ? QString::fromUtf16((ushort *)string, size)
00109     : QString::fromUcs4((uint *)string, size);
00110 }
00111 
00112 void KApplication_getProcessesIdForName( const QString& processName, QList<int>& pids )
00113 {
00114   qDebug() << "KApplication_getProcessesIdForName" << processName;
00115   PPERF_OBJECT_TYPE perfObject;
00116   PPERF_INSTANCE_DEFINITION perfInstance;
00117   PPERF_COUNTER_DEFINITION perfCounter, curCounter;
00118   PPERF_COUNTER_BLOCK counterPtr;
00119   DWORD bufSize = GETPID_TOTAL;
00120   PPERF_DATA_BLOCK perfData = (PPERF_DATA_BLOCK) malloc( bufSize );
00121 
00122   char key[64];
00123   sprintf(key,"%d %d", GETPID_PROCESS_OBJECT_INDEX, GETPID_PROC_ID_COUNTER);
00124   LONG lRes;
00125   while( (lRes = RegQueryValueExA( HKEY_PERFORMANCE_DATA,
00126                                key,
00127                                NULL,
00128                                NULL,
00129                                (LPBYTE) perfData,
00130                                &bufSize )) == ERROR_MORE_DATA )
00131   {
00132     // get a buffer that is big enough
00133     bufSize += GETPID_BYTEINCREMENT;
00134     perfData = (PPERF_DATA_BLOCK) realloc( perfData, bufSize );
00135   }
00136 
00137   // Get the first object type.
00138   perfObject = FirstObject( perfData );
00139 
00140   // Process all objects.
00141   for( uint i = 0; i < perfData->NumObjectTypes; i++ ) {
00142     if (perfObject->ObjectNameTitleIndex != GETPID_PROCESS_OBJECT_INDEX) {
00143       perfObject = NextObject( perfObject );
00144       continue;
00145     }
00146     pids.clear();
00147     perfCounter = FirstCounter( perfObject );
00148     perfInstance = FirstInstance( perfObject );
00149     // retrieve the instances
00150     qDebug() << "INSTANCES: " << perfObject->NumInstances;
00151     for( int instance = 0; instance < perfObject->NumInstances; instance++ ) {
00152       curCounter = perfCounter;
00153       const QString foundProcessName( 
00154         fromWChar( (wchar_t *)( (PBYTE)perfInstance + perfInstance->NameOffset ) ) );
00155       qDebug() << "foundProcessName: " << foundProcessName;
00156       if ( foundProcessName == processName ) {
00157         // retrieve the counters
00158         for( uint counter = 0; counter < perfObject->NumCounters; counter++ ) {
00159           if (curCounter->CounterNameTitleIndex == GETPID_PROC_ID_COUNTER) {
00160             counterPtr = CounterBlock(perfInstance);
00161             DWORD *value = (DWORD*)((LPBYTE) counterPtr + curCounter->CounterOffset);
00162             pids.append( int( *value ) );
00163             qDebug() << "found PID: " << int( *value );
00164             break;
00165           }
00166           curCounter = NextCounter( curCounter );
00167         }
00168       }
00169       perfInstance = NextInstance( perfInstance );
00170     }
00171   }
00172   free(perfData);
00173   RegCloseKey(HKEY_PERFORMANCE_DATA);
00174 }
00175 
00176 bool KApplication_otherProcessesExist( const QString& processName )
00177 {
00178   QList<int> pids;
00179   KApplication_getProcessesIdForName( processName, pids );
00180   int myPid = getpid();
00181   foreach ( int pid, pids ) {
00182     if (myPid != pid) {
00183 //      kDebug() << "Process ID is " << pid;
00184       return true;
00185     }
00186   }
00187   return false;
00188 }
00189 
00190 bool KApplication_killProcesses( const QString& processName )
00191 {
00192   QList<int> pids;
00193   KApplication_getProcessesIdForName( processName, pids );
00194   if ( pids.empty() )
00195     return true;
00196   qWarning() << "Killing process \"" << processName << " (pid=" << pids[0] << ")..";
00197   int overallResult = 0;
00198   foreach( int pid, pids ) {
00199     int result = kill( pid, SIGTERM );
00200     if ( result == 0 )
00201       continue;
00202     result = kill( pid, SIGKILL );
00203     if ( result != 0 )
00204       overallResult = result;
00205   }
00206   return overallResult == 0;
00207 }
00208 
00209 struct EnumWindowsStruct
00210 {
00211   EnumWindowsStruct() : windowId( 0 ) {}
00212   int pid;
00213   HWND windowId;
00214 };
00215 
00216 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
00217 {
00218   if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
00219     DWORD pidwin;
00220     GetWindowThreadProcessId(hwnd, &pidwin);
00221     if ( pidwin == ((EnumWindowsStruct*)lParam)->pid ) {
00222       ((EnumWindowsStruct*)lParam)->windowId = hwnd;
00223       return false;
00224     }
00225   }
00226   return true;
00227 }
00228 
00229 void KApplication_activateWindowForProcess( const QString& executableName )
00230 {
00231   QList<int> pids;
00232   KApplication_getProcessesIdForName( executableName, pids );
00233   int myPid = getpid();
00234   int foundPid = 0;
00235   foreach ( int pid, pids ) {
00236     if (myPid != pid) {
00237       qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
00238       foundPid = pid;
00239       break;
00240     }
00241   }
00242   if ( foundPid == 0 )
00243     return;
00244   EnumWindowsStruct winStruct;
00245   winStruct.pid = foundPid;
00246   EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
00247   if ( winStruct.windowId == NULL )
00248     return;
00249   SetForegroundWindow( winStruct.windowId );
00250 }
00251 
00252 // </copy>
00253 
00254 // returns true if dbus patched for KDE is used
00255 bool KApplication_dbusIsPatched()
00256 {
00257 # ifdef __GNUC__
00258 #  define DBUSLIB_PREFIX "lib"
00259 # else
00260 #  define DBUSLIB_PREFIX ""
00261 # endif
00262 # ifdef _DEBUG
00263 #  define DBUSLIB_SUFFIX "d"
00264 # else
00265 #  define DBUSLIB_SUFFIX ""
00266 # endif
00267    QLibrary myLib(DBUSLIB_PREFIX "dbus-1" DBUSLIB_SUFFIX);
00268    return myLib.resolve("dbus_kde_patch");
00269 }

KDEUI

Skip menu "KDEUI"
  • 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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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