Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ include(KDEGitCommitHooks)

find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Concurrent Quick WaylandClient DBus LinguistTools Sql)
find_package(Dtk${DTK_VERSION_MAJOR} REQUIRED COMPONENTS Core Gui)
find_package(ICU 74.2 REQUIRED COMPONENTS uc i18n io)
find_package(WaylandProtocols REQUIRED)
find_package(PkgConfig REQUIRED)

Expand Down
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Build-Depends:
libdtk6core-dev,
libdtk6widget-dev,
libdtkcommon-dev,
libicu-dev,
libxcb-ewmh-dev,
libxcb-res0-dev,
libxcb-util-dev,
Expand Down
3 changes: 3 additions & 0 deletions panels/notification/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ target_link_libraries(ds-notification-shared PUBLIC
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Sql
Dtk${DTK_VERSION_MAJOR}::Core
ICU::uc
ICU::i18n
ICU::io
)

install(TARGETS ds-notification-shared DESTINATION "${LIB_INSTALL_DIR}")
Expand Down
61 changes: 53 additions & 8 deletions panels/notification/center/notifyitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

#include "notifyitem.h"

#include <QDateTime>

Check warning on line 7 in panels/notification/center/notifyitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDateTime> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLoggingCategory>

Check warning on line 8 in panels/notification/center/notifyitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <unicode/reldatefmt.h> // For RelativeDateTimeFormatter

Check warning on line 10 in panels/notification/center/notifyitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unicode/reldatefmt.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unicode/smpdtfmt.h> // For SimpleDateFormat

Check warning on line 11 in panels/notification/center/notifyitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unicode/smpdtfmt.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "notifyaccessor.h"

namespace notification {
Expand Down Expand Up @@ -62,12 +65,48 @@
return m_time;
}

namespace
{
QString toQString(const icu::UnicodeString &icuString)
{
// Get a pointer to the internal UTF-16 buffer of the icu::UnicodeString.
// The buffer is not necessarily null-terminated, so we also need the length.
const UChar *ucharData = icuString.getBuffer();
int32_t length = icuString.length();

// QString has a constructor that takes a const QChar* and a length.
// UChar is typically a 16-bit unsigned integer, which is compatible with QChar.
// Static_cast is used here for explicit type conversion, though often
// UChar and QChar are typedefs to the same underlying type (e.g., unsigned short).
return QString(reinterpret_cast<const QChar *>(ucharData), length);
}

icu::UnicodeString fromQString(const QString &qstr)
{
return icu::UnicodeString(qstr.utf16(), qstr.length());
}
} // anonymous namespace

void AppNotifyItem::updateTime()
{
QDateTime time = QDateTime::fromMSecsSinceEpoch(m_entity.cTime());
if (!time.isValid())
return;

using namespace icu;
static std::unique_ptr<RelativeDateTimeFormatter> formatter;
static UErrorCode cachedStatus = U_ZERO_ERROR;
if (!formatter) {
cachedStatus = U_ZERO_ERROR;
formatter = std::make_unique<RelativeDateTimeFormatter>(icu::Locale::getDefault(),
nullptr, // Use default NumberFormat
UDAT_STYLE_LONG,
UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE,
cachedStatus);
}
UErrorCode status = U_ZERO_ERROR; // For any per-call ICU operations
UnicodeString result;

QString ret;
QDateTime currentTime = QDateTime::currentDateTime();
auto elapsedDay = time.daysTo(currentTime);
Expand All @@ -77,21 +116,27 @@
if (minute <= 0) {
ret = tr("Just now");
} else if (minute > 0 && minute < 60) {
ret = tr("%1 minutes ago").arg(minute);
formatter->format(minute, UDAT_DIRECTION_LAST, UDAT_RELATIVE_MINUTES, result, status);
ret = toQString(result);
} else {
const auto hour = minute / 60;
if (hour == 1) {
ret = tr("1 hour ago");
} else {
ret = tr("%1 hours ago").arg(hour);
}
formatter->format(hour, UDAT_DIRECTION_LAST, UDAT_RELATIVE_HOURS, result, status);
ret = toQString(result);
}
} else if (elapsedDay >= 1 && elapsedDay < 2) {
ret = tr("Yesterday ") + " " + time.toString("hh:mm");
formatter->format(1, UDAT_DIRECTION_LAST, UDAT_RELATIVE_DAYS, result, status);
UnicodeString combinedString;
UErrorCode timeStatus = U_ZERO_ERROR;
SimpleDateFormat timeFormatter("HH:mm", icu::Locale::getDefault(), timeStatus);
UnicodeString timeString;
UDate udate = static_cast<UDate>(m_entity.cTime());
timeFormatter.format(udate, timeString, timeStatus);
formatter->combineDateAndTime(result, timeString, combinedString, status);
ret = toQString(combinedString);
} else if (elapsedDay >= 2 && elapsedDay < 7) {
ret = time.toString("ddd hh:mm");
} else {
ret = time.toString("yyyy/MM/dd");
ret = time.toString(QLocale::system().dateFormat(QLocale::ShortFormat));
}

m_time = ret;
Expand Down