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

KDECore

klockfile_win.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (c) 2007 Ralf Habacker <ralf.habacker@freenet.de>
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 "klockfile.h"
00021 #include "kcomponentdata.h"
00022 #include "kdebug.h"
00023 
00024 #include <windows.h>
00025 
00031 class KLockFile::Private
00032 {
00033 public:
00034     Private(const KComponentData &c)
00035         : componentData(c)
00036     {
00037     }
00038 
00039     QString file;
00040     bool isLocked;
00041     int staleTime;
00042     KComponentData componentData;
00043     HANDLE h;
00044 };
00045 
00046 KLockFile::KLockFile(const QString &file, const KComponentData &componentData)
00047     : d(new Private(componentData))
00048 {
00049     d->file = file;
00050     d->isLocked = false;
00051     d->staleTime = 0;
00052 }
00053 
00054 KLockFile::~KLockFile()
00055 {
00056     unlock();
00057     delete d;
00058 }
00059 
00060 int
00061 KLockFile::staleTime() const
00062 {
00063   return d->staleTime;
00064 }
00065 
00066 
00067 void
00068 KLockFile::setStaleTime(int _staleTime)
00069 {
00070   d->staleTime = _staleTime;
00071 }
00072 
00073 KLockFile::LockResult 
00074 KLockFile::lock(LockFlags options)
00075 {
00076     if (d->isLocked)
00077         return LockOK;
00078 
00079     LockResult result;
00080 
00081     d->h = CreateFileW(
00082                 (WCHAR *)d->file.utf16(),
00083                 GENERIC_READ | GENERIC_WRITE,
00084                 0,
00085                 0,
00086                 CREATE_ALWAYS,
00087                 FILE_ATTRIBUTE_NORMAL,
00088                 0
00089            );
00090     if (!d->h)
00091         result = LockError;
00092 
00093     else if (GetLastError() == NO_ERROR) 
00094     {
00095 //        kDebug(151) << "'" << d->file << "' locked";
00096         result = LockOK;
00097     }
00098     else if (GetLastError() == ERROR_ALREADY_EXISTS) 
00099     {
00100         // handle stale lock file 
00101         //kDebug(151) << "stale lock file '" << d->file << "' found, reused file";
00102         // we reuse this file
00103         result = LockOK;
00104     }
00105     else if (GetLastError() == ERROR_SHARING_VIOLATION) 
00106     {
00107         CloseHandle(d->h);
00108         d->h = 0;
00109         //kDebug(151) << "could not lock file '" << d->file << "' it is locked by another process";
00110         result = LockFail;
00111     }
00112     else {
00113         //kDebug(151) << "could not lock '" << d->file << "' error= " << GetLastError();
00114         result = LockError;
00115     }
00116     
00117     if (result == LockOK)
00118         d->isLocked = true;
00119     return result;
00120 }
00121 
00122 bool 
00123 KLockFile::isLocked() const
00124 {
00125     return d->isLocked;
00126 }
00127 
00128 void 
00129 KLockFile::unlock()
00130 {
00131     if (d->isLocked)
00132     {
00133          //kDebug(151) << "lock removed for file '" << d->file << "'";
00134          CloseHandle(d->h);
00135          DeleteFileW((WCHAR *)d->file.utf16());
00136          d->h = 0;
00137          d->isLocked = false;
00138     }
00139 }
00140 
00141 bool 
00142 KLockFile::getLockInfo(int &pid, QString &hostname, QString &appname)
00143 {
00144   return false;
00145 }

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