Skip to content

Commit 035bbac

Browse files
committed
Use username instead of user id for single instance mode #1061
1 parent 8c685a9 commit 035bbac

15 files changed

+157
-29
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(KSNIP_SRCS
5555
${CMAKE_SOURCE_DIR}/src/common/provider/PathFromCaptureProvider.cpp
5656
${CMAKE_SOURCE_DIR}/src/common/provider/scaledSizeProvider/ScaledSizeProvider.cpp
5757
${CMAKE_SOURCE_DIR}/src/common/provider/TempFileProvider.cpp
58+
${CMAKE_SOURCE_DIR}/src/common/provider/UsernameProvider.cpp
5859
${CMAKE_SOURCE_DIR}/src/common/platform/HdpiScaler.cpp
5960
${CMAKE_SOURCE_DIR}/src/common/platform/PlatformChecker.cpp
6061
${CMAKE_SOURCE_DIR}/src/common/platform/CommandRunner.cpp

src/bootstrapper/BootstrapperFactory.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ QSharedPointer<IBootstrapper> BootstrapperFactory::create(DependencyInjector *de
2323
{
2424
auto logger = dependencyInjector->get<ILogger>();
2525
auto config = dependencyInjector->get<IConfig>();
26+
mInstanceLock = dependencyInjector->get<IInstanceLock>();
2627

2728
if(config->useSingleInstance()) {
28-
if (mInstanceLock.lock()) {
29+
if (mInstanceLock->lock()) {
2930
logger->log(QLatin1String("SingleInstance mode detected, we are the server"));
3031
return QSharedPointer<IBootstrapper>(new SingleInstanceServerBootstrapper(dependencyInjector));
3132
} else {

src/bootstrapper/BootstrapperFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BootstrapperFactory
3838
QSharedPointer<IBootstrapper> create(DependencyInjector *dependencyInjector);
3939

4040
private:
41-
InstanceLock mInstanceLock;
41+
QSharedPointer<IInstanceLock> mInstanceLock;
4242
};
4343

4444
#endif //KSNIP_BOOTSTRAPPERFACTORY_H
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301, USA.
18+
*/
19+
20+
#ifndef KSNIP_IINSTANCELOCK_H
21+
#define KSNIP_IINSTANCELOCK_H
22+
23+
class IInstanceLock : public QObject
24+
{
25+
public:
26+
IInstanceLock() = default;
27+
~IInstanceLock() override = default;
28+
29+
virtual bool lock() = 0;
30+
};
31+
32+
#endif //KSNIP_IINSTANCELOCK_H

src/bootstrapper/singleInstance/InstanceLock.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
#include "InstanceLock.h"
2121

22-
InstanceLock::InstanceLock()
22+
InstanceLock::InstanceLock(const QSharedPointer<IUsernameProvider> &usernameProvider)
2323
{
24-
QString key = QString("KsnipInstanceLock_%1").arg(getuid());
24+
auto key = SingleInstance::InstanceLockName.arg(usernameProvider->getUsername());
25+
2526
mSingular = new QSharedMemory(key, this);
2627
}
2728

@@ -45,12 +46,3 @@ bool InstanceLock::create()
4546

4647
return mSingular->create(1);
4748
}
48-
49-
bool InstanceLock::attachDetach()
50-
{
51-
if (mSingular->attach(QSharedMemory::ReadOnly)) {
52-
mSingular->detach();
53-
return true;
54-
}
55-
return false;
56-
}

src/bootstrapper/singleInstance/InstanceLock.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@
2020
#ifndef KSNIP_INSTANCELOCK_H
2121
#define KSNIP_INSTANCELOCK_H
2222

23+
#include <QSharedPointer>
2324
#include <QSharedMemory>
2425
#include <QCoreApplication>
2526

26-
#include <iostream>
27-
#include <unistd.h>
28-
#include <sys/types.h>
27+
#include "IInstanceLock.h"
28+
#include "SingleInstanceConstants.h"
29+
#include "src/common/provider/IUsernameProvider.h"
2930

30-
class InstanceLock : public QObject
31+
class InstanceLock : public IInstanceLock
3132
{
3233
Q_OBJECT
3334
public:
34-
InstanceLock();
35+
explicit InstanceLock(const QSharedPointer<IUsernameProvider> &usernameProvider);
3536
~InstanceLock() override;
3637

37-
bool lock();
38+
bool lock() override;
3839

3940
private:
4041
QSharedMemory *mSingular;
4142

4243
bool create();
43-
bool attachDetach();
4444
};
4545

4646

src/bootstrapper/singleInstance/SingleInstanceClientBootstrapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ SingleInstanceClientBootstrapper::SingleInstanceClientBootstrapper(DependencyInj
2424
mIpcClient(new IpcClient),
2525
mImageFromStdInputReader(dependencyInjector->get<IImageFromStdInputReader>())
2626
{
27-
mIpcClient->connectTo(SingleInstance::ServerName());
27+
auto usernameProvider = dependencyInjector->get<IUsernameProvider>();
28+
mIpcClient->connectTo(SingleInstance::ServerName.arg(usernameProvider->getUsername()));
2829
}
2930

3031
SingleInstanceClientBootstrapper::~SingleInstanceClientBootstrapper()

src/bootstrapper/singleInstance/SingleInstanceClientBootstrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "src/bootstrapper/singleInstance/SingleInstanceConstants.h"
2929
#include "src/bootstrapper/singleInstance/SingleInstanceParameterTranslator.h"
3030
#include "src/backend/ipc/IpcClient.h"
31+
#include "src/common/provider/IUsernameProvider.h"
3132

3233
class SingleInstanceClientBootstrapper : public StandAloneBootstrapper
3334
{

src/bootstrapper/singleInstance/SingleInstanceConstants.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@
2222

2323
#include <QString>
2424

25-
#include <unistd.h>
26-
#include <sys/types.h>
27-
2825
inline namespace SingleInstance {
2926

30-
static const QString ServerName() {
31-
return QString("org.ksnip.ksnip.singleInstanceServer_%1").arg(getuid());
32-
}
27+
const QString ServerName(QStringLiteral("org.ksnip.ksnip.singleInstanceServer_%1"));
28+
29+
const QString InstanceLockName(QStringLiteral("KsnipInstanceLock_%1"));
3330

3431
} // namespace SingleInstance
3532

src/bootstrapper/singleInstance/SingleInstanceServerBootstrapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
SingleInstanceServerBootstrapper::SingleInstanceServerBootstrapper(DependencyInjector *dependencyInjector) :
2323
StandAloneBootstrapper(dependencyInjector),
24-
mIpcServer(new IpcServer)
24+
mIpcServer(new IpcServer),
25+
mUsernameProvider(dependencyInjector->get<IUsernameProvider>())
2526
{
2627
}
2728

@@ -38,7 +39,7 @@ int SingleInstanceServerBootstrapper::start(const QApplication &app)
3839

3940
void SingleInstanceServerBootstrapper::startServer() const
4041
{
41-
mIpcServer->listen(SingleInstance::ServerName());
42+
mIpcServer->listen(SingleInstance::ServerName.arg(mUsernameProvider->getUsername()));
4243
connect(mIpcServer, &IpcServer::received, this, &SingleInstanceServerBootstrapper::processData);
4344
}
4445

0 commit comments

Comments
 (0)