Files
kdeconnect-kde-mirror/smsapp/main.cpp
Joshua Goins 4573742555 smsapp: Restore the last used device on start-up
I usually have two Android devices connected to my computers: a
non-cellular tablet and my actually-SMS-capable phone. Due to some
sorting somewhere, my tablet is always the first one selected when
starting KDE Connect SMS and thus none of the messages I want to see are
shown.

So I made it so AppData (the data object holding among other things, the
currently used device) writes and loads the last used device into
KStateConfig. This works really well, except with one caveat: if the
device is no longer reachable or is otherwise invalid, the user has to
manually reselect a valid device. I think this is overall pretty minor,
as this is the rare case (no one is frequently creating invalid
devices.) Trying to work around this will be hard due to how the device
list is initialized in our app too.
2025-11-18 17:13:55 -05:00

153 lines
5.8 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* SPDX-FileCopyrightText: 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
* SPDX-FileCopyrightText: 2018 Simon Redman <simon@ergotech.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "attachmentshelper.h"
#include "conversationlistmodel.h"
#include "conversationmodel.h"
#include "conversationssortfilterproxymodel.h"
#include "kdeconnect-version.h"
#include "thumbnailsprovider.h"
#include <KAboutData>
#include <KColorSchemeManager>
#include <KConfigGroup>
#include <KCrash>
#include <KDBusService>
#include <KLocalizedContext>
#include <KLocalizedString>
#include <KSharedConfig>
#include <QApplication>
#include <QCommandLineParser>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include "smshelper.h"
class AppData : public QObject
{
Q_OBJECT
Q_PROPERTY(QString initialMessage MEMBER m_initialMessage NOTIFY initialMessageChanged)
Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
public:
AppData(const QString &initialMessage, const QString &deviceId)
: m_initialMessage(initialMessage)
, m_deviceId(deviceId)
{
// If not set from the CLI, load from state config
if (deviceId.isEmpty()) {
KConfigGroup stateConfig = KSharedConfig::openStateConfig()->group(QString());
m_deviceId = stateConfig.readEntry(QStringLiteral("LastDeviceId"));
}
}
void setDeviceId(const QString &newDeviceId)
{
// Write the last set device id to restore on next app start
KConfigGroup stateConfig = KSharedConfig::openStateConfig()->group(QString());
stateConfig.writeEntry(QStringLiteral("LastDeviceId"), newDeviceId);
m_deviceId = newDeviceId;
Q_EMIT deviceIdChanged();
}
QString deviceId() const
{
return m_deviceId;
}
QString m_initialMessage;
Q_SIGNALS:
void initialMessageChanged();
void deviceIdChanged();
private:
QString m_deviceId;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
KLocalizedString::setApplicationDomain("kdeconnect-sms");
app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kdeconnect")));
KAboutData aboutData(QStringLiteral("kdeconnect.sms"),
i18n("KDE Connect SMS"),
QStringLiteral(KDECONNECT_VERSION_STRING),
i18n("SMS Instant Messaging"),
KAboutLicense::GPL,
i18n("© 20182025 KDE Connect Team"));
aboutData.addAuthor(i18n("Simon Redman"), {}, QStringLiteral("simon@ergotech.com"));
aboutData.addAuthor(i18n("Aleix Pol Gonzalez"), {}, QStringLiteral("aleixpol@kde.org"));
aboutData.addAuthor(i18n("Nicolas Fella"), {}, QStringLiteral("nicolas.fella@gmx.de"));
aboutData.setBugAddress(QStringLiteral("https://bugs.kde.org/enter_bug.cgi?product=kdeconnect&component=messaging-application").toUtf8());
KAboutData::setApplicationData(aboutData);
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
// Ensure we have a suitable color theme set for light/dark mode. KColorSchemeManager implicitly applies
// a suitable default theme.
KColorSchemeManager::instance();
// Force breeze style to ensure coloring works consistently in dark mode. Specifically tab colors have
// troubles on windows.
QApplication::setStyle(QStringLiteral("breeze"));
// Force breeze icon theme to ensure we can correctly adapt icons to color changes WRT dark/light mode.
// Without this we may end up with hicolor and fail to support icon recoloring.
QIcon::setThemeName(QStringLiteral("breeze"));
#else
QIcon::setFallbackThemeName(QStringLiteral("breeze"));
#endif
// Default to org.kde.desktop style unless the user forces another style
if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE")) {
QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
}
KCrash::initialize();
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id")));
parser.addOption(QCommandLineOption(QStringLiteral("message"), i18n("Send a message"), i18n("message")));
parser.process(app);
aboutData.processCommandLine(&parser);
AppData data(parser.value(QStringLiteral("message")), parser.value(QStringLiteral("device")));
KDBusService service(KDBusService::Unique);
QObject::connect(&service, &KDBusService::activateRequested, &service, [&parser, &data](const QStringList &args, const QString & /*workDir*/) {
parser.parse(args);
data.m_initialMessage = parser.value(QStringLiteral("message"));
data.setDeviceId(parser.value(QStringLiteral("device")));
Q_EMIT data.deviceIdChanged();
Q_EMIT data.initialMessageChanged();
});
qmlRegisterType<ConversationsSortFilterProxyModel>("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<ConversationModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationModel");
qmlRegisterType<ConversationListModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel");
qmlRegisterSingletonType<SmsHelper>("org.kde.kdeconnect.sms", 1, 0, "SmsHelper", SmsHelper::singletonProvider);
qmlRegisterSingletonType<SmsHelper>("org.kde.kdeconnect.sms", 1, 0, "AttachmentsHelper", AttachmentsHelper::singletonProvider);
qmlRegisterSingletonInstance<AppData>("org.kde.kdeconnect.sms", 1, 0, "AppData", &data);
QQmlApplicationEngine engine;
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
engine.addImageProvider(QStringLiteral("thumbnailsProvider"), new ThumbnailsProvider);
engine.loadFromModule("org.kde.kdeconnect.sms", "Main");
return app.exec();
}
#include "main.moc"