summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2017-04-07 09:17:07 (GMT)
committerTobias Markmann <tm@ayena.de>2017-04-07 10:42:24 (GMT)
commited2226782ac15345aeb8e615b41d30e5aab67b51 (patch)
treea519ef4b314570fb134b969bb5fcff0e1f2be6e9 /Swift/QtUI
parent7d2e5e200d8449a4492c6fafa4811197e6fbe40b (diff)
downloadswift-ed2226782ac15345aeb8e615b41d30e5aab67b51.zip
swift-ed2226782ac15345aeb8e615b41d30e5aab67b51.tar.bz2
Make day change chat system message DST aware
Moved the code for day change message handling from Swift/Controllers to Swift/QtUI. Use QDateTime in local time time spec, which allows DST aware calculation of the duration to the next midnight. Added Swift Qt UI unit tests, which are build when Qt has been successfully detected. Test-Information: Added unit tests for duration to next midnight calculation. Set clock shortly before midnight and verified that a single day change message is added to the chat log at midnight. Tested on macOS 10.12.4 with Qt 5.4.2. Change-Id: I34d69eaa3272981fd220a7963a0417f73ff78e68
Diffstat (limited to 'Swift/QtUI')
-rw-r--r--Swift/QtUI/QtChatWindow.cpp20
-rw-r--r--Swift/QtUI/QtChatWindow.h5
-rw-r--r--Swift/QtUI/QtUtilities.cpp15
-rw-r--r--Swift/QtUI/QtUtilities.h11
-rw-r--r--Swift/QtUI/SConscript15
-rw-r--r--Swift/QtUI/UnitTest/QtUtilitiesTest.cpp30
6 files changed, 90 insertions, 6 deletions
diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index c509ab3..48d331e 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -36,6 +36,7 @@
#include <QTextDocument>
#include <QTextEdit>
#include <QTime>
+#include <QTimer>
#include <QToolButton>
#include <QUrl>
@@ -189,9 +190,20 @@ QtChatWindow::QtChatWindow(const QString& contact, QtChatTheme* theme, UIEventSt
settings_->onSettingChanged.connect(boost::bind(&QtChatWindow::handleSettingChanged, this, _1));
messageLog_->showEmoticons(settings_->getSetting(QtUISettingConstants::SHOW_EMOTICONS));
setMinimumSize(100, 100);
+
+ dayChangeTimer = new QTimer(this);
+ dayChangeTimer->setSingleShot(true);
+ connect(dayChangeTimer, &QTimer::timeout, [this](){
+ addSystemMessage(ChatMessage(Q2PSTRING(tr("The day is now %1").arg(QDateTime::currentDateTime().date().toString(Qt::SystemLocaleLongDate)))), ChatWindow::DefaultDirection);
+ onContinuationsBroken();
+ resetDayChangeTimer();
+ });
+
+ resetDayChangeTimer();
}
QtChatWindow::~QtChatWindow() {
+ dayChangeTimer->stop();
settings_->onSettingChanged.disconnect(boost::bind(&QtChatWindow::handleSettingChanged, this, _1));
if (mucConfigurationWindow_) {
delete mucConfigurationWindow_.data();
@@ -206,7 +218,7 @@ void QtChatWindow::handleSettingChanged(const std::string& setting) {
}
void QtChatWindow::handleLogCleared() {
- onLogCleared();
+ onContinuationsBroken();
}
void QtChatWindow::handleOccupantSelectionChanged(RosterItem* item) {
@@ -656,6 +668,12 @@ std::vector<JID> QtChatWindow::jidListFromQByteArray(const QByteArray& dataBytes
return invites;
}
+void QtChatWindow::resetDayChangeTimer() {
+ assert(dayChangeTimer);
+ // Add a second so the handled is definitly called on the next day, and not multiple times exactly at midnight.
+ dayChangeTimer->start(QtUtilities::secondsToNextMidnight(QDateTime::currentDateTime()) * 1000 + 1000);
+}
+
void QtChatWindow::setAvailableOccupantActions(const std::vector<OccupantAction>& actions) {
treeWidget_->setAvailableOccupantActions(actions);
}
diff --git a/Swift/QtUI/QtChatWindow.h b/Swift/QtUI/QtChatWindow.h
index 46186ba..361d7c6 100644
--- a/Swift/QtUI/QtChatWindow.h
+++ b/Swift/QtUI/QtChatWindow.h
@@ -192,6 +192,8 @@ namespace Swift {
static std::vector<JID> jidListFromQByteArray(const QByteArray& dataBytes);
+ void resetDayChangeTimer();
+
private:
int unreadCount_;
bool contactIsTyping_;
@@ -219,7 +221,7 @@ namespace Swift {
bool tabCompletion_;
UIEventStream* eventStream_;
bool isOnline_;
- QSplitter *logRosterSplitter_;
+ QSplitter* logRosterSplitter_;
Tristate correctionEnabled_;
Tristate fileTransferEnabled_;
QString alertStyleSheet_;
@@ -238,5 +240,6 @@ namespace Swift {
std::unique_ptr<QMenu> emojisMenu_;
QPointer<QtEmojisSelector> emojisGrid_;
std::map<std::string, std::string> emoticonsMap_;
+ QTimer* dayChangeTimer = nullptr;
};
}
diff --git a/Swift/QtUI/QtUtilities.cpp b/Swift/QtUI/QtUtilities.cpp
index 401af17..6eb0b04 100644
--- a/Swift/QtUI/QtUtilities.cpp
+++ b/Swift/QtUI/QtUtilities.cpp
@@ -1,10 +1,10 @@
/*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2017 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#include "QtUtilities.h"
+#include <Swift/QtUI/QtUtilities.h>
#include <QtGui>
#if defined (Q_OS_UNIX) && !defined(Q_OS_MAC)
@@ -14,8 +14,9 @@
#endif
#include <QTextDocument>
#include <QWidget>
+#include <QDateTime>
-#include "Swift/Controllers/ApplicationInfo.h"
+#include <Swift/Controllers/ApplicationInfo.h>
namespace QtUtilities {
@@ -42,4 +43,12 @@ QString htmlEscape(const QString& s) {
#endif
}
+long long secondsToNextMidnight(const QDateTime& currentTime) {
+ auto secondsToMidnight = 0ll;
+ auto nextMidnight = currentTime.addDays(1);
+ nextMidnight.setTime(QTime(0,0));
+ secondsToMidnight = currentTime.secsTo(nextMidnight);
+ return secondsToMidnight;
+}
+
}
diff --git a/Swift/QtUI/QtUtilities.h b/Swift/QtUI/QtUtilities.h
index ad58499..c6f4311 100644
--- a/Swift/QtUI/QtUtilities.h
+++ b/Swift/QtUI/QtUtilities.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2017 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,6 +8,7 @@
class QWidget;
class QString;
+class QDateTime;
#include <QKeyEvent>
@@ -19,4 +20,12 @@ namespace QtUtilities {
#else
const Qt::KeyboardModifier ctrlHardwareKeyModifier = Qt::ControlModifier;
#endif
+
+ /**
+ * @brief secondsToNextMidnight calculates the seconds until next midnight.
+ * @param currentTime This is the current time, which SHOULD have a Qt::TimeSpec
+ * of Qt::LocalTime to correctly handle DST changes in the current locale.
+ * @return
+ */
+ long long secondsToNextMidnight(const QDateTime& currentTime);
}
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index 2f95b3e..3ce2057 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -71,6 +71,21 @@ if env["PLATFORM"] != "win32" and env["PLATFORM"] != "darwin" :
myenv.EnableQt4Modules(qt4modules, debug = False, version = qt_version)
+
+## Qt related unit tests
+testQtEnv = env.Clone();
+testQtEnv.Tool("qt4", toolpath = ["#/BuildTools/SCons/Tools"])
+testQtEnv.EnableQt4Modules(qt4modules, debug = False, version = qt_version)
+env["SWIFT_QTUI_TEST_FLAGS"] = {
+ "CPPFLAGS": testQtEnv["CPPFLAGS"],
+ "LIBS": testQtEnv["LIBS"],
+ "LINKFLAGS": testQtEnv["LINKFLAGS"],
+}
+
+env.Append(UNITTEST_SOURCES = [
+ File("UnitTest/QtUtilitiesTest.cpp")
+])
+
myenv.Append(CPPPATH = ["."])
# Qt requires applications to be build with the -fPIC flag on some 32-bit Linux distributions.
diff --git a/Swift/QtUI/UnitTest/QtUtilitiesTest.cpp b/Swift/QtUI/UnitTest/QtUtilitiesTest.cpp
new file mode 100644
index 0000000..45d2239
--- /dev/null
+++ b/Swift/QtUI/UnitTest/QtUtilitiesTest.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2017 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <gtest/gtest.h>
+
+#include <QDateTime>
+#include <QLocale>
+
+#include <Swift/QtUI/QtUtilities.h>
+#include <Swift/QtUI/QtUtilities.cpp>
+
+TEST(QtUtilitiesTest, testDSTawareness) {
+ QLocale::setDefault(QLocale(QLocale::English, QLocale::Germany));
+
+ auto beforeDSTpoint = QDateTime(QDate(2017, 3, 26), QTime(0, 0));
+
+ ASSERT_EQ(23 * 60 * 60, QtUtilities::secondsToNextMidnight(beforeDSTpoint));
+}
+
+
+TEST(QtUtilitiesTest, testNoDSTChange) {
+ QLocale::setDefault(QLocale(QLocale::English, QLocale::Germany));
+
+ auto beforeDSTpoint = QDateTime(QDate(2017, 3, 23), QTime(0, 0));
+
+ ASSERT_EQ(24 * 60 * 60, QtUtilities::secondsToNextMidnight(beforeDSTpoint));
+}