• Skip to content
  • Skip to link menu
KDE 4.4 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 #include <kwindowsystem.h>
00024 
00025 #include <QTranslator>
00026 #include <QLocale>
00027 #include <QLibraryInfo>
00028 #include <QLibrary>
00029 
00030 #include <stdio.h>
00031 
00043 void KApplication_init_windows()
00044 {
00045     //QString qt_transl_file = ::locate( "locale", KGlobal::locale()->language()
00046     //  + "/LC_MESSAGES/qt_" + KGlobal::locale()->language() + ".qm" );
00047 
00048     QString qt_transl_file = QString("qt_") + QLocale::system().name();
00049     qt_transl_file.truncate(5);
00050     QTranslator *qt_transl = new QTranslator();
00051     if (qt_transl->load( qt_transl_file,
00052         QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
00053         qApp->installTranslator( qt_transl );
00054     else
00055         delete qt_transl;
00056 
00057     // For apps like KMail which have lots of open files, the default is too low
00058     // so increase it to the maximum.
00059     _setmaxstdio(2048);
00060 
00061 }
00062 
00063 // <copy of kdepim/libkdepim/utils.cpp, TODO: move to a shared helper library>
00064 
00065 #include <windows.h>
00066 #include <winperf.h>
00067 #include <psapi.h>
00068 #include <signal.h>
00069 #include <unistd.h>
00070 
00071 #include <QtCore/QList>
00072 #include <QtCore/QtDebug>
00073 
00074 static PPERF_OBJECT_TYPE FirstObject( PPERF_DATA_BLOCK PerfData )
00075 {
00076   return (PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength);
00077 }
00078 
00079 static PPERF_INSTANCE_DEFINITION FirstInstance( PPERF_OBJECT_TYPE PerfObj )
00080 {
00081   return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength);
00082 }
00083 
00084 static PPERF_OBJECT_TYPE NextObject( PPERF_OBJECT_TYPE PerfObj )
00085 {
00086   return (PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength);
00087 }
00088 
00089 static PPERF_COUNTER_DEFINITION FirstCounter( PPERF_OBJECT_TYPE PerfObj )
00090 {
00091   return (PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj + PerfObj->HeaderLength);
00092 }
00093 
00094 static PPERF_INSTANCE_DEFINITION NextInstance( PPERF_INSTANCE_DEFINITION PerfInst )
00095 {
00096   PPERF_COUNTER_BLOCK PerfCntrBlk
00097     = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength);
00098   return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk + PerfCntrBlk->ByteLength);
00099 }
00100 
00101 static PPERF_COUNTER_DEFINITION NextCounter( PPERF_COUNTER_DEFINITION PerfCntr )
00102 {
00103   return (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength);
00104 }
00105 
00106 static PPERF_COUNTER_BLOCK CounterBlock(PPERF_INSTANCE_DEFINITION PerfInst)
00107 {
00108   return (PPERF_COUNTER_BLOCK) ((LPBYTE) PerfInst + PerfInst->ByteLength);
00109 }
00110 
00111 #define GETPID_TOTAL 64 * 1024
00112 #define GETPID_BYTEINCREMENT 1024
00113 #define GETPID_PROCESS_OBJECT_INDEX 230
00114 #define GETPID_PROC_ID_COUNTER 784
00115 
00116 QString fromWChar(const wchar_t *string, int size = -1)
00117 {
00118   return (sizeof(wchar_t) == sizeof(QChar)) ? QString::fromUtf16((ushort *)string, size)
00119     : QString::fromUcs4((uint *)string, size);
00120 }
00121 
00122 void KApplication_getProcessesIdForName( const QString& processName, QList<int>& pids )
00123 {
00124   qDebug() << "KApplication_getProcessesIdForName" << processName;
00125   PPERF_OBJECT_TYPE perfObject;
00126   PPERF_INSTANCE_DEFINITION perfInstance;
00127   PPERF_COUNTER_DEFINITION perfCounter, curCounter;
00128   PPERF_COUNTER_BLOCK counterPtr;
00129   DWORD bufSize = GETPID_TOTAL;
00130   PPERF_DATA_BLOCK perfData = (PPERF_DATA_BLOCK) malloc( bufSize );
00131 
00132   char key[64];
00133   sprintf(key,"%d %d", GETPID_PROCESS_OBJECT_INDEX, GETPID_PROC_ID_COUNTER);
00134   LONG lRes;
00135   while( (lRes = RegQueryValueExA( HKEY_PERFORMANCE_DATA,
00136                                key,
00137                                NULL,
00138                                NULL,
00139                                (LPBYTE) perfData,
00140                                &bufSize )) == ERROR_MORE_DATA )
00141   {
00142     // get a buffer that is big enough
00143     bufSize += GETPID_BYTEINCREMENT;
00144     perfData = (PPERF_DATA_BLOCK) realloc( perfData, bufSize );
00145   }
00146 
00147   // Get the first object type.
00148   perfObject = FirstObject( perfData );
00149 
00150   // Process all objects.
00151   for( uint i = 0; i < perfData->NumObjectTypes; i++ ) {
00152     if (perfObject->ObjectNameTitleIndex != GETPID_PROCESS_OBJECT_INDEX) {
00153       perfObject = NextObject( perfObject );
00154       continue;
00155     }
00156     pids.clear();
00157     perfCounter = FirstCounter( perfObject );
00158     perfInstance = FirstInstance( perfObject );
00159     // retrieve the instances
00160     qDebug() << "INSTANCES: " << perfObject->NumInstances;
00161     for( int instance = 0; instance < perfObject->NumInstances; instance++ ) {
00162       curCounter = perfCounter;
00163       const QString foundProcessName(
00164         fromWChar( (wchar_t *)( (PBYTE)perfInstance + perfInstance->NameOffset ) ) );
00165       qDebug() << "foundProcessName: " << foundProcessName;
00166       if ( foundProcessName == processName ) {
00167         // retrieve the counters
00168         for( uint counter = 0; counter < perfObject->NumCounters; counter++ ) {
00169           if (curCounter->CounterNameTitleIndex == GETPID_PROC_ID_COUNTER) {
00170             counterPtr = CounterBlock(perfInstance);
00171             DWORD *value = (DWORD*)((LPBYTE) counterPtr + curCounter->CounterOffset);
00172             pids.append( int( *value ) );
00173             qDebug() << "found PID: " << int( *value );
00174             break;
00175           }
00176           curCounter = NextCounter( curCounter );
00177         }
00178       }
00179       perfInstance = NextInstance( perfInstance );
00180     }
00181   }
00182   free(perfData);
00183   RegCloseKey(HKEY_PERFORMANCE_DATA);
00184 }
00185 
00186 bool KApplication_otherProcessesExist( const QString& processName )
00187 {
00188   QList<int> pids;
00189   KApplication_getProcessesIdForName( processName, pids );
00190   int myPid = getpid();
00191   foreach ( int pid, pids ) {
00192     if (myPid != pid) {
00193 //      kDebug() << "Process ID is " << pid;
00194       return true;
00195     }
00196   }
00197   return false;
00198 }
00199 
00200 bool KApplication_killProcesses( const QString& processName )
00201 {
00202   QList<int> pids;
00203   KApplication_getProcessesIdForName( processName, pids );
00204   if ( pids.empty() )
00205     return true;
00206   qWarning() << "Killing process \"" << processName << " (pid=" << pids[0] << ")..";
00207   int overallResult = 0;
00208   foreach( int pid, pids ) {
00209     int result = kill( pid, SIGTERM );
00210     if ( result == 0 )
00211       continue;
00212     result = kill( pid, SIGKILL );
00213     if ( result != 0 )
00214       overallResult = result;
00215   }
00216   return overallResult == 0;
00217 }
00218 
00219 struct EnumWindowsStruct
00220 {
00221   EnumWindowsStruct() : windowId( 0 ) {}
00222   int pid;
00223   HWND windowId;
00224 };
00225 
00226 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
00227 {
00228   if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
00229     DWORD pidwin;
00230     GetWindowThreadProcessId(hwnd, &pidwin);
00231     if ( pidwin == ((EnumWindowsStruct*)lParam)->pid ) {
00232       ((EnumWindowsStruct*)lParam)->windowId = hwnd;
00233       return false;
00234     }
00235   }
00236   return true;
00237 }
00238 
00239 void KApplication_activateWindowForProcess( const QString& executableName )
00240 {
00241   QList<int> pids;
00242   KApplication_getProcessesIdForName( executableName, pids );
00243   int myPid = getpid();
00244   int foundPid = 0;
00245   foreach ( int pid, pids ) {
00246     if (myPid != pid) {
00247       qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
00248       foundPid = pid;
00249       break;
00250     }
00251   }
00252   if ( foundPid == 0 )
00253     return;
00254   EnumWindowsStruct winStruct;
00255   winStruct.pid = foundPid;
00256   EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
00257   if ( winStruct.windowId == NULL )
00258     return;
00259   KWindowSystem::forceActiveWindow( winStruct.windowId, 0 );
00260 }
00261 
00262 // </copy>
00263 
00264 // returns true if dbus patched for KDE is used
00265 bool KApplication_dbusIsPatched()
00266 {
00267 # ifdef __GNUC__
00268 #  define DBUSLIB_PREFIX "lib"
00269 # else
00270 #  define DBUSLIB_PREFIX ""
00271 # endif
00272 # ifdef _DEBUG
00273 #  define DBUSLIB_SUFFIX "d"
00274 # else
00275 #  define DBUSLIB_SUFFIX ""
00276 # endif
00277    QLibrary myLib(DBUSLIB_PREFIX "dbus-1" DBUSLIB_SUFFIX);
00278    return myLib.resolve("dbus_kde_patch");
00279 }

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
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  •     Sodep
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.9-20090814
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