Commit 17d4dd38 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Clean up KateRunningInstanceInfo



- No manual memory management
Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 7979144c
......@@ -13,8 +13,6 @@
#include <QRegularExpression>
#include <QStringList>
int KateRunningInstanceInfo::dummy_session = 0;
bool fillinRunningKateAppInstances(KateRunningInstanceMap *map)
{
QDBusConnectionInterface *i = QDBusConnection::sessionBus().interface();
......@@ -35,25 +33,16 @@ bool fillinRunningKateAppInstances(KateRunningInstanceMap *map)
for (const QString &s : qAsConst(services)) {
if (s.startsWith(QLatin1String("org.kde.kate")) && !s.endsWith(my_pid)) {
KateRunningInstanceInfo *rii = new KateRunningInstanceInfo(s);
if (rii->valid) {
if (map->contains(rii->sessionName)) {
KateRunningInstanceInfo rii(s);
if (rii.valid) {
if (map->find(rii.sessionName) != map->end()) {
return false; // ERROR no two instances may have the same session name
}
map->insert(rii->sessionName, rii);
auto sessionName = rii.sessionName;
map->emplace(sessionName, std::move(rii));
// std::cerr<<qPrintable(s)<<"running instance:"<< rii->sessionName.toUtf8().data()<<std::endl;
} else {
delete rii;
}
}
}
return true;
}
void cleanupRunningKateAppInstanceMap(KateRunningInstanceMap *map)
{
for (KateRunningInstanceMap::const_iterator it = map->constBegin(); it != map->constEnd(); ++it) {
delete it.value();
}
map->clear();
}
......@@ -13,21 +13,20 @@
#include <QMap>
#include <QVariant>
#include <iostream>
#include <memory>
class KateRunningInstanceInfo : public QObject
class KateRunningInstanceInfo
{
Q_OBJECT
public:
KateRunningInstanceInfo() = default;
KateRunningInstanceInfo(const QString &serviceName_)
: QObject()
, valid(false)
: valid(false)
, serviceName(serviceName_)
, dbus_if(new QDBusInterface(serviceName_,
QStringLiteral("/MainApplication"),
QString(), // I don't know why it does not work if I specify org.kde.Kate.Application here
QDBusConnection::sessionBus(),
this))
QDBusConnection::sessionBus()))
{
if (!dbus_if->isValid()) {
std::cerr << qPrintable(QDBusConnection::sessionBus().lastError().message()) << std::endl;
......@@ -48,22 +47,18 @@ public:
valid = true;
}
}
~KateRunningInstanceInfo() override
{
delete dbus_if;
}
bool valid;
bool valid = false;
const QString serviceName;
QDBusInterface *dbus_if;
std::unique_ptr<QDBusInterface> dbus_if;
QString sessionName;
private:
static int dummy_session;
static inline int dummy_session = 0;
};
typedef QMap<QString, KateRunningInstanceInfo *> KateRunningInstanceMap;
typedef std::map<QString, KateRunningInstanceInfo> KateRunningInstanceMap;
Q_DECL_EXPORT bool fillinRunningKateAppInstances(KateRunningInstanceMap *map);
Q_DECL_EXPORT void cleanupRunningKateAppInstanceMap(KateRunningInstanceMap *map);
#endif
......@@ -340,8 +340,9 @@ int main(int argc, char **argv)
}
QStringList kateServices;
for (KateRunningInstanceMap::const_iterator it = mapSessionRii.constBegin(); it != mapSessionRii.constEnd(); ++it) {
QString serviceName = (*it)->serviceName;
for (const auto &[_, katerunninginstanceinfo] : mapSessionRii) {
Q_UNUSED(_)
QString serviceName = katerunninginstanceinfo.serviceName;
if (currentActivity.length() != 0) {
QDBusMessage m = QDBusMessage::createMethodCall(serviceName,
......@@ -381,16 +382,14 @@ int main(int argc, char **argv)
force_new = true;
} else if (parser.isSet(startSessionOption)) {
start_session = parser.value(startSessionOption);
if (mapSessionRii.contains(start_session)) {
serviceName = mapSessionRii[start_session]->serviceName;
auto it = mapSessionRii.find(start_session);
if (it != mapSessionRii.end()) {
serviceName = it->second.serviceName;
force_new = false;
session_already_opened = true;
}
}
// cleanup map
cleanupRunningKateAppInstanceMap(&mapSessionRii);
// if no new instance is forced and no already opened session is requested,
// check if a pid is given, which should be reused.
// two possibilities: pid given or not...
......
......@@ -115,7 +115,7 @@ bool KateSessionManager::activateSession(KateSession::Ptr session, const bool cl
return false;
}
if (instances.contains(session->name())) {
if (instances.find(session->name()) != instances.end()) {
if (KMessageBox::questionYesNo(nullptr,
i18n("Session '%1' is already opened in another kate instance, change there instead of reopening?", session->name()),
QString(),
......@@ -123,13 +123,10 @@ bool KateSessionManager::activateSession(KateSession::Ptr session, const bool cl
KStandardGuiItem::no(),
QStringLiteral("katesessionmanager_switch_instance"))
== KMessageBox::Yes) {
instances[session->name()]->dbus_if->call(QStringLiteral("activate"));
cleanupRunningKateAppInstanceMap(&instances);
instances[session->name()].dbus_if->call(QStringLiteral("activate"));
return false;
}
}
cleanupRunningKateAppInstanceMap(&instances);
}
// try to close and save last session
if (closeAndSaveLast) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment