Verified Commit 2db4c8f6 authored by Linus Jahn's avatar Linus Jahn

QmlUtils: Make singleton and use directly, without the kaidan object in QML

parent 199556fe
......@@ -52,7 +52,6 @@ Kaidan *Kaidan::s_instance = nullptr;
Kaidan::Kaidan(QGuiApplication *app, bool enableLogging, QObject *parent)
: QObject(parent),
m_utils(new QmlUtils(this)),
m_database(new Database()),
m_dbThrd(new QThread()),
m_msgDb(new MessageDb()),
......
......@@ -43,7 +43,6 @@
class QGuiApplication;
class Database;
class QXmppClient;
class QmlUtils;
using namespace Enums;
......@@ -60,7 +59,6 @@ class Kaidan : public QObject
{
Q_OBJECT
Q_PROPERTY(QmlUtils* utils READ getUtils CONSTANT)
Q_PROPERTY(RosterModel* rosterModel READ getRosterModel CONSTANT)
Q_PROPERTY(MessageModel* messageModel READ getMessageModel CONSTANT)
Q_PROPERTY(AvatarFileStorage* avatarStorage READ getAvatarStorage NOTIFY avatarStorageChanged)
......@@ -189,11 +187,6 @@ public:
return m_caches->settings;
}
QmlUtils* getUtils() const
{
return m_utils;
}
ClientWorker *getClient() const;
/**
......@@ -422,7 +415,6 @@ private:
*/
void notifyLoginUriNotFound();
QmlUtils *m_utils;
Database *m_database;
QThread *m_dbThrd;
MessageDb *m_msgDb;
......
......@@ -43,9 +43,26 @@
// QXmpp
#include "qxmpp-exts/QXmppColorGenerator.h"
static QmlUtils *s_instance;
QmlUtils *QmlUtils::instance()
{
if (!s_instance)
return new QmlUtils(QGuiApplication::instance());
return s_instance;
}
QmlUtils::QmlUtils(QObject *parent)
: QObject(parent)
: QObject(parent)
{
Q_ASSERT(!s_instance);
s_instance = this;
}
QmlUtils::~QmlUtils()
{
s_instance = nullptr;
}
QString QmlUtils::presenceTypeToIcon(Enums::AvailabilityTypes type)
......
......@@ -48,7 +48,10 @@ class QmlUtils : public QObject
Q_OBJECT
public:
static QmlUtils *instance();
QmlUtils(QObject *parent = nullptr);
~QmlUtils();
/**
* Returns the icon belonging to the given presence status type
......@@ -79,7 +82,7 @@ public:
*/
Q_INVOKABLE static QString getVersionString()
{
return VERSION_STRING;
return QStringLiteral(VERSION_STRING);
}
/**
......
......@@ -199,7 +199,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qputenv("GST_PLUGIN_PATH_1_0", QByteArray());
qputenv("GST_PLUGIN_SYSTEM_PATH_1_0", gstreamerPluginsPath.toLocal8Bit());
qputenv("GST_PLUGIN_SCANNER_1_0", QString::fromLatin1("%1/gst-plugin-scanner").arg(gstreamerPluginsPath).toLocal8Bit());
#endif
#endif // APPIMAGE
// register qMetaTypes
qRegisterMetaType<RosterItem>("RosterItem");
......@@ -227,7 +227,6 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qRegisterMetaType<CameraInfo>();
qRegisterMetaType<AudioDeviceInfo>();
qRegisterMetaType<MediaSettings>();
// qRegisterMetaType<CommonEncoderSettings>();
qRegisterMetaType<ImageEncoderSettings>();
qRegisterMetaType<AudioEncoderSettings>();
qRegisterMetaType<VideoEncoderSettings>();
......@@ -370,6 +369,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
QObject *instance = new MediaUtils(qApp);
return instance;
});
qmlRegisterSingletonType<QmlUtils>(APPLICATION_ID, 1, 0, "Utils", [](QQmlEngine *, QJSEngine *) {
return static_cast<QObject*>(QmlUtils::instance());
});
engine.rootContext()->setContextProperty("kaidan", &kaidan);
engine.load(QUrl("qrc:/qml/main.qml"));
......
......@@ -33,6 +33,8 @@ import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3 as Controls
import org.kde.kirigami 2.8 as Kirigami
import im.kaidan.kaidan 1.0
Controls.Dialog {
id: aboutDialog
modal: true
......@@ -46,7 +48,7 @@ Controls.Dialog {
rowSpacing: 20
Image {
source: kaidan.utils.getResourcePath("images/kaidan.svg")
source: Utils.getResourcePath("images/kaidan.svg")
Layout.preferredWidth: Kirigami.Units.gridUnit * 9
Layout.preferredHeight: Kirigami.Units.gridUnit * 9
Layout.fillWidth: true
......@@ -63,7 +65,7 @@ Controls.Dialog {
spacing: Kirigami.gridUnit * 0.6
Kirigami.Heading {
text: "Kaidan " + kaidan.utils.getVersionString()
text: "Kaidan " + Utils.getVersionString()
textFormat: Text.PlainText
wrapMode: Text.WordWrap
Layout.fillWidth: true
......
......@@ -393,7 +393,7 @@ Kirigami.ScrollablePage {
background: Image {
id: bgimage
source: kaidan.utils.getResourcePath("images/chat.png")
source: Utils.getResourcePath("images/chat.png")
anchors.fill: parent
fillMode: Image.Tile
horizontalAlignment: Image.AlignLeft
......
......@@ -32,10 +32,12 @@ import QtQuick.Controls 2.3 as Controls
import org.kde.kirigami 2.8 as Kirigami
import QtQuick 2.7
import im.kaidan.kaidan 1.0
Kirigami.Page {
background: Image {
id: bgimage
source: kaidan.utils.getResourcePath("images/chat.png")
source: Utils.getResourcePath("images/chat.png")
anchors.fill: parent
fillMode: Image.Tile
horizontalAlignment: Image.AlignLeft
......
......@@ -35,8 +35,8 @@ import "settings"
Kirigami.GlobalDrawer {
id: globalDrawer
title: "Kaidan"
titleIcon: kaidan.utils.getResourcePath("images/kaidan.svg")
bannerImageSource: kaidan.utils.getResourcePath("images/banner.png")
titleIcon: Utils.getResourcePath("images/kaidan.svg")
bannerImageSource: Utils.getResourcePath("images/banner.png")
SettingsSheet {
id: settingsSheet
......@@ -47,7 +47,7 @@ Kirigami.GlobalDrawer {
text: qsTr("Invite friends")
iconName: "mail-invitation"
onTriggered: {
kaidan.utils.copyToClipboard(
Utils.copyToClipboard(
"https://i.kaidan.im/#" + kaidan.jid)
passiveNotification(
qsTr("Invitation link copied to clipboard"))
......
......@@ -146,15 +146,15 @@ Kirigami.Page {
spacing: Kirigami.Units.smallSpacing
Kirigami.Icon {
source: kaidan.utils.presenceTypeToIcon(presenceType)
source: Utils.presenceTypeToIcon(presenceType)
width: 26
height: 26
}
Controls.Label {
Layout.alignment: Qt.AlignVCenter
text: kaidan.utils.presenceTypeToText(presenceType)
color: kaidan.utils.presenceTypeToColor(presenceType)
text: Utils.presenceTypeToText(presenceType)
color: Utils.presenceTypeToColor(presenceType)
textFormat: Text.PlainText
}
......@@ -174,7 +174,7 @@ Kirigami.Page {
Layout.fillWidth: true
Controls.Label {
text: kaidan.utils.formatMessage(model.value)
text: Utils.formatMessage(model.value)
onLinkActivated: Qt.openUrlExternally(link)
textFormat: Text.StyledText
}
......
......@@ -125,9 +125,9 @@ RowLayout {
enabled: bodyLabel.visible
onTriggered: {
if (!isSpoiler || isShowingSpoiler)
kaidan.utils.copyToClipboard(messageBody);
Utils.copyToClipboard(messageBody);
else
kaidan.utils.copyToClipboard(spoilerHint);
Utils.copyToClipboard(spoilerHint);
}
}
......@@ -140,7 +140,7 @@ RowLayout {
Controls.MenuItem {
text: qsTr("Copy download URL")
enabled: mediaGetUrl
onTriggered: kaidan.utils.copyToClipboard(mediaGetUrl)
onTriggered: Utils.copyToClipboard(mediaGetUrl)
}
}
}
......@@ -254,7 +254,7 @@ RowLayout {
Controls.Label {
id: bodyLabel
visible: (root.mediaType === Enums.MessageType.MessageText || messageBody !== mediaGetUrl) && messageBody !== ""
text: kaidan.utils.formatMessage(messageBody)
text: Utils.formatMessage(messageBody)
textFormat: Text.StyledText
wrapMode: Text.Wrap
color: sentByMe ? Kirigami.Theme.textColor
......@@ -288,7 +288,7 @@ RowLayout {
Image {
id: checkmark
visible: (sentByMe && isDelivered)
source: kaidan.utils.getResourcePath("images/message_checkmark.svg")
source: Utils.getResourcePath("images/message_checkmark.svg")
Layout.preferredHeight: Kirigami.Units.gridUnit * 0.65
Layout.preferredWidth: Kirigami.Units.gridUnit * 0.65
sourceSize.height: Kirigami.Units.gridUnit * 0.65
......
......@@ -108,7 +108,7 @@ MediaPreview {
Controls.Label {
id: fileNameLabel
Layout.fillWidth: true
text: kaidan.utils.fileNameFromUrl(root.mediaSource)
text: Utils.fileNameFromUrl(root.mediaSource)
textFormat: Text.PlainText
elide: Text.ElideRight
maximumLineCount: 1
......@@ -130,7 +130,7 @@ MediaPreview {
id: fileSizeLabel
Layout.fillWidth: true
text: kaidan.utils.fileSizeFromUrl(root.mediaSource)
text: Utils.fileSizeFromUrl(root.mediaSource)
textFormat: Text.PlainText
elide: Text.ElideRight
maximumLineCount: 1
......
......@@ -33,6 +33,7 @@ import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3 as Controls
import QtGraphicalEffects 1.0
import org.kde.kirigami 2.8 as Kirigami
import im.kaidan.kaidan 1.0
Kirigami.SwipeListItem {
......@@ -62,7 +63,7 @@ Kirigami.SwipeListItem {
width: Kirigami.Units.gridUnit * 0.3
height: parent.height
color: kaidan.utils.presenceTypeToColor(presenceType)
color: Utils.presenceTypeToColor(presenceType)
}
// left: avatar
......@@ -126,7 +127,7 @@ Kirigami.SwipeListItem {
text: {
presenceType === Enums.PresError ? // error presence type
qsTr("Error: Please check the JID.") :
kaidan.utils.removeNewLinesFromString(lastMessage)
Utils.removeNewLinesFromString(lastMessage)
}
textFormat: Text.PlainText
font.pixelSize: Kirigami.Units.gridUnit * 0.8
......@@ -159,8 +160,8 @@ Kirigami.SwipeListItem {
}
// presence status type
string += "<font color='" + kaidan.utils.presenceTypeToColor(statusType) + "'>"
string += kaidan.utils.presenceTypeToText(statusType)
string += "<font color='" + Utils.presenceTypeToColor(statusType) + "'>"
string += Utils.presenceTypeToText(statusType)
string += "</font>"
// presence status message
......
......@@ -28,16 +28,18 @@
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
import org.kde.kirigami 2.8 as Kirigami
import QtQuick 2.7
import QtQuick.Layouts 1.3
import org.kde.kirigami 2.8 as Kirigami
import im.kaidan.kaidan 1.0
Rectangle {
id: avatar
property string name
color: Qt.lighter(kaidan.utils.getUserColor(name))
color: Qt.lighter(Utils.getUserColor(name))
radius: width * 0.5
Text {
......
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